2 * String helper functions
3 * Copyright (C) 2008 Andreas Ă–man
4 * Copyright (C) 2008 Mattias Wadman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "osx/OSXGNUReplacements.h"
26 #elif defined(_MSC_VER)
32 static void htsstr_argsplit_add(char ***argv, int *argc, char *s);
33 static int htsstr_format0(const char *str, char *out, char **map);
37 htsstr_unescape(char *str) {
40 for(s = str; *s; s++) {
46 else if(*(s + 1) == 'f')
48 else if(*(s + 1) == 'n')
50 else if(*(s + 1) == 'r')
52 else if(*(s + 1) == 't')
58 /* shift string left, copies terminator too */
59 memmove(s + 1, s + 2, strlen(s + 2) + 1);
67 htsstr_argsplit_add(char ***argv, int *argc, char *s)
69 *argv = realloc(*argv, sizeof((*argv)[0]) * (*argc + 1));
70 (*argv)[(*argc)++] = s;
74 htsstr_argsplit(const char *str) {
77 const char *start = NULL;
78 const char *stop = NULL;
83 for(s = str; *s; s++) {
85 htsstr_argsplit_add(&argv, &argc,
86 htsstr_unescape(strndup(start, stop - start)));
130 stop = str + strlen(str);
131 htsstr_argsplit_add(&argv, &argc,
132 htsstr_unescape(strndup(start, stop - start)));
135 htsstr_argsplit_add(&argv, &argc, NULL);
141 htsstr_argsplit_free(char **argv) {
144 for(i = 0; argv[i]; i++)
151 htsstr_format0(const char *str, char *out, char **map) {
159 f = map[(unsigned char)*(s + 1)];
160 if(*(s + 1) != '%' && f) {
161 s += 2; /* skip %f * */
180 return n + 1; /* + \0 */
184 htsstr_format(const char *str, char **map)
188 s = malloc(htsstr_format0(str, NULL, map));
189 htsstr_format0(str, s, map);