VPR-7.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
token.c
Go to the documentation of this file.
1 /**
2  * Jason Luu
3  * July 22, 2009
4  * Tokenizer
5  */
6 
7 #include <string.h>
8 #include <assert.h>
9 #include "util.h"
10 #include "token.h"
11 #include "ezxml.h"
12 #include "read_xml_util.h"
13 
14 enum e_token_type GetTokenTypeFromChar(INP enum e_token_type cur_token_type,
15  INP char cur);
16 
17 /* Returns a token list of the text for a given string. */
18 t_token *GetTokensFromString(INP const char* inString, OUTP int * num_tokens) {
19  const char *cur;
20  t_token * tokens;
21  int i, in_string_index, prev_in_string_index;
22  boolean has_null;
23  enum e_token_type cur_token_type, new_token_type;
24 
25  *num_tokens = i = 0;
26  cur_token_type = TOKEN_NULL;
27 
28  if (inString == NULL) {
29  return NULL;
30  };
31 
32  cur = inString;
33 
34  /* Count number of tokens */
35  while (*cur) {
36  new_token_type = GetTokenTypeFromChar(cur_token_type, *cur);
37  if (new_token_type != cur_token_type) {
38  cur_token_type = new_token_type;
39  if (new_token_type != TOKEN_NULL) {
40  i++;
41  }
42  }
43  ++cur;
44  }
45  *num_tokens = i;
46 
47  if (*num_tokens > 0) {
48  tokens = (t_token*)my_calloc(*num_tokens + 1, sizeof(t_token));
49  } else {
50  return NULL;
51  }
52 
53  /* populate tokens */
54  i = 0;
55  in_string_index = 0;
56  has_null = TRUE;
57  prev_in_string_index = 0;
58  cur_token_type = TOKEN_NULL;
59 
60  cur = inString;
61 
62  while (*cur) {
63 
64  new_token_type = GetTokenTypeFromChar(cur_token_type, *cur);
65  if (new_token_type != cur_token_type) {
66  if (!has_null) {
67  tokens[i - 1].data[in_string_index - prev_in_string_index] =
68  '\0'; /* NULL the end of the data string */
69  has_null = TRUE;
70  }
71  if (new_token_type != TOKEN_NULL) {
72  tokens[i].type = new_token_type;
73  tokens[i].data = my_strdup(inString + in_string_index);
74  prev_in_string_index = in_string_index;
75  has_null = FALSE;
76  i++;
77  }
78  cur_token_type = new_token_type;
79  }
80  ++cur;
81  in_string_index++;
82  }
83 
84  assert(i == *num_tokens);
85 
86  tokens[*num_tokens].type = TOKEN_NULL;
87  tokens[*num_tokens].data = NULL;
88 
89  /* Return the list */
90  return tokens;
91 }
92 
93 void freeTokens(INP t_token *tokens, INP int num_tokens) {
94  int i;
95  for (i = 0; i < num_tokens; i++) {
96  free(tokens[i].data);
97  }
98  free(tokens);
99 }
100 
102  INP char cur) {
103  if (IsWhitespace(cur)) {
104  return TOKEN_NULL;
105  } else {
106  if (cur == '[') {
108  } else if (cur == ']') {
110  } else if (cur == '{') {
112  } else if (cur == '}') {
114  } else if (cur == ':') {
115  return TOKEN_COLON;
116  } else if (cur == '.') {
117  return TOKEN_DOT;
118  } else if (cur >= '0' && cur <= '9' && cur_token_type != TOKEN_STRING) {
119  return TOKEN_INT;
120  } else {
121  return TOKEN_STRING;
122  }
123  }
124 }
125 
126 boolean checkTokenType(INP t_token token, OUTP enum e_token_type token_type) {
127  if (token.type != token_type) {
128  return FALSE;
129  }
130  return TRUE;
131 }
132 
133 void my_atof_2D(INOUTP float **matrix, INP int max_i, INP int max_j,
134  INP char *instring) {
135  int i, j;
136  char *cur, *cur2, *copy, *final;
137 
138  copy = my_strdup(instring);
139  final = copy;
140  while (*final != '\0') {
141  final++;
142  }
143 
144  cur = copy;
145  i = j = 0;
146  while (cur != final) {
147  while (IsWhitespace(*cur) && cur != final) {
148  if (j == max_j) {
149  i++;
150  j = 0;
151  }
152  cur++;
153  }
154  if (cur == final) {
155  break;
156  }
157  cur2 = cur;
158  while (!IsWhitespace(*cur2) && cur2 != final) {
159  cur2++;
160  }
161  *cur2 = '\0';
162  assert(i < max_i && j < max_j);
163  matrix[i][j] = atof(cur);
164  j++;
165  cur = cur2;
166  *cur = ' ';
167  }
168 
169  assert((i == max_i && j == 0) || (i == max_i - 1 && j == max_j));
170 
171  free(copy);
172 }
173 
boolean checkTokenType(INP t_token token, OUTP enum e_token_type token_type)
Definition: token.c:126
void my_atof_2D(INOUTP float **matrix, INP int max_i, INP int max_j, INP char *instring)
Definition: token.c:133
enum e_token_type GetTokenTypeFromChar(INP enum e_token_type cur_token_type, INP char cur)
Definition: token.c:101
void * my_calloc(size_t nelem, size_t size)
Definition: util.c:132
t_token * GetTokensFromString(INP const char *inString, OUTP int *num_tokens)
Definition: token.c:18
boolean IsWhitespace(char c)
e_token_type
Definition: token.h:10
#define INOUTP
Definition: util.h:21
Definition: util.h:12
char * data
Definition: token.h:24
#define INP
Definition: util.h:19
#define OUTP
Definition: util.h:20
Definition: token.h:22
enum e_token_type type
Definition: token.h:23
void freeTokens(INP t_token *tokens, INP int num_tokens)
Definition: token.c:93
char * my_strdup(const char *str)
Definition: util.c:101
Definition: util.h:12