source: MondoRescue/branches/3.1/mondo/src/lib/mr_str.c@ 3147

Last change on this file since 3147 was 3147, checked in by Bruno Cornec, 11 years ago
  • First pass on svn merge -r 2935:3146 ../3.0
  • Property svn:eol-style set to native
File size: 3.9 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/**
16 * Safe alternative to standard function strtok()
17 * @param instr
18 * @param delims
19 * @param lastpos
20 * @return @p
21 * @note this function allocates memory that needs to be freed by caller
22 **/
23char *mr_strtok(char *instr, const char *delims, int *lastpos)
24{
25
26 char *token = NULL;
27 char *strptr = NULL;
28 size_t pos1 = 0;
29 size_t pos2 = 0;
30
31 if (instr == NULL) {
32 *lastpos = 0;
33 return token;
34 }
35
36 if (delims == NULL) {
37 *lastpos = 0;
38 return token;
39 }
40
41 if (strlen(instr) <= *lastpos) {
42 *lastpos = 0;
43 return token;
44 }
45
46 strptr = instr + *lastpos;
47 pos2 = strspn(strptr, delims);
48 strptr += pos2;
49 pos1 = strcspn(strptr, delims);
50 token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
51 strncpy(token, strptr, pos1);
52 token[pos1] = '\0';
53 *lastpos = *lastpos + pos1 + pos2 + 1;
54
55 return token;
56}
57
58
59/**
60 * Returns the string fed to it 'inptr' with all characters to escape given
61 * in 'toesc' prepended by escaping character 'escchr'.
62 * (Prepare strings for use in system() or popen() with this function.)
63 * @param instr
64 * @param toesc
65 * @param escchr
66 * @note this function allocates memory that needs to be freed by caller
67 **/
68char *mr_stresc(char *instr, char *toesc, const char escchr) {
69 char *inptr = NULL;
70 char *retstr = NULL;
71 char *retptr = NULL;
72 char *escptr = NULL;
73 int cnt = 0;
74
75 inptr = instr;
76
77 // Count how many characters need escaping.
78 while (*inptr != '\0') {
79 escptr = toesc;
80 while (*escptr != '\0') {
81 if (*inptr == *escptr) {
82 // Found it, skip the rest.
83 cnt++;
84 break;
85 }
86 escptr++;
87 }
88 inptr++;
89 }
90 inptr = instr;
91
92 retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
93 retptr = retstr;
94
95 // Prepend specified characters with escape character.
96 while (*inptr != '\0') {
97 escptr = toesc;
98 while (*escptr != '\0') {
99 if (*inptr == *escptr) {
100 // Found it, skip the rest.
101 *retptr = escchr;
102 retptr++;
103 break;
104 }
105 escptr++;
106 }
107 *retptr = *inptr;
108 retptr++;
109 inptr++;
110 }
111 *retptr = '\0';
112
113 return retstr;
114}
115
116/**
117 * Remove all characters in caracs from begining and end of string @p in_out
118 * @param in_out The string to strip char characters from (modified).
119 */
120void mr_strip_char(char *in_out, char *caracs) {
121 int i = 0;
122 int j = 0;
123 int length = 0;
124
125 if (caracs == NULL) {
126 return;
127 }
128 if (in_out == NULL) {
129 return;
130 }
131 length = (int)strlen(in_out);
132
133 /* Skip initial caracs */
134 for (i = 0; index(caracs, in_out[i]) != NULL && i < length ; i++);
135
136 /* Shift the string to the begining if needed */
137 if (i != 0) {
138 for (j = 0; i < length ; i++, j++) {
139 in_out[j] = in_out[i];
140 }
141 /* Erase the end of the string if needed */
142 j++;
143 in_out[j] = '\0';
144 }
145
146 /* Skip final spaces and special chars */
147 for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
148
149 /* The string now ends after that char */
150 i++;
151 in_out[i] = '\0';
152}
153
154/**
155 * Remove all characters whose ASCII value is less than or equal to 32
156 * (spaces and control characters) from both sides of @p in_out.
157 * @param in_out The string to strip spaces/control characters from (modified).
158 */
159void mr_strip_spaces(char *in_out) {
160
161 int i;
162 char *tmp = NULL;
163
164 mr_asprintf(tmp, "");
165
166 /* Build the string of char to avoid: ctrl char up to space*/
167 for (i = 0; i <= ' '; i++) {
168 mr_strcat(tmp, "%c", i);
169 }
170
171 mr_strip_char(in_out, tmp);
172 mr_free(tmp);
173}
174
175/*
176 * Remove '\n' char from both sides of @p in_out.
177 * @param in_out The string to strip characters from (modified).
178 */
179void mr_chomp(char *in_out) {
180
181 mr_strip_char(in_out, "\n");
182}
183
184/* Return an allocated string containing the date */
185char *mr_date(void) {
186
187 time_t tcurr;
188 char *tmp = NULL;
189
190 tcurr = time(NULL);
191 mr_asprintf(tmp, "%s", ctime(&tcurr));
192 mr_chomp(tmp);
193 return(tmp);
194}
195
196
Note: See TracBrowser for help on using the repository browser.