OSDN Git Service

ff4ff70982fc6438d50756dad75cdc84ddb5101d
[ntch/develop.git] / src / utils / text.c
1 /* Copyright 2013 Akira Ohta (akohta001@gmail.com)
2     This file is part of ntch.
3
4     The ntch is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8
9     The ntch is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with ntch.  If not, see <http://www.gnu.org/licenses/>.
16     
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <wchar.h>
21 #include <string.h>
22 #include <assert.h>
23
24 #include "utils/nt_std_t.h"
25 #include "utils/text.h"
26
27
28 wchar_t* nt_w_str_resize(wchar_t *old_src, size_t old_size, size_t new_size)
29 {
30         wchar_t *wrk_buf;
31         
32         assert(old_size < new_size && new_size > 0);
33         
34         wrk_buf = malloc(sizeof(wchar_t) * (new_size));
35         if(!wrk_buf){
36                 return NULL;
37         }
38         
39         if(!old_src){
40                 wrk_buf[0] = L'\0';
41                 return wrk_buf;
42         }
43         wmemcpy(wrk_buf, old_src, old_size);
44         wrk_buf[old_size] = L'\0';
45         free(old_src);
46         return wrk_buf;
47 }
48
49 BOOL nt_w_str_move(wchar_t *buf, size_t buf_len, 
50                                         int move_index, int offset)
51 {
52         int str_len, tmp, i;
53         assert(buf);
54
55         str_len = wcslen(buf);
56
57         if(0 > move_index || str_len <= move_index)
58                 return FALSE;
59
60         tmp = str_len + offset;
61         if(tmp >= buf_len)
62                 return FALSE;
63
64         if(offset == 0){
65         // Do nothing.
66                 return TRUE;
67         }else if(offset < 0){
68         // Move the block to forward.
69                 if(move_index + offset < 0)
70                         return FALSE;
71                 //str_len += offset;
72                 for(i = move_index; i < str_len; i++){
73                         buf[i+offset] = buf[i];
74                 }
75                 buf[str_len+offset] =  L'\0';
76         }else{/* if(offset > 0) */
77         // Move the block to backward.
78                 if(str_len + offset >= buf_len)
79                         return FALSE;
80                 for(i = str_len - 1; i >= move_index; i--){
81                         buf[i + offset] = buf[i];
82                 }
83                 buf[str_len + offset] = L'\0';
84                 buf[move_index] = L'\0';
85         }
86         return TRUE;
87 }
88
89
90
91 wchar_t *nt_w_trim(const wchar_t *source)
92 {
93         int i, j, status;
94         wchar_t *cptr;
95         int len;
96
97         assert(source != NULL);
98
99         len = wcslen(source);
100
101         for(i=0; i<len; i++){
102                 switch(source[i]){
103                 case L' ':
104                 case L'\t':
105                 case L'\r':
106                 case L'\n':
107                         status = 1;
108                         break;
109                 default:
110                         status = 0;
111                         break;
112                 }
113                 if(status == 0)
114                         break;
115         }/* end for */
116         if(i == len){
117                 cptr = malloc(sizeof(wchar_t));
118                 if(cptr != NULL)
119                         cptr[0] = L'\0';
120                 return cptr;
121         }
122         for(j = len - 1; j > i; j--){
123                 switch(source[j]){
124                 case L' ':
125                 case L'\t':
126                 case L'\r':
127                 case L'\n':
128                         status = 1;
129                         break;
130                 default:
131                         status = 0;
132                         break;
133                 }
134                 if(status == 0)
135                         break;
136         }/* end for */
137         assert(i <= j);
138         len = j - i + 1;
139         cptr = malloc(sizeof(wchar_t)*(len + 1));
140         if(cptr != NULL){
141                 memcpy(cptr, source+i, len*sizeof(wchar_t));
142                 cptr[len] = '\0';
143         }
144         return cptr;
145 }
146
147 wchar_t *nt_w_trim_quotes(const wchar_t *source)
148 {
149         assert(source != NULL);
150         int len = wcslen(source);
151         int i, j, status = 0;
152         wchar_t *cptr;
153         BOOL dquote = FALSE;
154         BOOL squote = FALSE;
155
156         for(i=0; i<len; i++){
157                 switch(source[i]){
158                 case L' ':
159                 case L'\t':
160                 case L'\r':
161                 case L'\n':
162                         if(status == 2){
163                                 status = 0;
164                                 break;
165                         }
166                         status = 1;
167                         break;
168                 case '\"':
169                         dquote = TRUE;
170                         status = 2;
171                         break;
172                 case '\'':
173                         squote = TRUE;
174                         status = 2;
175                         break;
176                 default:
177                         status = 0;
178                         break;
179                 }
180                 if(status == 0)
181                         break;
182         }/* end for */
183         if(i == len){
184                 cptr = malloc(sizeof(wchar_t));
185                 if(cptr != NULL)
186                         cptr[0] = L'\0';
187                 return cptr;
188         }
189         for(j = len - 1; j > i; j--){
190                 switch(source[j]){
191                 case L' ':
192                 case L'\t':
193                 case L'\r':
194                 case L'\n':
195                         if(status == 2)
196                                 status = 0;
197                         else
198                                 status = 1;
199                         break;
200                 case '\"':
201                         if(!dquote)
202                                 status = 0;
203                         else
204                                 status = 2;
205                         break;
206                 case '\'':
207                         if(!squote)
208                                 status = 0;
209                         else
210                                 status = 2;
211                         break;
212                 default:
213                         status = 0;
214                         break;
215                 }
216                 if(status == 0)
217                         break;
218         }/* end for */
219         len = j - i + 1;
220         if(i < j){
221                 cptr = malloc(sizeof(wchar_t)*(len + 1));
222                 if(cptr != NULL){
223                         memcpy(cptr, source+i, len*sizeof(wchar_t));
224                         cptr[len] = L'\0';
225                 }
226         }else{
227                 cptr = malloc(sizeof(wchar_t));
228                 if(cptr != NULL){
229                         cptr[0] = L'\0';
230                 }
231         }
232         return cptr;
233 }
234
235 char *nt_trim2(const char *source, char *buf, size_t buf_len)
236 {
237         int i, j, status;
238         char *cptr;
239         int len;
240
241         assert(source != NULL);
242
243         len = strlen(source);
244
245         if(!buf){
246                 cptr = calloc(len+1, sizeof(char));
247                 if(!cptr)
248                         return NULL;
249         }else{
250                 if(buf_len < len+1)
251                         return NULL;
252                 cptr = buf;
253         }
254         for(i=0; i<len; i++){
255                 switch(source[i]){
256                 case ' ':
257                 case '\t':
258                 case '\r':
259                 case '\n':
260                         status = 1;
261                         break;
262                 default:
263                         status = 0;
264                         break;
265                 }
266                 if(status == 0)
267                         break;
268         }/* end for */
269         if(i == len){
270                 cptr[0] = L'\0';
271                 return cptr;
272         }
273         for(j = len - 1; j > i; j--){
274                 switch(source[j]){
275                 case ' ':
276                 case '\t':
277                 case '\r':
278                 case '\n':
279                         status = 1;
280                         break;
281                 default:
282                         status = 0;
283                         break;
284                 }
285                 if(status == 0)
286                         break;
287         }/* end for */
288         assert(i <= j);
289         len = j - i + 1;
290         memcpy(cptr, source+i, len*sizeof(char));
291         cptr[len] = '\0';
292         return cptr;
293 }
294
295 wchar_t* nt_w_str_clone(const wchar_t* src)
296 {
297         wchar_t *cptr;
298         cptr = malloc(sizeof(wchar_t) * (wcslen(src)+1));
299         if(!cptr)
300                 return NULL;
301         wcscpy(cptr, src);
302         return cptr;
303 }
304
305 char* nt_str_clone(const char* src)
306 {
307         char *cptr;
308         cptr = malloc(strlen(src)+1);
309         if(!cptr)
310                 return NULL;
311         strcpy(cptr, src);
312         return cptr;
313 }
314
315 char* nt_substr(const char* src, int start, int end)
316 {
317         char *cptr;
318         int len;
319
320         assert(start >= 0);
321         assert(start <= end);
322
323         len = end - start;
324         cptr = malloc(len + 1);
325         if(!cptr)
326                 return NULL;
327         
328         if(len == 0){
329                 *cptr =  '\0';
330                 return cptr;
331         }
332         
333         memcpy(cptr, src+start, len);
334         cptr[len] = '\0';
335         return cptr;
336 }
337
338 BOOL nt_strtok(const char *str, char delim,
339         const char** start, const char **end)
340 {
341         int i, len, state;
342
343         assert(str);
344
345         len = strlen(str);
346         *start = str;
347         *end = str + len;
348
349         state = 0;
350         for(i = 0; i < len; i++){
351                 if(str[i] == delim){
352                         if(state == 0){
353                                 *start = str + i + 1;
354                         }else if(state == 1){
355                                 *end = str + i;
356                                 return TRUE;
357                         }
358                 }else{
359                         if(state == 0)
360                                 state = 1;
361                 }
362         }
363         return TRUE;
364 }