/* ============== = = SCRIPT_PutNumber = ============== */ void SCRIPT_PutNumber ( int32 scripthandle, char * sectionname, char * entryname, int32 number, boolean hexadecimal, boolean defaultvalue ) { /* DDOI - I don't know what "defaultvalue" is for so it's ignored */ scriptnode_t *head; scriptnode_t *section; scriptnode_t *node; if(scripthandle >= MAX_SCRIPTS || scripthandle < 0) return; head = script_headnode[scripthandle]; /* We're screwed if there's no head */ if (head == NULL) return; section = SCRIPT_findinchildren (head, sectionname); if (section == NULL) { /* Add the section if it does not exist */ section = SCRIPT_constructnode (); section->type = SCRIPTFLAG_CATEGORY; section->key = SCRIPT_copystring (sectionname); SCRIPT_addchild (head, section); } node = SCRIPT_findinchildren (section, entryname); if (node == NULL) { /* Add the section if it does not exist */ node = SCRIPT_constructnode (); node->key = SCRIPT_copystring (entryname); SCRIPT_addchild (section, node); } if (hexadecimal) node->type = SCRIPTFLAG_HEX; else node->type = SCRIPTFLAG_DECIMAL; node->data.number = number; }
/* ============== = = SCRIPT_PutDoubleString = ============== */ void SCRIPT_PutDoubleString ( int32 scripthandle, char * sectionname, char * entryname, char * string1, char * string2 ) { scriptnode_t *head; scriptnode_t *section; scriptnode_t *node; if(scripthandle >= MAX_SCRIPTS || scripthandle < 0) return; head = script_headnode[scripthandle]; /* We're screwed if there's no head */ if (head == NULL) return; section = SCRIPT_findinchildren (head, sectionname); if (section == NULL) { /* Add the section if it does not exist */ section = SCRIPT_constructnode (); section->type = SCRIPTFLAG_CATEGORY; section->key = SCRIPT_copystring (sectionname); SCRIPT_addchild (head, section); } node = SCRIPT_findinchildren (section, entryname); if (node == NULL) { /* Add the section if it does not exist */ node = SCRIPT_constructnode (); node->type = SCRIPTFLAG_TWOSTRING; node->key = SCRIPT_copystring (entryname); SCRIPT_addchild (section, node); } else { free (node->data.string[0]); free (node->data.string[1]); } node->data.string[0] = SCRIPT_copystring (string1); node->data.string[1] = SCRIPT_copystring (string2); }
int32 SCRIPT_Load ( char * filename ) { FILE *fp; char curline[128]; scriptnode_t *headnode = NULL; scriptnode_t *cur_subsection = NULL; if (script_numscriptsopen == MAX_SCRIPTS) return -1; /* The main program does not check for any sort of */ /* error in loading, so each SCRIPT_ function needs */ /* to check if the handle is -1 before doing anything */ fp = fopen (filename, "r"); if (fp == NULL) return -1; /* Start loading the script */ /* Loads and parse the entire file into a tree */ script_numscriptsopen++; /* script_nexthandle is the current handle until we increment it */ script_headnode[script_nexthandle] = SCRIPT_constructnode (); headnode = script_headnode[script_nexthandle]; memset (curline, 0, 128); while (fgets (curline, 128, fp)) { /* Skip comments */ if (curline[0] == ';') continue; /* Parse line */ /* We have two options... it starts with a [, making it */ /* a new subsection (child of headnode) or it starts with */ /* a letter, making it a child of a subsection. */ if (curline[0] == '[') { scriptnode_t *node; int i; /* Remove [] manually */ for (i = 0; i < 127; i++) curline[i] = curline[i+1]; for (i = 127; i >= 0; i--) { if (curline[i] == ']') { curline[i] = '\0'; break; } else { curline[i] = '\0'; } } /* Insert into head */ node = SCRIPT_constructnode (); node->type = SCRIPTFLAG_CATEGORY; node->key = SCRIPT_copystring (curline); SCRIPT_addchild (headnode, node); cur_subsection = node; /* printf ("Working in section \"%s\"\n", node->key); */ } else if (isalpha (curline[0])) { scriptnode_t *node; /* Ignore if not under a subsection */ if (cur_subsection == NULL) continue; node = SCRIPT_constructnode (); /* TODO: Parse line here */ SCRIPT_parseline (curline, node); if (node != NULL) { /* printf ("Adding node with key \"%s\"\n", node->key); */ SCRIPT_addchild (cur_subsection, node); } } memset (curline, 0, 128); } fclose (fp); return script_nexthandle++; /* postincrement is important here */ }
/* Fills in a scriptnode with the interpreted contents of a line */ static void SCRIPT_parseline (char *curline, scriptnode_t *node) { char token[128]; int i; /* Needs to handle 5 cases: */ /* key = someint */ /* key = 0xsomehexnum */ /* key = ~ */ /* key = "onestring" */ /* key = "two" "strings" */ assert (node != NULL); assert (curline != NULL); i = SCRIPT_getnexttoken (curline, token, 0); node->key = SCRIPT_copystring (token); i = SCRIPT_getnexttoken (curline, token, i); /* Sanity check... this token should be "=" */ if (strcmp (token, "=")) { /* Error state, free the memory allocated */ SCRIPT_recursivefree (node); return; } /* This is where the real fun begins... */ /* we can begin to determine which of the 6 */ /* possibilities the node is now */ i = SCRIPT_getnexttoken (curline, token, i); if (!strncmp (token, "0x", 2)) { /* Found a hex digit! */ node->type = SCRIPTFLAG_HEX; node->data.number = strtol (token, NULL, 16); }else if (token[strlen(token) - 1] == 'f') { /* Found a float */ node->type = SCRIPTFLAG_FLOAT; node->data.floatnumber = atof(token); }else if (isdigit (token[0])) { /* Found a number! */ node->type = SCRIPTFLAG_DECIMAL; node->data.number = atoi (token); }else if (token[0] == '-' && isdigit (token[1])) { /* Found a negative number! */ node->type = SCRIPTFLAG_DECIMAL; node->data.number = atoi (token); //- FIX_00067: *.cfg parser fails to read negative values (as it could be for the Midi selection) // was node->data.number = 0; Can't find a reason for this. }else if (token[0] == '~') { /* Found a ... who knows */ node->type = SCRIPTFLAG_DECIMAL; node->data.number = -1; }else if (token[0] == '"') { char workbuf[128]; int r; /* Found one of possibly two strings */ strcpy (workbuf, token); while (token != NULL && workbuf[strlen(workbuf)-1] != '"') { i = SCRIPT_getnexttoken (curline, token, i); strcat (workbuf, " "); strcat (workbuf, token); } r = sscanf(workbuf, "\"%[^\"]\"", workbuf); if (r == 0) workbuf[0] = '\0'; node->type = SCRIPTFLAG_ONESTRING; node->data.string[0] = SCRIPT_copystring (workbuf); /* Check for a second string */ i = SCRIPT_getnexttoken (curline, token, i); if (token[0] == '"') { strcpy (workbuf, token); while (token != NULL && workbuf[strlen(workbuf)-1] != '"') { i = SCRIPT_getnexttoken (curline, token, i); strcat (workbuf, " "); strcat (workbuf, token); } r = sscanf(workbuf, "\"%[^\"]\"", workbuf); if (r == 0) workbuf[0] = '\0'; node->type = SCRIPTFLAG_TWOSTRING; node->data.string[1] = SCRIPT_copystring (workbuf); } } else { /* Error state! */ SCRIPT_recursivefree (node); } }