[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 | **/
|
---|
[3614] | 27 | char *mr_strtok(char *instr, const char *delims, int *lastpos) {
|
---|
[1054] | 28 |
|
---|
[3614] | 29 | char *token = NULL;
|
---|
| 30 | char *strptr = NULL;
|
---|
| 31 | size_t pos1 = 0;
|
---|
| 32 | size_t pos2 = 0;
|
---|
[1054] | 33 |
|
---|
[3614] | 34 | if (instr == NULL) {
|
---|
| 35 | *lastpos = 0;
|
---|
| 36 | return token;
|
---|
| 37 | }
|
---|
[2970] | 38 |
|
---|
[3614] | 39 | if (delims == NULL) {
|
---|
| 40 | *lastpos = 0;
|
---|
| 41 | return token;
|
---|
| 42 | }
|
---|
[2972] | 43 |
|
---|
[3614] | 44 | if (strlen(instr) <= *lastpos) {
|
---|
| 45 | *lastpos = 0;
|
---|
| 46 | return token;
|
---|
| 47 | }
|
---|
[1054] | 48 |
|
---|
[3614] | 49 | strptr = instr + *lastpos;
|
---|
| 50 | pos2 = strspn(strptr, delims);
|
---|
| 51 | strptr += pos2;
|
---|
| 52 | pos1 = strcspn(strptr, delims);
|
---|
| 53 | token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
|
---|
| 54 | strncpy(token, strptr, pos1);
|
---|
| 55 | token[pos1] = '\0';
|
---|
| 56 | *lastpos = *lastpos + pos1 + pos2 + 1;
|
---|
[1054] | 57 |
|
---|
[3614] | 58 | return token;
|
---|
[1054] | 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Returns the string fed to it 'inptr' with all characters to escape given
|
---|
| 64 | * in 'toesc' prepended by escaping character 'escchr'.
|
---|
| 65 | * (Prepare strings for use in system() or popen() with this function.)
|
---|
| 66 | * @param instr
|
---|
| 67 | * @param toesc
|
---|
| 68 | * @param escchr
|
---|
| 69 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 70 | **/
|
---|
[3171] | 71 | char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) {
|
---|
[1054] | 72 |
|
---|
[3614] | 73 | char *inptr = NULL;
|
---|
| 74 | char *retstr = NULL;
|
---|
| 75 | char *retptr = NULL;
|
---|
| 76 | char *escptr = NULL;
|
---|
| 77 | int cnt = 0;
|
---|
| 78 | bool found = FALSE;
|
---|
| 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++;
|
---|
| 88 | // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars
|
---|
| 89 | if (*inptr == specialchr) {
|
---|
| 90 | cnt += 2;
|
---|
[1054] | 91 | }
|
---|
[3614] | 92 | break;
|
---|
[1054] | 93 | }
|
---|
[3614] | 94 | escptr++;
|
---|
[1054] | 95 | }
|
---|
[3614] | 96 | inptr++;
|
---|
| 97 | }
|
---|
[3171] | 98 |
|
---|
[3614] | 99 | inptr = instr;
|
---|
| 100 | retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
|
---|
| 101 | retptr = retstr;
|
---|
[1054] | 102 |
|
---|
[3614] | 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.
|
---|
| 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++;
|
---|
[1054] | 119 | }
|
---|
[3614] | 120 | break;
|
---|
[1054] | 121 | }
|
---|
[3614] | 122 | escptr++;
|
---|
| 123 | }
|
---|
| 124 | *retptr = *inptr;
|
---|
| 125 | retptr++;
|
---|
| 126 | inptr++;
|
---|
| 127 | if (found) {
|
---|
| 128 | // finish to put the remaining specialchr
|
---|
| 129 | *retptr = specialchr;
|
---|
[1054] | 130 | retptr++;
|
---|
[3614] | 131 | found = FALSE;
|
---|
[1054] | 132 | }
|
---|
[3614] | 133 | }
|
---|
| 134 | *retptr = '\0';
|
---|
[1054] | 135 |
|
---|
[3614] | 136 | return(retstr);
|
---|
[1054] | 137 | }
|
---|
[1087] | 138 |
|
---|
| 139 | /* Return a string containing the date */
|
---|
| 140 | char *mr_date(void) {
|
---|
| 141 |
|
---|
[3614] | 142 | time_t tcurr;
|
---|
| 143 |
|
---|
| 144 | tcurr = time(NULL);
|
---|
| 145 | return(ctime(&tcurr));
|
---|
[1087] | 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
|
---|
[3614] | 163 | * (spaces and control characters) from both sides of @p instr.
|
---|
| 164 | * @param instr The string to strip spaces/control characters from
|
---|
| 165 | * returns a newly allocated string without the spaces that needs to be freed
|
---|
| 166 | * by the caller
|
---|
[1168] | 167 | */
|
---|
[3614] | 168 | char *mr_strip_spaces(const char *instr) {
|
---|
[1168] | 169 |
|
---|
[3614] | 170 | char *q = NULL;
|
---|
| 171 | char *outstr = NULL;
|
---|
[1168] | 172 |
|
---|
[3614] | 173 | if (instr == NULL) {
|
---|
| 174 | return(NULL);
|
---|
| 175 | }
|
---|
[1205] | 176 |
|
---|
[3614] | 177 | /* Skip initial spaces and special chars */
|
---|
[3627] | 178 | while (*instr <= ' ' && *instr != '\0') {
|
---|
| 179 | instr++;
|
---|
[3614] | 180 | }
|
---|
[3723] | 181 |
|
---|
[3724] | 182 | if (*instr == '\0') {
|
---|
[3723] | 183 | mr_asprintf(outstr, "");
|
---|
| 184 | return(outstr);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[3627] | 187 | mr_asprintf(outstr, "%s", instr);
|
---|
[3723] | 188 | q = outstr + strlen(outstr) - 1;
|
---|
[1168] | 189 |
|
---|
[3614] | 190 | /* Skip final spaces and special chars */
|
---|
| 191 | while (*q <= ' ' && q != outstr) {
|
---|
| 192 | q--;
|
---|
[1168] | 193 | }
|
---|
[3193] | 194 |
|
---|
[3614] | 195 | /* The string now ends after that char */
|
---|
| 196 | q++;
|
---|
| 197 | *q = '\0';
|
---|
| 198 | return(outstr);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[3794] | 201 | /* Subsitute the string "token" in the string "in" by the string "subst" */
|
---|
[3294] | 202 | char *mr_str_substitute(const char *in, const char *token, const char *subst) {
|
---|
[3193] | 203 |
|
---|
[3294] | 204 | char *output = NULL;
|
---|
| 205 | char *tmp = NULL;
|
---|
| 206 | int i = 0;
|
---|
| 207 |
|
---|
| 208 | mr_asprintf(output, "%s", in);
|
---|
| 209 | tmp = strstr(output, token);
|
---|
| 210 | if (tmp == NULL) {
|
---|
| 211 | // token not found returning initial string unmodified
|
---|
| 212 | return(output);
|
---|
| 213 | }
|
---|
[3641] | 214 | // token found: end string here for now
|
---|
[3294] | 215 | *tmp = '\0';
|
---|
| 216 |
|
---|
| 217 | // Add subst content to the string
|
---|
| 218 | mr_strcat(output,subst);
|
---|
| 219 |
|
---|
| 220 | // now add the rest of in
|
---|
| 221 | tmp = strstr(in, token);
|
---|
| 222 | for (; i < strlen(token) ; i++) {
|
---|
| 223 | tmp++;
|
---|
| 224 | }
|
---|
| 225 | mr_strcat(output,tmp);
|
---|
| 226 | return(output);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
[3193] | 230 | /*
|
---|
| 231 | * Remove '\n' char from both sides of @p in_out.
|
---|
| 232 | * @param in_out The string to strip characters from (modified).
|
---|
| 233 |
|
---|
| 234 | void mr_chomp(char *in_out) {
|
---|
| 235 |
|
---|
| 236 | mr_strip_char(in_out, "\n");
|
---|
| 237 | }
|
---|
| 238 | */
|
---|
| 239 |
|
---|
| 240 | /**
|
---|
| 241 | * Remove all characters in caracs from begining and end of string @p in_out
|
---|
| 242 | * @param in_out The string to strip char characters from (modified).
|
---|
| 243 |
|
---|
| 244 | void mr_strip_char(char *in_out, char *caracs) {
|
---|
| 245 | int i = 0;
|
---|
| 246 | int j = 0;
|
---|
| 247 | size_t length = 0;
|
---|
| 248 |
|
---|
| 249 | if (caracs == NULL) {
|
---|
| 250 | return;
|
---|
| 251 | }
|
---|
| 252 | if (in_out == NULL) {
|
---|
| 253 | return;
|
---|
| 254 | }
|
---|
| 255 | length = strlen(in_out);
|
---|
| 256 |
|
---|
[3194] | 257 | // Skip initial caracs
|
---|
[3193] | 258 | for (i = 0; index(caracs, in_out[i]) != NULL && i < (int)length ; i++);
|
---|
| 259 |
|
---|
[3194] | 260 | // Shift the string to the begining if needed
|
---|
[3193] | 261 | if (i != 0) {
|
---|
| 262 | for (j = 0; i < (int)length ; i++, j++) {
|
---|
| 263 | in_out[j] = in_out[i];
|
---|
| 264 | }
|
---|
[3194] | 265 | // Erase the end of the string if needed
|
---|
[3193] | 266 | j++;
|
---|
| 267 | in_out[j] = '\0';
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[3194] | 270 | // Skip final caracs
|
---|
[3193] | 271 | for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
|
---|
| 272 |
|
---|
[3194] | 273 | // The string now ends after that char
|
---|
[3193] | 274 | i++;
|
---|
| 275 | in_out[i] = '\0';
|
---|
| 276 | }
|
---|
| 277 | */
|
---|
| 278 |
|
---|