Advertisement
Guest User

Untitled

a guest
Oct 25th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. Token* TokenizeString(char *input)
  2. {
  3.     const int size_of_token = 30;
  4.     int len = 0;
  5.     int num_of_tokens = 0;
  6.     int i = 0, j = 0, k = 0;
  7.     int newline = 1;
  8.     char *buf;
  9.     //char **token = NULL;
  10.     char token_word[30] = {0};
  11.     char tmp[20] = {0};
  12.     FILE *f = NULL;
  13.  
  14.     Token *tokens;
  15.  
  16.     len = strlen(input);
  17.     buf = (char *)calloc(len, sizeof(char));
  18.     if (buf == NULL)
  19.         printf("Couldn't allocate memory for buf\n");
  20.  
  21.     num_of_tokens = len / 3;
  22.  
  23.     tokens = calloc(num_of_tokens, sizeof(Token));
  24.     for (i = 0; i < num_of_tokens; i++)
  25.         tokens[i].word = calloc(size_of_token, sizeof(char));
  26.  
  27.     input = RemoveRedundantNewLines(input);
  28.     f = fopen("newline.txt", "w");
  29.  
  30.     if (f != NULL)
  31.         fputs(input, f);
  32.  
  33.     fclose(f);
  34.  
  35.     for (i = 0; i < len; i++)
  36.     {
  37.         if (input[i] != ' ' && input[i] != '\n' && input[i] != '\t' && input[i] != ',')
  38.         {
  39.             token_word[j++] = input[i];
  40.         }
  41.         else if (token_word[0] != 0)
  42.         {
  43.             j = 0;
  44.             tokens[k].line = newline;
  45.             strcpy(tokens[k++].word, token_word);
  46.             ZeroString(token_word, size_of_token);
  47.         }
  48.  
  49.         if (input[i] == '\n')
  50.         {
  51.             newline++;
  52.         }
  53.     }
  54.     tokens[k].line = newline;
  55.     strcpy(tokens[k].word, token_word);
  56.  
  57.     f = fopen("tokens.txt", "w");
  58.  
  59.     for (i = 0; i <= k; i++)
  60.     {
  61.         sprintf(tmp, ":%d  ", tokens[i].line);
  62.         strcat(buf, tmp);
  63.         strcat(buf, tokens[i].word);
  64.         strcat(buf, "\n");
  65.     }
  66.  
  67.     fputs(buf, f);
  68.     fclose(f);
  69.     free(buf);
  70.  
  71.     return tokens;
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement