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