Utility library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems
Dependents: mtsas mtsas thermostat_fan_demo--fan mtsas ... more
NOTE: MTS-Utils has moved to GitHub. This version will not be updated. For updates, go to the GitHub version.
MTSText.cpp@15:ae12624eb600, 2017-03-21 (annotated)
- Committer:
- Mike Fiore
- Date:
- Tue Mar 21 15:26:50 2017 -0500
- Revision:
- 15:ae12624eb600
- Parent:
- 14:1d88cf5266c8
update from git revision 37b619a6e4e6e3b49b64c402429cdd8710d960a6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
2:7779ede60c3d | 1 | #include "MTSText.h" |
Mike Fiore |
12:7818d55b35c6 | 2 | #include "ctype.h" |
Mike Fiore |
13:4709c2dfcd11 | 3 | #include <math.h> |
Mike Fiore |
2:7779ede60c3d | 4 | |
Mike Fiore |
2:7779ede60c3d | 5 | using namespace mts; |
Mike Fiore |
2:7779ede60c3d | 6 | |
Mike Fiore |
14:1d88cf5266c8 | 7 | std::string BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; |
Mike Fiore |
14:1d88cf5266c8 | 8 | |
Mike Fiore |
2:7779ede60c3d | 9 | std::string Text::getLine(const std::string& source, const size_t& start, size_t& cursor) { |
Mike Fiore |
2:7779ede60c3d | 10 | char delimiters[2]; |
Mike Fiore |
2:7779ede60c3d | 11 | delimiters[0] = '\n'; |
Mike Fiore |
2:7779ede60c3d | 12 | delimiters[1] = '\r'; |
Mike Fiore |
2:7779ede60c3d | 13 | size_t end = source.find_first_of(delimiters, start, 2); |
Mike Fiore |
2:7779ede60c3d | 14 | std::string line(source.substr(start, end - start)); |
Mike Fiore |
2:7779ede60c3d | 15 | if (end < source.size()) { |
Mike Fiore |
2:7779ede60c3d | 16 | if (end < source.size() - 1) |
Mike Fiore |
2:7779ede60c3d | 17 | if ((source[end] == '\n' && source[end + 1] == '\r') || (source[end] == '\r' && source[end + 1] == '\n')) { |
Mike Fiore |
2:7779ede60c3d | 18 | //Advance an additional character in scenarios where lines end in \r\n or \n\r |
Mike Fiore |
2:7779ede60c3d | 19 | end++; |
Mike Fiore |
2:7779ede60c3d | 20 | } |
Mike Fiore |
2:7779ede60c3d | 21 | end++; |
Mike Fiore |
2:7779ede60c3d | 22 | } |
Mike Fiore |
2:7779ede60c3d | 23 | cursor = end; |
Mike Fiore |
2:7779ede60c3d | 24 | return line; |
Mike Fiore |
2:7779ede60c3d | 25 | } |
Mike Fiore |
2:7779ede60c3d | 26 | |
Mike Fiore |
2:7779ede60c3d | 27 | std::vector<std::string> Text::split(const std::string& str, char delimiter, int limit) { |
Mike Fiore |
2:7779ede60c3d | 28 | return split(str, std::string(1, delimiter), limit); |
Mike Fiore |
2:7779ede60c3d | 29 | } |
Mike Fiore |
2:7779ede60c3d | 30 | |
Mike Fiore |
2:7779ede60c3d | 31 | std::vector<std::string> Text::split(const std::string& str, const std::string& delimiter, int limit) { |
Mike Fiore |
2:7779ede60c3d | 32 | std::vector<std::string> result; |
Mike Fiore |
2:7779ede60c3d | 33 | if(str.size() == 0) { |
Mike Fiore |
2:7779ede60c3d | 34 | return result; |
Mike Fiore |
2:7779ede60c3d | 35 | } |
Mike Fiore |
2:7779ede60c3d | 36 | size_t start = 0; |
Mike Fiore |
2:7779ede60c3d | 37 | size_t end = str.find(delimiter, start); |
Mike Fiore |
2:7779ede60c3d | 38 | for (int i = 1; i < limit || (limit <= 0 && (end != std::string::npos)); ++i) { |
Mike Fiore |
2:7779ede60c3d | 39 | result.push_back(str.substr(start, end - start)); |
Mike Fiore |
2:7779ede60c3d | 40 | start = end + delimiter.length(); |
Mike Fiore |
2:7779ede60c3d | 41 | end = str.find(delimiter, start); |
Mike Fiore |
2:7779ede60c3d | 42 | } |
Mike Fiore |
2:7779ede60c3d | 43 | result.push_back(str.substr(start)); |
Mike Fiore |
2:7779ede60c3d | 44 | return result; |
Mike Fiore |
2:7779ede60c3d | 45 | } |
Mike Fiore |
2:7779ede60c3d | 46 | |
Mike Fiore |
2:7779ede60c3d | 47 | std::string Text::readString(char* index, int length) |
Mike Fiore |
2:7779ede60c3d | 48 | { |
Mike Fiore |
2:7779ede60c3d | 49 | std::string result = std::string(index, length); |
Mike Fiore |
2:7779ede60c3d | 50 | index += length; |
Mike Fiore |
2:7779ede60c3d | 51 | return result; |
Mike Fiore |
13:4709c2dfcd11 | 52 | } |
Mike Fiore |
13:4709c2dfcd11 | 53 | |
Mike Fiore |
12:7818d55b35c6 | 54 | std::string Text::toUpper(const std::string str) |
Mike Fiore |
12:7818d55b35c6 | 55 | { |
Mike Fiore |
12:7818d55b35c6 | 56 | std::string ret = str; |
Mike Fiore |
12:7818d55b35c6 | 57 | |
Mike Fiore |
12:7818d55b35c6 | 58 | for (unsigned int i = 0; i < ret.size(); i++) |
Mike Fiore |
12:7818d55b35c6 | 59 | { |
Mike Fiore |
12:7818d55b35c6 | 60 | ret[i] = toupper(ret[i]); |
Mike Fiore |
12:7818d55b35c6 | 61 | } |
Mike Fiore |
12:7818d55b35c6 | 62 | |
Mike Fiore |
12:7818d55b35c6 | 63 | return ret; |
Mike Fiore |
12:7818d55b35c6 | 64 | } |
Mike Fiore |
13:4709c2dfcd11 | 65 | |
Mike Fiore |
13:4709c2dfcd11 | 66 | std::string Text::float2String(double val, int precision) { |
Mike Fiore |
13:4709c2dfcd11 | 67 | char buff[100]; |
Mike Fiore |
13:4709c2dfcd11 | 68 | sprintf(buff, "%d.%d", (int)val, (int)((val - floor(val)) * (int)pow(10.0, precision))); |
Mike Fiore |
13:4709c2dfcd11 | 69 | return std::string(buff); |
Mike Fiore |
13:4709c2dfcd11 | 70 | } |
Mike Fiore |
13:4709c2dfcd11 | 71 | |
Mike Fiore |
15:ae12624eb600 | 72 | std::string Text::bin2hexString(const std::vector<uint8_t>& data, const char* delim, bool leadingZeros, bool bytePadding) { |
Mike Fiore |
15:ae12624eb600 | 73 | std::string ret; |
Mike Fiore |
15:ae12624eb600 | 74 | uint8_t *data_arr = new uint8_t[data.size()]; |
Mike Fiore |
13:4709c2dfcd11 | 75 | |
Mike Fiore |
15:ae12624eb600 | 76 | for (size_t i = 0; i < data.size(); i++) |
Mike Fiore |
13:4709c2dfcd11 | 77 | data_arr[i] = data[i]; |
Mike Fiore |
13:4709c2dfcd11 | 78 | |
Mike Fiore |
15:ae12624eb600 | 79 | ret = bin2hexString(data_arr, data.size(), delim, leadingZeros, bytePadding); |
Mike Fiore |
15:ae12624eb600 | 80 | |
Mike Fiore |
15:ae12624eb600 | 81 | delete[] data_arr; |
Mike Fiore |
15:ae12624eb600 | 82 | |
Mike Fiore |
15:ae12624eb600 | 83 | return ret; |
Mike Fiore |
13:4709c2dfcd11 | 84 | } |
Mike Fiore |
13:4709c2dfcd11 | 85 | |
Mike Fiore |
15:ae12624eb600 | 86 | std::string Text::bin2hexString(const uint8_t* data, const uint32_t len, const char* delim, bool leadingZeros, bool bytePadding) { |
Mike Fiore |
13:4709c2dfcd11 | 87 | std::string str; |
Mike Fiore |
13:4709c2dfcd11 | 88 | char lead[] = "0x"; |
Mike Fiore |
15:ae12624eb600 | 89 | char buf[5]; |
Mike Fiore |
13:4709c2dfcd11 | 90 | |
Mike Fiore |
13:4709c2dfcd11 | 91 | for (uint32_t i = 0; i < len; i++) { |
Mike Fiore |
13:4709c2dfcd11 | 92 | if (leadingZeros) |
Mike Fiore |
13:4709c2dfcd11 | 93 | str.append(lead); |
Mike Fiore |
15:ae12624eb600 | 94 | |
Mike Fiore |
15:ae12624eb600 | 95 | if (bytePadding) |
Mike Fiore |
15:ae12624eb600 | 96 | snprintf(buf, sizeof(buf), "%02x", data[i]); |
Mike Fiore |
15:ae12624eb600 | 97 | else |
Mike Fiore |
15:ae12624eb600 | 98 | snprintf(buf, sizeof(buf), "%x", data[i]); |
Mike Fiore |
15:ae12624eb600 | 99 | |
Mike Fiore |
13:4709c2dfcd11 | 100 | str.append(buf, strlen(buf)); |
Mike Fiore |
13:4709c2dfcd11 | 101 | if (i < len - 1) |
Mike Fiore |
13:4709c2dfcd11 | 102 | str.append(delim); |
Mike Fiore |
13:4709c2dfcd11 | 103 | } |
Mike Fiore |
13:4709c2dfcd11 | 104 | |
Mike Fiore |
13:4709c2dfcd11 | 105 | return str; |
Mike Fiore |
13:4709c2dfcd11 | 106 | } |
Mike Fiore |
14:1d88cf5266c8 | 107 | |
Mike Fiore |
14:1d88cf5266c8 | 108 | std::string Text::bin2base64(const uint8_t* data, size_t size) { |
Mike Fiore |
14:1d88cf5266c8 | 109 | std::string out; |
Mike Fiore |
14:1d88cf5266c8 | 110 | uint8_t b; |
Mike Fiore |
14:1d88cf5266c8 | 111 | |
Mike Fiore |
14:1d88cf5266c8 | 112 | for (size_t i = 0; i < size; i+=3) { |
Mike Fiore |
14:1d88cf5266c8 | 113 | b = (data[i] & 0xfc) >> 2; |
Mike Fiore |
14:1d88cf5266c8 | 114 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 115 | b = (data[i] & 0x03) << 4; |
Mike Fiore |
14:1d88cf5266c8 | 116 | if (i+1 < size) { |
Mike Fiore |
14:1d88cf5266c8 | 117 | b |= (data[i+1] & 0xf0) >> 4; |
Mike Fiore |
14:1d88cf5266c8 | 118 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 119 | b = (data[i + 1] & 0x0f) << 2; |
Mike Fiore |
14:1d88cf5266c8 | 120 | if (i+2 < size) { |
Mike Fiore |
14:1d88cf5266c8 | 121 | b |= (data[i+2] & 0xc0) >> 6; |
Mike Fiore |
14:1d88cf5266c8 | 122 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 123 | b = data[i+2] & 0x3f; |
Mike Fiore |
14:1d88cf5266c8 | 124 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 125 | } else { |
Mike Fiore |
14:1d88cf5266c8 | 126 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 127 | out.append("="); |
Mike Fiore |
14:1d88cf5266c8 | 128 | } |
Mike Fiore |
14:1d88cf5266c8 | 129 | } else { |
Mike Fiore |
14:1d88cf5266c8 | 130 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 131 | out.append("=="); |
Mike Fiore |
14:1d88cf5266c8 | 132 | } |
Mike Fiore |
14:1d88cf5266c8 | 133 | } |
Mike Fiore |
14:1d88cf5266c8 | 134 | |
Mike Fiore |
14:1d88cf5266c8 | 135 | return out; |
Mike Fiore |
14:1d88cf5266c8 | 136 | } |
Mike Fiore |
14:1d88cf5266c8 | 137 | |
Mike Fiore |
14:1d88cf5266c8 | 138 | std::string Text::bin2base64(const std::vector<uint8_t>& data) { |
Mike Fiore |
14:1d88cf5266c8 | 139 | std::string out; |
Mike Fiore |
14:1d88cf5266c8 | 140 | uint8_t b; |
Mike Fiore |
14:1d88cf5266c8 | 141 | |
Mike Fiore |
14:1d88cf5266c8 | 142 | for (size_t i = 0; i < data.size(); i+=3) { |
Mike Fiore |
14:1d88cf5266c8 | 143 | b = (data[i] & 0xfc) >> 2; |
Mike Fiore |
14:1d88cf5266c8 | 144 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 145 | b = (data[i] & 0x03) << 4; |
Mike Fiore |
14:1d88cf5266c8 | 146 | if (i+1 < data.size()) { |
Mike Fiore |
14:1d88cf5266c8 | 147 | b |= (data[i+1] & 0xf0) >> 4; |
Mike Fiore |
14:1d88cf5266c8 | 148 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 149 | b = (data[i + 1] & 0x0f) << 2; |
Mike Fiore |
14:1d88cf5266c8 | 150 | if (i+2 < data.size()) { |
Mike Fiore |
14:1d88cf5266c8 | 151 | b |= (data[i+2] & 0xc0) >> 6; |
Mike Fiore |
14:1d88cf5266c8 | 152 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 153 | b = data[i+2] & 0x3f; |
Mike Fiore |
14:1d88cf5266c8 | 154 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 155 | } else { |
Mike Fiore |
14:1d88cf5266c8 | 156 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 157 | out.append("="); |
Mike Fiore |
14:1d88cf5266c8 | 158 | } |
Mike Fiore |
14:1d88cf5266c8 | 159 | } else { |
Mike Fiore |
14:1d88cf5266c8 | 160 | out.push_back(BASE64CODE[b]); |
Mike Fiore |
14:1d88cf5266c8 | 161 | out.append("=="); |
Mike Fiore |
14:1d88cf5266c8 | 162 | } |
Mike Fiore |
14:1d88cf5266c8 | 163 | } |
Mike Fiore |
14:1d88cf5266c8 | 164 | |
Mike Fiore |
14:1d88cf5266c8 | 165 | return out; |
Mike Fiore |
14:1d88cf5266c8 | 166 | } |
Mike Fiore |
14:1d88cf5266c8 | 167 | |
Mike Fiore |
14:1d88cf5266c8 | 168 | bool Text::base642bin(const std::string in, std::vector<uint8_t>& out) { |
Mike Fiore |
14:1d88cf5266c8 | 169 | |
Mike Fiore |
14:1d88cf5266c8 | 170 | if (in.find_first_not_of(BASE64CODE) != std::string::npos) { |
Mike Fiore |
14:1d88cf5266c8 | 171 | return false; |
Mike Fiore |
14:1d88cf5266c8 | 172 | } |
Mike Fiore |
14:1d88cf5266c8 | 173 | |
Mike Fiore |
14:1d88cf5266c8 | 174 | if (in.size() % 4 != 0) { |
Mike Fiore |
14:1d88cf5266c8 | 175 | return false; |
Mike Fiore |
14:1d88cf5266c8 | 176 | } |
Mike Fiore |
14:1d88cf5266c8 | 177 | |
Mike Fiore |
14:1d88cf5266c8 | 178 | uint8_t a,b,c,d; |
Mike Fiore |
14:1d88cf5266c8 | 179 | |
Mike Fiore |
14:1d88cf5266c8 | 180 | for (uint32_t i = 0; i < in.size(); i+=4) { |
Mike Fiore |
14:1d88cf5266c8 | 181 | a = BASE64CODE.find(in[i]); |
Mike Fiore |
14:1d88cf5266c8 | 182 | b = BASE64CODE.find(in[i+1]); |
Mike Fiore |
14:1d88cf5266c8 | 183 | c = BASE64CODE.find(in[i+2]); |
Mike Fiore |
14:1d88cf5266c8 | 184 | d = BASE64CODE.find(in[i+3]); |
Mike Fiore |
14:1d88cf5266c8 | 185 | out.push_back(a << 2 | b >> 4); |
Mike Fiore |
14:1d88cf5266c8 | 186 | out.push_back(b << 4 | c >> 2); |
Mike Fiore |
14:1d88cf5266c8 | 187 | out.push_back(c << 6 | d); |
Mike Fiore |
14:1d88cf5266c8 | 188 | } |
Mike Fiore |
14:1d88cf5266c8 | 189 | |
Mike Fiore |
14:1d88cf5266c8 | 190 | return true; |
Mike Fiore |
14:1d88cf5266c8 | 191 | } |
Mike Fiore |
14:1d88cf5266c8 | 192 | |
Mike Fiore |
13:4709c2dfcd11 | 193 | |
Mike Fiore |
13:4709c2dfcd11 | 194 | void Text::ltrim(std::string& str, const char* args) { |
Mike Fiore |
13:4709c2dfcd11 | 195 | size_t startpos = str.find_first_not_of(args); |
Mike Fiore |
13:4709c2dfcd11 | 196 | if (startpos != std::string::npos) |
Mike Fiore |
13:4709c2dfcd11 | 197 | str = str.substr(startpos); |
Mike Fiore |
13:4709c2dfcd11 | 198 | } |
Mike Fiore |
13:4709c2dfcd11 | 199 | |
Mike Fiore |
13:4709c2dfcd11 | 200 | void Text::rtrim(std::string& str, const char* args) { |
Mike Fiore |
13:4709c2dfcd11 | 201 | size_t endpos = str.find_last_not_of(args); |
Mike Fiore |
13:4709c2dfcd11 | 202 | if (endpos != std::string::npos) |
Mike Fiore |
13:4709c2dfcd11 | 203 | str = str.substr(0, endpos + 1); |
Mike Fiore |
13:4709c2dfcd11 | 204 | } |
Mike Fiore |
13:4709c2dfcd11 | 205 | |
Mike Fiore |
13:4709c2dfcd11 | 206 | void Text::trim(std::string& str, const char* args) { |
Mike Fiore |
13:4709c2dfcd11 | 207 | ltrim(str, args); |
Mike Fiore |
13:4709c2dfcd11 | 208 | rtrim(str, args); |
Mike Fiore |
13:4709c2dfcd11 | 209 | } |