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

Last change on this file since 2970 was 2970, checked in by Bruno Cornec, 12 years ago
  • Fixes some valgrind warnings
  • 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 <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 (strlen(instr) <= *lastpos) {
37 *lastpos = 0;
38 return token;
39 }
40
41 strptr = instr + *lastpos;
42 pos2 = strspn(strptr, delims);
43 strptr += pos2;
44 pos1 = strcspn(strptr, delims);
45 token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
46 strncpy(token, strptr, pos1);
47 token[pos1] = '\0';
48 *lastpos = *lastpos + pos1 + pos2 + 1;
49
50 return token;
51}
52
53
54/**
55 * Returns the string fed to it 'inptr' with all characters to escape given
56 * in 'toesc' prepended by escaping character 'escchr'.
57 * (Prepare strings for use in system() or popen() with this function.)
58 * @param instr
59 * @param toesc
60 * @param escchr
61 * @note this function allocates memory that needs to be freed by caller
62 **/
63char *mr_stresc(char *instr, char *toesc, const char escchr) {
64 char *inptr = NULL;
65 char *retstr = NULL;
66 char *retptr = NULL;
67 char *escptr = NULL;
68 int cnt = 0;
69
70 inptr = instr;
71
72 // Count how many characters need escaping.
73 while (*inptr != '\0') {
74 escptr = toesc;
75 while (*escptr != '\0') {
76 if (*inptr == *escptr) {
77 // Found it, skip the rest.
78 cnt++;
79 break;
80 }
81 escptr++;
82 }
83 inptr++;
84 }
85 inptr = instr;
86
87 retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
88 retptr = retstr;
89
90 // Prepend specified characters with escape character.
91 while (*inptr != '\0') {
92 escptr = toesc;
93 while (*escptr != '\0') {
94 if (*inptr == *escptr) {
95 // Found it, skip the rest.
96 *retptr = escchr;
97 retptr++;
98 break;
99 }
100 escptr++;
101 }
102 *retptr = *inptr;
103 retptr++;
104 inptr++;
105 }
106 *retptr = '\0';
107
108 return retstr;
109}
110
111/* Return a string containing the date */
112char *mr_date(void) {
113
114 time_t tcurr;
115
116 tcurr = time(NULL);
117 return(ctime(&tcurr));
118}
119
120
121/**
122 * Remove all characters whose ASCII value is less than or equal to 32
123 * (spaces and control characters) from both sides of @p in_out.
124 * @param in_out The string to strip spaces/control characters from (modified).
125 */
126void mr_strip_spaces(char *in_out) {
127 int i = 0;
128 int j = 0;
129 size_t length;
130
131 if (in_out == NULL) {
132 return;
133 }
134 length = strlen(in_out);
135
136 /* Skip initial spaces and special chars */
137 for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
138 /* Shift the string to the begining if needed */
139 if (i != 0) {
140 for (j = 0; i < (int)length ; i++, j++) {
141 in_out[j] = in_out[i];
142 }
143 /* Erase the end of the string if needed */
144 j++;
145 in_out[j] = '\0';
146 }
147
148 /* Skip final spaces and special chars */
149 for (i = (int)strlen(in_out) - 1; i >= 0 && in_out[i] <= ' '; i--);
150
151 /* The string now ends after that char */
152 i++;
153 in_out[i] = '\0';
154}
Note: See TracBrowser for help on using the repository browser.