1 /* strtools -- portable string-handling functions */
2 /* $Id: strtools.c,v 1.2 2005/05/28 03:17:45 bitman Exp $ */
3 /* Copyright (C) 2002 Ryan Phillips <bitman@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "strtools.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29
str_dup(char * s)30 char * str_dup(char * s)
31 {
32 char* copy = (char *) malloc(sizeof(char) * (strlen(s) + 1));
33 if (copy == NULL)
34 return NULL;
35
36 strcpy(copy, s);
37 return copy;
38 }
39
str_dupmin(char * s,unsigned int min)40 char * str_dupmin(char * s, unsigned int min)
41 {
42 char* copy;
43 int duplen = strlen(s);
44 if (duplen < min) duplen = min;
45
46 copy = (char *) malloc(sizeof(char) * (duplen + 1));
47 if (copy == NULL)
48 return NULL;
49
50 strncpy(copy, s, duplen);
51 copy[duplen] = '\0';
52 return copy;
53 }
54
str_dupmax(char * s,unsigned int max)55 char * str_dupmax(char * s, unsigned int max)
56 {
57 char* copy;
58 int duplen = strlen(s);
59 if (duplen > max) duplen = max;
60
61 copy = (char *) malloc(sizeof(char) * (duplen + 1));
62 if (copy == NULL)
63 return NULL;
64
65 strncpy(copy, s, duplen);
66 copy[duplen] = '\0';
67 return copy;
68 }
69
str_duplen(char * s,unsigned int len)70 char * str_duplen(char * s, unsigned int len)
71 {
72 char* copy = (char *) malloc(sizeof(char) * (len + 1));
73 if (copy == NULL)
74 return NULL;
75
76 strncpy(copy, s, len);
77 copy[len] = '\0';
78 return copy;
79 }
80
str_dupadd(char * s,unsigned int add)81 char * str_dupadd(char * s, unsigned int add)
82 {
83 char* copy;
84 int duplen = strlen(s) + add;
85
86 if (add < 0)
87 return NULL;
88
89 copy = (char *) malloc(sizeof(char) * (duplen + 1));
90 if (copy == NULL)
91 return NULL;
92
93 strncpy(copy, s, duplen);
94 copy[duplen] = '\0';
95 return copy;
96 }
97
str_create(unsigned int len)98 char * str_create(unsigned int len)
99 {
100 char* string = (char *) malloc(sizeof(char) * (len + 1));
101 if (string == NULL)
102 return NULL;
103
104 string[0] = '\0';
105 return string;
106 }
107
108
109 /* str_lowercase - used by str_equ to convert a string to lowercase,
110 * same as strlwr() in djgpp, but more compatible.
111 * introduced by Elchonon Edelson for linux port */
str_lowercase(char * string)112 char* str_lowercase(char* string)
113 {
114 char* s = string;
115 while (*s) {
116 *s = tolower(*s);
117 s++;
118 }
119
120 return string;
121 }
122
123
124 /* str_equ - string comparison */
str_equ(const char * str1,const char * str2,int flags)125 int str_equ(const char *str1, const char *str2, int flags)
126 {
127 char *lwr1, *lwr2;
128 int i;
129 int isequ = 1; /* Strings are equal until proven otherwise */
130
131 if (str1[0] == '\x0' && str2[0] == '\x0')
132 return 1;
133 else if (str1[0] == '\x0' || str2[0] == '\x0')
134 return 0;
135
136 lwr1 = (char *) malloc(strlen(str1) * sizeof(char) + 1);
137 lwr2 = (char *) malloc(strlen(str2) * sizeof(char) + 1);
138 if (lwr1 == NULL || lwr2 == NULL)
139 return -1;
140
141 strcpy(lwr1, str1);
142 strcpy(lwr2, str2);
143
144 if (flags & STREQU_UNCASE) {
145 str_lowercase(lwr1);
146 str_lowercase(lwr2);
147 }
148
149 for (i = 0; lwr1[i] != '\x0' && lwr2[i] != '\x0'; i++)
150 if (lwr1[i] != lwr2[i]) {
151 isequ = 0;
152 break;
153 }
154
155 if (lwr1[i] != lwr2[i]) { /* If the strings do not end together */
156 if (!(flags & STREQU_FRONT)) /* Unless only checking string fronts */
157 isequ = 0;
158 /* If the left string ends first and the right is expected to be the front
159 * part of the left string, they are not equal */
160 if ((flags & STREQU_RFRONT) && lwr1[i] == '\x0')
161 isequ = 0;
162 }
163
164 free(lwr1);
165 free(lwr2);
166
167 return isequ;
168 }
169
170 /* lookupString - Table lookup for a string using str_equ()
171 * Returns index of string if found, otherwise -1 */
lookupString(const char * table[],int size,char * value,int equflags)172 int lookupString(const char * table[], int size, char * value, int equflags)
173 {
174 int i = 0;
175
176 for (i = 0; i < size; i++)
177 if (str_equ(value, table[i], equflags))
178 return i;
179
180 return -1;
181 }
182
183