1 /*
2 * $Id: nstring.c,v 1.3 2003/02/16 12:42:14 isizaka Exp isizaka $
3 *
4 * This file is part of "Ngraph for X11".
5 *
6 * Copyright (C) 2002, Satoshi ISHIZAKA. isizaka@msa.biglobe.ne.jp
7 *
8 * "Ngraph for X11" is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * "Ngraph for X11" is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 *
22 */
23
24 /**
25 *
26 * $Log: nstring.c,v $
27 * Revision 1.3 2003/02/16 12:42:14 isizaka
28 * for release 6.13.18
29 *
30 * Revision 1.2 2002/07/06 08:51:42 isizaka
31 * change to GPL.
32 *
33 * Revision 1.1 1999/03/17 13:46:09 isizaka
34 * Initial revision
35 *
36 *
37 **/
38
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include "object.h"
43 #include "nstring.h"
44
45 #define NSTRLEN 256
46
47 #define TRUE 1
48 #define FALSE 0
49
nstrnew(void)50 char *nstrnew(void)
51 {
52 char *po;
53
54 if ((po=memalloc(NSTRLEN))==NULL) return NULL;
55 po[0]='\0';
56 return po;
57 }
58
nstrccat(char * po,char ch)59 char *nstrccat(char *po,char ch)
60 {
61 size_t len,num;
62 char *po2;
63
64 if (po==NULL) return NULL;
65 len=strlen(po);
66 num=len/NSTRLEN;
67 if (len%NSTRLEN==NSTRLEN-1) {
68 if ((po2=memrealloc(po,NSTRLEN*(num+2)))==NULL) {
69 memfree(po);
70 return NULL;
71 }
72 po=po2;
73 }
74 po[len]=ch;
75 po[len+1]='\0';
76 return po;
77 }
78
nstrcat(char * po,char * s)79 char *nstrcat(char *po,char *s)
80 {
81 size_t i;
82
83 if (po==NULL) return NULL;
84 if (s==NULL) return po;
85 for (i=0;s[i]!='\0';i++)
86 if ((po=nstrccat(po,s[i]))==NULL) return NULL;
87 return po;
88 }
89
nstrncat(char * po,char * s,size_t n)90 char *nstrncat(char *po,char *s,size_t n)
91 {
92 size_t i;
93
94 if (po==NULL) return NULL;
95 if (s==NULL) return po;
96 for (i=0;(s[i]!='\0') && (i<n);i++)
97 if ((po=nstrccat(po,s[i]))==NULL) return NULL;
98 return po;
99 }
100
strcmp0(const char * s1,const char * s2)101 int strcmp0(const char *s1, const char *s2)
102 {
103 const char *s3,*s4;
104
105 if ((s1==NULL) || (s2==NULL)) return 1;
106 s3=s1;
107 s4=s2;
108 while (s3[0]==s4[0]) {
109 if ((s3[0]=='\0') && (s4[0]=='\0')) return 0;
110 s3++;
111 s4++;
112 }
113 return 1;
114 }
115
strcmp2(char * s1,char * s2)116 int strcmp2(char *s1,char *s2)
117 {
118 int len1,len2,len,c;
119
120 len1=strlen(s1);
121 len2=strlen(s2);
122 if (len1<len2) len=len1;
123 else len=len2;
124 c=strncmp(s1,s2,len);
125 if (c==0) {
126 if (len1<len2) return -1;
127 else if (len1>len2) return 1;
128 else return 0;
129 } else return c;
130 }
131
wildmatch2(char * pat,char * s,int flags)132 int wildmatch2(char *pat,char *s,int flags)
133 {
134 char *spo,*patpo,*po;
135
136 if ((s==NULL) || (pat==NULL)) return 0;
137 spo=s;
138 patpo=pat;
139 while (1) {
140 if ((*spo=='\0') && (*patpo=='\0')) return 1;
141 else if (*patpo=='\0') return 0;
142 else if ((flags & WILD_PATHNAME) && (*spo=='/')) {
143 if (*patpo!='/') return 0;
144 patpo++;
145 spo++;
146 } else if (*patpo=='?') {
147 if (*spo=='\0') return 0;
148 patpo++;
149 spo++;
150 } else if (*patpo=='*') {
151 patpo++;
152 while (1) {
153 if (wildmatch2(patpo,spo,flags)) return 1;
154 if (*spo=='\0') return 0;
155 spo++;
156 }
157 } else if (*patpo=='[') {
158 for(po=patpo+1;(*po!='\0') && (*po!=']');po++);
159 if (*po=='\0') {
160 if (*patpo==*spo) {
161 patpo++;
162 spo++;
163 } else return 0;
164 } else {
165 patpo++;
166 while (patpo!=po) {
167 if ((*(patpo+1)=='-') && (*(patpo+2)!=']')) {
168 if ((*patpo<=*spo) && (*spo<=*(patpo+2))) {
169 patpo=po+1;
170 spo++;
171 break;
172 } else patpo+=3;
173 } else {
174 if (*patpo==*spo) {
175 patpo=po+1;
176 spo++;
177 break;
178 } else patpo++;
179 }
180 }
181 }
182 } else if (*patpo==*spo) {
183 patpo++;
184 spo++;
185 } else return 0;
186 }
187 }
188
wildmatch(char * pat,char * s,int flags)189 int wildmatch(char *pat,char *s,int flags)
190 {
191 if ((s==NULL) || (pat==NULL)) return 0;
192 if (flags & WILD_PERIOD) {
193 /* "." and ".." should not match "*" */
194 if (s[0]=='.') {
195 if (pat[0]=='.') return wildmatch2(pat+1,s+1,flags);
196 else return 0;
197 } else return wildmatch2(pat,s,flags);
198 } else return wildmatch2(pat,s,flags);
199 }
200
getitok(char ** s,int * len,char * ifs)201 char *getitok(char **s,int *len,char *ifs)
202 {
203 char *po,*spo;
204 int i;
205
206 if (*s==NULL) return NULL;
207 po=*s;
208 for (i=0;(po[i]!='\0') && (strchr(ifs,po[i])!=NULL);i++);
209 if (po[i]=='\0') {
210 *len=0;
211 return NULL;
212 }
213 spo=po+i;
214 for (;(po[i]!='\0') && (strchr(ifs,po[i])==NULL);i++);
215 *s+=i;
216 *len=*s-spo;
217 return spo;
218 }
219
getitok2(char ** s,int * len,char * ifs)220 char *getitok2(char **s,int *len,char *ifs)
221 {
222 char *po,*s2;
223
224 if ((s2=getitok(s,len,ifs))==NULL) return NULL;
225 if ((po=memalloc(*len+1))==NULL) {
226 *len=-1;
227 return NULL;
228 }
229 strncpy(po,s2,*len);
230 po[*len]='\0';
231 return po;
232 }
233
getitok3(char ** s,int * len,char * ifs)234 char *getitok3(char **s,int *len,char *ifs)
235 {
236 char *po,*spo;
237 int i,quote;
238
239 if (*s==NULL) return NULL;
240 quote=FALSE;
241 po=*s;
242 for (i=0;(po[i]!='\0') && (po[i]!='"') && (strchr(ifs,po[i])!=NULL);i++);
243 if (po[i]=='\0') {
244 *len=0;
245 return NULL;
246 }
247 if (po[i]=='"') {
248 quote=TRUE;
249 i++;
250 }
251 spo=po+i;
252 if (quote) {
253 for (;(po[i]!='\0') && (po[i]!='"');i++);
254 *s+=i;
255 *len=*s-spo;
256 if (po[i]=='"') (*s)++;
257 } else {
258 for (;(po[i]!='\0') && (strchr(ifs,po[i])==NULL);i++);
259 *s+=i;
260 *len=*s-spo;
261 }
262 return spo;
263 }
264
getitok4(char ** s,int * len,char * ifs)265 char *getitok4(char **s,int *len,char *ifs)
266 {
267 char *po,*s2;
268
269 if ((s2=getitok3(s,len,ifs))==NULL) return NULL;
270 if ((po=memalloc(*len+1))==NULL) {
271 *len=-1;
272 return NULL;
273 }
274 strncpy(po,s2,*len);
275 po[*len]='\0';
276 return po;
277 }
278
279