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