source: branches/3.2/mondo/src/lib/mr_str.c @ 3614

Last change on this file since 3614 was 3614, checked in by Bruno Cornec, 4 years ago

mr_strip_spaces now returns a dynamically allocated string

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