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