1 /*This file is a part of luscus project*/
2 /*Licensed under the Academic Free License version 3.0*/
3 #include<stdio.h>
4 #include<stdlib.h>
5 #include<string.h>
6 #include"luscus.h"
7 #include"gv.h"
8 #include"gv_functions.h"
9
10 /*char *my_read_str_value(char *str)
11 {
12 int i;
13 char *ptr;
14 char *val = NULL;
15
16 ptr = strchr(str, '=') + 1;
17 while(ptr[0] == 32 || ptr[0] == 9) ptr++;
18
19 if (ptr == NULL) return NULL;
20 i = 0;
21 val = (char*) malloc(sizeof(char) * (i+1));
22 val[i] = 0;
23 while(ptr[i] > 32 && ptr[i] < 127)
24 {
25 val = (char*) realloc(val, sizeof(char) * (++i+1));
26 val[i-1] = ptr[i-1];
27 val[i] = 0;
28 }
29 return val;
30 }*/
31
32 /*05.07.2013. This function can read quoted string as one word*/
my_read_str_value(char * str)33 char *my_read_str_value(char *str)
34 {
35 int i, j;
36 char *ptr;
37 char *val = NULL;
38 int next, quoted = 0;
39
40 if (str == NULL) return NULL;
41 if (str[0] == 0) return NULL;
42 ptr = strchr(str, '=');
43 if (ptr == NULL) return NULL;
44 ptr++;
45 if (ptr[0] == 0) return NULL;
46
47 while(ptr[0] == 32 || ptr[0] == 9)
48 {
49 if (ptr[0] == 0) return NULL;
50 ptr++;
51 }
52 if (ptr[0] == 0) return NULL;
53
54 i = j = 0;
55 val = (char*) malloc(sizeof(char) * (i+1));
56 val[i] = 0;
57 next = 1;
58 if (ptr[i] < 33 || ptr[i] > 126) next = 0;
59 while(next)
60 {
61 if (ptr[j] == 0) return val;
62 else if (ptr[j] == 34)
63 {
64 if (quoted) quoted = 0;
65 else quoted = 1;
66 }
67 else
68 {
69 val = (char*) realloc(val, sizeof(char) * (++i+1));
70 val[i-1] = ptr[j];
71 val[i] = 0;
72 }
73 j++;
74 if (quoted && (ptr[j] < 9 || (ptr[j] > 13 && ptr[j] < 32) || ptr[j] > 126)) next = 0;
75 else if (!quoted && (ptr[j] < 33 || ptr[j] > 126)) next = 0;
76 }
77
78 return val;
79 }
80
my_read_int_value(char * str)81 int my_read_int_value(char *str)
82 {
83 char *ptr;
84 ptr = strchr(str, '=') + 1;
85 if (ptr) return atoi(ptr);
86 else return 0;
87 }
88
my_read_dble_value(char * str)89 double my_read_dble_value(char *str)
90 {
91 char *ptr;
92 double tmp;
93 ptr = strchr(str, '=') + 1;
94 if (ptr)
95 {
96 sscanf(ptr, "%lf", &tmp);
97 return tmp; /*corrected 2014.01.24*/
98 /*return atof(ptr);*/
99 }
100 else return 0.F;
101 }
102
103 /*This function is rewritten from mydiet; it is simpler and handles string in place*/
strdiet(char * in)104 char *strdiet(char* in)
105 {
106 int i, j, k;
107 j = strlen(in) - 1;
108 while(in[j] == 32 || in[j] == 9) j--;
109 in[j] = 0;
110 for(i = 0; in[i] == 32 || in[i] == 9; i++);
111 for(k = 0; k < j-i; k++) in[k] = in[k+i];
112 in[j-1] = 0;
113 return in;
114 }
115
116 /*char *get_one_word(char *str)
117 {
118 int i = 0;
119 char *n;
120 n = (char*) malloc(sizeof(char));
121 n[0] = 0;
122 while(str[i] != 32 && str[i] != 9 && str[i] != 0)
123 {
124 n = (char*) realloc(n, sizeof(char) * ++i + 1);
125 n[i-1] = str[i-1];
126 n[i] = 0;
127 }
128 return n;
129 }*/
130
131 /*05.07.2013 this function is upgraded; now it can accept quoted strings*/
get_one_word(char * str)132 char *get_one_word(char *str)
133 {
134 int i = 0, j = 0;
135 char *n;
136 int next = 1, quoted = 0;
137 n = (char*) malloc(sizeof(char));
138 n[0] = 0;
139
140 while(next)
141 {
142 if (str[j] == 0) return n;
143 else if (str[j] == 34)
144 {
145 if (quoted) quoted = 0;
146 else quoted = 1;
147 }
148 else
149 {
150 n = (char*) realloc(n, sizeof(char) * ++i + 1);
151 n[i-1] = str[j];
152 n[i] = 0;
153 }
154 j++;
155 if (quoted && (str[j] < 8 || str[j] > 126)) next = 0;
156 else if (!quoted && (str[j] < 33 || str[j] > 126)) next = 0;
157 }
158
159 return n;
160 }
161
162 /*char *get_ptr_ith_word(char *str, int i)
163 {
164 int j = 0;
165 while(str[j] == 32 || str[j] == 9) j++;
166 while(i-1)
167 {
168 while(str[j] != 32 && str[j] != 9 && str[j] != 0)
169 {
170 if (str[j] == 0) return NULL;
171 j++;
172 }
173 while(str[j] == 32 || str[j] == 9) j++;
174 i--;
175 }
176 return str+j;
177 }*/
178
179 /*05.07.2013. this function is upgraded so it can recognize quoted string as one word*/
get_ptr_ith_word(char * str,int i)180 char *get_ptr_ith_word(char *str, int i)
181 {
182 int j = 0;
183 int quoted = 0;
184 int next;
185 while(str[j] == 32 || str[j] == 9) j++;
186 while(i-1)
187 {
188 next = 1;
189
190 while(next)
191 {
192 if (str[j] == 0) return NULL;
193 else if (str[j] == 34)
194 {
195 if (quoted) quoted = 0;
196 else quoted = 1;
197 }
198 j++;
199 if (quoted && str[j] < 8) next = 0;
200 else if (!quoted && (str[j] == 32 || str[j] == 9)) next = 0;
201 }
202 while(str[j] == 32 || str[j] == 9) j++;
203 i--;
204 }
205 return str+j;
206 }
207
line_is_empty(char * line)208 int line_is_empty(char *line)
209 {
210 int i = 0;
211 if (line == NULL) return 1;
212 while(line[i] != 0)
213 {
214 if (line[i] > 32) return 0;
215 i++;
216 }
217 return 1;
218 }
219
220