[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 |
|
---|
| 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 | **/
|
---|
[1241] | 58 | char *mr_stresc(char *instr, char *toesc, const char escchr) {
|
---|
[1054] | 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 | }
|
---|
[1551] | 78 | inptr++;
|
---|
[1054] | 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 | }
|
---|
[1087] | 105 |
|
---|
[1168] | 106 | /**
|
---|
[2421] | 107 | * Remove all characters in caracs from begining and end of string @p in_out
|
---|
| 108 | * @param in_out The string to strip char characters from (modified).
|
---|
[1168] | 109 | */
|
---|
[2421] | 110 | void mr_strip_char(char *in_out, char *caracs) {
|
---|
[1215] | 111 | int i = 0;
|
---|
| 112 | int j = 0;
|
---|
[2421] | 113 | int length = 0;
|
---|
[1168] | 114 |
|
---|
[2421] | 115 | if (caracs == NULL) {
|
---|
| 116 | return;
|
---|
| 117 | }
|
---|
[1168] | 118 | if (in_out == NULL) {
|
---|
| 119 | return;
|
---|
| 120 | }
|
---|
[2421] | 121 | length = (int)strlen(in_out);
|
---|
[1168] | 122 |
|
---|
[2421] | 123 | /* Skip initial caracs */
|
---|
| 124 | for (i = 0; index(caracs, in_out[i]) != NULL && i < length ; i++);
|
---|
| 125 |
|
---|
[1215] | 126 | /* Shift the string to the begining if needed */
|
---|
| 127 | if (i != 0) {
|
---|
[2421] | 128 | for (j = 0; i < length ; i++, j++) {
|
---|
[1215] | 129 | in_out[j] = in_out[i];
|
---|
| 130 | }
|
---|
| 131 | /* Erase the end of the string if needed */
|
---|
| 132 | j++;
|
---|
| 133 | in_out[j] = '\0';
|
---|
[1168] | 134 | }
|
---|
[1205] | 135 |
|
---|
[1168] | 136 | /* Skip final spaces and special chars */
|
---|
[2421] | 137 | for (i = (int)strlen(in_out) - 1; i >= 0 && index(caracs, in_out[i]) != NULL; i--);
|
---|
[1168] | 138 |
|
---|
| 139 | /* The string now ends after that char */
|
---|
| 140 | i++;
|
---|
| 141 | in_out[i] = '\0';
|
---|
| 142 | }
|
---|
[2421] | 143 |
|
---|
| 144 | /**
|
---|
| 145 | * Remove all characters whose ASCII value is less than or equal to 32
|
---|
| 146 | * (spaces and control characters) from both sides of @p in_out.
|
---|
| 147 | * @param in_out The string to strip spaces/control characters from (modified).
|
---|
| 148 | */
|
---|
| 149 | void mr_strip_spaces(char *in_out) {
|
---|
| 150 |
|
---|
| 151 | int i;
|
---|
| 152 | char *tmp = NULL;
|
---|
| 153 |
|
---|
| 154 | mr_asprintf(tmp, "");
|
---|
| 155 |
|
---|
| 156 | /* Build the string of char to avoid: ctrl char up to space*/
|
---|
| 157 | for (i = 0; i <= ' '; i++) {
|
---|
| 158 | mr_strcat(tmp, "%c", i);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | mr_strip_char(in_out, tmp);
|
---|
| 162 | mr_free(tmp);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*
|
---|
| 166 | * Remove '\n' char from both sides of @p in_out.
|
---|
| 167 | * @param in_out The string to strip characters from (modified).
|
---|
| 168 | */
|
---|
| 169 | void mr_chomp(char *in_out) {
|
---|
| 170 |
|
---|
| 171 | mr_strip_char(in_out, "\n");
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /* Return an allocated string containing the date */
|
---|
| 175 | char *mr_date(void) {
|
---|
| 176 |
|
---|
| 177 | time_t tcurr;
|
---|
| 178 | char *tmp = NULL;
|
---|
| 179 |
|
---|
| 180 | tcurr = time(NULL);
|
---|
| 181 | mr_asprintf(tmp, "%s", ctime(&tcurr));
|
---|
| 182 | mr_chomp(tmp);
|
---|
| 183 | return(tmp);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 |
|
---|