source: MondoRescue/branches/2.2.10/mondo/src/lib/mr_str.c@ 2421

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