-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrutils.c
236 lines (185 loc) · 4.73 KB
/
strutils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/***********************************************************************
SPL, the Shakespeare Programming Language
Copyright (C) 2001 Karl Hasselström and Jon Åslund
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
***********************************************************************/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "strutils.h"
char *cat2(char *str1, char *str2)
{
int len;
char *ret;
/* malloc new string */
len = strlen(str1) + strlen(str2) + 1;
ret = (char *) malloc(len*sizeof(char));
/* copy the strings */
strcpy(ret, str1);
strcat(ret, str2);
/* free old strings */
free(str1);
free(str2);
return ret;
}
char *cat3(char *str1, char *str2, char *str3)
{
return cat2(cat2(str1, str2), str3);
}
char *cat4(char *str1, char *str2, char *str3, char *str4)
{
return cat2(cat2(str1, str2), cat2(str3, str4));
}
char *cat5(char *str1, char *str2, char *str3, char *str4,
char *str5)
{
return cat3(cat2(str1, str2), cat2(str3, str4), str5);
}
char *cat6(char *str1, char *str2, char *str3, char *str4,
char *str5, char *str6)
{
return cat3(cat2(str1, str2), cat2(str3, str4), cat2(str5, str6));
}
char *cat7(char *str1, char *str2, char *str3, char *str4,
char *str5, char *str6, char *str7)
{
return cat4(cat2(str1, str2), cat2(str3, str4), cat2(str5, str6), str7);
}
char *cat8(char *str1, char *str2, char *str3, char *str4,
char *str5, char *str6, char *str7, char *str8)
{
return cat4(cat2(str1, str2), cat2(str3, str4),
cat2(str5, str6), cat2(str7, str8));
}
char *cat9(char *str1, char *str2, char *str3, char *str4,
char *str5, char *str6, char *str7, char *str8,
char *str9)
{
return cat5(cat2(str1, str2), cat2(str3, str4), cat2(str5, str6),
cat2(str7, str8), str9);
}
char *int2str(int i)
{
char *str;
str = (char *) malloc(25*sizeof(char));
sprintf(str, "%d", i);
return str;
}
char *newstr(const char *str)
{
int len;
char *ret;
/* malloc new string */
len = strlen(str) + 1;
ret = (char *) malloc(len*sizeof(char));
/* copy the strings */
strcpy(ret, str);
return ret;
}
char *space2underscore(char *str)
{
char *c;
for(c = str; *c != '\0'; c++) {
if(isspace((int) *c))
*c = '_';
}
return str;
}
char *singlespace(char *str)
{
char *read;
char *write;
int last_space = 0;
int this_space = 0;
/* Walk through the string, deleting multiple spaces. */
for (read = write = str; *read != '\0'; read++) {
/* Was the last character a space? */
last_space = this_space;
/* Is this character a space? */
this_space = isspace((int) *read);
/* Copy the character unless this would make more than one space
in a row. */
if (!(last_space && this_space)) {
/* Copy. */
*write = *read;
/* Advance write position. */
write++;
}
}
/* Write \0 at end of string. */
*write = '\0';
/* Return the modified string. */
return str;
}
char *str2lower(char *str)
{
char *c;
for (c = str; *c != '\0'; c++) {
*c = (char) tolower((int) *c);
}
return str;
}
char *str2upper(char *str)
{
char *c;
for (c = str; *c != '\0'; c++) {
*c = (char) toupper((int) *c);
}
return str;
}
char *str2varname(char *str)
{
return str2lower(space2underscore(singlespace(str)));
}
char *strindent(char *str, char *indent)
{
int newlines, len;
char *newstr;
int i, j;
newlines = 1;
for (i = 0; str[i] != '\0'; i++) {
if (str[i] == '\n') {
newlines++;
}
}
len = strlen(str) + strlen(indent)*newlines + 1;
newstr = (char *) malloc(len*sizeof(char));
j = 0;
for (i = 0; str[i] != '\0'; i++) {
if (i == 0 || str[i - 1] == '\n') {
strcpy(newstr + j, indent);
j += strlen(indent);
}
newstr[j] = str[i];
j++;
}
newstr[j] = '\0';
free(str);
free(indent);
return newstr;
}
char *strpad(char *str, int length, char fill)
{
int i;
if (strlen(str) >= length) {
return str;
}
str = (char *) realloc(str, (length+1)*sizeof(char));
for (i = strlen(str); i < length; i++) {
str[i] = fill;
}
str[length] = '\0';
return str;
}