| 1 |
/*
|
| 2 |
* LMS version 1.1-cvs
|
| 3 |
*
|
| 4 |
* (C) Copyright 2001-2003 LMS Developers
|
| 5 |
*
|
| 6 |
* Please, see the doc/AUTHORS for more information about authors!
|
| 7 |
*
|
| 8 |
* This program is free software; you can redistribute it and/or modify
|
| 9 |
* it under the terms of the GNU General Public License Version 2 as
|
| 10 |
* published by the Free Software Foundation.
|
| 11 |
*
|
| 12 |
* This program is distributed in the hope that it will be useful,
|
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
* GNU General Public License for more details.
|
| 16 |
*
|
| 17 |
* You should have received a copy of the GNU General Public License
|
| 18 |
* along with this program; if not, write to the Free Software
|
| 19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
| 20 |
* USA.
|
| 21 |
*
|
| 22 |
* $Id: util.c,v 1.2 2003/08/15 12:00:41 lexx Exp $
|
| 23 |
*/
|
| 24 |
|
| 25 |
|
| 26 |
#include <assert.h>
|
| 27 |
#include <stdlib.h>
|
| 28 |
#include <string.h>
|
| 29 |
#include "inifile.h"
|
| 30 |
#include "lmsd.h"
|
| 31 |
#include "util.h"
|
| 32 |
|
| 33 |
void *amalloc(int size)
|
| 34 |
{
|
| 35 |
void * m;
|
| 36 |
m = malloc(size);
|
| 37 |
assert(m);
|
| 38 |
return(m);
|
| 39 |
}
|
| 40 |
|
| 41 |
void *arealloc(void *m, int size)
|
| 42 |
{
|
| 43 |
m = realloc(m, size);
|
| 44 |
assert(m);
|
| 45 |
return(m);
|
| 46 |
}
|
| 47 |
|
| 48 |
unsigned char * str_replace(unsigned char * pattern, unsigned char * replacement, unsigned char * string, int stop)
|
| 49 |
{
|
| 50 |
unsigned char * occurence;
|
| 51 |
int n = 0;
|
| 52 |
|
| 53 |
while(occurence = strstr(string, pattern))
|
| 54 |
{
|
| 55 |
int l;
|
| 56 |
unsigned char * new;
|
| 57 |
|
| 58 |
*occurence = 0;
|
| 59 |
l = strlen(string);
|
| 60 |
|
| 61 |
occurence += strlen(pattern);
|
| 62 |
l += strlen(occurence);
|
| 63 |
l += strlen(replacement);
|
| 64 |
|
| 65 |
new = (unsigned char*) malloc(l + 2);
|
| 66 |
snprintf(new, l + 1, "%s%s%s", string, replacement, occurence);
|
| 67 |
free(string);
|
| 68 |
string = new;
|
| 69 |
|
| 70 |
n++;
|
| 71 |
if(n == stop) break;
|
| 72 |
}
|
| 73 |
|
| 74 |
return(string);
|
| 75 |
|
| 76 |
}
|
| 77 |
|
| 78 |
struct module_arg * parse_module_argstring(unsigned char * argstring)
|
| 79 |
{
|
| 80 |
int argc = 0;
|
| 81 |
struct module_arg * argv = NULL;
|
| 82 |
unsigned char * eq;
|
| 83 |
|
| 84 |
while((eq = index(argstring, '=')))
|
| 85 |
{
|
| 86 |
unsigned char * key;
|
| 87 |
unsigned char * value;
|
| 88 |
int l;
|
| 89 |
|
| 90 |
*eq = 0;
|
| 91 |
key = strdup(argstring);
|
| 92 |
eq++;
|
| 93 |
|
| 94 |
if(*eq == '"')
|
| 95 |
{
|
| 96 |
value = ini_parse(eq + 1, &l, '"');
|
| 97 |
if(*(eq + l) == 0)
|
| 98 |
eq += l;
|
| 99 |
else
|
| 100 |
eq += l + 1;
|
| 101 |
|
| 102 |
argstring = eq;
|
| 103 |
}
|
| 104 |
else
|
| 105 |
{
|
| 106 |
if(*eq == 0) break;
|
| 107 |
value = ini_parse(eq, &l, ',');
|
| 108 |
|
| 109 |
if(*(eq + l) == 0)
|
| 110 |
eq += l;
|
| 111 |
else
|
| 112 |
eq += l + 1;
|
| 113 |
|
| 114 |
argstring = eq;
|
| 115 |
}
|
| 116 |
|
| 117 |
argv = (struct module_arg*) arealloc(argv, (argc + 1) * sizeof(struct module_arg));
|
| 118 |
argv[argc].k = key;
|
| 119 |
argv[argc].v = value;
|
| 120 |
argc++;
|
| 121 |
|
| 122 |
}
|
| 123 |
argv = (struct module_arg*) arealloc(argv, (argc + 1) * sizeof(struct module_arg));
|
| 124 |
argv[argc].k = NULL;
|
| 125 |
argv[argc].v = NULL;
|
| 126 |
return(argv);
|
| 127 |
}
|
| 128 |
|
| 129 |
unsigned char * str_concat(unsigned char * s1, unsigned char * s2)
|
| 130 |
{
|
| 131 |
int l = strlen(s1) + strlen(s2) + 1;
|
| 132 |
unsigned char * ret = amalloc(l);
|
| 133 |
|
| 134 |
snprintf(ret, l, "%s%s", s1, s2);
|
| 135 |
free(s1);
|
| 136 |
free(s2);
|
| 137 |
return(ret);
|
| 138 |
}
|