OSDN Git Service

syntax test を復活
[unagi/old-svn-converted.git] / client / trunk / textutil.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "type.h"
5 #include "textutil.h"
6 #ifndef DEBUG
7  #define DEBUG 0
8 #endif
9
10 #define PRINT(msg) \
11         if(DEBUG == 1){ \
12                 printf("%s %s\n",  __FUNCTION__, msg);\
13         }
14
15 int text_load(char *buf, int length, char **text)
16 {
17         int line = 0;
18         char pastdata = '#';
19         
20         text[line] = buf;
21         line++;
22         while(length != 0){
23                 int current_crlf = 0;
24                 switch(*buf){
25                 case '\n':
26                 case '\r':
27                         *buf = '\0';
28                         current_crlf = 1;
29                         break;
30                 }
31                 switch(pastdata){
32                 case '\0':
33                         if(line >= TEXT_MAXLINE){
34                                 PRINT("line over")
35                                 return 0;
36                         }
37                         if(current_crlf == 0){
38                                 text[line] = buf;
39                                 line++;
40                         }
41                         break;
42                 }
43                 pastdata = *buf;
44                 buf++;
45                 length--;
46         }
47         buf--;
48         *buf = '\0';
49         return line;
50 }
51
52 int word_load(char *buf, char **text)
53 {
54         int word = 0;
55         char pastdata = '#';
56         
57         switch(*buf){
58         case '\t':
59         case ' ':
60                 break;
61         case '#':
62                 text[word] = buf;
63                 return 1;
64         default:
65                 text[word] = buf;
66                 word++;
67                 break;
68         }
69         
70         while(*buf != '\0'){
71                 int current_spc = 0;
72                 switch(*buf){
73                 case '\t':
74                 case ' ':
75                         *buf = '\0';
76                         current_spc = 1;
77                         break;
78                 }
79                 switch(pastdata){
80                 case '\0':
81                         if(word >= TEXT_MAXWORD){
82                                 PRINT("word over")
83                                 return 0;
84                         }
85                         if(current_spc == 0){
86                                 /*if(*buf == '#'){
87                                         return word;
88                                 }*/
89                                 text[word] = buf;
90                                 word++;
91                         }
92                         break;
93                 }
94                 pastdata = *buf;
95                 buf++;
96         }
97         return word;
98 }
99
100 int value_get(const char *str, long *val)
101 {
102         int base = 10;
103         int sign = 1;
104         //-¤¬¤Ä¤¤¤Æ¤ë¤«
105         switch(*str){
106         case '\0':
107                 return NG;
108         case '-':
109                 sign = -1;
110                 str++;
111                 if(*str == '\0'){
112                         return NG;
113                 }
114                 break;
115         }
116         //0x, 0b, $, % ¤¬¤Ä¤¤¤Æ¤ë¤«
117         switch(*str){
118         case '0':
119                 switch(str[1]){
120                 case '\0':
121                         //¤¿¤ó¤Ê¤ë 0 ¤Ê¤Î¤Ç OK
122                         break;
123                 case 'x':
124                         base = 0x10;
125                         str += 2;
126                         break;
127                 case 'b':
128                         base = 2;
129                         str += 2;
130                         break;
131                 case '0': case '1': case '2': case '3':
132                 case '4': case '5': case '6': case '7':
133                 case '8': case '9':
134                         //C¤Ê¤é8¿Ê¿ô°·¤¤¤À¤±¤É10¿Ê¿ô°·¤¤¤Ë¤¹¤ë
135                         break;
136                 default:
137                         return NG;
138                 }
139                 break;
140         case '$':
141                 base = 0x10;
142                 str += 1;
143                 break;
144         case '%':
145                 base = 2;
146                 str += 1;
147                 break;
148         }
149         //¤³¤Î»þÅÀ¤Ç¤Î str ¤Ï ¿ô»ú¤ÎÀèƬ¤È¤·¤Æ¤¿¾ì½ê
150         char *error;
151         *val = strtol(str, &error, base);
152         if(error[0] != '\0'){
153                 //x01
154                 //4M\0
155                 if((error[1] == '\0') && (base == 10)){
156                         switch(error[0]){
157                         /*case 'K': ¤¤¤¯¤Ä¤À¤Ã¤±?
158                                 *val *= 0x400;
159                                 break;*/
160                         case 'M':
161                                 *val *= 0x20000;
162                                 break;
163                         default:
164                                 return NG;
165                         }
166                 }else{
167                         return NG;
168                 }
169         }
170         
171         if(sign == -1){
172                 *val = -(*val);
173         }
174         return OK;
175 }
176
177 struct operator_cmp{
178         char *str;
179         int operator;
180 };
181 static const struct operator_cmp CMP[] = {
182         {"+", OPERATOR_PLUS},
183         {">>", OPERATOR_SHIFT_LEFT},
184         {"<<", OPERATOR_SHIFT_RIGHT},
185         {"&", OPERATOR_AND},
186         {"|", OPERATOR_OR},
187         {"^", OPERATOR_XOR}
188 };
189
190 int operator_get(char *str)
191 {
192         const struct operator_cmp *c;
193         int i = OPERATOR_ERROR;
194         c = CMP;
195         while(i != 0){
196                 if(strcmp(c->str, str) == 0){
197                         return c->operator;
198                 }
199                 c++;
200                 i--;
201         }
202         return OPERATOR_ERROR;
203 }
204