1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <assert.h>
#include "MiAsc2Bin.h"
namespace mimp
{
static inline bool IsWhitespace(char c)
{
if ( c == ' ' || c == 9 || c == 13 || c == 10 || c == ',' ) return true;
return false;
}
static inline const char * SkipWhitespace(const char *str)
{
if ( str )
{
while ( *str && IsWhitespace(*str) ) str++;
}
return str;
}
static char ToLower(char c)
{
if ( c >= 'A' && c <= 'Z' ) c+=32;
return c;
}
static inline MiU32 GetHex(MiU8 c)
{
MiU32 v = 0;
c = (MiU8)ToLower((char)c);
if ( c >= '0' && c <= '9' )
v = MiU32(c-'0');
else
{
if ( c >= 'a' && c <= 'f' )
{
v = MiU32(10 + c-'a');
}
}
return v;
}
static inline MiU8 GetHEX1(const char *foo,const char **endptr)
{
MiU32 ret = 0;
ret = (GetHex((MiU8)foo[0])<<4) | GetHex((MiU8)foo[1]);
if ( endptr )
{
*endptr = foo+2;
}
return (MiU8) ret;
}
static inline unsigned short GetHEX2(const char *foo,const char **endptr)
{
MiU32 ret = 0;
ret = (GetHex((MiU8)foo[0])<<12) | (GetHex((MiU8)foo[1])<<8) | (GetHex((MiU8)foo[2])<<4) | GetHex((MiU8)foo[3]);
if ( endptr )
{
*endptr = foo+4;
}
return (unsigned short) ret;
}
static inline MiU32 GetHEX4(const char *foo,const char **endptr)
{
MiU32 ret = 0;
for (MiI32 i=0; i<8; i++)
{
ret = (ret<<4) | GetHex((MiU8)foo[i]);
}
if ( endptr )
{
*endptr = foo+8;
}
return ret;
}
static inline MiU32 GetHEX(const char *foo,const char **endptr)
{
MiU32 ret = 0;
while ( *foo )
{
MiU8 c = (MiU8)ToLower( *foo );
MiU32 v = 0;
if ( c >= '0' && c <= '9' )
v = MiU8(c-'0');
else
{
if ( c >= 'a' && c <= 'f' )
{
v = MiU8(10 + c-'a');
}
else
break;
}
ret = (ret<<4)|v;
foo++;
}
if ( endptr ) *endptr = foo;
return ret;
}
#define MAXNUM 32
static inline MiF32 GetFloatValue(const char *str,const char **next)
{
MiF32 ret = 0;
if ( next ) *next = 0;
str = SkipWhitespace(str);
char dest[MAXNUM];
char *dst = dest;
const char *hex = 0;
for (MiI32 i=0; i<(MAXNUM-1); i++)
{
char c = *str;
if ( c == 0 || IsWhitespace(c) )
{
if ( next ) *next = str;
break;
}
else if ( c == '$' )
{
hex = str+1;
}
*dst++ = ToLower(c);
str++;
}
*dst = 0;
if ( hex )
{
MiU32 iv = GetHEX(hex,0);
MiF32 *v = (MiF32 *)&iv;
ret = *v;
}
else if ( dest[0] == 'f' )
{
if ( strcmp(dest,"fltmax") == 0 || strcmp(dest,"fmax") == 0 )
{
ret = FLT_MAX;
}
else if ( strcmp(dest,"fltmin") == 0 || strcmp(dest,"fmin") == 0 )
{
ret = FLT_MIN;
}
}
else if ( dest[0] == 't' ) // t or 'true' is treated as the value '1'.
{
ret = 1;
}
else
{
ret = (MiF32)atof(dest);
}
return ret;
}
static inline MiI32 GetIntValue(const char *str,const char **next)
{
MiI32 ret = 0;
if ( next ) *next = 0;
str = SkipWhitespace(str);
char dest[MAXNUM];
char *dst = dest;
for (MiI32 i=0; i<(MAXNUM-1); i++)
{
char c = *str;
if ( c == 0 || IsWhitespace(c) )
{
if ( next ) *next = str;
break;
}
*dst++ = c;
str++;
}
*dst = 0;
ret = atoi(dest);
return ret;
}
#ifdef PLAYSTATION3
#include <ctype.h> // for tolower()
#endif
enum Atype { AT_FLOAT, AT_INT, AT_CHAR, AT_BYTE, AT_SHORT, AT_STR, AT_HEX1, AT_HEX2, AT_HEX4, AT_LAST };
#define MAXARG 64
#if 0
void TestAsc2bin(void)
{
Asc2Bin("1 2 A 3 4 Foo AB ABCD FFEDFDED",1,"f d c b h p x1 x2 x4", 0);
}
#endif
void * Asc2Bin(const char *source,const MiI32 count,const char *spec,void *dest)
{
MiI32 cnt = 0;
MiI32 size = 0;
Atype types[MAXARG];
const char *ctype = spec;
while ( *ctype )
{
switch ( ToLower(*ctype) )
{
case 'f': size+= sizeof(MiF32); types[cnt] = AT_FLOAT; cnt++; break;
case 'd': size+= sizeof(MiI32); types[cnt] = AT_INT; cnt++; break;
case 'c': size+=sizeof(char); types[cnt] = AT_CHAR; cnt++; break;
case 'b': size+=sizeof(char); types[cnt] = AT_BYTE; cnt++; break;
case 'h': size+=sizeof(short); types[cnt] = AT_SHORT; cnt++; break;
case 'p': size+=sizeof(const char *); types[cnt] = AT_STR; cnt++; break;
case 'x':
{
Atype type = AT_HEX4;
MiI32 sz = 4;
switch ( ctype[1] )
{
case '1': type = AT_HEX1; sz = 1; ctype++; break;
case '2': type = AT_HEX2; sz = 2; ctype++; break;
case '4': type = AT_HEX4; sz = 4; ctype++; break;
}
types[cnt] = type;
size+=sz;
cnt++;
}
break;
}
if ( cnt == MAXARG ) return 0; // over flowed the maximum specification!
ctype++;
}
bool myalloc = false;
if ( dest == 0 )
{
myalloc = true;
dest = (char *) MI_ALLOC(sizeof(char)*count*size);
}
// ok...ready to parse lovely data....
memset(dest,0,size_t(count*size)); // zero out memory
char *dst = (char *) dest; // where we are storing the results
for (MiI32 i=0; i<count; i++)
{
for (MiI32 j=0; j<cnt; j++)
{
source = SkipWhitespace(source); // skip white spaces.
if (source == NULL || *source == 0 ) // we hit the end of the input data before we successfully parsed all input!
{
if ( myalloc )
{
MI_FREE(dest);
}
return 0;
}
switch ( types[j] )
{
case AT_FLOAT:
{
MiF32 *v = (MiF32 *) dst;
*v = GetFloatValue(source,&source);
dst+=sizeof(MiF32);
}
break;
case AT_INT:
{
MiI32 *v = (MiI32 *) dst;
*v = GetIntValue( source, &source );
dst+=sizeof(MiI32);
}
break;
case AT_CHAR:
{
*dst++ = *source++;
}
break;
case AT_BYTE:
{
char *v = (char *) dst;
*v = (char)GetIntValue(source,&source);
dst+=sizeof(char);
}
break;
case AT_SHORT:
{
short *v = (short *) dst;
*v = (short)(unsigned short)GetIntValue( source,&source );
dst+=sizeof(short);
}
break;
case AT_STR:
{
const char **ptr = (const char **) dst;
*ptr = source;
dst+=sizeof(const char *);
while ( *source && !IsWhitespace(*source) ) source++;
}
break;
case AT_HEX1:
{
MiU32 hex = GetHEX1(source,&source);
MiU8 *v = (MiU8 *) dst;
*v = (MiU8)hex;
dst+=sizeof(MiU8);
}
break;
case AT_HEX2:
{
MiU32 hex = GetHEX2(source,&source);
unsigned short *v = (unsigned short *) dst;
*v = (unsigned short)hex;
dst+=sizeof(unsigned short);
}
break;
case AT_HEX4:
{
MiU32 hex = GetHEX4(source,&source);
MiU32 *v = (MiU32 *) dst;
*v = hex;
dst+=sizeof(MiU32);
}
break;
case AT_LAST: // Make compiler happy
break;
}
}
}
return dest;
}
};
|