Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <memory.h>
- #define unsigned char bool;
- const int MaxCount = 255;
- FILE* pFile = NULL;
- char* Buf;
- typedef struct
- {
- char* s;
- int size, allocated_size;
- } string;
- typedef struct
- {
- string* S;
- int size, allocated_size;
- } array_of_string;
- array_of_string Words;
- void init_s(string* S)
- {
- S->size = 0;
- S->allocated_size = 1;
- S->s = (char*)malloc((S->allocated_size + 1) * sizeof(char));
- S->s[0] = '\0';
- }
- void init_as(array_of_string *A)
- {
- A->size = 0;
- A->allocated_size = 1;
- A->S = (string*)malloc(A->allocated_size * sizeof(string));
- }
- void push_back_s(string *S, char c)
- {
- if (S->size++ == S->allocated_size)
- {
- S->allocated_size *= 2;
- char *big_buf = (char*)malloc((S->allocated_size + 1) * sizeof(char));
- strncpy(big_buf, S->s, S->size + 1);
- free(S->s);
- S->s = big_buf;
- }
- char *curr_word = (char*)malloc(2 * sizeof(char));
- curr_word[0] = c;
- curr_word[1] = '\0';
- strcat(S->s, curr_word);
- }
- void push_back_as(array_of_string *A, string S)
- {
- if (A->size++ == A->allocated_size)
- {
- A->allocated_size *= 2;
- string *big_buf = (string*)malloc((A->allocated_size) * sizeof(string));
- memcpy(big_buf, A->S, (A->size - 1) * sizeof(string));
- free(A->S);
- A->S = big_buf;
- }
- A->S[A->size - 1] = S;
- }
- bool isAlpha(char c)
- {
- return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') || c == '_' || c == '-';
- }
- bool isSpace(char c)
- {
- return c == ' ' || c == '\t' || c == '\0' || c == '&' || c == ',' || c == ';' || c == '.';
- }
- bool isApostrophe(char c)
- {
- return c == '\'';
- }
- bool isQuotmark(char c)
- {
- return c == '"';
- }
- void read_line(const char* fileName)
- {
- Buf = (char*)malloc(MaxCount * sizeof(char));
- pFile = fopen(fileName, "r");
- if (pFile != NULL)
- {
- fgets(Buf, MaxCount, pFile);
- fclose(pFile);
- }
- }
- void init_s(char* str)
- {
- free(str);
- str = (char*)malloc(MaxCount * sizeof(char));
- }
- void parse()
- {
- enum { collect, free, apostrophe, quotmark };
- char state = free;
- int cnt = 0;
- char ch = Buf[cnt++];
- string curr_word;
- while (ch != '\n')
- {
- switch (state)
- {
- case collect: case free:
- if (isAlpha(ch))
- {
- if (state == collect)
- push_back_s(&curr_word, ch);
- else
- {
- init_s(&curr_word);
- push_back_s(&curr_word, ch);
- state = collect;
- }
- }
- else
- {
- if (state == collect)
- push_back_as(&Words, curr_word);
- state = free;
- init_s(&curr_word);
- if (isApostrophe(ch))
- {
- state = apostrophe;
- push_back_s(&curr_word, ch);
- }
- else
- if (isQuotmark(ch))
- {
- state = quotmark;
- push_back_s(&curr_word, ch);
- }
- else
- if (!isSpace(ch))
- {
- push_back_s(&curr_word, ch);
- push_back_as(&Words, curr_word);
- init_s(&curr_word);
- }
- }
- break;
- case apostrophe:
- push_back_s(&curr_word, ch);
- if (isApostrophe(ch))
- {
- push_back_as(&Words, curr_word);
- init_s(&curr_word);
- state = free;
- }
- break;
- case quotmark:
- push_back_s(&curr_word, ch);
- if (isQuotmark(ch))
- {
- push_back_as(&Words, curr_word);
- init_s(&curr_word);
- state = free;
- }
- break;
- }
- }
- if (curr_word.size != 0)
- {
- push_back_as(&Words, curr_word);
- init_s(&curr_word);
- }
- }
- void print_words()
- {
- for (int i = 0; i < Words.size; i++)
- printf("%s\n", Words.S[i]);
- printf("\n");
- }
- int main(int argc, const char* argv[])
- {
- if (argc > 1)
- fprintf(stderr, "Wrong input!");
- read_line(argv[0]);
- parse();
- print_words();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement