Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 2001-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 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 |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | #include <cc++/config.h> |
| 40 | #include <cc++/export.h> |
| 41 | #include <cc++/missing.h> |
| 42 | #include <cc++/strchar.h> |
| 43 | |
| 44 | #include <cstdio> |
| 45 | #include <cstdlib> |
| 46 | #include <cstring> |
| 47 | |
| 48 | #ifdef WIN32 |
| 49 | #include <malloc.h> |
| 50 | //#define alloca(x) _alloca(x) |
| 51 | #endif |
| 52 | |
| 53 | #ifdef CCXX_NAMESPACES |
| 54 | namespace ost { |
| 55 | using namespace std; |
| 56 | #endif |
| 57 | |
| 58 | char *find(const char *cs, char *str, size_t len) |
| 59 | { |
| 60 | unsigned pos = 0; |
| 61 | |
| 62 | if(!len) |
| 63 | len = strlen(str); |
| 64 | |
| 65 | while(pos < len) { |
| 66 | if(strchr(cs, str[pos])) |
| 67 | return str + pos; |
| 68 | ++pos; |
| 69 | } |
| 70 | if(!str[pos]) |
| 71 | return str + pos; |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | char *rfind(const char *cs, char *str, size_t len) |
| 76 | { |
| 77 | if(!len) |
| 78 | len = strlen(str); |
| 79 | |
| 80 | while(len--) { |
| 81 | if(strchr(cs, str[len])) |
| 82 | return str + len; |
| 83 | } |
| 84 | return str; |
| 85 | } |
| 86 | |
| 87 | char *ifind(const char *cs, char *str, size_t len) |
| 88 | { |
| 89 | unsigned pos = 0; |
| 90 | |
| 91 | if(!len) |
| 92 | len = strlen(str); |
| 93 | |
| 94 | while(pos < len) { |
| 95 | if(!strchr(cs, str[pos])) |
| 96 | return str + pos; |
| 97 | ++pos; |
| 98 | } |
| 99 | if(!str[pos]) |
| 100 | return str + pos; |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | char *strip(const char *chars, char *str, size_t len) |
| 105 | { |
| 106 | len = strtrim(chars, str, len); |
| 107 | |
| 108 | if(!len) |
| 109 | return str; |
| 110 | |
| 111 | return ifind(chars, str, len); |
| 112 | } |
| 113 | |
| 114 | size_t strtrim(const char *cs, char *str, size_t len) |
| 115 | { |
| 116 | if(!str) |
| 117 | return 0; |
| 118 | |
| 119 | if(!len) |
| 120 | len = strlen(str); |
| 121 | |
| 122 | if(!len) |
| 123 | return 0; |
| 124 | |
| 125 | while(len--) { |
| 126 | if(!strchr(cs, str[len])) |
| 127 | return ++len; |
| 128 | |
| 129 | str[len] = 0; |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | size_t strchop(const char *cs, char *str, size_t len) |
| 135 | { |
| 136 | unsigned pos = 0; |
| 137 | |
| 138 | if(!str) |
| 139 | return 0; |
| 140 | |
| 141 | if(!len) |
| 142 | len = strlen(str); |
| 143 | |
| 144 | if(!len) |
| 145 | return 0; |
| 146 | |
| 147 | while(pos < len) { |
| 148 | if(!strchr(cs, str[pos])) |
| 149 | break; |
| 150 | ++pos; |
| 151 | } |
| 152 | |
| 153 | if(pos == len) { |
| 154 | *str = 0; |
| 155 | return 0; |
| 156 | } |
| 157 | memmove(str, str + pos, len - pos + 1); |
| 158 | return len - pos; |
| 159 | } |
| 160 | |
| 161 | char *rsetField(char *dest, size_t size, const char *src, const char fill) |
| 162 | { |
| 163 | size_t len = 0; |
| 164 | |
| 165 | if(src) |
| 166 | len = strlen(src); |
| 167 | |
| 168 | if(len > size) |
| 169 | len = size; |
| 170 | |
| 171 | if(len) |
| 172 | memmove(dest + size - len, (void *)src, len); |
| 173 | |
| 174 | if(len < size && fill) |
| 175 | memset(dest, fill, size - len); |
| 176 | |
| 177 | return dest; |
| 178 | } |
| 179 | |
| 180 | char *lsetField(char *dest, size_t size, const char *src, const char fill) |
| 181 | { |
| 182 | size_t len = 0; |
| 183 | |
| 184 | if(src) |
| 185 | len = strlen(src); |
| 186 | |
| 187 | if(len > size) |
| 188 | len = size; |
| 189 | |
| 190 | if(len) |
| 191 | memmove(dest, src, len); |
| 192 | |
| 193 | if(len < size && fill) |
| 194 | memset(dest + len, fill, size - len); |
| 195 | |
| 196 | return dest; |
| 197 | } |
| 198 | |
| 199 | char *setUpper(char *string, size_t size) |
| 200 | { |
| 201 | char *ret = string; |
| 202 | |
| 203 | if(!size) |
| 204 | size = strlen(string); |
| 205 | |
| 206 | while(size && *string) { |
| 207 | *string = toupper(*string); |
| 208 | ++string; |
| 209 | --size; |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | char *setLower(char *string, size_t size) |
| 216 | { |
| 217 | char *ret = string; |
| 218 | |
| 219 | if(!size) |
| 220 | size = strlen(string); |
| 221 | |
| 222 | while(size && *string) { |
| 223 | *string = tolower(*string); |
| 224 | ++string; |
| 225 | --size; |
| 226 | } |
| 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | char *setString(char *dest, size_t size, const char *src) |
| 232 | { |
| 233 | size_t len = strlen(src); |
| 234 | |
| 235 | if(size == 1) |
| 236 | *dest = 0; |
| 237 | |
| 238 | if(size < 2) |
| 239 | return dest; |
| 240 | |
| 241 | if(len >= size) |
| 242 | len = size - 1; |
| 243 | |
| 244 | if(!len) { |
| 245 | dest[0] = 0; |
| 246 | return dest; |
| 247 | } |
| 248 | |
| 249 | memcpy(dest, src, len); |
| 250 | dest[len] = 0; |
| 251 | return dest; |
| 252 | } |
| 253 | |
| 254 | char *addString(char *dest, size_t size, const char *src) |
| 255 | { |
| 256 | size_t len = strlen(dest); |
| 257 | |
| 258 | if(len < size) |
| 259 | setString(dest + len, size - len, src); |
| 260 | return dest; |
| 261 | } |
| 262 | |
| 263 | char *newString(const char *src, size_t size) |
| 264 | { |
| 265 | char *dest; |
| 266 | |
| 267 | if(!size) |
| 268 | size = strlen(src) + 1; |
| 269 | |
| 270 | dest = new char[size]; |
| 271 | return setString(dest, size, src); |
| 272 | } |
| 273 | |
| 274 | void delString(char *str) |
| 275 | { |
| 276 | delete[] str; |
| 277 | } |
| 278 | |
| 279 | #ifdef CCXX_NAMESPACES |
| 280 | } |
| 281 | #endif |