source: MondoRescue/branches/3.3/mondo/src/lib/mr_str.c

Last change on this file was 3893, checked in by Bruno Cornec, 6 weeks ago

Remove more warnings - switch and size_t/int comparisons

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1/*
2 * $Id$
3 *
4 * New generation of string handling functions safe from a memory management point of view
5 * Developped by Andree Leidenfrost
6 */
7
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11#include <time.h>
12
13#include "mr_mem.h"
14#include "mr_str.h"
15
16// to get bool type
17#define _MY_STUFF_H_
18#include "my-stuff.h"
19
20/**
21 * Safe alternative to standard function strtok()
22 * @param instr
23 * @param delims
24 * @param lastpos
25 * @return @p
26 * @note this function allocates memory that needs to be freed by caller
27 **/
28char *mr_strtok(char *instr, const char *delims, size_t *lastpos) {
29
30char *token = NULL;
31char *strptr = NULL;
32size_t pos1 = 0;
33size_t pos2 = 0;
34
35if (instr == NULL) {
36 *lastpos = 0;
37 return token;
38}
39
40if (delims == NULL) {
41 *lastpos = 0;
42 return token;
43}
44
45if (strlen(instr) <= *lastpos) {
46 *lastpos = 0;
47 return token;
48}
49
50strptr = instr + *lastpos;
51pos2 = strspn(strptr, delims);
52strptr += pos2;
53pos1 = strcspn(strptr, delims);
54token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
55mr_strncpy(token, strptr, pos1);
56token[pos1] = '\0';
57*lastpos = *lastpos + pos1 + pos2 + 1;
58
59return(token);
60}
61
62
63/**
64 * Returns the string fed to it 'inptr' with all characters to escape given
65 * in 'toesc' prepended by escaping character 'escchr'.
66 * (Prepare strings for use in system() or popen() with this function.)
67 * @param instr
68 * @param toesc
69 * @param escchr
70 * @note this function allocates memory that needs to be freed by caller
71 **/
72char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
73
74char *inptr = NULL;
75char *retstr = NULL;
76char *retptr = NULL;
77char *escptr = NULL;
78int cnt = 0;
79bool found = FALSE;
80
81inptr = instr;
82// Count how many characters need escaping.
83while (*inptr != '\0') {
84 escptr = toesc;
85 while (*escptr != '\0') {
86 if (*inptr == *escptr) {
87 // Found it, skip the rest.
88 cnt++;
89 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
90 if (*inptr == specialchr) {
91 cnt += 2;
92 }
93 break;
94 }
95 escptr++;
96 }
97 inptr++;
98}
99
100inptr = instr;
101retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
102retptr = retstr;
103
104// Prepend specified characters with escape character.
105while (*inptr != '\0') {
106 escptr = toesc;
107 while (*escptr != '\0') {
108 if (*inptr == *escptr) {
109 // Found it, skip the rest.
110 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
111 if (*inptr == specialchr) {
112 *retptr = specialchr;
113 retptr++;
114 *retptr = escchr;
115 retptr++;
116 found = TRUE;
117 } else {
118 *retptr = escchr;
119 retptr++;
120 }
121 break;
122 }
123 escptr++;
124 }
125 *retptr = *inptr;
126 retptr++;
127 inptr++;
128 if (found) {
129 // finish to put the remaining specialchr
130 *retptr = specialchr;
131 retptr++;
132 found = FALSE;
133 }
134}
135*retptr = '\0';
136
137return(retstr);
138}
139
140/* Return a string containing the date */
141char *mr_date(void) {
142
143time_t tcurr;
144
145tcurr = time(NULL);
146return(ctime(&tcurr));
147}
148
149/* Return an allocated string containing the date
150char *mr_date(void) {
151
152 time_t tcurr;
153 char *tmp = NULL;
154
155 tcurr = time(NULL);
156 mr_asprintf(tmp, "%s", ctime(&tcurr));
157 mr_chomp(tmp);
158 return(tmp);
159}
160 */
161
162/**
163 * Remove all characters whose ASCII value is less than or equal to 32
164 * (spaces and control characters) from both sides of @p instr.
165 * @param instr The string to strip spaces/control characters from
166 * returns a newly allocated string without the spaces that needs to be freed
167 * by the caller
168 */
169char *mr_strip_spaces(const char *instr) {
170
171char *q = NULL;
172char *outstr = NULL;
173
174if (instr == NULL) {
175 return(NULL);
176}
177
178/* Skip initial spaces and special chars */
179while (*instr <= ' ' && *instr != '\0') {
180 instr++;
181}
182
183if (*instr == '\0') {
184 mr_asprintf(outstr, "");
185 return(outstr);
186}
187
188mr_asprintf(outstr, "%s", instr);
189q = outstr + strlen(outstr) - 1;
190
191/* Skip final spaces and special chars */
192while (*q <= ' ' && q != outstr) {
193 q--;
194}
195
196/* The string now ends after that char */
197q++;
198*q = '\0';
199return(outstr);
200}
201
202/* Subsitute the string "token" in the string "in" by the string "subst" */
203char *mr_str_substitute(const char *in, const char *token, const char *subst) {
204
205char *output = NULL;
206char *tmp = NULL;
207size_t i = 0;
208
209mr_asprintf(output, "%s", in);
210tmp = strstr(output, token);
211if (tmp == NULL) {
212 // token not found returning initial string unmodified
213 return(output);
214}
215// token found: end string here for now
216*tmp = '\0';
217
218// Add subst content to the string
219mr_strcat(output,subst);
220
221// now add the rest of in
222tmp = strstr(in, token);
223for (; i < strlen(token) ; i++) {
224 tmp++;
225}
226mr_strcat(output,tmp);
227return(output);
228}
229
230
231/*
232 * Remove '\n' char from both sides of @p in_out.
233 * @param in_out The string to strip characters from (modified).
234
235void mr_chomp(char *in_out) {
236
237 mr_strip_char(in_out, "\n");
238}
239*/
240
241/**
242 * Remove all characters in caracs from begining and end of string @p in_out
243 * @param in_out The string to strip char characters from (modified).
244
245void mr_strip_char(char *in_out, char *caracs) {
246 int i = 0;
247 int j = 0;
248 size_t length = 0;
249
250 if (caracs == NULL) {
251 return;
252 }
253 if (in_out == NULL) {
254 return;
255 }
256 length = strlen(in_out);
257
258 // Skip initial caracs
259 for (i = 0; index(caracs, in_out[i]) != NULL && i < (int)length ; i++);
260
261 // Shift the string to the begining if needed
262 if (i != 0) {
263 for (j = 0; i < (int)length ; i++, j++) {
264 in_out[j] = in_out[i];
265 }
266 // Erase the end of the string if needed
267 j++;
268 in_out[j] = '\0';
269 }
270
271 // Skip final caracs
272 for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
273
274 // The string now ends after that char
275 i++;
276 in_out[i] = '\0';
277}
278*/
279
Note: See TracBrowser for help on using the repository browser.