/*! *********************************************************************** * \brief * Parses the character array buf and writes global variable input, which is defined in * configfile.h. This hack will continue to be necessary to facilitate the addition of * new parameters through the Map[] mechanism (Need compiler-generated addresses in map[]). * \param buf * buffer to be parsed * \param bufsize * buffer size of buffer *********************************************************************** */ void ParseContent (char *buf, int bufsize) { char *items[MAX_ITEMS_TO_PARSE]; int MapIdx; int item = 0; int InString = 0, InItem = 0; char *p = buf; char *bufend = &buf[bufsize]; int IntContent; double DoubleContent; int i; // Stage one: Generate an argc/argv-type list in items[], without comments and whitespace. // This is context insensitive and could be done most easily with lex(1). while (p < bufend) { switch (*p) { case 13: p++; break; case '#': // Found comment *p = '\0'; // Replace '#' with '\0' in case of comment immediately following integer or string while (*p != '\n' && p < bufend) // Skip till EOL or EOF, whichever comes first p++; InString = 0; InItem = 0; break; case '\n': InItem = 0; InString = 0; *p++='\0'; break; case ' ': case '\t': // Skip whitespace, leave state unchanged if (InString) p++; else { // Terminate non-strings once whitespace is found *p++ = '\0'; InItem = 0; } break; case '"': // Begin/End of String *p++ = '\0'; if (!InString) { items[item++] = p; InItem = ~InItem; } else InItem = 0; InString = ~InString; // Toggle break; default: if (!InItem) { items[item++] = p; InItem = ~InItem; } p++; } } item--; for (i=0; i<item; i+= 3) { if (0 > (MapIdx = ParameterNameToMapIndex (items[i]))) { snprintf (errortext, ET_SIZE, " Parsing error in config file: Parameter Name '%s' not recognized.", items[i]); error (errortext, 300); } if (strcasecmp ("=", items[i+1])) { snprintf (errortext, ET_SIZE, " Parsing error in config file: '=' expected as the second token in each line."); error (errortext, 300); } // Now interpret the Value, context sensitive... switch (Map[MapIdx].Type) { case 0: // Numerical if (1 != sscanf (items[i+2], "%d", &IntContent)) { snprintf (errortext, ET_SIZE, " Parsing error: Expected numerical value for Parameter of %s, found '%s'.", items[i], items[i+2]); error (errortext, 300); } * (int *) (Map[MapIdx].Place) = IntContent; printf ("."); break; case 1: strncpy ((char *) Map[MapIdx].Place, items [i+2], FILE_NAME_SIZE); printf ("."); break; case 2: // Numerical double if (1 != sscanf (items[i+2], "%lf", &DoubleContent)) { snprintf (errortext, ET_SIZE, " Parsing error: Expected numerical value for Parameter of %s, found '%s'.", items[i], items[i+2]); error (errortext, 300); } * (double *) (Map[MapIdx].Place) = DoubleContent; printf ("."); break; default: assert ("Unknown value type in the map definition of configfile.h"); } } memcpy (input, &configinput, sizeof (InputParameters)); }
static XDAS_Int32 ParseContent (XDAS_Int8 *buf, XDAS_Int32 bufsize) { /* Total number of parameter initialisation lines in the file * this excludes comment lines and blank lines if any */ XDAS_Int8 *items[MAX_ITEMS_TO_PARSE]; XDAS_Int32 item = 0; /* Index into the token map array - into which the * corresponding parameter values needs to be initialised * from the param file */ XDAS_Int32 MapIdx; /* Flags - which indicate the current state of the state machine * InString - indicates that buffer pointer is currently in between a valid * string - which inturn can contain multiple items * InItem - indicates that buffer pointer is within a valid item */ XDAS_Int32 InString = 0, InItem = 0; /* Dynamic pointer movign allong the param file buffer byte-by-byte */ XDAS_Int8 *p = buf; /* end of buffer - used for terminating the parse loop */ XDAS_Int8 *bufend = &buf[bufsize]; /* Parameter value read from the file */ XDAS_Int32 IntContent; /* loop counter */ XDAS_Int32 i; FILE *fpErr = stderr; /* Stage one: Generate an argc/argv-type list in items[], * excluding comments and whitespaces. This is context insensitive * and could be done most easily with lex(1). */ while (p < bufend) { switch (*p) { /* blank space - skip the character and go to next */ case 13: p++; break; /* Found a comment line skip all characters until end-of-line is found */ case '#': *p = '\0'; /* Replace '#' with '\0' in case of comment immediately following the integer or string */ // Skip till EOL or EOF, whichever comes first while (*p != '\n' && p < bufend) p++; InString = 0; InItem = 0; break; /* end of line - skip the character and go to next - reset the * InItem flag to indicate that we are not in any valid item */ case '\n': InItem = 0; InString = 0; *p++='\0'; break; /* Whitespaces and tabs indicate end of an item - if the state machine * is within a string */ case ' ': case '\t': // Skip whitespace, leave state unchanged if (InString) p++; else { /* Terminate non-strings once whitespace is found */ *p++ = '\0'; InItem = 0; } break; /* begining or end of string - toggle the string indication flag */ case '"': // Begin/End of String *p++ = '\0'; if (!InString) { items[item++] = p; InItem = ~InItem; } else InItem = 0; InString = ~InString; // Toggle break; /* Any othe character is taken into the item provided the * state machine is within a valid Item */ default: if (!InItem) { items[item++] = p; InItem = ~InItem; } p++; } } item--; /* for all the items found - check if they correspond to any valid parameter * names specified by the user through token map array * Note: Contigous three items are grouped into triplets. Thsi oredered * triplet is used to identify the valid parameter entry in the Token map * array. * First item in the triplet has to eb a parameter name. * Second item has to be a "=" symbol * Third item has to be a integer value. * Any violation of the above order in the triplets found would lead to error * condition */ for (i=0; i<item; i+= 3) { /* Get the map index into the token map array - corresponding to the * first item - in a item-triplet */ if (0 > (MapIdx = ParameterNameToMapIndex (items[i]))) { fprintf(fpErr, " \nParameter Name '%s' not recognized.. ", items[i]); return -1 ; } /* Assert if the second item is indeed "=" symbol */ if (strcmp ("=", (xdc_Char *)items[i+1])) { fprintf(fpErr, " \nfile: '=' expected as the second token in each line."); return -1 ; } /* Convert the thrid item into an integer and populate the corresponding * entry in the token map array with this value */ sscanf ((xdc_Char *)items[i+2], "%d", &IntContent) ; * ((XDAS_Int32 *) (sTokenMap[MapIdx].place)) = IntContent; } return 0 ; }
static XDAS_Int32 ParseContent (XDAS_Int8 *buf, XDAS_Int32 bufsize,int baseParamsOnly) { XDAS_Int8 *items[MAX_ITEMS_TO_PARSE]; XDAS_Int32 MapIdx; XDAS_Int32 item = 0; XDAS_Int32 InString = 0, InItem = 0; XDAS_Int8 *p = buf; XDAS_Int8 *bufend = &buf[bufsize]; XDAS_Int32 IntContent; XDAS_Int32 i; FILE *fpErr = stderr; // Stage one: Generate an argc/argv-type list in items[], without comments and // whitespace. // This is context insensitive and could be done most easily with lex(1). while (p < bufend) { switch (*p) { case 13: p++; break; case '#': // Found comment *p = '\0'; // Replace '#' with '\0' in case of comment //immediately following integer or string // Skip till EOL or EOF, whichever comes first while (*p != '\n' && p < bufend) p++; InString = 0; InItem = 0; break; case '\n': InItem = 0; InString = 0; *p++='\0'; break; case ' ': case '\t': // Skip whitespace, leave state unchanged if (InString) p++; else { // Terminate non-strings once whitespace is found *p++ = '\0'; InItem = 0; } break; case '"': // Begin/End of String *p++ = '\0'; if (!InString) { items[item++] = p; InItem = ~InItem; } else InItem = 0; InString = ~InString; // Toggle break; default: if (!InItem) { items[item++] = p; InItem = ~InItem; } p++; } } item--; for (i=0; i<item; i+= 3) { if (0 > (MapIdx = ParameterNameToMapIndex (items[i],baseParamsOnly))) { fprintf(fpErr, " \nParameter Name '%s' not recognized.. ", items[i]); return -1 ; } if (strcmp ("=", items[i+1])) { fprintf(fpErr, " \nfile: '=' expected as the second token in each line."); return -1 ; } sscanf (items[i+2], "%d", &IntContent) ; if(baseParamsOnly==1) { * ((XDAS_Int32 *) (sTokenMap_base[MapIdx].place)) = IntContent; } else { * ((XDAS_Int32 *) (sTokenMap[MapIdx].place)) = IntContent; } } return 0 ; }