Debian Codegraph @

terps/hugo/source/stringfn.c

raw symbols SCIP
1 /*
2 STRINGFN.C
3
4 String manipulation functions
5
6 Copyright (c) 1995-2009 by Kent Tessman
7
8 Only if ALLOW_NESTING is #defined may these calls be nested, and then
9 only two at a time (noting that it takes another 1025 bytes of global
10 storage to do so).
11 */
12
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <ctype.h>
17
18 char *Left(char *a, int l);
19 char *Ltrim(char *a);
20 char *Mid(char *a, int pos, int n);
21 char *Right(char *a, int l);
22 char *Rtrim(char *a);
23
24 #ifdef QUICKC
25 #define OMIT_EXTRA_STRING_FUNCTIONS
26 #endif
27
28 #ifndef OMIT_EXTRA_STRING_FUNCTIONS
29 #define EXTRA_STRING_FUNCTIONS
30 #endif
31
32 #if defined (EXTRA_STRING_FUNCTIONS)
33 char *itoa(int a, char *buf, int base);
34 char *strlwr(char *s);
35 char *strnset(char *s, int c, size_t l);
36 char *strupr(char *s);
37 #endif
38
39
40 /* GETTEMPSTRING
41
42 Prevents having to make sure each string-returning function has its
43 own static char array to copy into. In other words, for five
44 functions, we don't need 5 * 1K each of static char array.
45
46 NOTE: Assumes that no more than NUM_TEMPSTRINGS nested
47 string-manipulations will be done at once.
48 */
49
50 #ifndef ALLOW_NESTING
51 static char tempstring[1025];
52 #else
53 #define NUM_TEMPSTRINGS 2
54 static char tempstring[NUM_TEMPSTRINGS][1025];
55 static char tempstring_count = 0;
56
57 static char *GetTempString(void)
58 {
59 static char *r;
60
61 r = &tempstring[(int)tempstring_count][0];
62 if (++tempstring_count >= NUM_TEMPSTRINGS) tempstring_count = 0;
63
64 return r;
65 }
66 #endif
67
68
69 /* The following string-manipulation functions closely mimic BASIC-language
70 string functionality. They do not alter the provided string; instead,
71 they return a pointer to a static (modified) copy.
72 */
73
74
75 /* LEFT */
76
77 char *Left(char a[], int l)
78 {
79 static char *temp;
80 int i;
81
82 #ifdef ALLOW_NESTING
83 temp = GetTempString();
84 #else
85 temp = &tempstring[0];
86 #endif
87 if (l > (int)strlen(a))
88 l = strlen(a);
89 for (i = 0; i<l; i++)
90 temp[i] = a[i];
91 temp[i] = '\0';
92 return temp;
93 }
94
95
96 /* LTRIM */
97
98 char *Ltrim(char a[])
99 {
100 static char *temp;
101
102 #ifdef ALLOW_NESTING
103 temp = GetTempString();
104 #else
105 temp = &tempstring[0];
106 #endif
107 strcpy(temp, a);
108 while (temp[0]==' ' || temp[0]=='\t')
109 strcpy(temp, temp+1);
110 return temp;
111 }
112
113
114 /* MID */
115
116 char *Mid(char a[], int pos, int n)
117 {
118 static char *temp;
119 int i;
120
121 #ifdef ALLOW_NESTING
122 temp = GetTempString();
123 #else
124 temp = &tempstring[0];
125 #endif
126 pos--;
127 if (pos+n > (int)strlen(a))
128 n = strlen(a)-pos;
129 for (i = 0; i<n; i++)
130 temp[i] = a[pos+i];
131 temp[i] = '\0';
132 return temp;
133 }
134
135
136 /* RIGHT */
137
138 char *Right(char a[], int l)
139 {
140 static char *temp;
141 int i;
142
143 #ifdef ALLOW_NESTING
144 temp = GetTempString();
145 #else
146 temp = &tempstring[0];
147 #endif
148 if (l > (int)strlen(a))
149 l = strlen(a);
150 for (i = 0; i<l; i++)
151 temp[i] = a[strlen(a)-l+i];
152 temp[i] = '\0';
153 return temp;
154 }
155
156
157 /* RTRIM */
158
159 char *Rtrim(char a[])
160 {
161 static char *temp;
162 int len;
163
164 #ifdef ALLOW_NESTING
165 temp = GetTempString();
166 #else
167 temp = &tempstring[0];
168 #endif
169 strcpy(temp, a);
170 while (((len = strlen(temp))) && (temp[len-1]==' ' || temp[len-1]=='\t'))
171 strcpy(temp, Left(temp, len-1));
172 return temp;
173 }
174
175
176 #if defined (EXTRA_STRING_FUNCTIONS)
177
178 char *itoa(int a, char *buf, int base)
179 {
180 (void)base;
181 /* This only works if base is 10 (which it will be) */
182 sprintf(buf, "%d" ,a);
183 return buf;
184 }
185
186 char *strlwr(char *s)
187 {
188 int i;
189
190 i = 0;
191 while (*(s+i)!='\0')
192 {
193 *(s+i) = tolower(*(s+i));
194 i++;
195 }
196 return s;
197 }
198
199 char *strnset(char *s, int c, size_t l)
200 {
201 int i;
202
203 for (i=0; i<(int)l; i++) *(s+i) = (char)c;
204 return s;
205 }
206
207 char *strupr(char *s)
208 {
209 int i;
210
211 i = 0;
212 while (*(s+i)!='\0')
213 {
214 *(s+i) = toupper(*(s+i));
215 i++;
216 }
217 return s;
218 }
219
220 #endif
221
222 /* Sean Barrett wrote this routine to fix some strcpy problems
223 we saw when strcpy was used on overlapping strings. */
224 char *hugo_strcpy(char *s, const char *t)
225 {
226 char *r = s;
227 while ((*s++ = *t++) != 0) ;
228 return r;
229 }