Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 1999-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 <ucommon-config.h> |
| 40 | #include <ctype.h> |
| 41 | #include <commoncpp/config.h> |
| 42 | #include <commoncpp/export.h> |
| 43 | #include <commoncpp/string.h> |
| 44 | #include <commoncpp/numbers.h> |
| 45 | |
| 46 | NAMESPACE_COMMONCPP |
| 47 | |
| 48 | char *newString(const char *src, size_t size) |
| 49 | { |
| 50 | char *dest; |
| 51 | |
| 52 | if(!size) |
| 53 | size = strlen(src) + 1; |
| 54 | |
| 55 | dest = new char[size]; |
| 56 | return setString(dest, size, src); |
| 57 | } |
| 58 | |
| 59 | void delString(char *str) |
| 60 | { |
| 61 | delete[] str; |
| 62 | } |
| 63 | |
| 64 | char *setUpper(char *string, size_t size) |
| 65 | { |
| 66 | char *ret = string; |
| 67 | |
| 68 | if(!size) |
| 69 | size = strlen(string); |
| 70 | |
| 71 | while(size && *string) { |
| 72 | *string = toupper(*string); |
| 73 | ++string; |
| 74 | --size; |
| 75 | } |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | char *setLower(char *string, size_t size) |
| 81 | { |
| 82 | char *ret = string; |
| 83 | |
| 84 | if(!size) |
| 85 | size = strlen(string); |
| 86 | |
| 87 | while(size && *string) { |
| 88 | *string = tolower(*string); |
| 89 | ++string; |
| 90 | --size; |
| 91 | } |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | char *rsetField(char *dest, size_t size, const char *src, const char fill) |
| 97 | { |
| 98 | size_t len = 0; |
| 99 | |
| 100 | if(src) |
| 101 | len = strlen(src); |
| 102 | |
| 103 | if(len > size) |
| 104 | len = size; |
| 105 | |
| 106 | if(len) |
| 107 | memmove(dest + size - len, (void *)src, len); |
| 108 | |
| 109 | if(len < size && fill) |
| 110 | memset(dest, fill, size - len); |
| 111 | |
| 112 | return dest; |
| 113 | } |
| 114 | |
| 115 | char *lsetField(char *dest, size_t size, const char *src, const char fill) |
| 116 | { |
| 117 | size_t len = 0; |
| 118 | |
| 119 | if(src) |
| 120 | len = strlen(src); |
| 121 | |
| 122 | if(len > size) |
| 123 | len = size; |
| 124 | |
| 125 | if(len) |
| 126 | memmove(dest, src, len); |
| 127 | |
| 128 | if(len < size && fill) |
| 129 | memset(dest + len, fill, size - len); |
| 130 | |
| 131 | return dest; |
| 132 | } |
| 133 | |
| 134 | END_NAMESPACE |