void SCRIPT_PutString ( int32 scripthandle, const char * sectionname, const char * entryname, const char * string ) { char *raw,*p; const char *q; int len = 3; if (!string) string = ""; for (q=string; *q; q++) { if (*q == '\r' || *q == '\n' || *q == '\t' || *q == '\\' || *q == '"') len+=2; else if (*q >= ' ') len++; } p = raw = Bmalloc(len); *(p++) = '"'; for (q=string; *q; q++) { if (*q == '\r') { *(p++) = '\\'; *(p++) = 'r'; } else if (*q == '\n') { *(p++) = '\\'; *(p++) = 'n'; } else if (*q == '\t') { *(p++) = '\\'; *(p++) = 't'; } else if (*q == '\\' || *q == '"') { *(p++) = '\\'; *(p++) = *q; } else if (*q >= ' ') *(p++) = *q; } *(p++) = '"'; *p=0; SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); Bfree(raw); }
void SCRIPT_PutRaw ( int32 scripthandle, const char * sectionname, const char * entryname, const char * raw ) { SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
void SCRIPT_PutDoubleString ( int32_t scripthandle, char * sectionname, char * entryname, char * string1, char * string2 ) { char *raw,*q,*p; int32_t len = 6; if (!string1) string1 = ""; if (!string2) string2 = ""; for (q=string1; *q; q++) { if (*q == '\r' || *q == '\n' || *q == '\t' || *q == '\\' || *q == '"') len+=2; else if (*q >= ' ') len++; } for (q=string2; *q; q++) { if (*q == '\r' || *q == '\n' || *q == '\t' || *q == '\\' || *q == '"') len+=2; else if (*q >= ' ') len++; } p = raw = Bmalloc(len); *(p++) = '"'; for (q=string1; *q; q++) { if (*q == '\r') { *(p++) = '\\'; *(p++) = 'r'; } else if (*q == '\n') { *(p++) = '\\'; *(p++) = 'n'; } else if (*q == '\t') { *(p++) = '\\'; *(p++) = 't'; } else if (*q == '\\' || *q == '"') { *(p++) = '\\'; *(p++) = *q; } else if (*q >= ' ') *(p++) = *q; } *(p++) = '"'; *(p++) = ' '; *(p++) = '"'; for (q=string2; *q; q++) { if (*q == '\r') { *(p++) = '\\'; *(p++) = 'r'; } else if (*q == '\n') { *(p++) = '\\'; *(p++) = 'n'; } else if (*q == '\t') { *(p++) = '\\'; *(p++) = 't'; } else if (*q == '\\' || *q == '"') { *(p++) = '\\'; *(p++) = *q; } else if (*q >= ' ') *(p++) = *q; } *(p++) = '"'; *p=0; SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); Bfree(raw); }
void SCRIPT_PutBoolean ( int32 scripthandle, const char * sectionname, const char * entryname, boolean boole ) { char raw[2] = "0"; if (boole) raw[0] = '1'; SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
void SCRIPT_PutDouble ( int32 scripthandle, const char * sectionname, const char * entryname, double number, boolean UNUSED(defaultvalue) ) { char raw[64]; sprintf(raw, "%g", number); SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
void SCRIPT_PutDouble ( int32_t scripthandle, char * sectionname, char * entryname, double number, int32_t defaultvalue ) { char raw[64]; UNREFERENCED_PARAMETER(defaultvalue); Bsprintf(raw, "%g", number); SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
void SCRIPT_PutNumber ( int32 scripthandle, const char * sectionname, const char * entryname, int32 number, boolean hexadecimal, boolean UNUSED(defaultvalue) ) { char raw[64]; if (hexadecimal) sprintf(raw, "0x%X", number); else sprintf(raw, "%d", number); SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
void SCRIPT_PutNumber ( int32_t scripthandle, char * sectionname, char * entryname, int32_t number, int32_t hexadecimal, int32_t defaultvalue ) { char raw[64]; UNREFERENCED_PARAMETER(defaultvalue); if (hexadecimal) Bsprintf(raw, "0x%X", number); else Bsprintf(raw, "%d", number); SCRIPT_AddEntry(scripthandle, sectionname, entryname, raw); }
int32 SCRIPT_ParseBuffer(int32 scripthandle, char *data, int32 length) { char *fence = data + length; char *dp, *sp, ch=0, lastch=0; char *currentsection = ""; char *currententry = NULL; char *currentvalue = NULL; enum { ParsingIdle, ParsingSectionBegin, ParsingSectionName, ParsingEntry, ParsingValueBegin, ParsingValue }; enum { ExpectingSection = 1, ExpectingEntry = 2, ExpectingAssignment = 4, ExpectingValue = 8, ExpectingComment = 16 }; int state; int expect; int linenum=1; int rv = 0; #define SETRV(v) if (v>rv||rv==0) rv=v if (!data) return 1; if (length < 0) return 1; dp = sp = data; state = ParsingIdle; expect = ExpectingSection | ExpectingEntry; #define EATLINE(p) while (length > 0 && *p != '\n' && *p != '\r') { p++; length--; } #define LETTER() { lastch = ch; ch = *(sp++); length--; } while (length > 0) { switch (state) { case ParsingIdle: LETTER(); switch (ch) { // whitespace case ' ': case '\t': continue; case '\n': if (lastch == '\r') continue; linenum++; continue; case '\r': linenum++; continue; case ';': /*case '#':*/ EATLINE(sp); continue; case '[': if (!(expect & ExpectingSection)) { // Unexpected section start printf("Unexpected start of section on line %d.\n", linenum); SETRV(-1); EATLINE(sp); continue; } else { state = ParsingSectionBegin; continue; } default: if (isalpha(ch)) { if (!(expect & ExpectingEntry)) { // Unexpected name start printf("Unexpected entry label on line %d.\n", linenum); SETRV(-1); EATLINE(sp); continue; } else { currententry = dp = sp-1; state = ParsingEntry; continue; } } else { // Unexpected character printf("Illegal character (ASCII %d) on line %d.\n", ch, linenum); SETRV(-1); EATLINE(sp); continue; } } case ParsingSectionBegin: currentsection = dp = sp; state = ParsingSectionName; case ParsingSectionName: LETTER(); switch (ch) { case '\n': case '\r': // Unexpected newline printf("Unexpected newline on line %d.\n", linenum); SETRV(-1); state = ParsingIdle; linenum++; continue; case ']': *(dp) = 0; // Add new section expect = ExpectingSection | ExpectingEntry; state = ParsingIdle; EATLINE(sp); continue; default: dp++; continue; } case ParsingEntry: LETTER(); switch (ch) { case ';': /*case '#':*/ // unexpected comment EATLINE(sp); printf("Unexpected comment on line %d.\n", linenum); SETRV(-1); case '\n': case '\r': // Unexpected newline printf("Unexpected newline on line %d.\n", linenum); SETRV(-1); expect = ExpectingSection | ExpectingEntry; state = ParsingIdle; linenum++; continue; case '=': // Entry name finished, now for the value while (*dp == ' ' || *dp == '\t') dp--; *(++dp) = 0; state = ParsingValueBegin; continue; default: dp++; continue; } case ParsingValueBegin: currentvalue = dp = sp; state = ParsingValue; case ParsingValue: LETTER(); switch (ch) { case '\n': case '\r': // value complete, add it using parsed name while (*dp == ' ' && *dp == '\t') dp--; *(dp) = 0; while (*currentvalue == ' ' || *currentvalue == '\t') currentvalue++; state = ParsingIdle; linenum++; SCRIPT_AddSection(scripthandle,currentsection); SCRIPT_AddEntry(scripthandle,currentsection,currententry,currentvalue); continue; default: dp++; continue; } default: length=0; continue; } } if (sp > fence) printf("Stepped outside the fence!\n"); return rv; }