OSDN Git Service

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