Beispiel #1
0
bool GCFS_Utils::ParseConfigString(char * string, GCFS_Utils::keyValueArray_t& vValues)
{
   // replace $(process) with @(process) to get rid of '$' character. It is delimiter as well
   ReplaceToken(string, "$(process)", "@(process)");
   
   // Parse all the commands/assignments
   char * pPos = NULL;
   // TODO: Write own tokenizer to allow escaping and quotes to avoid splitting
   char * pToken = strtok_r(string, GCFS_CONFIG_DELIMITERS, &pPos);
   
   while(pToken != NULL)
   {
      ReplaceToken(pToken, "@(process)", "$(process)");
      
      vValues.push_back(ParseAssignment(pToken));
      
      pToken = strtok_r(NULL, GCFS_CONFIG_DELIMITERS, &pPos);
   }
   
   return true;
}
BOOLEAN ScreenTokenList( LIST TokenList )
{
  BOOLEAN  ReturnValue = FALSE;   /* Used to hold the function return value. */
  Token    TokenToCharacterize;   /* The current token being processed.            */
  CARDINAL Error;                 /* Used to hold the error code returned by the
                                     list functions in LIST.H.                     */
  pSTRING  pUppercaseToken;       /* Pointer to string in which to stored uppercase*/
                                  /* copy of the TokenToCharacterize               */
  unsigned int   TokenPosition;


  /* Start with the first token in the list. */
  GoToStartOfTokenList(TokenList,&Error);

  if (Error)
  {
    ReportError(Internal_Error);  /* Report the error. */
    return ReturnValue;
  } /* endif */

  /* Loop through the tokens in the list. */
  while ( (Error == 0) && (! TokenListEmpty(TokenList,&Error)  ) )
  {
    /* Get the current token. */
    GetToken(TokenList,sizeof(Token),&TokenToCharacterize,&TokenPosition,&Error);

    if (Error == 0)
    {
      /* Select the action to take based on the current value of the TokenType field. */
      switch (TokenToCharacterize.TokenType)
      {
  case Space:
  case MultiSpace:
  case Tab:
  case MultiTab:
    DeleteToken(TokenList,&Error);
    break;
        case Separator:
          switch (TokenToCharacterize.pTokenText[0])
          {
            case ':':
              TokenToCharacterize.TokenType = Colon;
              break;
            case ';':
              TokenToCharacterize.TokenType = SemiColon;
              break;
            case ',':
              TokenToCharacterize.TokenType = Comma;
              break;
            case '(' :
              TokenToCharacterize.TokenType = Open_Paren;
              break;
            case ')' :
              TokenToCharacterize.TokenType = Close_Paren;
              break;
            case '[' :
              TokenToCharacterize.TokenType = Open_Bracket;
              break;
            case ']' :
              TokenToCharacterize.TokenType = Close_Bracket;
              break;
            case '{' :
              TokenToCharacterize.TokenType = Open_Brace;
              break;
            case '}' :
              TokenToCharacterize.TokenType = Close_Brace;
              break;
            case '=' :
              TokenToCharacterize.TokenType = LVM_EQ_Sign;
              break;
          } /* endswitch */
          /* Replace the current token in the list. */
          ReplaceToken(TokenList,sizeof(Token),&TokenToCharacterize,TokenPosition,&Error);
          break;
        case String:
          {
            unsigned int    index1=0;

            /* Compare the identifier's value against the value element of the */
            /* table that is marked as a reserved word until a match is found  */
            /* or the end of the table is reached.   (The last element in the  */
            /* table is marked as type "None" such that when this element is   */
            /* reached, the while loop will terminate.                         */
            /* Convert the string to uppercase before comparing it with the    */
            /* tables elements because the elements are uppercase.             */
            pUppercaseToken = (pSTRING)malloc(strlen(TokenToCharacterize.pTokenText)+1);
            if (pUppercaseToken != NULL)
            {

              for (index1=0; index1 < strlen(TokenToCharacterize.pTokenText); index1++)
              {
                pUppercaseToken[index1] = toupper(TokenToCharacterize.pTokenText[index1]);
              } /* endfor */
              /* Append NULL character to uppercase version of string */
              pUppercaseToken[index1] = '\0';

              index1 = 0;
              while (ReservedWords[index1].TokenType != Eof)
              {
                /* Compare the uppercase version of the token with the TokenText */
                /* field in the ReservedWords table. */
                if (strcmp(pUppercaseToken, ReservedWords[index1].pTokenText)==0)
                {
                  /* Since the uppercase version of the token matched an entry */
                  /* in the ReservedWords table, change the TokenType field    */
                  /* from String to the TokenType value for this table entry.  */
                  TokenToCharacterize.TokenType = ReservedWords[index1].TokenType;


                  /* If the token's ordinal value is greater than FileNameStr */
                  if (TokenToCharacterize.TokenType > FileNameStr)
                  {
                    /* Free the memory allocated for this token's string value, */
                    /* it can never be a valid acceptable name nor filename     */
                    /* because it does not begin with a letter. */

                    /* NOTE: Testing the range of ordinal values is made possible */
                    /* because the grouping of the entries in the enumerated type */
                    /* TokenTypes has been specifically done such that any value  */
                    /* above FileNameStr represents a token which can never be an */
                    /* "acceptable character" string or a filename. */
                    if (TokenToCharacterize.pTokenText != NULL)
                      free(TokenToCharacterize.pTokenText);

                    /* Make sure the pointer to this token's text field is NULL */
                    TokenToCharacterize.pTokenText = NULL;
                  } /* endif */

                  /* Replace the current token in the list. */
                  ReplaceToken(TokenList,sizeof(Token),&TokenToCharacterize,TokenPosition,&Error);

                  break;
                }
                else
                {
                  /* Increment the index1 variable to access the next element. */
                  index1++;
                } /* endif */
              } /* endwhile */
              /* Free the memory used to store the uppercase version of the token */
              free(pUppercaseToken);
            }
            else
            {
              ReportError(Internal_Error);  /* Report the error. */
              return ReturnValue;
            } /* endif */

            /* If the token type is still String (i.e. it was not a reserved word) */
            if (TokenToCharacterize.TokenType == String)
            {
              { /* start of block */
                unsigned int    index2=0;
                unsigned int    index3=0;

                /* Assume this string contains only "acceptable characters" as define */
                /* by the grammar.  Then check each character in the string to make   */
                /* sure this is truly the case.  If a character is found that is not  */
                /* "acceptable", change the TokenType to FileNameStr.                 */
                TokenToCharacterize.TokenType = AcceptableCharsStr;
                for (index2=0; index2 < strlen(TokenToCharacterize.pTokenText); index2++)
                {
                  if (!isgraph(TokenToCharacterize.pTokenText[index2]))
                  {
                    /* An OS/2 filename cannot contain a forward slash or */
                    /* double quote so check the remaining characters to  */
                    /* make sure neither of these two exist in the        */
                    /* remainder of the string. */
                    for (index3=index2;index3 < strlen(TokenToCharacterize.pTokenText); index3++)
                    {
                      if ((TokenToCharacterize.pTokenText[index2] == '/')
                          ||
                          (TokenToCharacterize.pTokenText[index2] == '"'))
                      {
                        break;
                      } /* endif */
                      TokenToCharacterize.TokenType = FileNameStr;
                    } /* endfor */
                    break;
                  } /* endif */
                } /* endfor */

                /* Replace the current token in the list. */
                ReplaceToken(TokenList,sizeof(Token),&TokenToCharacterize,TokenPosition,&Error);
              } /* end of block */
            } /* endif */
          }
          break;
      } /* endswitch */

      /* If the current token is NOT Eof, Space, MultiSpace, Tab, MultiTab,
   advance to the next item in the list. */
      switch(TokenToCharacterize.TokenType)
      {
   case Eof:
   case Space:
   case MultiSpace:
   case Tab:
   case MultiTab:
     break;
   default:
     NextToken(TokenList,&Error);
     break;
      }
      /* If Eof, then break out of loop */
      if ( TokenToCharacterize.TokenType == Eof )
   break;
    } /* endif */
  } /* end of while */

  /* If we successfully characterized all of the tokens */
  if ( !Error )
  {
    ReturnValue = TRUE;
  }
  else
  {
    ReturnValue = FALSE;
    ReportError(Internal_Error);  /* Report the error. */
  }

  return ReturnValue;
}