Changeset 3614 in MondoRescue for branches/3.2/mondo/src/lib/mr_str.c
- Timestamp:
- Nov 18, 2016, 5:31:43 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mondo/src/lib/mr_str.c
r3566 r3614 25 25 * @note this function allocates memory that needs to be freed by caller 26 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 35 if (instr == NULL) { 36 *lastpos = 0; 37 return token; 38 } 39 40 if (delims == NULL) { 41 *lastpos = 0; 42 return token; 43 } 44 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 27 char *mr_strtok(char *instr, const char *delims, int *lastpos) { 28 29 char *token = NULL; 30 char *strptr = NULL; 31 size_t pos1 = 0; 32 size_t pos2 = 0; 33 34 if (instr == NULL) { 35 *lastpos = 0; 59 36 return token; 37 } 38 39 if (delims == NULL) { 40 *lastpos = 0; 41 return token; 42 } 43 44 if (strlen(instr) <= *lastpos) { 45 *lastpos = 0; 46 return token; 47 } 48 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; 57 58 return token; 60 59 } 61 60 … … 71 70 **/ 72 71 char *mr_stresc(char *instr, char *toesc, const char escchr, const char specialchr) { 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; 91 } 92 break; 72 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; 93 91 } 94 escptr++;92 break; 95 93 } 96 inptr++;97 } 98 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.109 // if specialchar (' or ") then replace it with '\'' or "\"" so adds 2 chars110 if (*inptr == specialchr) {111 *retptr = specialchr;112 retptr++;113 *retptr = escchr;114 115 found = TRUE;116 } else {117 *retptr = escchr;118 retptr++;119 }120 break;94 escptr++; 95 } 96 inptr++; 97 } 98 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. 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++; 121 119 } 122 escptr++;120 break; 123 121 } 124 *retptr = *inptr; 122 escptr++; 123 } 124 *retptr = *inptr; 125 retptr++; 126 inptr++; 127 if (found) { 128 // finish to put the remaining specialchr 129 *retptr = specialchr; 125 130 retptr++; 126 inptr++; 127 if (found) { 128 // finish to put the remaining specialchr 129 *retptr = specialchr; 130 retptr++; 131 found = FALSE; 132 } 133 } 134 *retptr = '\0'; 135 136 return retstr; 131 found = FALSE; 132 } 133 } 134 *retptr = '\0'; 135 136 return(retstr); 137 137 } 138 138 139 139 /* Return a string containing the date */ 140 140 char *mr_date(void) { 141 142 143 144 145 141 142 time_t tcurr; 143 144 tcurr = time(NULL); 145 return(ctime(&tcurr)); 146 146 } 147 147 … … 161 161 /** 162 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). 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 165 167 */ 166 void mr_strip_spaces(char *in_out) {167 int i = 0; 168 int j = 0;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++); 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 in_out[j] = '\0'; 185 } 186 187 /* Skip final spaces and special chars */ 188 for (i = (int)strlen(in_out) - 1; i >= 0 && in_out[i] <= ' '; i--); 189 190 /* The string now ends after that char */ 191 i++;192 in_out[i] = '\0';168 char *mr_strip_spaces(const char *instr) { 169 170 char *p = NULL; 171 char *q = NULL; 172 char *outstr = NULL; 173 174 if (instr == NULL) { 175 return(NULL); 176 } 177 p = instr; 178 179 /* Skip initial spaces and special chars */ 180 while (*p <= ' ' && *p != '\0') { 181 p++; 182 } 183 mr_asprintf(outstr, "%s", p); 184 q = outstr + strlen(outstr) -1; 185 186 /* Skip final spaces and special chars */ 187 while (*q <= ' ' && q != outstr) { 188 q--; 189 } 190 191 /* The string now ends after that char */ 192 q++; 193 *q = '\0'; 194 return(outstr); 193 195 } 194 196
Note:
See TracChangeset
for help on using the changeset viewer.