OSDN Git Service

flash memory script 実装途中
[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         default:
62                 text[word] = buf;
63                 word++;
64                 break;
65         }
66         
67         while(*buf != '\0'){
68                 int current_spc = 0;
69                 switch(*buf){
70                 case '\t':
71                 case ' ':
72                         *buf = '\0';
73                         current_spc = 1;
74                         break;
75                 }
76                 switch(pastdata){
77                 case '\0':
78                         if(word >= TEXT_MAXWORD){
79                                 PRINT("word over")
80                                 return 0;
81                         }
82                         if(current_spc == 0){
83                                 text[word] = buf;
84                                 word++;
85                         }
86                         break;
87                 }
88                 pastdata = *buf;
89                 buf++;
90         }
91         return word;
92 }
93
94 int value_get(const char *str, long *val)
95 {
96         int base = 10;
97         int sign = 1;
98         //-¤¬¤Ä¤¤¤Æ¤ë¤«
99         switch(*str){
100         case '\0':
101                 return NG;
102         case '-':
103                 sign = -1;
104                 str++;
105                 if(*str == '\0'){
106                         return NG;
107                 }
108                 break;
109         }
110         //0x, 0b, $, % ¤¬¤Ä¤¤¤Æ¤ë¤«
111         switch(*str){
112         case '0':
113                 switch(str[1]){
114                 case '\0':
115                         //¤¿¤ó¤Ê¤ë 0 ¤Ê¤Î¤Ç OK
116                         break;
117                 case 'x':
118                         base = 0x10;
119                         str += 2;
120                         break;
121                 case 'b':
122                         base = 2;
123                         str += 2;
124                         break;
125                 case '0': case '1': case '2': case '3':
126                 case '4': case '5': case '6': case '7':
127                 case '8': case '9':
128                         //C¤Ê¤é8¿Ê¿ô°·¤¤¤À¤±¤É10¿Ê¿ô°·¤¤¤Ë¤¹¤ë
129                         break;
130                 default:
131                         return NG;
132                 }
133                 break;
134         case '$':
135                 base = 0x10;
136                 str += 1;
137                 break;
138         case '%':
139                 base = 2;
140                 str += 1;
141                 break;
142         }
143         //¤³¤Î»þÅÀ¤Ç¤Î str ¤Ï ¿ô»ú¤ÎÀèƬ¤È¤·¤Æ¤¿¾ì½ê
144         char *error;
145         *val = strtol(str, &error, base);
146         if(error[0] != '\0'){
147                 //x01
148                 //4M\0
149                 if((error[1] == '\0') && (base == 10)){
150                         switch(error[0]){
151                         /*case 'K': ¤¤¤¯¤Ä¤À¤Ã¤±?
152                                 *val *= 0x400;
153                                 break;*/
154                         case 'M':
155                                 *val *= 0x20000;
156                                 break;
157                         default:
158                                 return NG;
159                         }
160                 }else{
161                         return NG;
162                 }
163         }
164         
165         if(sign == -1){
166                 *val = -(*val);
167         }
168         return OK;
169 }
170
171 struct operator_cmp{
172         char *str;
173         int operator;
174 };
175 static const struct operator_cmp CMP[] = {
176         {"+", OPERATOR_PLUS},
177         {">>", OPERATOR_SHIFT_LEFT},
178         {"<<", OPERATOR_SHIFT_RIGHT},
179         {"&", OPERATOR_AND},
180         {"|", OPERATOR_OR},
181         {"^", OPERATOR_XOR}
182 };
183
184 int operator_get(char *str)
185 {
186         const struct operator_cmp *c;
187         int i = OPERATOR_ERROR;
188         c = CMP;
189         while(i != 0){
190                 if(strcmp(c->str, str) == 0){
191                         return c->operator;
192                 }
193                 c++;
194                 i--;
195         }
196         return OPERATOR_ERROR;
197 }
198