[1054] | 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 |
|
---|
[1924] | 8 | #include <stdlib.h>
|
---|
[1054] | 9 | #include <stdio.h>
|
---|
| 10 | #include <string.h>
|
---|
[1087] | 11 | #include <time.h>
|
---|
[1054] | 12 |
|
---|
| 13 | #include "mr_mem.h"
|
---|
[3171] | 14 |
|
---|
| 15 | // to get bool type
|
---|
| 16 | #define _MY_STUFF_H_
|
---|
| 17 | #include "my-stuff.h"
|
---|
[1054] | 18 |
|
---|
| 19 | /**
|
---|
[1594] | 20 | * Safe alternative to standard function strtok()
|
---|
[1054] | 21 | * @param instr
|
---|
| 22 | * @param delims
|
---|
| 23 | * @param lastpos
|
---|
| 24 | * @return @p
|
---|
| 25 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 26 | **/
|
---|
| 27 | char *mr_strtok(char *instr, const char *delims, int *lastpos)
|
---|
| 28 | {
|
---|
| 29 |
|
---|
| 30 | char *token = NULL;
|
---|
| 31 | char *strptr = NULL;
|
---|
| 32 | size_t pos1 = 0;
|
---|
| 33 | size_t pos2 = 0;
|
---|
| 34 |
|
---|
[2970] | 35 | if (instr == NULL) {
|
---|
| 36 | *lastpos = 0;
|
---|
| 37 | return token;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[2972] | 40 | if (delims == NULL) {
|
---|
| 41 | *lastpos = 0;
|
---|
| 42 | return token;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[1054] | 45 | if (strlen(instr) <= *lastpos) {
|
---|
| 46 | *lastpos = 0;
|
---|
| 47 | return token;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | strptr = instr + *lastpos;
|
---|
| 51 | pos2 = strspn(strptr, delims);
|
---|
| 52 | strptr += pos2;
|
---|
| 53 | pos1 = strcspn(strptr, delims);
|
---|
| 54 | token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
|
---|
| 55 | strncpy(token, strptr, pos1);
|
---|
| 56 | token[pos1] = '\0';
|
---|
| 57 | *lastpos = *lastpos + pos1 + pos2 + 1;
|
---|
| 58 |
|
---|
| 59 | return token;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Returns the string fed to it 'inptr' with all characters to escape given
|
---|
| 65 | * in 'toesc' prepended by escaping character 'escchr'.
|
---|
| 66 | * (Prepare strings for use in system() or popen() with this function.)
|
---|
| 67 | * @param instr
|
---|
| 68 | * @param toesc
|
---|
| 69 | * @param escchr
|
---|
| 70 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 71 | **/
|
---|
[3171] | 72 | char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
|
---|
[1054] | 73 | char *inptr = NULL;
|
---|
| 74 | char *retstr = NULL;
|
---|
| 75 | char *retptr = NULL;
|
---|
| 76 | char *escptr = NULL;
|
---|
| 77 | int cnt = 0;
|
---|
[3171] | 78 | bool found = FALSE;
|
---|
[1054] | 79 |
|
---|
| 80 | inptr = instr;
|
---|
| 81 | // Count how many characters need escaping.
|
---|
| 82 | while (*inptr != '\0') {
|
---|
| 83 | escptr = toesc;
|
---|
| 84 | while (*escptr != '\0') {
|
---|
| 85 | if (*inptr == *escptr) {
|
---|
| 86 | // Found it, skip the rest.
|
---|
| 87 | cnt++;
|
---|
[3171] | 88 | // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
|
---|
| 89 | if (*inptr == specialchr) {
|
---|
| 90 | cnt += 2;
|
---|
| 91 | }
|
---|
[1054] | 92 | break;
|
---|
| 93 | }
|
---|
| 94 | escptr++;
|
---|
| 95 | }
|
---|
[1551] | 96 | inptr++;
|
---|
[1054] | 97 | }
|
---|
[3171] | 98 |
|
---|
[1054] | 99 | inptr = instr;
|
---|
| 100 | retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
|
---|
| 101 | retptr = retstr;
|
---|
| 102 |
|
---|
| 103 | // Prepend specified characters with escape character.
|
---|
| 104 | while (*inptr != '\0') {
|
---|
| 105 | escptr = toesc;
|
---|
| 106 | while (*escptr != '\0') {
|
---|
| 107 | if (*inptr == *escptr) {
|
---|
| 108 | // Found it, skip the rest.
|
---|
[3171] | 109 | // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
|
---|
| 110 | if (*inptr == specialchr) {
|
---|
| 111 | *retptr = specialchr;
|
---|
| 112 | retptr++;
|
---|
| 113 | *retptr = escchr;
|
---|
| 114 | retptr++;
|
---|
| 115 | found = TRUE;
|
---|
| 116 | } else {
|
---|
| 117 | *retptr = escchr;
|
---|
| 118 | retptr++;
|
---|
| 119 | }
|
---|
[1054] | 120 | break;
|
---|
| 121 | }
|
---|
| 122 | escptr++;
|
---|
| 123 | }
|
---|
| 124 | *retptr = *inptr;
|
---|
| 125 | retptr++;
|
---|
| 126 | inptr++;
|
---|
[3171] | 127 | if (found) {
|
---|
| 128 | // finish to put the remaining specialchr
|
---|
| 129 | *retptr = specialchr;
|
---|
| 130 | retptr++;
|
---|
| 131 | found = FALSE;
|
---|
| 132 | }
|
---|
[1054] | 133 | }
|
---|
| 134 | *retptr = '\0';
|
---|
| 135 |
|
---|
| 136 | return retstr;
|
---|
| 137 | }
|
---|
[1087] | 138 |
|
---|
| 139 | /* Return a string containing the date */
|
---|
| 140 | char *mr_date(void) {
|
---|
| 141 |
|
---|
| 142 | time_t tcurr;
|
---|
| 143 |
|
---|
| 144 | tcurr = time(NULL);
|
---|
| 145 | return(ctime(&tcurr));
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[3193] | 148 | /* Return an allocated string containing the date
|
---|
| 149 | char *mr_date(void) {
|
---|
| 150 |
|
---|
| 151 | time_t tcurr;
|
---|
| 152 | char *tmp = NULL;
|
---|
[1168] | 153 |
|
---|
[3193] | 154 | tcurr = time(NULL);
|
---|
| 155 | mr_asprintf(tmp, "%s", ctime(&tcurr));
|
---|
| 156 | mr_chomp(tmp);
|
---|
| 157 | return(tmp);
|
---|
| 158 | }
|
---|
| 159 | */
|
---|
| 160 |
|
---|
[1168] | 161 | /**
|
---|
| 162 | * Remove all characters whose ASCII value is less than or equal to 32
|
---|
| 163 | * (spaces and control characters) from both sides of @p in_out.
|
---|
| 164 | * @param in_out The string to strip spaces/control characters from (modified).
|
---|
| 165 | */
|
---|
[1178] | 166 | void mr_strip_spaces(char *in_out) {
|
---|
[1215] | 167 | int i = 0;
|
---|
| 168 | int j = 0;
|
---|
[1168] | 169 | size_t length;
|
---|
| 170 |
|
---|
| 171 | if (in_out == NULL) {
|
---|
| 172 | return;
|
---|
| 173 | }
|
---|
| 174 | length = strlen(in_out);
|
---|
| 175 |
|
---|
| 176 | /* Skip initial spaces and special chars */
|
---|
| 177 | for (i = 0; in_out[i] <= ' ' && i < (int)length ; i++);
|
---|
[1215] | 178 | /* Shift the string to the begining if needed */
|
---|
| 179 | if (i != 0) {
|
---|
| 180 | for (j = 0; i < (int)length ; i++, j++) {
|
---|
| 181 | in_out[j] = in_out[i];
|
---|
| 182 | }
|
---|
| 183 | /* Erase the end of the string if needed */
|
---|
| 184 | j++;
|
---|
| 185 | in_out[j] = '\0';
|
---|
[1168] | 186 | }
|
---|
[1205] | 187 |
|
---|
[1168] | 188 | /* Skip final spaces and special chars */
|
---|
[1217] | 189 | for (i = (int)strlen(in_out) - 1; i >= 0 && in_out[i] <= ' '; i--);
|
---|
[1168] | 190 |
|
---|
| 191 | /* The string now ends after that char */
|
---|
| 192 | i++;
|
---|
| 193 | in_out[i] = '\0';
|
---|
| 194 | }
|
---|
[3193] | 195 |
|
---|
[3294] | 196 | char *mr_str_substitute(const char *in, const char *token, const char *subst) {
|
---|
[3193] | 197 |
|
---|
[3294] | 198 | char *output = NULL;
|
---|
| 199 | char *tmp = NULL;
|
---|
| 200 | int i = 0;
|
---|
| 201 |
|
---|
| 202 | mr_asprintf(output, "%s", in);
|
---|
| 203 | tmp = strstr(output, token);
|
---|
| 204 | if (tmp == NULL) {
|
---|
| 205 | // token not found returning initial string unmodified
|
---|
| 206 | return(output);
|
---|
| 207 | }
|
---|
| 208 | // token found end string here for now
|
---|
| 209 | *tmp = '\0';
|
---|
| 210 |
|
---|
| 211 | // Add subst content to the string
|
---|
| 212 | mr_strcat(output,subst);
|
---|
| 213 |
|
---|
| 214 | // now add the rest of in
|
---|
| 215 | tmp = strstr(in, token);
|
---|
| 216 | for (; i < strlen(token) ; i++) {
|
---|
| 217 | tmp++;
|
---|
| 218 | }
|
---|
| 219 | mr_strcat(output,tmp);
|
---|
| 220 | return(output);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 |
|
---|
[3193] | 224 | /*
|
---|
| 225 | * Remove '\n' char from both sides of @p in_out.
|
---|
| 226 | * @param in_out The string to strip characters from (modified).
|
---|
| 227 |
|
---|
| 228 | void mr_chomp(char *in_out) {
|
---|
| 229 |
|
---|
| 230 | mr_strip_char(in_out, "\n");
|
---|
| 231 | }
|
---|
| 232 | */
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Remove all characters in caracs from begining and end of string @p in_out
|
---|
| 236 | * @param in_out The string to strip char characters from (modified).
|
---|
| 237 |
|
---|
| 238 | void mr_strip_char(char *in_out, char *caracs) {
|
---|
| 239 | int i = 0;
|
---|
| 240 | int j = 0;
|
---|
| 241 | size_t length = 0;
|
---|
| 242 |
|
---|
| 243 | if (caracs == NULL) {
|
---|
| 244 | return;
|
---|
| 245 | }
|
---|
| 246 | if (in_out == NULL) {
|
---|
| 247 | return;
|
---|
| 248 | }
|
---|
| 249 | length = strlen(in_out);
|
---|
| 250 |
|
---|
[3194] | 251 | // Skip initial caracs
|
---|
[3193] | 252 | for (i = 0; index(caracs, in_out[i]) != NULL && i < (int)length ; i++);
|
---|
| 253 |
|
---|
[3194] | 254 | // Shift the string to the begining if needed
|
---|
[3193] | 255 | if (i != 0) {
|
---|
| 256 | for (j = 0; i < (int)length ; i++, j++) {
|
---|
| 257 | in_out[j] = in_out[i];
|
---|
| 258 | }
|
---|
[3194] | 259 | // Erase the end of the string if needed
|
---|
[3193] | 260 | j++;
|
---|
| 261 | in_out[j] = '\0';
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[3194] | 264 | // Skip final caracs
|
---|
[3193] | 265 | for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
|
---|
| 266 |
|
---|
[3194] | 267 | // The string now ends after that char
|
---|
[3193] | 268 | i++;
|
---|
| 269 | in_out[i] = '\0';
|
---|
| 270 | }
|
---|
| 271 | */
|
---|
| 272 |
|
---|