source: MondoRescue/branches/stable/mondo/src/lib/mr_str.c@ 1168

Last change on this file since 1168 was 1168, checked in by Bruno Cornec, 17 years ago

strip_spaces => mr_strip_spaces in mr_str.c and corrected at the same time :-)

  • Property svn:eol-style set to native
File size: 3.1 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 <stdio.h>
9#include <string.h>
10#include <time.h>
11
12#include "mr_mem.h"
13
14/**
15 * Safe alternative to standard function strok()
16 * Build a partition name from a drive and a partition number.
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{
60
61 char *inptr = NULL;
62 char *retstr = NULL;
63 char *retptr = NULL;
64 char *escptr = NULL;
65 int cnt = 0;
66
67 inptr = instr;
68
69 // Count how many characters need escaping.
70 while (*inptr != '\0') {
71 escptr = toesc;
72 while (*escptr != '\0') {
73 if (*inptr == *escptr) {
74 // Found it, skip the rest.
75 cnt++;
76 inptr++;
77 break;
78 }
79 inptr++;
80 escptr++;
81 }
82 }
83 inptr = instr;
84
85 retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
86 retptr = retstr;
87
88 // Prepend specified characters with escape character.
89 while (*inptr != '\0') {
90 escptr = toesc;
91 while (*escptr != '\0') {
92 if (*inptr == *escptr) {
93 // Found it, skip the rest.
94 *retptr = escchr;
95 retptr++;
96 break;
97 }
98 escptr++;
99 }
100 *retptr = *inptr;
101 retptr++;
102 inptr++;
103 }
104 *retptr = '\0';
105
106 return retstr;
107}
108
109/* Return a string containing the date */
110char *mr_date(void) {
111
112 time_t tcurr;
113
114 tcurr = time(NULL);
115 return(ctime(&tcurr));
116}
117
118
119/**
120 * Remove all characters whose ASCII value is less than or equal to 32
121 * (spaces and control characters) from both sides of @p in_out.
122 * @param in_out The string to strip spaces/control characters from (modified).
123 */
124void mr_strip_spaces(char *in_out)
125{
126 /*@ int ******************************************************** */
127 int i;
128 int j;
129 size_t length;
130
131 /*@ end vars *************************************************** */
132
133 if (in_out == NULL) {
134 return;
135 }
136 length = strlen(in_out);
137
138 /* Skip initial spaces and special chars */
139 for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
140 /* Shift the string to the begining */
141 for (j = 0; i < (int)length ; i++, j++) {
142 in_out[j] = in_out[i];
143 }
144 /* Skip final spaces and special chars */
145 for (i = (int)strlen(in_out) - 1; in_out[i] <= ' ' && i >= 0 ; i--);
146
147 /* The string now ends after that char */
148 i++;
149 in_out[i] = '\0';
150}
Note: See TracBrowser for help on using the repository browser.