VPR-6.0
|
00001 /** 00002 * @file 00003 * 00004 * Jason Luu 00005 * July 22, 2009 00006 * Tokenizer 00007 */ 00008 00009 #include <string.h> 00010 #include <assert.h> 00011 #include "util.h" 00012 #include "token.h" 00013 #include "ezxml.h" 00014 #include "read_xml_util.h" 00015 00016 enum e_token_type GetTokenTypeFromChar(INP enum e_token_type cur_token_type, INP char cur); 00017 00018 /** Returns a token list of the text for a given string. */ 00019 t_token *GetTokensFromString(INP const char* inString, OUTP int * num_tokens) 00020 { 00021 const char *cur; 00022 t_token * tokens; 00023 int i, in_string_index, prev_in_string_index; 00024 boolean has_null; 00025 enum e_token_type cur_token_type, new_token_type; 00026 00027 *num_tokens = i = 0; 00028 cur_token_type = TOKEN_NULL; 00029 00030 if(inString == NULL) { return NULL; }; 00031 00032 cur = inString; 00033 00034 /* Count number of tokens */ 00035 while(*cur) 00036 { 00037 new_token_type = GetTokenTypeFromChar(cur_token_type, *cur); 00038 if(new_token_type != cur_token_type) { 00039 cur_token_type = new_token_type; 00040 if(new_token_type != TOKEN_NULL) { 00041 i++; 00042 } 00043 } 00044 ++cur; 00045 } 00046 *num_tokens = i; 00047 00048 if(*num_tokens > 0) { 00049 tokens = my_calloc(*num_tokens + 1, sizeof(t_token)); 00050 } else { 00051 return NULL; 00052 } 00053 00054 /* populate tokens */ 00055 i = 0; 00056 in_string_index = 0; 00057 has_null = TRUE; 00058 prev_in_string_index = 0; 00059 cur_token_type = TOKEN_NULL; 00060 00061 cur = inString; 00062 00063 while(*cur) 00064 { 00065 00066 new_token_type = GetTokenTypeFromChar(cur_token_type, *cur); 00067 if(new_token_type != cur_token_type) { 00068 if(!has_null) { 00069 tokens[i-1].data[in_string_index - prev_in_string_index] = '\0'; /* NULL the end of the data string */ 00070 has_null = TRUE; 00071 } 00072 if(new_token_type != TOKEN_NULL) { 00073 tokens[i].type = new_token_type; 00074 tokens[i].data = my_strdup(inString + in_string_index); 00075 prev_in_string_index = in_string_index; 00076 has_null = FALSE; 00077 i++; 00078 } 00079 cur_token_type = new_token_type; 00080 } 00081 ++cur; 00082 in_string_index++; 00083 } 00084 00085 assert(i == *num_tokens); 00086 00087 tokens[*num_tokens].type = TOKEN_NULL; 00088 tokens[*num_tokens].data = NULL; 00089 00090 00091 /* Return the list */ 00092 return tokens; 00093 } 00094 00095 void freeTokens(INP t_token *tokens, INP int num_tokens) { 00096 int i; 00097 for(i = 0; i < num_tokens; i++) { 00098 free(tokens[i].data); 00099 } 00100 free(tokens); 00101 } 00102 00103 00104 enum e_token_type GetTokenTypeFromChar(INP enum e_token_type cur_token_type, INP char cur) { 00105 if(IsWhitespace(cur)) { 00106 return TOKEN_NULL; 00107 } else { 00108 if(cur == '[') { 00109 return TOKEN_OPEN_SQUARE_BRACKET; 00110 } else if(cur == ']') { 00111 return TOKEN_CLOSE_SQUARE_BRACKET; 00112 } else if(cur == '{') { 00113 return TOKEN_OPEN_SQUIG_BRACKET; 00114 } else if(cur == '}') { 00115 return TOKEN_CLOSE_SQUIG_BRACKET; 00116 } else if(cur == ':') { 00117 return TOKEN_COLON; 00118 } else if(cur == '.') { 00119 return TOKEN_DOT; 00120 } else if(cur >= '0' && cur <= '9' && cur_token_type != TOKEN_STRING) { 00121 return TOKEN_INT; 00122 } else { 00123 return TOKEN_STRING; 00124 } 00125 } 00126 } 00127 00128 boolean checkTokenType(INP t_token token, OUTP enum e_token_type token_type) { 00129 if(token.type != token_type) { 00130 return FALSE; 00131 } 00132 return TRUE; 00133 } 00134 00135 00136 00137 00138 void my_atof_2D(INOUTP float **matrix, INP int max_i, INP int max_j, INP char *instring) { 00139 int i, j; 00140 char *cur, *cur2, *copy, *final; 00141 int string_count; 00142 00143 copy = my_strdup(instring); 00144 final = copy; 00145 while(*final != '\0') { 00146 final++; 00147 } 00148 00149 00150 cur = copy; 00151 i = j = 0; 00152 while(cur != final) { 00153 string_count = 0; 00154 while(IsWhitespace(*cur) && cur != final) { 00155 if(j == max_j) { 00156 i++; 00157 j = 0; 00158 } 00159 cur++; 00160 } 00161 if(cur == final) { 00162 break; 00163 } 00164 cur2 = cur; 00165 while(!IsWhitespace(*cur2) && cur2 != final) { 00166 cur2++; 00167 } 00168 *cur2 = '\0'; 00169 assert(i < max_i && j < max_j); 00170 matrix[i][j] = atof(cur); 00171 j++; 00172 cur = cur2; 00173 *cur = ' '; 00174 } 00175 00176 assert ((i == max_i && j == 0) || (i == max_i - 1 && j == max_j)); 00177 00178 free(copy); 00179 } 00180