/
parser.c
91 lines (83 loc) · 2.01 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define _GNU_SOURCE
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
#include "list_s.h"
#include "parser.h"
#include "constants.h"
#include "common.h"
int parse_string (const string_t client_string, list_s lista) {
string_t almost_done = NULL;
string_t done = NULL;
FILE * strf = NULL;
int result = CODE_OK;
assert (client_string != NULL && lista != NULL);
strf = fmemopen (client_string, strlen(client_string) + 1, "r");
if (strf != NULL) {
Lexer * l = lexer_new (strf);
lexer_next_to (l, "\n");
almost_done = lexer_item (l);
if (strncmp (client_string, almost_done, strlen (client_string) - 2) != 0)
return BAD_EOL;
lexer_destroy (l);
fclose (strf);
if (strlen (almost_done) > 1 && almost_done != NULL) {
strf = fmemopen (almost_done, strlen(almost_done), "r");
if (strf != NULL) {
Lexer * j = lexer_new (strf);
lexer_next_to (j, "\r");
done = lexer_item (j);
lexer_destroy (j);
fclose (strf);
if (strlen (done) > 1 && done != NULL) {
strf = fmemopen (done, strlen(done), "r");
if (strf){
string_t valid_elem = NULL;
Lexer * p = lexer_new (strf);
while (!lexer_is_off (p)) {
lexer_next (p, SUPERALPHANUMERIC);
valid_elem = lexer_item (p);
list_adjoin_back (lista, valid_elem);
lexer_skip (p, BLANK);
}
lexer_destroy (p); p = NULL;
fclose (strf);
}
else {
fprintf (stderr, "fmemopen");
result = INTERNAL_ERROR;
}
}
else {
if (done != NULL)
result = STRING_VOID;
else
result = INTERNAL_ERROR;
}
}
else {
fprintf (stderr, "fmemopen");
result = INTERNAL_ERROR;
}
}
else {
if (almost_done != NULL)
result = STRING_VOID;
else
result = INTERNAL_ERROR;
}
if(almost_done != NULL) {
free (almost_done); almost_done = NULL;
}
if(done != NULL) {
free (done); done = NULL;
}
}
else {
fprintf (stderr, "fmemopen");
result = INTERNAL_ERROR;
}
return result;
}