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

Last change on this file since 3892 was 3892, checked in by Bruno Cornec, 4 months ago

More compiler fixes

  • Fix unused vars
  • Fix FreeBSD #if alone
  • Use MDSTAT_FILE everywhere
  • Fix missing break
  • Fix some strncpy. mr_strncpy used when safe
  • Fix wrong g_isoform_header_str proto !
  • find-cd & find-dvd => find-optical
  • Property svn:eol-style set to native
File size: 5.4 KB
RevLine 
[1054]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
[1924]8#include <stdlib.h>
[1054]9#include <stdio.h>
10#include <string.h>
[1087]11#include <time.h>
[1054]12
13#include "mr_mem.h"
[3892]14#include "mr_str.h"
[3171]15
16// to get bool type
17#define _MY_STUFF_H_
18#include "my-stuff.h"
[1054]19
20/**
[1594]21 * Safe alternative to standard function strtok()
[1054]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 **/
[3614]28char *mr_strtok(char *instr, const char *delims, int *lastpos) {
[1054]29
[3614]30char *token = NULL;
31char *strptr = NULL;
32size_t pos1 = 0;
33size_t pos2 = 0;
[1054]34
[3614]35if (instr == NULL) {
36 *lastpos = 0;
37 return token;
38}
[2970]39
[3614]40if (delims == NULL) {
41 *lastpos = 0;
42 return token;
43}
[2972]44
[3614]45if (strlen(instr) <= *lastpos) {
46 *lastpos = 0;
47 return token;
48}
[1054]49
[3614]50strptr = instr + *lastpos;
51pos2 = strspn(strptr, delims);
52strptr += pos2;
53pos1 = strcspn(strptr, delims);
54token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
[3892]55mr_strncpy(token, strptr, pos1);
[3614]56token[pos1] = '\0';
57*lastpos = *lastpos + pos1 + pos2 + 1;
[1054]58
[3614]59return token;
[1054]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 **/
[3171]72char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
[1054]73
[3614]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;
[1054]92 }
[3614]93 break;
[1054]94 }
[3614]95 escptr++;
[1054]96 }
[3614]97 inptr++;
98}
[3171]99
[3614]100inptr = instr;
101retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
102retptr = retstr;
[1054]103
[3614]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++;
[1054]120 }
[3614]121 break;
[1054]122 }
[3614]123 escptr++;
124 }
125 *retptr = *inptr;
126 retptr++;
127 inptr++;
128 if (found) {
129 // finish to put the remaining specialchr
130 *retptr = specialchr;
[1054]131 retptr++;
[3614]132 found = FALSE;
[1054]133 }
[3614]134}
135*retptr = '\0';
[1054]136
[3614]137return(retstr);
[1054]138}
[1087]139
140/* Return a string containing the date */
141char *mr_date(void) {
142
[3614]143time_t tcurr;
144
145tcurr = time(NULL);
146return(ctime(&tcurr));
[1087]147}
148
[3193]149/* Return an allocated string containing the date
150char *mr_date(void) {
151
152 time_t tcurr;
153 char *tmp = NULL;
[1168]154
[3193]155 tcurr = time(NULL);
156 mr_asprintf(tmp, "%s", ctime(&tcurr));
157 mr_chomp(tmp);
158 return(tmp);
159}
160 */
161
[1168]162/**
163 * Remove all characters whose ASCII value is less than or equal to 32
[3614]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
[1168]168 */
[3614]169char *mr_strip_spaces(const char *instr) {
[1168]170
[3614]171char *q = NULL;
172char *outstr = NULL;
[1168]173
[3614]174if (instr == NULL) {
175 return(NULL);
176}
[1205]177
[3614]178/* Skip initial spaces and special chars */
[3627]179while (*instr <= ' ' && *instr != '\0') {
180 instr++;
[3614]181}
[3723]182
[3724]183if (*instr == '\0') {
[3723]184 mr_asprintf(outstr, "");
185 return(outstr);
186}
187
[3627]188mr_asprintf(outstr, "%s", instr);
[3723]189q = outstr + strlen(outstr) - 1;
[1168]190
[3614]191/* Skip final spaces and special chars */
192while (*q <= ' ' && q != outstr) {
193 q--;
[1168]194}
[3193]195
[3614]196/* The string now ends after that char */
197q++;
198*q = '\0';
199return(outstr);
200}
201
[3794]202/* Subsitute the string "token" in the string "in" by the string "subst" */
[3294]203char *mr_str_substitute(const char *in, const char *token, const char *subst) {
[3193]204
[3294]205char *output = NULL;
206char *tmp = NULL;
207int 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}
[3641]215// token found: end string here for now
[3294]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
[3193]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
[3194]258 // Skip initial caracs
[3193]259 for (i = 0; index(caracs, in_out[i]) != NULL && i < (int)length ; i++);
260
[3194]261 // Shift the string to the begining if needed
[3193]262 if (i != 0) {
263 for (j = 0; i < (int)length ; i++, j++) {
264 in_out[j] = in_out[i];
265 }
[3194]266 // Erase the end of the string if needed
[3193]267 j++;
268 in_out[j] = '\0';
269 }
270
[3194]271 // Skip final caracs
[3193]272 for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
273
[3194]274 // The string now ends after that char
[3193]275 i++;
276 in_out[i] = '\0';
277}
278*/
279
Note: See TracBrowser for help on using the repository browser.