Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Purpose: Functions to convert an ascii string to an integer value. 4 : * 5 : ****************************************************************************** 6 : * Copyright (c) 2011 7 : * PCI Geomatics, 90 Allstate Parkway, Markham, Ontario, Canada. 8 : * 9 : * Permission is hereby granted, free of charge, to any person obtaining a 10 : * copy of this software and associated documentation files (the "Software"), 11 : * to deal in the Software without restriction, including without limitation 12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 : * and/or sell copies of the Software, and to permit persons to whom the 14 : * Software is furnished to do so, subject to the following conditions: 15 : * 16 : * The above copyright notice and this permission notice shall be included 17 : * in all copies or substantial portions of the Software. 18 : * 19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 : * DEALINGS IN THE SOFTWARE. 26 : ****************************************************************************/ 27 : 28 : #ifndef PCIDSK_SCANINT_H 29 : #define PCIDSK_SCANINT_H 30 : 31 : #include "pcidsk_config.h" 32 : #include <cassert> 33 : #include <cmath> 34 : 35 : namespace PCIDSK 36 : { 37 : 38 : extern const int16 ganCharTo1[256]; 39 : extern const int16 ganCharTo10[256]; 40 : extern const int16 ganCharTo100[256]; 41 : extern const int16 ganCharTo1000[256]; 42 : extern const int32 ganCharTo10000[256]; 43 : extern const int32 ganCharTo100000[256]; 44 : extern const int32 ganCharTo1000000[256]; 45 : extern const int32 ganCharTo10000000[256]; 46 : extern const int32 ganCharTo100000000[256]; 47 : extern const int64 ganCharTo1000000000[256]; 48 : extern const int64 ganCharTo10000000000[256]; 49 : extern const int64 ganCharTo100000000000[256]; 50 : 51 : inline int16 ScanInt1(const uint8 * string) 52 : { 53 : if (*string != '-') 54 : { 55 : int16 nValue = 56 : (ganCharTo1 [*(string)]); 57 : 58 : return nValue; 59 : } 60 : else 61 : { 62 : return 0; 63 : } 64 : } 65 : 66 : inline int16 ScanInt2(const uint8 * string) 67 : { 68 : if (*string != '-') 69 : { 70 : int16 nValue = 71 : (ganCharTo10[*(string )] + 72 : ganCharTo1 [*(string + 1)]); 73 : 74 : return nValue; 75 : } 76 : else 77 : { 78 : return ganCharTo1[*(string + 1)] * -1; 79 : } 80 : } 81 : 82 24 : inline int16 ScanInt3(const uint8 * string) 83 : { 84 24 : int16 nValue = 85 24 : (ganCharTo100[*(string )] + 86 24 : ganCharTo10 [*(string + 1)] + 87 24 : ganCharTo1 [*(string + 2)]); 88 : 89 24 : if (nValue < 0) 90 : { 91 0 : const uint8 * pbyIter = string; 92 0 : while (*pbyIter != '-') 93 0 : ++pbyIter; 94 0 : return (int16) -std::pow((double) 10, 95 0 : 2 - (int) (pbyIter - string)) - nValue; 96 : } 97 : 98 24 : return nValue; 99 : } 100 : 101 102 : inline int16 ScanInt4(const uint8 * string) 102 : { 103 102 : int16 nValue = 104 102 : (ganCharTo1000[*(string )] + 105 102 : ganCharTo100 [*(string + 1)] + 106 102 : ganCharTo10 [*(string + 2)] + 107 102 : ganCharTo1 [*(string + 3)]); 108 : 109 102 : if (nValue < 0) 110 : { 111 0 : const uint8 * pbyIter = string; 112 0 : while (*pbyIter != '-') 113 0 : ++pbyIter; 114 0 : return (int16) -std::pow((double) 10, 115 0 : 3 - (int) (pbyIter - string)) - nValue; 116 : } 117 : 118 102 : return nValue; 119 : } 120 : 121 : inline int32 ScanInt5(const uint8 * string) 122 : { 123 : int32 nValue = 124 : (ganCharTo10000[*(string )] + 125 : ganCharTo1000 [*(string + 1)] + 126 : ganCharTo100 [*(string + 2)] + 127 : ganCharTo10 [*(string + 3)] + 128 : ganCharTo1 [*(string + 4)]); 129 : 130 : if (nValue < 0) 131 : { 132 : const uint8 * pbyIter = string; 133 : while (*pbyIter != '-') 134 : ++pbyIter; 135 : return (int32) -std::pow((double) 10, 136 : 4 - (int) (pbyIter - string)) - nValue; 137 : } 138 : 139 : return nValue; 140 : } 141 : 142 : inline int32 ScanInt6(const uint8 * string) 143 : { 144 : int32 nValue = 145 : (ganCharTo100000[*(string )] + 146 : ganCharTo10000 [*(string + 1)] + 147 : ganCharTo1000 [*(string + 2)] + 148 : ganCharTo100 [*(string + 3)] + 149 : ganCharTo10 [*(string + 4)] + 150 : ganCharTo1 [*(string + 5)]); 151 : 152 : if (nValue < 0) 153 : { 154 : const uint8 * pbyIter = string; 155 : while (*pbyIter != '-') 156 : ++pbyIter; 157 : return (int32) -std::pow((double) 10, 158 : 5 - (int) (pbyIter - string)) - nValue; 159 : } 160 : 161 : return nValue; 162 : } 163 : 164 : inline int32 ScanInt7(const uint8 * string) 165 : { 166 : int32 nValue = 167 : (ganCharTo1000000[*(string )] + 168 : ganCharTo100000 [*(string + 1)] + 169 : ganCharTo10000 [*(string + 2)] + 170 : ganCharTo1000 [*(string + 3)] + 171 : ganCharTo100 [*(string + 4)] + 172 : ganCharTo10 [*(string + 5)] + 173 : ganCharTo1 [*(string + 6)]); 174 : 175 : if (nValue < 0) 176 : { 177 : const uint8 * pbyIter = string; 178 : while (*pbyIter != '-') 179 : ++pbyIter; 180 : return (int32) -std::pow((double) 10, 181 : 6 - (int) (pbyIter - string)) - nValue; 182 : } 183 : 184 : return nValue; 185 : } 186 : 187 248 : inline int32 ScanInt8(const uint8 * string) 188 : { 189 248 : int32 nValue = 190 248 : (ganCharTo10000000[*(string )] + 191 248 : ganCharTo1000000 [*(string + 1)] + 192 248 : ganCharTo100000 [*(string + 2)] + 193 248 : ganCharTo10000 [*(string + 3)] + 194 248 : ganCharTo1000 [*(string + 4)] + 195 248 : ganCharTo100 [*(string + 5)] + 196 248 : ganCharTo10 [*(string + 6)] + 197 248 : ganCharTo1 [*(string + 7)]); 198 : 199 248 : if (nValue < 0) 200 : { 201 10 : const uint8 * pbyIter = string; 202 70 : while (*pbyIter != '-') 203 60 : ++pbyIter; 204 20 : return (int32) -std::pow((double) 10, 205 10 : 7 - (int) (pbyIter - string)) - nValue; 206 : } 207 : 208 238 : return nValue; 209 : } 210 : 211 : inline int32 ScanInt9(const uint8 * string) 212 : { 213 : int32 nValue = 214 : (ganCharTo100000000[*(string )] + 215 : ganCharTo10000000 [*(string + 1)] + 216 : ganCharTo1000000 [*(string + 2)] + 217 : ganCharTo100000 [*(string + 3)] + 218 : ganCharTo10000 [*(string + 4)] + 219 : ganCharTo1000 [*(string + 5)] + 220 : ganCharTo100 [*(string + 6)] + 221 : ganCharTo10 [*(string + 7)] + 222 : ganCharTo1 [*(string + 8)]); 223 : 224 : if (nValue < 0) 225 : { 226 : const uint8 * pbyIter = string; 227 : while (*pbyIter != '-') 228 : ++pbyIter; 229 : return (int32) -std::pow((double) 10, 230 : 8 - (int) (pbyIter - string)) - nValue; 231 : } 232 : 233 : return nValue; 234 : } 235 : 236 : inline int64 ScanInt10(const uint8 * string) 237 : { 238 : int64 nValue = 239 : (ganCharTo1000000000[*(string )] + 240 : ganCharTo100000000 [*(string + 1)] + 241 : ganCharTo10000000 [*(string + 2)] + 242 : ganCharTo1000000 [*(string + 3)] + 243 : ganCharTo100000 [*(string + 4)] + 244 : ganCharTo10000 [*(string + 5)] + 245 : ganCharTo1000 [*(string + 6)] + 246 : ganCharTo100 [*(string + 7)] + 247 : ganCharTo10 [*(string + 8)] + 248 : ganCharTo1 [*(string + 9)]); 249 : 250 : if (nValue < 0) 251 : { 252 : const uint8 * pbyIter = string; 253 : while (*pbyIter != '-') 254 : ++pbyIter; 255 : return (int64) -std::pow((double) 10, 256 : 9 - (int) (pbyIter - string)) - nValue; 257 : } 258 : 259 : return nValue; 260 : } 261 : 262 : inline int64 ScanInt11(const uint8 * string) 263 : { 264 : int64 nValue = 265 : (ganCharTo10000000000[*(string )] + 266 : ganCharTo1000000000 [*(string + 1)] + 267 : ganCharTo100000000 [*(string + 2)] + 268 : ganCharTo10000000 [*(string + 3)] + 269 : ganCharTo1000000 [*(string + 4)] + 270 : ganCharTo100000 [*(string + 5)] + 271 : ganCharTo10000 [*(string + 6)] + 272 : ganCharTo1000 [*(string + 7)] + 273 : ganCharTo100 [*(string + 8)] + 274 : ganCharTo10 [*(string + 9)] + 275 : ganCharTo1 [*(string + 10)]); 276 : 277 : if (nValue < 0) 278 : { 279 : const uint8 * pbyIter = string; 280 : while (*pbyIter != '-') 281 : ++pbyIter; 282 : return (int64) -std::pow((double) 10, 283 : 10 - (int) (pbyIter - string)) - nValue; 284 : } 285 : 286 : return nValue; 287 : } 288 : 289 26 : inline int64 ScanInt12(const uint8 * string) 290 : { 291 26 : int64 nValue = 292 26 : (ganCharTo100000000000[*(string )] + 293 26 : ganCharTo10000000000 [*(string + 1)] + 294 26 : ganCharTo1000000000 [*(string + 2)] + 295 26 : ganCharTo100000000 [*(string + 3)] + 296 26 : ganCharTo10000000 [*(string + 4)] + 297 26 : ganCharTo1000000 [*(string + 5)] + 298 26 : ganCharTo100000 [*(string + 6)] + 299 26 : ganCharTo10000 [*(string + 7)] + 300 26 : ganCharTo1000 [*(string + 8)] + 301 26 : ganCharTo100 [*(string + 9)] + 302 26 : ganCharTo10 [*(string + 10)] + 303 26 : ganCharTo1 [*(string + 11)]); 304 : 305 26 : if (nValue < 0) 306 : { 307 0 : const uint8 * pbyIter = string; 308 0 : while (*pbyIter != '-') 309 0 : ++pbyIter; 310 0 : return (int64) -std::pow((double) 10, 311 0 : 11 - (int) (pbyIter - string)) - nValue; 312 : } 313 : 314 26 : return nValue; 315 : } 316 : 317 : } // namespace PCIDSK 318 : 319 : #endif