| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <iomanip> |
| 22 | #include <sstream> |
| 23 | |
| 24 | #include "Utils/Integer.h" |
| 25 | |
| 26 | namespace Utils |
| 27 | { |
| 28 | int Integer::StringToInteger(const std::string& value, const int defaultValue) |
| 29 | { |
| 30 | size_t valueSize = value.length(); |
| 31 | if (valueSize == 0) |
| 32 | { |
| 33 | return defaultValue; |
| 34 | } |
| 35 | |
| 36 | char* end; |
| 37 | const char* start = value.c_str(); |
| 38 | long longValue = std::strtol(start, &end, 10); |
| 39 | if (errno == ERANGE || start == end) |
| 40 | { |
| 41 | return defaultValue; |
| 42 | } |
| 43 | if (longValue > INT_MAX || longValue < INT_MIN) |
| 44 | { |
| 45 | return defaultValue; |
| 46 | } |
| 47 | return static_cast<int>(longValue); |
| 48 | } |
| 49 | |
| 50 | int Integer::StringToInteger(const std::string& value, const int min, const int max) |
| 51 | { |
| 52 | int intValue = StringToInteger(value, min); |
| 53 | |
| 54 | if (intValue < min) |
| 55 | { |
| 56 | return min; |
| 57 | } |
| 58 | |
| 59 | if (intValue > max) |
| 60 | { |
| 61 | return max; |
| 62 | } |
| 63 | |
| 64 | return intValue; |
| 65 | } |
| 66 | |
| 67 | long Integer::HexToInteger(const std::string& value, const long defaultValue) |
| 68 | { |
| 69 | size_t valueSize = value.length(); |
| 70 | if (valueSize == 0) |
| 71 | { |
| 72 | return defaultValue; |
| 73 | } |
| 74 | |
| 75 | char* end; |
| 76 | const char* start = value.c_str(); |
| 77 | long longValue = std::strtol(start, &end, 16); |
| 78 | if (errno == ERANGE || start == end) |
| 79 | { |
| 80 | return defaultValue; |
| 81 | } |
| 82 | return longValue; |
| 83 | } |
| 84 | |
| 85 | signed char Integer::HexToChar(signed char c) |
| 86 | { |
| 87 | if (c >= 'a') |
| 88 | { |
| 89 | c -= 'a' - 10; |
| 90 | } |
| 91 | else if (c >= 'A') |
| 92 | { |
| 93 | c -= 'A' - 10; |
| 94 | } |
| 95 | else if (c >= '0') |
| 96 | { |
| 97 | c -= '0'; |
| 98 | } |
| 99 | |
| 100 | return c > 15 ? 0 : c; |
| 101 | } |
| 102 | |
| 103 | void Integer::IntToDataBigEndian(const uint32_t i, unsigned char* buffer) |
| 104 | { |
| 105 | buffer[0] = (i >> 24); |
| 106 | buffer[1] = ((i >> 16) & 0xFF); |
| 107 | buffer[2] = ((i >> 8) & 0xFF); |
| 108 | buffer[3] = (i & 0xFF); |
| 109 | } |
| 110 | |
| 111 | uint32_t Integer::DataBigEndianToInt(const unsigned char* buffer) |
| 112 | { |
| 113 | uint32_t i = buffer[0]; |
| 114 | i <<= 8; |
| 115 | i |= buffer[1]; |
| 116 | i <<= 8; |
| 117 | i |= buffer[2]; |
| 118 | i <<= 8; |
| 119 | i |= buffer[3]; |
| 120 | return i; |
| 121 | } |
| 122 | |
| 123 | void Integer::ShortToDataBigEndian(const uint16_t i, unsigned char* buffer) |
| 124 | { |
| 125 | buffer[0] = (i >> 8); |
| 126 | buffer[1] = (i & 0xFF); |
| 127 | } |
| 128 | |
| 129 | uint16_t Integer::DataBigEndianToShort(const unsigned char* buffer) |
| 130 | { |
| 131 | uint16_t i = buffer[0]; |
| 132 | i <<= 8; |
| 133 | i |= buffer[1]; |
| 134 | return i; |
| 135 | } |
| 136 | |
| 137 | void Integer::IntToDataLittleEndian(const uint32_t i, unsigned char* buffer) |
| 138 | { |
| 139 | buffer[0] = (i & 0xFF); |
| 140 | buffer[1] = ((i >> 8) & 0xFF); |
| 141 | buffer[2] = ((i >> 16) & 0xFF); |
| 142 | buffer[3] = (i >> 24); |
| 143 | } |
| 144 | |
| 145 | uint32_t Integer::DataLittleEndianToInt(const unsigned char* buffer) |
| 146 | { |
| 147 | return buffer[0] |
| 148 | + (buffer[1] << 8) |
| 149 | + (buffer[2] << 16) |
| 150 | + (buffer[3] << 24); |
| 151 | } |
| 152 | |
| 153 | void Integer::ShortToDataLittleEndian(const uint16_t i, unsigned char* buffer) |
| 154 | { |
| 155 | buffer[0] = (i & 0xFF); |
| 156 | buffer[1] = (i >> 8); |
| 157 | } |
| 158 | |
| 159 | uint16_t Integer::DataLittleEndianToShort(const unsigned char* buffer) |
| 160 | { |
| 161 | return buffer[0] |
| 162 | + (buffer[1] << 8); |
| 163 | } |
| 164 | |
| 165 | std::string Integer::IntegerToBCD(const unsigned int input) |
| 166 | { |
| 167 | unsigned char zero = (input >> 4); |
| 168 | zero &= 0x0000000F; |
| 169 | zero += '0'; |
| 170 | unsigned char one = input; |
| 171 | one &= 0x0000000F; |
| 172 | one += '0'; |
| 173 | std::string output; |
| 174 | output.append(1, zero); |
| 175 | output.append(1, one); |
| 176 | return output; |
| 177 | } |
| 178 | |
| 179 | std::string Integer::IntegerToHex(const unsigned int input, const unsigned int size) |
| 180 | { |
| 181 | if (input == 0) |
| 182 | { |
| 183 | return "0"; |
| 184 | } |
| 185 | std::string output; |
| 186 | |
| 187 | unsigned int decimal = input; |
| 188 | unsigned int internalSize = 0; |
| 189 | while (decimal) |
| 190 | { |
| 191 | std::stringstream part; |
| 192 | part << std::setfill('0') << std::setw(1) << std::hex << (decimal & 0xF); |
| 193 | output = part.str() + output; |
| 194 | decimal >>= 4; |
| 195 | ++internalSize; |
| 196 | } |
| 197 | while (internalSize < size) |
| 198 | { |
| 199 | ++internalSize; |
| 200 | output = "0" + output; |
| 201 | } |
| 202 | return output; |
| 203 | } |
| 204 | } |