root/trunk/freewrt/tools/nfotizer/interpret.c

Revision 2100, 2.1 kB (checked in by n0-1, 5 years ago)

added license information and (C)

Line 
1 /* nfotizer - split information given in info.nfo files into several places.
2  * Copyright (C) 2007  Phil Sutter <n0-1@freewrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #include "nfotizer.h"
19 #include <stdlib.h>             /* calloc */
20 #include <string.h>             /* strlen */
21 #include <stdbool.h>            /* bool */
22
23 /* toggle a boolean */
24 #define TOGGLE(x)       ( x = !x )
25
26 /* Interpret the string fmt.
27  * idid is the separator used for variables in fmt.
28  * The values for the variables found are taken from htable. */
29 char *
30 interpret(const char *idid, const char *fmt, struct htab *htable)
31 {
32         int myfmt_len;
33         char *myfmt, *fmtdup, *fmtduptmp, *result = 0, *token, *tmp;
34         bool is_id;
35
36         if (!idid || !fmt)
37                 return NULL;
38
39         myfmt_len = strlen(fmt);
40         myfmt = calloc(myfmt_len, sizeof(char));
41         fmtdup = strdup(fmt);
42         fmtduptmp = fmtdup;
43         is_id = (*fmt == *idid);
44
45         for (;; fmtduptmp = NULL, TOGGLE(is_id)) {
46                 token = strtok(fmtduptmp, idid);
47                 DBG("next token is \"%s\"", token);
48                 if (token == NULL)
49                         break;
50                 if (is_id) {
51                         if ((result = htab_get(token, *htable)) == NULL) {
52                                 PERR("key %s not found in hash table", token);
53                                 continue;
54                         }
55                         asprintf(&tmp, "%s%s", myfmt, result);
56                         zap(myfmt);
57                         zap(result);
58                         myfmt = tmp;
59                 } else {
60                         asprintf(&tmp, "%s%s", myfmt, token);
61                         zap(myfmt);
62                         /* zap(token); */
63                         myfmt = tmp;
64                 }
65         }
66         free(fmtdup);
67         return myfmt;
68 }
69
70 /* allocating strcat */
71 char *
72 my_strcat(char *dest, char *src)
73 {
74         char *ret = NULL;
75         if (asprintf(&ret, "%s%s", dest, src) < 0)
76                 return NULL;
77
78         return ret;
79 }
80
Note: See TracBrowser for help on using the browser.