|
Revision 2100, 2.1 kB
(checked in by n0-1, 5 years ago)
|
added license information and (C)
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
#include "nfotizer.h" |
|---|
| 19 |
#include <stdlib.h> |
|---|
| 20 |
#include <string.h> |
|---|
| 21 |
#include <stdbool.h> |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#define TOGGLE(x) ( x = !x ) |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 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 |
|
|---|
| 63 |
myfmt = tmp; |
|---|
| 64 |
} |
|---|
| 65 |
} |
|---|
| 66 |
free(fmtdup); |
|---|
| 67 |
return myfmt; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 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 |
|
|---|