@@ -65,6 +65,11 @@ static bool vstr_is_float(const char vstr[16])
6565 return false ;
6666}
6767
68+ static bool vstr_is_string (const char vstr[16 ])
69+ {
70+ return isalpha (vstr[0 ]) || vstr[0 ] == ' \" ' ;
71+ }
72+
6873static float vstr_to_float (const char vstr[16 ])
6974{
7075 double v = 0.0 ;
@@ -281,12 +286,13 @@ static int dump_param(const char* parampath, const char* parambinpath, const cha
281286 int id = 0 ;
282287 while (fscanf (fp, " %d=" , &id) == 1 )
283288 {
284- fwrite (&id, sizeof (int ), 1 , mp);
285-
286289 bool is_array = id <= -23300 ;
287290
288291 if (is_array)
289292 {
293+ fwrite (&id, sizeof (int ), 1 , mp);
294+
295+ // old style array
290296 int len = 0 ;
291297 nscan = fscanf (fp, " %d" , &len);
292298 if (nscan != 1 )
@@ -320,18 +326,157 @@ static int dump_param(const char* parampath, const char* parambinpath, const cha
320326 fwrite (&v, sizeof (int ), 1 , mp);
321327 }
322328 }
329+
330+ continue ;
323331 }
324- else
332+
333+ char vstr[16 ];
334+ char comma[4 ];
335+ int nscan = fscanf (fp, " %15[^,\n ]" , vstr);
336+ if (nscan != 1 )
325337 {
326- char vstr[16 ];
327- nscan = fscanf (fp, " %15s" , vstr);
328- if (nscan != 1 )
338+ fprintf (stderr, " read value failed\n " );
339+ return -1 ;
340+ }
341+
342+ bool is_string = vstr_is_string (vstr);
343+ if (is_string)
344+ {
345+ id = -id - 23400 ;
346+ fwrite (&id, sizeof (int ), 1 , mp);
347+
348+ // scan the remaining string
349+ char vstr2[256 ];
350+ vstr2[241 ] = ' \0 ' ; // max 255 = 15 + 240
351+
352+ if (vstr[0 ] == ' \" ' )
329353 {
330- fprintf (stderr, " read value failed %d\n " , nscan);
331- return -1 ;
354+ int len = 0 ;
355+ while (vstr[len] != ' \0 ' )
356+ len++;
357+ char end = vstr[len - 1 ];
358+ if (end != ' \" ' )
359+ {
360+ nscan = fscanf (fp, " %255[^\"\n ]\" " , vstr2);
361+ }
362+ else
363+ nscan = 0 ; // already ended with a quote, no need to scan more
364+ }
365+ else
366+ {
367+ nscan = fscanf (fp, " %255[^\n ]" , vstr2);
368+ }
369+
370+ std::string str;
371+ if (nscan == 1 )
372+ {
373+ if (vstr2[241 ] != ' \0 ' )
374+ {
375+ fprintf (stderr, " string too long (id=%d)\n " , id);
376+ return -1 ;
377+ }
378+
379+ if (vstr[0 ] == ' \" ' )
380+ str = std::string (&vstr[1 ]) + vstr2;
381+ else
382+ str = std::string (vstr) + vstr2;
383+ }
384+ else
385+ {
386+ if (vstr[0 ] == ' \" ' )
387+ str = std::string (&vstr[1 ]);
388+ else
389+ str = std::string (vstr);
390+ }
391+
392+ if (str[str.size () - 1 ] == ' \" ' )
393+ str.resize (str.size () - 1 );
394+
395+ int len = (int )str.length ();
396+ fwrite (&len, sizeof (int ), 1 , mp);
397+ fwrite (str.data (), sizeof (float ), len, mp);
398+
399+ continue ;
400+ }
401+
402+ bool is_float = vstr_is_float (vstr);
403+
404+ nscan = fscanf (fp, " %1[,]" , comma);
405+ is_array = nscan == 1 ;
406+
407+ if (is_array)
408+ {
409+ id = -id - 23300 ;
410+ fwrite (&id, sizeof (int ), 1 , mp);
411+
412+ std::vector<float > af;
413+ std::vector<int > ai;
414+
415+ if (is_float)
416+ {
417+ af.push_back (vstr_to_float (vstr));
418+ }
419+ else
420+ {
421+ int v = 0 ;
422+ nscan = sscanf (vstr, " %d" , &v);
423+ if (nscan != 1 )
424+ {
425+ fprintf (stderr, " parse value failed\n " );
426+ return -1 ;
427+ }
428+
429+ ai.push_back (v);
430+ }
431+
432+ while (1 )
433+ {
434+ nscan = fscanf (fp, " %15[^,\n ]" , vstr);
435+ if (nscan != 1 )
436+ {
437+ break ;
438+ }
439+
440+ if (is_float)
441+ {
442+ af.push_back (vstr_to_float (vstr));
443+ }
444+ else
445+ {
446+ int v = 0 ;
447+ nscan = sscanf (vstr, " %d" , &v);
448+ if (nscan != 1 )
449+ {
450+ fprintf (stderr, " parse value failed\n " );
451+ return -1 ;
452+ }
453+
454+ ai.push_back (v);
455+ }
456+
457+ nscan = fscanf (fp, " %1[,]" , comma);
458+ if (nscan != 1 )
459+ {
460+ break ;
461+ }
332462 }
333463
334- bool is_float = vstr_is_float (vstr);
464+ if (is_float)
465+ {
466+ int len = (int )af.size ();
467+ fwrite (&len, sizeof (int ), 1 , mp);
468+ fwrite (af.data (), sizeof (float ), len, mp);
469+ }
470+ else
471+ {
472+ int len = (int )ai.size ();
473+ fwrite (&len, sizeof (int ), 1 , mp);
474+ fwrite (ai.data (), sizeof (int ), len, mp);
475+ }
476+ }
477+ else
478+ {
479+ fwrite (&id, sizeof (int ), 1 , mp);
335480
336481 if (is_float)
337482 {
0 commit comments