source: MondoRescue/branches/3.0/mondo/src/lib/mr_str.c@ 2972

Last change on this file since 2972 was 2972, checked in by Bruno Cornec, 12 years ago
  • Uses mr_strcat again instead of multiple mr_asprintf
  • Generats the correct mdadm create command when using UUID for fixing #596. Needs to be checked
  • More valgrind warning reported that may be fixed here.
  • Property svn:eol-style set to native
File size: 3.2 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/* Return a string containing the date */
117char *mr_date(void) {
118
119 time_t tcurr;
120
121 tcurr = time(NULL);
122 return(ctime(&tcurr));
123}
124
125
126/**
127 * Remove all characters whose ASCII value is less than or equal to 32
128 * (spaces and control characters) from both sides of @p in_out.
129 * @param in_out The string to strip spaces/control characters from (modified).
130 */
131void mr_strip_spaces(char *in_out) {
132 int i = 0;
133 int j = 0;
134 size_t length;
135
136 if (in_out == NULL) {
137 return;
138 }
139 length = strlen(in_out);
140
141 /* Skip initial spaces and special chars */
142 for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
143 /* Shift the string to the begining if needed */
144 if (i != 0) {
145 for (j = 0; i < (int)length ; i++, j++) {
146 in_out[j] = in_out[i];
147 }
148 /* Erase the end of the string if needed */
149 j++;
150 in_out[j] = '\0';
151 }
152
153 /* Skip final spaces and special chars */
154 for (i = (int)strlen(in_out) - 1; i >= 0 && in_out[i] <= ' '; i--);
155
156 /* The string now ends after that char */
157 i++;
158 in_out[i] = '\0';
159}
Note: See TracBrowser for help on using the repository browser.