Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [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 <cc++/config.h> |
| 40 | #include <cc++/string.h> |
| 41 | #include <cc++/thread.h> |
| 42 | #include <cc++/export.h> |
| 43 | #include <cc++/numbers.h> |
| 44 | #include <cstdlib> |
| 45 | |
| 46 | #ifdef CCXX_NAMESPACES |
| 47 | namespace ost { |
| 48 | #endif |
| 49 | |
| 50 | Number::Number(char *buf, unsigned width) |
| 51 | { |
| 52 | if(width > 10) |
| 53 | width = 10; |
| 54 | if(width < 1) |
| 55 | width = 1; |
| 56 | size = width; |
| 57 | |
| 58 | buffer = buf; |
| 59 | } |
| 60 | |
| 61 | long Number::getValue(void) const |
| 62 | { |
| 63 | int count = size; |
| 64 | bool sign = false; |
| 65 | long ret = 0; |
| 66 | char *bp = buffer; |
| 67 | |
| 68 | if(*bp == '-') { |
| 69 | --count; |
| 70 | ++bp; |
| 71 | sign = true; |
| 72 | } |
| 73 | else if(*bp == '+') { |
| 74 | --count; |
| 75 | ++bp; |
| 76 | } |
| 77 | while(count && *bp >='0' && *bp <='9') { |
| 78 | ret = ret * 10l + (*bp - '0'); |
| 79 | --count; |
| 80 | ++bp; |
| 81 | } |
| 82 | |
| 83 | if(sign) |
| 84 | ret = -ret; |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | void Number::setValue(long value) |
| 89 | { |
| 90 | int count = size; |
| 91 | char *bp = buffer; |
| 92 | long max = 1; |
| 93 | int exp; |
| 94 | bool z = false; |
| 95 | |
| 96 | if(value < 0) { |
| 97 | value = -value; |
| 98 | --count; |
| 99 | *(bp++) = '-'; |
| 100 | } |
| 101 | |
| 102 | exp = count; |
| 103 | while(--exp) |
| 104 | max *= 10; |
| 105 | |
| 106 | while(max) { |
| 107 | if(value >= max || z) { |
| 108 | --count; |
| 109 | *(bp++) = '0' + ((char)(value / max)); |
| 110 | } |
| 111 | if(value >= max) { |
| 112 | z = true; |
| 113 | value -= (value / max) * max; |
| 114 | } |
| 115 | max /= 10; |
| 116 | } |
| 117 | while(count-- && *bp >= '0' && *bp <='9') |
| 118 | *(bp++) = ' '; |
| 119 | } |
| 120 | |
| 121 | long Number::operator=(long value) |
| 122 | { |
| 123 | setValue(value); |
| 124 | return value; |
| 125 | } |
| 126 | |
| 127 | long Number::operator+=(const long value) |
| 128 | { |
| 129 | long value1 = getValue() + value; |
| 130 | setValue(value1); |
| 131 | return value1; |
| 132 | } |
| 133 | |
| 134 | long Number::operator-=(const long value) |
| 135 | { |
| 136 | long value1 = getValue() - value; |
| 137 | setValue(value1); |
| 138 | return value1; |
| 139 | } |
| 140 | |
| 141 | long Number::operator--() |
| 142 | { |
| 143 | long val = getValue(); |
| 144 | setValue(--val); |
| 145 | return val; |
| 146 | } |
| 147 | |
| 148 | long Number::operator++() |
| 149 | { |
| 150 | long val = getValue(); |
| 151 | setValue(++val); |
| 152 | return val; |
| 153 | } |
| 154 | |
| 155 | int Number::operator==(const Number &num) |
| 156 | { |
| 157 | return getValue() == num.getValue(); |
| 158 | } |
| 159 | |
| 160 | int Number::operator!=(const Number &num) |
| 161 | { |
| 162 | return getValue() != num.getValue(); |
| 163 | } |
| 164 | |
| 165 | int Number::operator<(const Number &num) |
| 166 | { |
| 167 | return getValue() < num.getValue(); |
| 168 | } |
| 169 | |
| 170 | int Number::operator<=(const Number &num) |
| 171 | { |
| 172 | return getValue() <= num.getValue(); |
| 173 | } |
| 174 | |
| 175 | int Number::operator>(const Number &num) |
| 176 | { |
| 177 | return getValue() > num.getValue(); |
| 178 | } |
| 179 | |
| 180 | int Number::operator>=(const Number &num) |
| 181 | { |
| 182 | return getValue() >= num.getValue(); |
| 183 | } |
| 184 | |
| 185 | ZNumber::ZNumber(char *buf, unsigned chars) : |
| 186 | Number(buf, chars) |
| 187 | {} |
| 188 | |
| 189 | void ZNumber::setValue(long value) |
| 190 | { |
| 191 | int count = size; |
| 192 | char *bp = buffer; |
| 193 | long max = 1; |
| 194 | int exp; |
| 195 | |
| 196 | if(value < 0) { |
| 197 | value = -value; |
| 198 | --count; |
| 199 | *(bp++) = '-'; |
| 200 | } |
| 201 | |
| 202 | exp = count; |
| 203 | while(--exp) |
| 204 | max *= 10; |
| 205 | |
| 206 | while(max) { |
| 207 | --count; |
| 208 | *(bp++) = '0' + (char)(value / max); |
| 209 | value -= (value / max) * max; |
| 210 | max /= 10; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | long ZNumber::operator=(long value) |
| 215 | { |
| 216 | setValue(value); |
| 217 | return value; |
| 218 | } |
| 219 | |
| 220 | long operator+(const Number &num, long val) |
| 221 | { |
| 222 | return num.getValue() + val; |
| 223 | } |
| 224 | |
| 225 | long operator+(long val, const Number &num) |
| 226 | { |
| 227 | return num.getValue() + val; |
| 228 | } |
| 229 | |
| 230 | long operator-(const Number &num, long val) |
| 231 | { |
| 232 | return num.getValue() - val; |
| 233 | } |
| 234 | |
| 235 | long operator-(long val, const Number &num) |
| 236 | { |
| 237 | return num.getValue() - val; |
| 238 | } |
| 239 | |
| 240 | #ifdef CCXX_NAMESPACES |
| 241 | } |
| 242 | #endif |