[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"
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
[1594] | 16 | * Safe alternative to standard function strtok()
|
---|
[1054] | 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 |
|
---|
[3147] | 31 | if (instr == NULL) {
|
---|
| 32 | *lastpos = 0;
|
---|
| 33 | return token;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if (delims == NULL) {
|
---|
| 37 | *lastpos = 0;
|
---|
| 38 | return token;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[1054] | 41 | if (strlen(instr) <= *lastpos) {
|
---|
| 42 | *lastpos = 0;
|
---|
| 43 | return token;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | strptr = instr + *lastpos;
|
---|
| 47 | pos2 = strspn(strptr, delims);
|
---|
| 48 | strptr += pos2;
|
---|
| 49 | pos1 = strcspn(strptr, delims);
|
---|
| 50 | token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
|
---|
| 51 | strncpy(token, strptr, pos1);
|
---|
| 52 | token[pos1] = '\0';
|
---|
| 53 | *lastpos = *lastpos + pos1 + pos2 + 1;
|
---|
| 54 |
|
---|
| 55 | return token;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Returns the string fed to it 'inptr' with all characters to escape given
|
---|
| 61 | * in 'toesc' prepended by escaping character 'escchr'.
|
---|
| 62 | * (Prepare strings for use in system() or popen() with this function.)
|
---|
| 63 | * @param instr
|
---|
| 64 | * @param toesc
|
---|
| 65 | * @param escchr
|
---|
| 66 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 67 | **/
|
---|
[1241] | 68 | char *mr_stresc(char *instr, char *toesc, const char escchr) {
|
---|
[1054] | 69 | char *inptr = NULL;
|
---|
| 70 | char *retstr = NULL;
|
---|
| 71 | char *retptr = NULL;
|
---|
| 72 | char *escptr = NULL;
|
---|
| 73 | int cnt = 0;
|
---|
| 74 |
|
---|
| 75 | inptr = instr;
|
---|
| 76 |
|
---|
| 77 | // Count how many characters need escaping.
|
---|
| 78 | while (*inptr != '\0') {
|
---|
| 79 | escptr = toesc;
|
---|
| 80 | while (*escptr != '\0') {
|
---|
| 81 | if (*inptr == *escptr) {
|
---|
| 82 | // Found it, skip the rest.
|
---|
| 83 | cnt++;
|
---|
| 84 | break;
|
---|
| 85 | }
|
---|
| 86 | escptr++;
|
---|
| 87 | }
|
---|
[1551] | 88 | inptr++;
|
---|
[1054] | 89 | }
|
---|
| 90 | inptr = instr;
|
---|
| 91 |
|
---|
| 92 | retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
|
---|
| 93 | retptr = retstr;
|
---|
| 94 |
|
---|
| 95 | // Prepend specified characters with escape character.
|
---|
| 96 | while (*inptr != '\0') {
|
---|
| 97 | escptr = toesc;
|
---|
| 98 | while (*escptr != '\0') {
|
---|
| 99 | if (*inptr == *escptr) {
|
---|
| 100 | // Found it, skip the rest.
|
---|
| 101 | *retptr = escchr;
|
---|
| 102 | retptr++;
|
---|
| 103 | break;
|
---|
| 104 | }
|
---|
| 105 | escptr++;
|
---|
| 106 | }
|
---|
| 107 | *retptr = *inptr;
|
---|
| 108 | retptr++;
|
---|
| 109 | inptr++;
|
---|
| 110 | }
|
---|
| 111 | *retptr = '\0';
|
---|
| 112 |
|
---|
| 113 | return retstr;
|
---|
| 114 | }
|
---|
[1087] | 115 |
|
---|
[1168] | 116 | /**
|
---|
[2421] | 117 | * Remove all characters in caracs from begining and end of string @p in_out
|
---|
| 118 | * @param in_out The string to strip char characters from (modified).
|
---|
[1168] | 119 | */
|
---|
[2421] | 120 | void mr_strip_char(char *in_out, char *caracs) {
|
---|
[1215] | 121 | int i = 0;
|
---|
| 122 | int j = 0;
|
---|
[2421] | 123 | int length = 0;
|
---|
[1168] | 124 |
|
---|
[2421] | 125 | if (caracs == NULL) {
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
[1168] | 128 | if (in_out == NULL) {
|
---|
| 129 | return;
|
---|
| 130 | }
|
---|
[2421] | 131 | length = (int)strlen(in_out);
|
---|
[1168] | 132 |
|
---|
[2421] | 133 | /* Skip initial caracs */
|
---|
| 134 | for (i = 0; index(caracs, in_out[i]) != NULL && i < length ; i++);
|
---|
| 135 |
|
---|
[1215] | 136 | /* Shift the string to the begining if needed */
|
---|
| 137 | if (i != 0) {
|
---|
[2421] | 138 | for (j = 0; i < length ; i++, j++) {
|
---|
[1215] | 139 | in_out[j] = in_out[i];
|
---|
| 140 | }
|
---|
| 141 | /* Erase the end of the string if needed */
|
---|
| 142 | j++;
|
---|
| 143 | in_out[j] = '\0';
|
---|
[1168] | 144 | }
|
---|
[1205] | 145 |
|
---|
[1168] | 146 | /* Skip final spaces and special chars */
|
---|
[2421] | 147 | for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
|
---|
[1168] | 148 |
|
---|
| 149 | /* The string now ends after that char */
|
---|
| 150 | i++;
|
---|
| 151 | in_out[i] = '\0';
|
---|
| 152 | }
|
---|
[2421] | 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Remove all characters whose ASCII value is less than or equal to 32
|
---|
| 156 | * (spaces and control characters) from both sides of @p in_out.
|
---|
| 157 | * @param in_out The string to strip spaces/control characters from (modified).
|
---|
| 158 | */
|
---|
| 159 | void mr_strip_spaces(char *in_out) {
|
---|
| 160 |
|
---|
| 161 | int i;
|
---|
| 162 | char *tmp = NULL;
|
---|
| 163 |
|
---|
| 164 | mr_asprintf(tmp, "");
|
---|
| 165 |
|
---|
| 166 | /* Build the string of char to avoid: ctrl char up to space*/
|
---|
| 167 | for (i = 0; i <= ' '; i++) {
|
---|
| 168 | mr_strcat(tmp, "%c", i);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | mr_strip_char(in_out, tmp);
|
---|
| 172 | mr_free(tmp);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | /*
|
---|
| 176 | * Remove '\n' char from both sides of @p in_out.
|
---|
| 177 | * @param in_out The string to strip characters from (modified).
|
---|
| 178 | */
|
---|
| 179 | void mr_chomp(char *in_out) {
|
---|
| 180 |
|
---|
| 181 | mr_strip_char(in_out, "\n");
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /* Return an allocated string containing the date */
|
---|
| 185 | char *mr_date(void) {
|
---|
| 186 |
|
---|
| 187 | time_t tcurr;
|
---|
| 188 | char *tmp = NULL;
|
---|
| 189 |
|
---|
| 190 | tcurr = time(NULL);
|
---|
| 191 | mr_asprintf(tmp, "%s", ctime(&tcurr));
|
---|
| 192 | mr_chomp(tmp);
|
---|
| 193 | return(tmp);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|