1 /* 2 * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 #include <ctype.h> 26 #include <string.h> 27 #include "ioUtils.hpp" 28 #include "IOBuf.hpp" 29 30 bool 31 scanInt(char** data, int* num) { 32 *num = 0; 33 34 // Skip whitespace 35 while ((**data != 0) && (isspace(**data))) { 36 ++*data; 37 } 38 39 if (**data == 0) { 40 return false; 41 } 42 43 while ((**data != 0) && (!isspace(**data))) { 44 char cur = **data; 45 if ((cur < '0') || (cur > '9')) { 46 return false; 47 } 48 *num *= 10; 49 *num += cur - '0'; 50 ++*data; 51 } 52 53 return true; 54 } 55 56 bool 57 scanUnsignedLong(char** data, unsigned long* num) { 58 *num = 0; 59 60 // Skip whitespace 61 while ((**data != 0) && (isspace(**data))) { 62 ++*data; 63 } 64 65 if (**data == 0) { 66 return false; 67 } 68 69 while ((**data != 0) && (!isspace(**data))) { 70 char cur = **data; 71 if ((cur < '0') || (cur > '9')) { 72 return false; 73 } 74 *num *= 10; 75 *num += cur - '0'; 76 ++*data; 77 } 78 79 return true; 80 } 81 82 bool 83 charToNibble(char ascii, int* value) { 84 if (ascii >= '0' && ascii <= '9') { 85 *value = ascii - '0'; 86 return true; 87 } else if (ascii >= 'A' && ascii <= 'F') { 88 *value = 10 + ascii - 'A'; 89 return true; 90 } else if (ascii >= 'a' && ascii <= 'f') { 91 *value = 10 + ascii - 'a'; 92 return true; 93 } 94 95 return false; 96 } 97 98 bool 99 scanAddress(char** data, unsigned long* addr) { 100 *addr = 0; 101 102 // Skip whitespace 103 while ((**data != 0) && (isspace(**data))) { 104 ++*data; 105 } 106 107 if (**data == 0) { 108 return false; 109 } 110 111 if (strncmp(*data, "0x", 2) != 0) { 112 return false; 113 } 114 115 *data += 2; 116 117 while ((**data != 0) && (!isspace(**data))) { 118 int val; 119 bool res = charToNibble(**data, &val); 120 if (!res) { 121 return false; 122 } 123 *addr <<= 4; 124 *addr |= val; 125 ++*data; 126 } 127 128 return true; 129 } 130 131 bool 132 scanAndSkipBinEscapeChar(char** data) { 133 // Skip whitespace 134 while ((**data != 0) && (isspace(**data))) { 135 ++*data; 136 } 137 138 if (!IOBuf::isBinEscapeChar(**data)) { 139 return false; 140 } 141 142 ++*data; 143 144 return true; 145 } 146 147 bool 148 scanBinUnsignedLong(char** data, unsigned long* num) { 149 *num = 0; 150 for (int i = 0; i < 4; i++) { 151 unsigned char val = (unsigned char) **data; 152 *num = (*num << 8) | val; 153 ++*data; 154 } 155 return true; 156 }