-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_parser.c
153 lines (140 loc) · 3.93 KB
/
string_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cobgdb.h"
struct st_parse parse_blank = {
.token = "" , // Initialize the char * token pointer as NULL
.size = 0, // Initialize the int size as 0
.type = 0, // Initialize the int type as 0
.next = NULL // Initialize the struct st_parse * next pointer as NULL
};
char * toStUpper(char* str) {
int j = 0;
while (str[j]) {
str[j]=toupper(str[j]);
j++;
}
return str;
}
int psSpaces(char * ch){
int count=0;
while(*ch==' ' || *ch=='\t'){
ch = ch + 1;
count++;
}
return count;
}
int psString(char * ch){
int count=0;
if(*ch=='\''){
count++;
ch = ch+1;
while(*ch!='\'' && *ch!='\0' && *ch!='\n'){
ch = ch + 1;
count++;
}
if(*ch=='\''){
ch = ch + 1;
count++;
}
}else{
count++;
ch = ch+1;
while(*ch!='"' && *ch!='\0' && *ch!='\n'){
ch = ch + 1;
count++;
}
if(*ch=='"'){
ch = ch + 1;
count++;
}
}
return count;
}
int psSymbols(char * ch){
int count=0;
while(*ch!=' ' && *ch!='\t' && *ch!='\n' && *ch!='\0' &&
*ch != '(' && *ch != '[' && *ch != '{' && *ch != ')'
&& *ch != ']' && *ch != '}' && *ch != '=' ){
ch = ch + 1;
count++;
}
return count;
}
int psAlpha(char * ch){
int count=0;
while(isalpha(*ch) || *ch=='.' || *ch=='-' || *ch=='_' || isdigit(*ch)){
ch = ch + 1;
count++;
}
return count;
}
int psNumber(char * ch){
int count=0;
int hex = 0;
while(isdigit(*ch) || *ch=='.' || *ch=='x' ||
((*ch=='a' || *ch=='b' || *ch=='c' || *ch=='d' || *ch=='e' || *ch=='f') && hex==1)){
if(*ch=='x') hex = 1;
ch = ch + 1;
count++;
}
return count;
}
struct st_parse * tk_val(struct st_parse line_parsed[100], int qtt_tk, int pos){
struct st_parse * tk = NULL;
if(pos<qtt_tk){
tk = &line_parsed[pos];
}
if(tk==NULL){ tk=&parse_blank; }
return tk;
}
void lineParse(char * line_to_parse, struct st_parse h[100], int *qtt ){
char * ch = line_to_parse;
*qtt=0;
while(*ch!='\0' && *qtt<100){
if(*ch==' ' || *ch=='\t'){
int size = psSpaces(ch);
ch+=size;
continue;
}else if(isdigit(*ch)){
h[*qtt].token=ch;
h[*qtt].type=TP_NUMBER;
h[*qtt].size = psNumber(ch);
ch+=h[*qtt].size;
}else if(*ch == '&' || *ch == '=' || *ch == '(' || *ch == '['
|| *ch == '{' || *ch == ')' || *ch == ']' || *ch == '}'){
h[*qtt].token=ch;
h[*qtt].type=TP_SYMBOL;
h[*qtt].size = 1;
ch+=h[*qtt].size;
}else if(isalpha(*ch)){
h[*qtt].token=ch;
h[*qtt].type=TP_ALPHA;
h[*qtt].size = psAlpha(ch);
ch+=h[*qtt].size;
}else if(!isalpha(*ch) && *ch!=' ' && *ch!='\t' && *ch!='\n' && *ch!='\0'){
h[*qtt].token=ch;
h[*qtt].type=TP_ALPHA;
h[*qtt].size = psSymbols(ch);
ch+=h[*qtt].size;
}else if(*ch=='\''){
h[*qtt].token=ch;
h[*qtt].type=TP_STRING1;
h[*qtt].size = psString(ch);
ch+=h[*qtt].size;
}else if(*ch=='"'){
h[*qtt].token=ch;
h[*qtt].type=TP_STRING2;
h[*qtt].size = psString(ch);
ch+=h[*qtt].size;
}else{
h[*qtt].token=ch;
h[*qtt].type=TP_OTHER;
h[*qtt].size = 1;
ch+=h[*qtt].size;
break;
}
*qtt = *qtt + 1;
}
}