[831] | 1 | /*
|
---|
| 2 | * mr_string.c - New generation of string handling functions
|
---|
| 3 | */
|
---|
[828] | 4 |
|
---|
[831] | 5 | #include <stdio.h>
|
---|
| 6 | #include <string.h>
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Safe alternative to standard function strok()
|
---|
| 10 | * @param instr
|
---|
| 11 | * @param delims
|
---|
| 12 | * @param lastpos
|
---|
| 13 | * @return @p
|
---|
| 14 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 15 | **/
|
---|
[828] | 16 | char *mr_strtok(char *instr, const char *delims, int *lastpos)
|
---|
| 17 | {
|
---|
| 18 |
|
---|
| 19 | char *token = NULL;
|
---|
| 20 | char *strptr = NULL;
|
---|
| 21 | size_t pos1 = 0;
|
---|
| 22 | size_t pos2 = 0;
|
---|
| 23 |
|
---|
| 24 | if (strlen(instr) <= *lastpos) {
|
---|
| 25 | *lastpos = 0;
|
---|
| 26 | return token;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | strptr = instr + *lastpos;
|
---|
| 30 | pos2 = strspn(strptr, delims);
|
---|
| 31 | strptr += pos2;
|
---|
| 32 | pos1 = strcspn(strptr, delims);
|
---|
| 33 | token = malloc(sizeof(*token) * (pos1 + 1));
|
---|
| 34 | strncpy(token, strptr, pos1);
|
---|
| 35 | token[pos1] = '\0';
|
---|
| 36 | *lastpos = *lastpos + pos1 + pos2 + 1;
|
---|
| 37 |
|
---|
| 38 | return token;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 |
|
---|
[831] | 42 | /**
|
---|
| 43 | * Returns the string fed to it 'inptr' with all characters to escape given
|
---|
| 44 | * in 'toesc' prepended by escaping character 'escchr'.
|
---|
| 45 | * (Prepare strings for use in system() or popen() with this function.)
|
---|
| 46 | * @param instr
|
---|
| 47 | * @param toesc
|
---|
| 48 | * @param escchr
|
---|
| 49 | * @note this function allocates memory that needs to be freed by caller
|
---|
| 50 | **/
|
---|
[828] | 51 | char *mr_stresc(char *instr, char *toesc, const char escchr)
|
---|
| 52 | {
|
---|
| 53 |
|
---|
| 54 | char *inptr = NULL;
|
---|
| 55 | char *retstr = NULL;
|
---|
| 56 | char *retptr = NULL;
|
---|
| 57 | char *escptr = NULL;
|
---|
| 58 | int cnt = 0;
|
---|
| 59 |
|
---|
| 60 | inptr = instr;
|
---|
| 61 |
|
---|
| 62 | // Count how many characters need escaping.
|
---|
| 63 | while (*inptr != '\0') {
|
---|
| 64 | escptr = toesc;
|
---|
| 65 | while (*escptr != '\0') {
|
---|
| 66 | if (*inptr == *escptr) {
|
---|
| 67 | // Found it, skip the rest.
|
---|
| 68 | cnt++;
|
---|
| 69 | break;
|
---|
| 70 | }
|
---|
| 71 | escptr++;
|
---|
| 72 | }
|
---|
[1552] | 73 | inptr++;
|
---|
[828] | 74 | }
|
---|
| 75 | inptr = instr;
|
---|
| 76 |
|
---|
| 77 | retstr = (char *) malloc(strlen(inptr) + cnt + 1);
|
---|
| 78 | retptr = retstr;
|
---|
| 79 |
|
---|
| 80 | // Prepend specified characters with escape character.
|
---|
| 81 | while (*inptr != '\0') {
|
---|
| 82 | escptr = toesc;
|
---|
| 83 | while (*escptr != '\0') {
|
---|
| 84 | if (*inptr == *escptr) {
|
---|
[1552] | 85 | // Found it, prepend with the escape char
|
---|
[828] | 86 | *retptr = escchr;
|
---|
| 87 | retptr++;
|
---|
| 88 | break;
|
---|
| 89 | }
|
---|
| 90 | escptr++;
|
---|
| 91 | }
|
---|
| 92 | *retptr = *inptr;
|
---|
| 93 | retptr++;
|
---|
| 94 | inptr++;
|
---|
| 95 | }
|
---|
| 96 | *retptr = '\0';
|
---|
| 97 |
|
---|
| 98 | return retstr;
|
---|
| 99 | }
|
---|