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 | **/ |
---|
23 | char *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 | **/ |
---|
58 | char *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 */ |
---|
110 | char *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 | */ |
---|
124 | void mr_strip_spaces(char *in_out) { |
---|
125 | int i = 0; |
---|
126 | int j = 0; |
---|
127 | size_t length; |
---|
128 | |
---|
129 | if (in_out == NULL) { |
---|
130 | return; |
---|
131 | } |
---|
132 | length = strlen(in_out); |
---|
133 | |
---|
134 | /* Skip initial spaces and special chars */ |
---|
135 | for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++); |
---|
136 | /* Shift the string to the begining if needed */ |
---|
137 | if (i != 0) { |
---|
138 | for (j = 0; i < (int)length ; i++, j++) { |
---|
139 | in_out[j] = in_out[i]; |
---|
140 | } |
---|
141 | /* Erase the end of the string if needed */ |
---|
142 | j++; |
---|
143 | in_out[j] = '\0'; |
---|
144 | } |
---|
145 | |
---|
146 | /* Skip final spaces and special chars */ |
---|
147 | for (i = (int)strlen(in_out) - 1; in_out[i] <= ' ' && i >= 0 ; i--); |
---|
148 | |
---|
149 | /* The string now ends after that char */ |
---|
150 | i++; |
---|
151 | in_out[i] = '\0'; |
---|
152 | } |
---|