/[LeafOK_CVS]/pvpgn-1.7.4/src/d2cs/xstring.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/d2cs/xstring.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

1 sysadm 1.1 /*
2     * Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
3     *
4     * This program is free software; you can redistribute it and/or
5     * modify it under the terms of the GNU General Public License
6     * as published by the Free Software Foundation; either version 2
7     * of the License, or (at your option) any later version.
8     *
9     * This program 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 this program; if not, write to the Free Software
16     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17     */
18     #include "common/setup_before.h"
19     #include "setup.h"
20    
21     #include <stdio.h>
22     #include <ctype.h>
23     #ifdef HAVE_STRING_H
24     # include <string.h>
25     #else
26     # ifdef HAVE_STRINGS_H
27     # include <strings.h>
28     # endif
29     # ifdef HAVE_MEMORY_H
30     # include <memory.h>
31     # endif
32     #endif
33     #ifdef STDC_HEADERS
34     # include <stdlib.h>
35     #else
36     # ifdef HAVE_MALLOC_H
37     # include <malloc.h>
38     # endif
39     #endif
40     #include "compat/memcpy.h"
41     #include "compat/memmove.h"
42     #include "compat/strdup.h"
43    
44     #include "common/xalloc.h"
45     #include "xstring.h"
46     #include "common/setup_after.h"
47    
48     extern char * strtolower(char * str)
49     {
50     unsigned int i;
51     unsigned char ch;
52    
53     if (!str) return NULL;
54     for (i=0; (ch=str[i]); i++) {
55     if ((isupper(ch))) str[i]=ch+('a'-'A');
56     }
57     return str;
58     }
59    
60     extern unsigned char xtoi(unsigned char ch)
61     {
62     unsigned char retval;
63    
64     if (isalpha(ch)) retval=tolower(ch);
65     else retval=ch;
66     if (retval < 'A') retval -= ('0'-0);
67     else retval -= ('a' - 0xa);
68     return retval;
69     }
70    
71     extern char * str_strip_affix(char * str, char const * affix)
72     {
73     unsigned int i, j, n;
74     int match;
75    
76     if (!str) return NULL;
77     if (!affix) return str;
78     for (i=0; str[i]; i++) {
79     match=0;
80     for (n=0; affix[n]; n++) {
81     if (str[i]==affix[n]) {
82     match=1;
83     break;
84     }
85     }
86     if (!match) break;
87     }
88     for (j=strlen(str)-1; j>=i; j--) {
89     match=0;
90     for (n=0; affix[n]; n++) {
91     if (str[j]==affix[n]) {
92     match=1;
93     break;
94     }
95     }
96     if (!match) break;
97     }
98     if (i>j) {
99     str[0]='\0';
100     } else {
101     memmove(str,str+i,j-i+1);
102     str[j-i+1]='\0';
103     }
104     return str;
105     }
106    
107     extern char * hexstrdup(unsigned char const * src)
108     {
109     char * dest;
110     int len;
111    
112     if (!src) return NULL;
113     dest=xstrdup(src);
114     len=hexstrtoraw(src,dest,strlen(dest)+1);
115     dest[len]='\0';
116     return dest;
117     }
118    
119     extern unsigned int hexstrtoraw(unsigned char const * src, char * data, unsigned int datalen)
120     {
121     unsigned char ch;
122     unsigned int i, j;
123    
124     for (i=0, j=0; j<datalen; i++) {
125     ch=src[i];
126     if (!ch) break;
127     if (ch=='\\') {
128     i++;
129     ch=src[i];
130     if (!ch) {
131     break;
132     } else if (ch=='\\') {
133     data[j++]=ch;
134     } else if (ch=='x') {
135     if (isxdigit(src[i+1])) {
136     if (isxdigit(src[i+2])) {
137     data[j++]=xtoi(src[i+1]) * 0x10 + xtoi(src[i+2]);
138     i+=2;
139     } else {
140     data[j++]=xtoi(src[i+1]);
141     i++;
142     }
143     } else {
144     data[j++]=ch;
145     }
146     } else if (ch=='n') {
147     data[j++]='\n';
148     } else if (ch=='r') {
149     data[j++]='\r';
150     } else if (ch=='a') {
151     data[j++]='\a';
152     } else if (ch=='t') {
153     data[j++]='\t';
154     } else if (ch=='b') {
155     data[j++]='\b';
156     } else if (ch=='f') {
157     data[j++]='\f';
158     } else if (ch=='v') {
159     data[j++]='\v';
160     } else {
161     data[j++]=ch;
162     }
163     } else {
164     data[j++]=ch;
165     continue;
166     }
167     }
168     return j;
169     }
170    
171     #define SPLIT_STRING_INIT_COUNT 32
172     #define SPLIT_STRING_INCREASEMENT 32
173     extern char * * strtoargv(char const * str, unsigned int * count)
174     {
175     unsigned int n, index_size;
176     char * temp;
177     unsigned int i;
178     int j;
179     int * pindex;
180     void ** ptrindex;
181     char * result;
182    
183     if (!str || !count) return NULL;
184     temp=xmalloc(strlen(str)+1);
185     n = SPLIT_STRING_INIT_COUNT;
186     pindex=xmalloc(n * sizeof (int));
187    
188     i=j=0;
189     *count=0;
190     while (str[i]) {
191     while (str[i]==' ' || str[i]=='\t') i++;
192     if (!str[i]) break;
193     if (*count >=n ) {
194     n += SPLIT_STRING_INCREASEMENT;
195     pindex=(int *)xrealloc(pindex,n * sizeof(int));
196     }
197     pindex[*count]=j;
198     (*count)++;
199     if (str[i]=='"') {
200     i++;
201     while (str[i]) {
202     if (str[i]=='\\') {
203     i++;
204     if (!str[i]) break;
205     } else if (str[i]=='"') {
206     i++;
207     break;
208     }
209     temp[j++]=str[i++];
210     }
211     } else {
212     while (str[i] && str[i] != ' ' && str[i] != '\t') {
213     temp[j++]=str[i++];
214     }
215     }
216     temp[j++]='\0';
217     }
218     index_size= *count * sizeof(char *);
219     if (!index_size) {
220     xfree(temp);
221     xfree(pindex);
222     return NULL;
223     }
224     result=xmalloc(j+index_size);
225     memcpy(result+index_size,temp,j);
226    
227     ptrindex=xmalloc(*count * sizeof (char*));
228     for (i=0; i< *count; i++) {
229     ptrindex[i] = result + index_size + pindex[i];
230     }
231     memcpy(result,ptrindex,index_size);
232     xfree(temp);
233     xfree(pindex);
234     xfree(ptrindex);
235     return (char * *)result;
236     }
237    
238     #define COMBINE_STRING_INIT_LEN 1024
239     #define COMBINE_STRING_INCREASEMENT 1024
240     extern char * arraytostr(char * * array, char const * delim, int count)
241     {
242     int i;
243     unsigned int n;
244     char * result;
245     int need_delim;
246    
247     if (!delim || !array) return NULL;
248    
249     n=COMBINE_STRING_INIT_LEN;
250     result=xmalloc(n);
251     result[0]='\0';
252    
253     need_delim=0;
254     for (i=0; i<count; i++) {
255     if (!array[i]) continue;
256     if (strlen(result)+strlen(array[i])+strlen(delim)>=n) {
257     n+=COMBINE_STRING_INCREASEMENT;
258     result=xrealloc(result,n);
259     }
260     if (need_delim) {
261     strcat(result,delim);
262     }
263     strcat(result,array[i]);
264     need_delim=1;
265     }
266     result=xrealloc(result,strlen(result)+1);
267     return result;
268     }
269    

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1