Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. //
  2.  
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <memory.h>
  7.  
  8. typedef unsigned char bool;
  9.  
  10. const int MaxCount = 255;
  11. FILE* pFile = NULL;
  12. char* Buf;
  13.  
  14. typedef struct
  15. {
  16. char* s;
  17. int size, allocated_size;
  18. } string;
  19.  
  20. typedef struct
  21. {
  22. string* S;
  23. int size, allocated_size;
  24. } array_of_string;
  25.  
  26. array_of_string Words;
  27.  
  28. void init_s(string* S)
  29. {
  30. S->size = 0;
  31. S->allocated_size = 1;
  32. S->s = (char*)malloc((S->allocated_size + 1) * sizeof(char));
  33. S->s[0] = '\0';
  34. }
  35.  
  36. void init_as(array_of_string *A)
  37. {
  38. A->size = 0;
  39. A->allocated_size = 1;
  40. A->S = (string*)malloc(A->allocated_size * sizeof(string));
  41. }
  42.  
  43. void push_back_s(string *S, char c)
  44. {
  45. if (S->size++ == S->allocated_size)
  46. {
  47. S->allocated_size *= 2;
  48. char *big_buf = (char*)malloc((S->allocated_size + 1) * sizeof(char));
  49. strncpy(big_buf, S->s, S->size + 1);
  50. free(S->s);
  51. S->s = big_buf;
  52. }
  53. char *curr_word = (char*)malloc(2 * sizeof(char));
  54. curr_word[0] = c;
  55. curr_word[1] = '\0';
  56. strcat(S->s, curr_word);
  57. }
  58.  
  59. void push_back_as(array_of_string *A, string S)
  60. {
  61. if (A->size++ == A->allocated_size)
  62. {
  63. A->allocated_size *= 2;
  64. string *big_buf = (string*)malloc((A->allocated_size) * sizeof(string));
  65. memcpy(big_buf, A->S, (A->size - 1) * sizeof(string));
  66. free(A->S);
  67. A->S = big_buf;
  68. }
  69.  
  70. A->S[A->size - 1] = S;
  71. }
  72.  
  73. bool isAlpha(char c)
  74. {
  75. return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '_' || c == '-';
  76. }
  77.  
  78. bool isSpace(char c)
  79. {
  80. return c == ' ' || c == '\t' || c == '\0' || c == '&' || c == ',' || c == ';' || c == '.';
  81. }
  82.  
  83. bool isApostrophe(char c)
  84. {
  85. return c == '\'';
  86. }
  87.  
  88. bool isQuotmark(char c)
  89. {
  90. return c == '"';
  91. }
  92.  
  93. void read_line(const char* fileName)
  94. {
  95. Buf = (char*)malloc(MaxCount * sizeof(char));
  96. pFile = fopen(fileName, "r");
  97. if (pFile != NULL)
  98. {
  99. fgets(Buf, MaxCount, pFile);
  100. fclose(pFile);
  101. }
  102. }
  103.  
  104. void parse()
  105. {
  106. enum { collect, free, apostrophe, quotmark };
  107.  
  108. char state = free;
  109. int cnt = 0;
  110. char ch = Buf[cnt++];
  111. string curr_word;
  112.  
  113. while (ch != '\n')
  114. {
  115. switch (state)
  116. {
  117. case collect: case free:
  118. if (isAlpha(ch))
  119. {
  120. if (state == collect)
  121. push_back_s(&curr_word, ch);
  122. else
  123. {
  124. init_s(&curr_word);
  125. push_back_s(&curr_word, ch);
  126. state = collect;
  127. }
  128. }
  129. else
  130. {
  131. if (state == collect)
  132. push_back_as(&Words, curr_word);
  133. state = free;
  134. init_s(&curr_word);
  135. if (isApostrophe(ch))
  136. {
  137. state = apostrophe;
  138. push_back_s(&curr_word, ch);
  139. }
  140. else
  141. if (isQuotmark(ch))
  142. {
  143. state = quotmark;
  144. push_back_s(&curr_word, ch);
  145. }
  146. else
  147. if (!isSpace(ch))
  148. {
  149. push_back_s(&curr_word, ch);
  150. push_back_as(&Words, curr_word);
  151. init_s(&curr_word);
  152. }
  153. }
  154. break;
  155.  
  156. case apostrophe:
  157. push_back_s(&curr_word, ch);
  158. if (isApostrophe(ch))
  159. {
  160. push_back_as(&Words, curr_word);
  161. init_s(&curr_word);
  162. state = free;
  163. }
  164. break;
  165.  
  166. case quotmark:
  167. push_back_s(&curr_word, ch);
  168. if (isQuotmark(ch))
  169. {
  170. push_back_as(&Words, curr_word);
  171. init_s(&curr_word);
  172. state = free;
  173. }
  174. break;
  175. }
  176. }
  177. if (curr_word.size != 0)
  178. {
  179. push_back_as(&Words, curr_word);
  180. init_s(&curr_word);
  181. }
  182. }
  183.  
  184. void print_words()
  185. {
  186. int i = 0;
  187. for (; i < Words.size; i++)
  188. printf("%s\n", (Words.S[i]).s);
  189. printf("\n");
  190. }
  191.  
  192. int main(int argc, const char* argv[])
  193. {
  194. if (argc > 1)
  195. fprintf(stderr, "Wrong input!");
  196.  
  197. read_line(argv[0]);
  198.  
  199. parse();
  200.  
  201. print_words();
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement