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

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

Use of conf file entries (iso_burning_*, media_device, media_size)
and adapatation of the rest of the code to that (including bkpinfo)

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