-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutil.c
128 lines (109 loc) · 2.96 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* LMS version 1.11-git
*
* (C) Copyright 2001-2012 LMS Developers
*
* Please, see the doc/AUTHORS for more information about authors!
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id$
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <ctype.h>
#include "util.h"
/* Replaces each instance of string 'old' on the string 'string' with string 'new' */
int str_replace(char **string, const char *old, const char *new)
{
size_t newLen = strlen(new);
size_t oldLen = strlen(old);
int n = 0;
char* temp = *string;
char* last = NULL;
char* scan = NULL;
while((temp = strstr(temp, old)) != NULL)
{
temp += oldLen;
n++;
}
char *buffer = (char*)malloc(strlen(*string) + (newLen - oldLen) * n + 1);
if( buffer == NULL )
return 0;
scan = buffer;
temp = *string;
last = *string;
while((temp = strstr(temp, old)) != NULL)
{
size_t skip = temp - last;
memcpy(scan, last, skip);
memcpy(scan + skip, new, newLen);
temp += oldLen;
scan += skip + newLen;
last = temp;
}
memcpy(scan, last, (*string) + strlen(*string) - last + 1);
free(*string); // warning string must be allocated
*string = buffer; //return new string
return n;
}
/* Save value to string (needed i.e. for database routines)*/
char * str_save(char *str, const char *val)
{
str = (char *) realloc(str, strlen(val)+1);
return strcpy(str, val);
}
/* Concatenate strings */
char * str_concat(const char *s1, const char *s2)
{
int l = strlen(s1) + strlen(s2) + 1;
char *ret = (char*) malloc(l);
snprintf(ret, l, "%s%s", s1, s2);
return(ret);
}
/* Convert string to lower case */
char * str_lwc(const char *s)
{
static char l[ASCIILINESZ+1];
int i;
if( s==NULL ) return NULL;
memset(l, 0, ASCIILINESZ+1);
i = 0;
while( s[i] && i<ASCIILINESZ )
{
l[i] = (char) tolower((int)s[i]);
i++;
}
l[ASCIILINESZ] = (char) 0;
return l;
}
/* Convert string to upper case */
char * str_upc(const char *s)
{
static char l[ASCIILINESZ+1];
int i;
if( s==NULL ) return NULL;
memset(l, 0, ASCIILINESZ+1);
i = 0;
while( s[i] && i<ASCIILINESZ )
{
l[i] = (char) toupper((int)s[i]);
i++;
}
l[ASCIILINESZ] = (char) 0;
return l;
}