Mark Underwood / CmdLine

Dependents:   MAX5715BOB_Tester MAX11131BOB_Tester MAX5171BOB_Tester MAX11410BOB_Tester ... more

Committer:
whismanoid
Date:
Tue Nov 30 00:48:26 2021 -0800
Revision:
19:b14922500968
Parent:
18:2c3c44a34d81
Child:
20:92a1fb8a5732
cmdLine final key=value arg wrongly included in parse_byteCount_byteList_ buffer #358

Who changed what in which revision?

UserRevisionLine numberNew contents of line
whismanoid 0:4d7de8b5c800 1 // /*******************************************************************************
whismanoid 0:4d7de8b5c800 2 // * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
whismanoid 0:4d7de8b5c800 3 // *
whismanoid 0:4d7de8b5c800 4 // * Permission is hereby granted, free of charge, to any person obtaining a
whismanoid 0:4d7de8b5c800 5 // * copy of this software and associated documentation files (the "Software"),
whismanoid 0:4d7de8b5c800 6 // * to deal in the Software without restriction, including without limitation
whismanoid 0:4d7de8b5c800 7 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
whismanoid 0:4d7de8b5c800 8 // * and/or sell copies of the Software, and to permit persons to whom the
whismanoid 0:4d7de8b5c800 9 // * Software is furnished to do so, subject to the following conditions:
whismanoid 0:4d7de8b5c800 10 // *
whismanoid 0:4d7de8b5c800 11 // * The above copyright notice and this permission notice shall be included
whismanoid 0:4d7de8b5c800 12 // * in all copies or substantial portions of the Software.
whismanoid 0:4d7de8b5c800 13 // *
whismanoid 0:4d7de8b5c800 14 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
whismanoid 0:4d7de8b5c800 15 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
whismanoid 0:4d7de8b5c800 16 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
whismanoid 0:4d7de8b5c800 17 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
whismanoid 0:4d7de8b5c800 18 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
whismanoid 0:4d7de8b5c800 19 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
whismanoid 0:4d7de8b5c800 20 // * OTHER DEALINGS IN THE SOFTWARE.
whismanoid 0:4d7de8b5c800 21 // *
whismanoid 0:4d7de8b5c800 22 // * Except as contained in this notice, the name of Maxim Integrated
whismanoid 0:4d7de8b5c800 23 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
whismanoid 0:4d7de8b5c800 24 // * Products, Inc. Branding Policy.
whismanoid 0:4d7de8b5c800 25 // *
whismanoid 0:4d7de8b5c800 26 // * The mere transfer of this software does not imply any licenses
whismanoid 0:4d7de8b5c800 27 // * of trade secrets, proprietary technology, copyrights, patents,
whismanoid 0:4d7de8b5c800 28 // * trademarks, maskwork rights, or any other form of intellectual
whismanoid 0:4d7de8b5c800 29 // * property whatsoever. Maxim Integrated Products, Inc. retains all
whismanoid 0:4d7de8b5c800 30 // * ownership rights.
whismanoid 0:4d7de8b5c800 31 // *******************************************************************************
whismanoid 0:4d7de8b5c800 32 // */
whismanoid 0:4d7de8b5c800 33 // *********************************************************************
whismanoid 0:4d7de8b5c800 34 // @file CmdLine.cpp
whismanoid 0:4d7de8b5c800 35 // *********************************************************************
whismanoid 0:4d7de8b5c800 36
whismanoid 0:4d7de8b5c800 37 #include "CmdLine.h"
whismanoid 12:447a747099e6 38 // toupper requires include <ctype.h>
whismanoid 12:447a747099e6 39 #include <ctype.h>
whismanoid 0:4d7de8b5c800 40
whismanoid 0:4d7de8b5c800 41 CmdLine::CmdLine(Stream& AssociatedSerialPort, const char *Name)
whismanoid 0:4d7de8b5c800 42 : associatedSerialPort(AssociatedSerialPort)
whismanoid 0:4d7de8b5c800 43 , name(Name)
whismanoid 0:4d7de8b5c800 44 , onEOLcommandParser()
whismanoid 0:4d7de8b5c800 45 {
whismanoid 0:4d7de8b5c800 46 indexOfNextEmptyCell = 0;
whismanoid 0:4d7de8b5c800 47 memset(buf, 0, COMMAND_BUFFER_LENGTH);
whismanoid 10:3e2ff983be1c 48 chSeparator = 0;
whismanoid 10:3e2ff983be1c 49 match_is_case_sensitive = false;
whismanoid 15:34b039027e5f 50 quiet = false;
whismanoid 0:4d7de8b5c800 51 }
whismanoid 0:4d7de8b5c800 52 /** CmdLine::clear empties the command-line buffer */
whismanoid 0:4d7de8b5c800 53 void CmdLine::clear(void)
whismanoid 0:4d7de8b5c800 54 {
whismanoid 0:4d7de8b5c800 55 indexOfNextEmptyCell = 0;
whismanoid 0:4d7de8b5c800 56 memset(buf, 0, COMMAND_BUFFER_LENGTH);
whismanoid 0:4d7de8b5c800 57 }
whismanoid 0:4d7de8b5c800 58 //void CmdLine::idleAppendIfReadable()
whismanoid 0:4d7de8b5c800 59 //{
whismanoid 0:4d7de8b5c800 60 // // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 61 // // Polymorphism fail: associatedSerialPort.readable()
whismanoid 0:4d7de8b5c800 62 // if (associatedSerialPort.readable()) {
whismanoid 0:4d7de8b5c800 63 // append(associatedSerialPort.getc());
whismanoid 0:4d7de8b5c800 64 // //
whismanoid 0:4d7de8b5c800 65 // // TODO1: set EOL timeout, so that we don't get lingering buffer cruft
whismanoid 0:4d7de8b5c800 66 // //
whismanoid 0:4d7de8b5c800 67 // }
whismanoid 0:4d7de8b5c800 68 //}
whismanoid 0:4d7de8b5c800 69 /** CmdLine::append handles an input character by appending the buffer,
whismanoid 0:4d7de8b5c800 70 * or handling an immediate function like backspace/delete
whismanoid 0:4d7de8b5c800 71 * or other custom immediate motor control functions.
whismanoid 0:4d7de8b5c800 72 */
whismanoid 0:4d7de8b5c800 73 void CmdLine::append(char ch)
whismanoid 0:4d7de8b5c800 74 {
whismanoid 6:88e92f832c9a 75 // void diagnostic_led_EOF();
whismanoid 0:4d7de8b5c800 76 void main_menu_status(CmdLine & cmdLine);
whismanoid 0:4d7de8b5c800 77
whismanoid 0:4d7de8b5c800 78 // append ch to buf, unless BS or EOL or other editing character
whismanoid 0:4d7de8b5c800 79 switch (ch)
whismanoid 0:4d7de8b5c800 80 {
whismanoid 0:4d7de8b5c800 81 case '\b': // Unicode (U+0008) BS BACKSPACE as destructive backspace
whismanoid 0:4d7de8b5c800 82 case '\x7f': // Unicode (U+007F) DEL DELETE as destructive backspace
whismanoid 0:4d7de8b5c800 83 if (indexOfNextEmptyCell > 0)
whismanoid 0:4d7de8b5c800 84 {
whismanoid 0:4d7de8b5c800 85 buf[--indexOfNextEmptyCell] = '\0'; // pre-decrement index, overwrite with null
whismanoid 15:34b039027e5f 86 if (!quiet) associatedSerialPort.printf("\b \b"); // tty: backspace, overwrite with space, backspace
whismanoid 0:4d7de8b5c800 87 }
whismanoid 0:4d7de8b5c800 88 break;
whismanoid 0:4d7de8b5c800 89 case '\r': // Unicode (U+000D) CR CARRIAGE RETURN(CR) as EOL end of line
whismanoid 0:4d7de8b5c800 90 case '\n': // Unicode (U+000A) LF LINE FEED(LF) as EOL end of line
whismanoid 0:4d7de8b5c800 91 //associatedSerialPort.printf("%c", ch); // echo line end
whismanoid 15:34b039027e5f 92 if (!quiet) associatedSerialPort.printf("\r\n"); // echo line end
whismanoid 0:4d7de8b5c800 93 //~ associatedSerialPort.printf("\r\n~%s~\r\n", buf); // DIAGNOSTIC: print line buffer
whismanoid 0:4d7de8b5c800 94 // parse and handle the command by invoking onEOLcommandParser callback
whismanoid 0:4d7de8b5c800 95 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 96 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 97 }
whismanoid 0:4d7de8b5c800 98 clear();
whismanoid 0:4d7de8b5c800 99 break;
whismanoid 0:4d7de8b5c800 100 #define ECHO_EOF_IMMEDIATELY 1
whismanoid 0:4d7de8b5c800 101 #if ECHO_EOF_IMMEDIATELY
whismanoid 0:4d7de8b5c800 102 case '\x04': // Unicode (U+0004) EOT END OF TRANSMISSION = CTRL+D as EOF end of file
whismanoid 6:88e92f832c9a 103 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 0:4d7de8b5c800 104 //~ main_menu_status(*this);
whismanoid 15:34b039027e5f 105 if (!quiet) associatedSerialPort.printf("** U+0004 EOT = EOF **"); // immediately echo EOF for test scripting
whismanoid 6:88e92f832c9a 106 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 15:34b039027e5f 107 if (!quiet) associatedSerialPort.printf("\r\n\x04\r\n"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 108 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 109 //~ associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 110 //~ associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 111 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 112 break;
whismanoid 0:4d7de8b5c800 113 case '\x1a': // Unicode (U+001A) SUB SUBSTITUTE = CTRL+Z as EOF end of file
whismanoid 6:88e92f832c9a 114 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 0:4d7de8b5c800 115 //~ main_menu_status(*this);
whismanoid 15:34b039027e5f 116 if (!quiet) associatedSerialPort.printf("** U+001A SUB = EOF **"); // immediately echo EOF for test scripting
whismanoid 6:88e92f832c9a 117 if (diagnostic_led_EOF) { diagnostic_led_EOF(); }
whismanoid 15:34b039027e5f 118 if (!quiet) associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 15:34b039027e5f 119 if (!quiet) associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 15:34b039027e5f 120 if (!quiet) associatedSerialPort.printf("\x1a"); // immediately echo EOF for test scripting
whismanoid 15:34b039027e5f 121 if (!quiet) associatedSerialPort.printf("\x04"); // immediately echo EOF for test scripting
whismanoid 0:4d7de8b5c800 122 clear(); // EOF discard any pending commands, to avoid surprise
whismanoid 0:4d7de8b5c800 123 break;
whismanoid 0:4d7de8b5c800 124 #endif
whismanoid 0:4d7de8b5c800 125 //
whismanoid 0:4d7de8b5c800 126 // Support commands that get handled immediately w/o waiting for EOL
whismanoid 0:4d7de8b5c800 127 // Avoid using characters that may appear in other commands,
whismanoid 0:4d7de8b5c800 128 // such as 0-9 A-Z a-z and some punctuation %*+-./=
whismanoid 0:4d7de8b5c800 129 // so these 25 characters are available: !"#$&'(),:;<>?@[\]^_`{|}~
whismanoid 0:4d7de8b5c800 130 //case '!': // immediate command !) example
whismanoid 0:4d7de8b5c800 131 // do_some_immediate_action();
whismanoid 0:4d7de8b5c800 132 // clear();
whismanoid 0:4d7de8b5c800 133 // break;
whismanoid 0:4d7de8b5c800 134 //case ' ':
whismanoid 0:4d7de8b5c800 135 // on_immediate_0x20(); // Unicode (U+0020) SPACE
whismanoid 0:4d7de8b5c800 136 // break;
whismanoid 14:9abcdf4eb4e4 137 //
whismanoid 14:9abcdf4eb4e4 138 // Note: these on_immediate case may fall through, by design.
whismanoid 14:9abcdf4eb4e4 139 // #if __GNUC__
whismanoid 14:9abcdf4eb4e4 140 // #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
whismanoid 14:9abcdf4eb4e4 141 // #endif
whismanoid 14:9abcdf4eb4e4 142 #if 1
whismanoid 0:4d7de8b5c800 143 case '!':
whismanoid 1:5b33e7447601 144 //~ on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 145 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 146 if (on_immediate_0x21) {
whismanoid 1:5b33e7447601 147 on_immediate_0x21(); // Unicode (U+0021) ! EXCLAMATION MARK
whismanoid 1:5b33e7447601 148 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 149 }
whismanoid 0:4d7de8b5c800 150 case '{':
whismanoid 1:5b33e7447601 151 //~ on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 152 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 153 if (on_immediate_0x7b) {
whismanoid 1:5b33e7447601 154 on_immediate_0x7b(); // Unicode (U+007B) { LEFT CURLY BRACKET
whismanoid 1:5b33e7447601 155 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 156 }
whismanoid 0:4d7de8b5c800 157 case '}':
whismanoid 1:5b33e7447601 158 //~ on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 159 //~ break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 160 if (on_immediate_0x7d) {
whismanoid 1:5b33e7447601 161 on_immediate_0x7d(); // Unicode (U+007D) } RIGHT CURLY BRACKET
whismanoid 1:5b33e7447601 162 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 163 }
whismanoid 0:4d7de8b5c800 164 //
whismanoid 0:4d7de8b5c800 165 // default:
whismanoid 0:4d7de8b5c800 166 case '"':
whismanoid 1:5b33e7447601 167 if (on_immediate_0x22) {
whismanoid 1:5b33e7447601 168 on_immediate_0x22(); // Unicode (U+0022) " QUOTATION MARK
whismanoid 1:5b33e7447601 169 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 170 }
whismanoid 0:4d7de8b5c800 171 case '#':
whismanoid 1:5b33e7447601 172 if (on_immediate_0x23) {
whismanoid 1:5b33e7447601 173 on_immediate_0x23(); // Unicode (U+0023) # NUMBER SIGN = pound sign, hash, crosshatch
whismanoid 1:5b33e7447601 174 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 175 }
whismanoid 0:4d7de8b5c800 176 case '$':
whismanoid 1:5b33e7447601 177 if (on_immediate_0x24) {
whismanoid 1:5b33e7447601 178 on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 1:5b33e7447601 179 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 180 }
whismanoid 0:4d7de8b5c800 181 //~ on_immediate_0x24(); // Unicode (U+0024) $ DOLLAR SIGN
whismanoid 0:4d7de8b5c800 182 //~ break; // handled as immediate command, do not append to buffer
whismanoid 0:4d7de8b5c800 183 //case '%':
whismanoid 0:4d7de8b5c800 184 // on_immediate_0x25(); // Unicode (U+0025) % PERCENT SIGN
whismanoid 0:4d7de8b5c800 185 // break;
whismanoid 0:4d7de8b5c800 186 case '&':
whismanoid 1:5b33e7447601 187 if (on_immediate_0x26) {
whismanoid 1:5b33e7447601 188 on_immediate_0x26(); // Unicode (U+0026) & AMPERSAND
whismanoid 1:5b33e7447601 189 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 190 }
whismanoid 0:4d7de8b5c800 191 case '\'':
whismanoid 1:5b33e7447601 192 if (on_immediate_0x27) {
whismanoid 1:5b33e7447601 193 on_immediate_0x27(); // Unicode (U+0027) ' APOSTROPHE
whismanoid 1:5b33e7447601 194 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 195 }
whismanoid 0:4d7de8b5c800 196 case '(':
whismanoid 1:5b33e7447601 197 if (on_immediate_0x28) {
whismanoid 1:5b33e7447601 198 on_immediate_0x28(); // Unicode (U+0028) ( LEFT PARENTHESIS
whismanoid 1:5b33e7447601 199 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 200 }
whismanoid 0:4d7de8b5c800 201 case ')':
whismanoid 1:5b33e7447601 202 if (on_immediate_0x29) {
whismanoid 1:5b33e7447601 203 on_immediate_0x29(); // Unicode (U+0029) ) RIGHT PARENTHESIS
whismanoid 1:5b33e7447601 204 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 205 }
whismanoid 0:4d7de8b5c800 206 //case '*':
whismanoid 0:4d7de8b5c800 207 // on_immediate_0x2a(); // Unicode (U+002A) * ASTERISK
whismanoid 0:4d7de8b5c800 208 // break;
whismanoid 0:4d7de8b5c800 209 //case '+':
whismanoid 0:4d7de8b5c800 210 // on_immediate_0x2b(); // Unicode (U+002B) + PLUS SIGN
whismanoid 0:4d7de8b5c800 211 // break;
whismanoid 0:4d7de8b5c800 212 case ',':
whismanoid 1:5b33e7447601 213 if (on_immediate_0x2c) {
whismanoid 1:5b33e7447601 214 on_immediate_0x2c(); // Unicode (U+002C) , COMMA
whismanoid 1:5b33e7447601 215 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 216 }
whismanoid 0:4d7de8b5c800 217 //case '-':
whismanoid 0:4d7de8b5c800 218 // on_immediate_0x2d(); // Unicode (U+002D) - HYPHEN-MINUS
whismanoid 0:4d7de8b5c800 219 // break;
whismanoid 0:4d7de8b5c800 220 //case '.':
whismanoid 0:4d7de8b5c800 221 // on_immediate_0x2e(); // Unicode (U+002E) . FULL STOP
whismanoid 0:4d7de8b5c800 222 // break;
whismanoid 0:4d7de8b5c800 223 //case '/':
whismanoid 0:4d7de8b5c800 224 // on_immediate_0x2f(); // Unicode (U+002F) / SOLIDUS =SLASH
whismanoid 0:4d7de8b5c800 225 // break;
whismanoid 0:4d7de8b5c800 226 case ':':
whismanoid 1:5b33e7447601 227 if (on_immediate_0x3a) {
whismanoid 1:5b33e7447601 228 on_immediate_0x3a(); // Unicode (U+003A) : COLON
whismanoid 1:5b33e7447601 229 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 230 }
whismanoid 0:4d7de8b5c800 231 case ';':
whismanoid 1:5b33e7447601 232 if (on_immediate_0x3b) {
whismanoid 1:5b33e7447601 233 on_immediate_0x3b(); // Unicode (U+003B) ; SEMICOLON
whismanoid 1:5b33e7447601 234 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 235 }
whismanoid 0:4d7de8b5c800 236 case '<':
whismanoid 1:5b33e7447601 237 if (on_immediate_0x3c) {
whismanoid 1:5b33e7447601 238 on_immediate_0x3c(); // Unicode (U+003C) < LESS-THAN SIGN
whismanoid 1:5b33e7447601 239 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 240 }
whismanoid 0:4d7de8b5c800 241 //case '=':
whismanoid 0:4d7de8b5c800 242 // on_immediate_0x3d(); // Unicode (U+003D) = EQUALS SIGN
whismanoid 0:4d7de8b5c800 243 // break;
whismanoid 0:4d7de8b5c800 244 case '>':
whismanoid 1:5b33e7447601 245 if (on_immediate_0x3e) {
whismanoid 1:5b33e7447601 246 on_immediate_0x3e(); // Unicode (U+003E) > GREATER-THAN SIGN
whismanoid 1:5b33e7447601 247 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 248 }
whismanoid 0:4d7de8b5c800 249 case '?':
whismanoid 1:5b33e7447601 250 if (on_immediate_0x3f) {
whismanoid 1:5b33e7447601 251 on_immediate_0x3f(); // Unicode (U+003F) ? QUESTION MARK
whismanoid 1:5b33e7447601 252 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 253 }
whismanoid 0:4d7de8b5c800 254 case '@':
whismanoid 1:5b33e7447601 255 if (on_immediate_0x40) {
whismanoid 1:5b33e7447601 256 on_immediate_0x40(); // Unicode (U+0040) @ COMMERCIAL AT = at sign
whismanoid 1:5b33e7447601 257 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 258 }
whismanoid 0:4d7de8b5c800 259 case '[':
whismanoid 1:5b33e7447601 260 if (on_immediate_0x5b) {
whismanoid 1:5b33e7447601 261 on_immediate_0x5b(); // Unicode (U+005B) [ LEFT SQUARE BRACKET
whismanoid 1:5b33e7447601 262 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 263 }
whismanoid 0:4d7de8b5c800 264 case '\\':
whismanoid 1:5b33e7447601 265 if (on_immediate_0x5c) {
whismanoid 1:5b33e7447601 266 on_immediate_0x5c(); // Unicode (U+005C) \ REVERSE SOLIDUS
whismanoid 1:5b33e7447601 267 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 268 }
whismanoid 0:4d7de8b5c800 269 case ']':
whismanoid 1:5b33e7447601 270 if (on_immediate_0x5d) {
whismanoid 1:5b33e7447601 271 on_immediate_0x5d(); // Unicode (U+005D) ] RIGHT SQUARE BRACKET
whismanoid 1:5b33e7447601 272 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 273 }
whismanoid 0:4d7de8b5c800 274 case '^':
whismanoid 1:5b33e7447601 275 if (on_immediate_0x5e) {
whismanoid 1:5b33e7447601 276 on_immediate_0x5e(); // Unicode (U+005E) ^ CIRCUMFLEX ACCENT
whismanoid 1:5b33e7447601 277 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 278 }
whismanoid 0:4d7de8b5c800 279 case '_':
whismanoid 1:5b33e7447601 280 if (on_immediate_0x5f) {
whismanoid 1:5b33e7447601 281 on_immediate_0x5f(); // Unicode (U+005F) _ LOW LINE =SPACING UNDERSCORE
whismanoid 1:5b33e7447601 282 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 283 }
whismanoid 0:4d7de8b5c800 284 case '`':
whismanoid 1:5b33e7447601 285 if (on_immediate_0x60) {
whismanoid 1:5b33e7447601 286 on_immediate_0x60(); // Unicode (U+0060) ` GRAVE ACCENT (also called backtick)
whismanoid 1:5b33e7447601 287 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 288 }
whismanoid 0:4d7de8b5c800 289 case '|':
whismanoid 1:5b33e7447601 290 if (on_immediate_0x7c) {
whismanoid 1:5b33e7447601 291 on_immediate_0x7c(); // Unicode (U+007C) | VERTICAL LINE
whismanoid 1:5b33e7447601 292 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 293 }
whismanoid 0:4d7de8b5c800 294 case '~':
whismanoid 1:5b33e7447601 295 if (on_immediate_0x7e) {
whismanoid 1:5b33e7447601 296 on_immediate_0x7e(); // Unicode (U+007E) ~ TILDE
whismanoid 1:5b33e7447601 297 break; // handled as immediate command, do not append to buffer
whismanoid 1:5b33e7447601 298 }
whismanoid 14:9abcdf4eb4e4 299 #endif
whismanoid 14:9abcdf4eb4e4 300 // #if __GNUC__
whismanoid 14:9abcdf4eb4e4 301 // #pragma GCC diagnostic pop
whismanoid 14:9abcdf4eb4e4 302 // #endif
whismanoid 14:9abcdf4eb4e4 303 // Note: these on_immediate case may fall through, by design.
whismanoid 0:4d7de8b5c800 304 //
whismanoid 0:4d7de8b5c800 305 default:
whismanoid 0:4d7de8b5c800 306 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 2);
whismanoid 0:4d7de8b5c800 307 buf[indexOfNextEmptyCell++] = ch; // append character, post-increment index
whismanoid 0:4d7de8b5c800 308 buf[indexOfNextEmptyCell] = '\0'; // null-terminate the buffer
whismanoid 0:4d7de8b5c800 309 MBED_ASSERT(indexOfNextEmptyCell <= COMMAND_BUFFER_LENGTH - 1);
whismanoid 15:34b039027e5f 310 if (!quiet) associatedSerialPort.printf("%c", ch); // echo
whismanoid 0:4d7de8b5c800 311 if (indexOfNextEmptyCell == COMMAND_BUFFER_LENGTH - 1)
whismanoid 0:4d7de8b5c800 312 {
whismanoid 0:4d7de8b5c800 313 // buffer is full, parse what we've got
whismanoid 0:4d7de8b5c800 314 if (onEOLcommandParser) {
whismanoid 0:4d7de8b5c800 315 onEOLcommandParser(*this);
whismanoid 0:4d7de8b5c800 316 }
whismanoid 0:4d7de8b5c800 317 clear();
whismanoid 0:4d7de8b5c800 318 }
whismanoid 0:4d7de8b5c800 319 break;
whismanoid 0:4d7de8b5c800 320 }
whismanoid 0:4d7de8b5c800 321 }
whismanoid 1:5b33e7447601 322
whismanoid 0:4d7de8b5c800 323 /** CmdLine::parse_and_remove_key matches "key"
whismanoid 0:4d7de8b5c800 324 *
whismanoid 0:4d7de8b5c800 325 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 326 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 327 * @param[out] valueBuf buffer is populated with the substring between key= and the first space delimiter (or end of string)
whismanoid 0:4d7de8b5c800 328 * @param[in] valueBufLen limits the size of valueBuf
whismanoid 0:4d7de8b5c800 329 *
whismanoid 0:4d7de8b5c800 330 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 331 * @post chSeparator is populated with the separator character '=' or '?' following key=, or null character if no separator
whismanoid 0:4d7de8b5c800 332 *
whismanoid 0:4d7de8b5c800 333 */
whismanoid 0:4d7de8b5c800 334 bool CmdLine::parse_and_remove_key(const char *key, char *valueBuf, size_t valueBufLen)
whismanoid 0:4d7de8b5c800 335 {
whismanoid 17:371f0c5956da 336 bool verbose = false;
whismanoid 17:371f0c5956da 337 if (verbose) { serial().printf("\r\n parse_and_remove_key(\"%s\")...", key); }
whismanoid 0:4d7de8b5c800 338 // match key inside buf[]?
whismanoid 0:4d7de8b5c800 339 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 340 {
whismanoid 0:4d7de8b5c800 341 if (buf[idxSearch] == '\0') {
whismanoid 17:371f0c5956da 342 if (verbose) { serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key); }
whismanoid 0:4d7de8b5c800 343 return false; /* no match */
whismanoid 0:4d7de8b5c800 344 }
whismanoid 10:3e2ff983be1c 345 if (match_is_case_sensitive) {
whismanoid 11:e8a4162d4fd1 346 // case-sensitive string comparison
whismanoid 10:3e2ff983be1c 347 if (buf[idxSearch] != key[0]) { continue; }
whismanoid 10:3e2ff983be1c 348 }
whismanoid 10:3e2ff983be1c 349 else {
whismanoid 11:e8a4162d4fd1 350 // case-insensitive string comparison using toupper()
whismanoid 10:3e2ff983be1c 351 if (toupper(buf[idxSearch]) != toupper(key[0])) { continue; }
whismanoid 10:3e2ff983be1c 352 }
whismanoid 0:4d7de8b5c800 353 // possible match; compare buf[idxSearch..] to key[0..]
whismanoid 0:4d7de8b5c800 354 unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
whismanoid 9:12e37800ecdd 355 unsigned int idxSeparator = idxSearch; // test whether key=value pair
whismanoid 0:4d7de8b5c800 356 unsigned int idxSpace = idxSearch; // end of key=value word
whismanoid 0:4d7de8b5c800 357 for (unsigned int offset = 0; offset < strlen(key); offset++)
whismanoid 0:4d7de8b5c800 358 {
whismanoid 11:e8a4162d4fd1 359 if (match_is_case_sensitive) {
whismanoid 11:e8a4162d4fd1 360 // case-sensitive string comparison
whismanoid 11:e8a4162d4fd1 361 if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
whismanoid 11:e8a4162d4fd1 362 }
whismanoid 11:e8a4162d4fd1 363 else {
whismanoid 11:e8a4162d4fd1 364 // case-insensitive string comparison using toupper()
whismanoid 11:e8a4162d4fd1 365 if (toupper(buf[idxKey + offset]) != toupper(key[offset])) { idxKey = 0; break; }
whismanoid 11:e8a4162d4fd1 366 }
whismanoid 9:12e37800ecdd 367 idxSpace = idxKey + offset + 1; // assume next char is a word break
whismanoid 9:12e37800ecdd 368 idxSeparator = idxKey + offset + 1; // assume next char is a separator
whismanoid 9:12e37800ecdd 369 if ((buf[idxSeparator] != '=') && (buf[idxSeparator] != '?')) { idxSeparator = 0; }
whismanoid 0:4d7de8b5c800 370 }
whismanoid 0:4d7de8b5c800 371 if (idxKey == 0) continue; // no match at idxSearch but keep searching
whismanoid 0:4d7de8b5c800 372 // ASSERT buf[idxKey..] == key[0..]
whismanoid 0:4d7de8b5c800 373 while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
whismanoid 17:371f0c5956da 374 if (verbose) {
whismanoid 17:371f0c5956da 375 serial().printf("\r\n parse_and_remove_key(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
whismanoid 17:371f0c5956da 376 key, idxKey, strlen(key), idxSeparator, idxSpace);
whismanoid 17:371f0c5956da 377 }
whismanoid 9:12e37800ecdd 378 if (idxSeparator != 0) {
whismanoid 9:12e37800ecdd 379 // found key=value: copy buf[idxSeparator+1..' '-1] into valueBuf[0..valueBufLen-1]
whismanoid 9:12e37800ecdd 380 chSeparator = buf[idxSeparator];
whismanoid 0:4d7de8b5c800 381 for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
whismanoid 0:4d7de8b5c800 382 {
whismanoid 9:12e37800ecdd 383 if (buf[idxSeparator + 1 + offset] == ' ') break;
whismanoid 9:12e37800ecdd 384 valueBuf[offset] = buf[idxSeparator + 1 + offset];
whismanoid 0:4d7de8b5c800 385 valueBuf[offset + 1] = '\0';
whismanoid 0:4d7de8b5c800 386 }
whismanoid 0:4d7de8b5c800 387 } else {
whismanoid 0:4d7de8b5c800 388 // found key but no =value: valueBuf[] = ""
whismanoid 9:12e37800ecdd 389 chSeparator = '\0';
whismanoid 0:4d7de8b5c800 390 valueBuf[0] = '\0';
whismanoid 0:4d7de8b5c800 391 }
whismanoid 0:4d7de8b5c800 392 // on successful match, the key=value should be deleted from cmdbuf
whismanoid 17:371f0c5956da 393 if (verbose) {
whismanoid 17:371f0c5956da 394 serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" before deleting key",
whismanoid 17:371f0c5956da 395 key, buf, valueBuf);
whismanoid 17:371f0c5956da 396 }
whismanoid 0:4d7de8b5c800 397 for (unsigned int offset = 0; offset < indexOfNextEmptyCell; offset++)
whismanoid 0:4d7de8b5c800 398 {
whismanoid 0:4d7de8b5c800 399 unsigned int idxCopyDst = idxKey + offset;
whismanoid 0:4d7de8b5c800 400 unsigned int idxCopySrc = idxSpace + 1 + offset;
whismanoid 0:4d7de8b5c800 401 if (idxCopyDst > indexOfNextEmptyCell) break;
whismanoid 19:b14922500968 402 // cmdLine final key=value arg wrongly included in parse_byteCount_byteList_ buffer #358
whismanoid 19:b14922500968 403 // if (idxCopySrc > indexOfNextEmptyCell) break;
whismanoid 19:b14922500968 404 if (idxCopySrc > indexOfNextEmptyCell)
whismanoid 19:b14922500968 405 {
whismanoid 19:b14922500968 406 buf[idxCopyDst] = ' ';
whismanoid 19:b14922500968 407 continue;
whismanoid 19:b14922500968 408 }
whismanoid 0:4d7de8b5c800 409 buf[idxCopyDst] = buf[idxCopySrc];
whismanoid 0:4d7de8b5c800 410 }
whismanoid 17:371f0c5956da 411 if (verbose) {
whismanoid 17:371f0c5956da 412 serial().printf("\r\n parse_and_remove_key(\"%s\") buf=\"%s\" valueBuf=\"%s\" after deleting key",
whismanoid 17:371f0c5956da 413 key, buf, valueBuf);
whismanoid 17:371f0c5956da 414 serial().printf("\r\n parse_and_remove_key(\"%s\") returning true: valueBuf=\"%s\"", key, valueBuf);
whismanoid 17:371f0c5956da 415 }
whismanoid 0:4d7de8b5c800 416 return true;
whismanoid 0:4d7de8b5c800 417 }
whismanoid 17:371f0c5956da 418 if (verbose) { serial().printf("\r\n parse_and_remove_key(\"%s\") no match", key); }
whismanoid 17:371f0c5956da 419 return false; // no match
whismanoid 17:371f0c5956da 420 }
whismanoid 17:371f0c5956da 421
whismanoid 17:371f0c5956da 422 /** CmdLine::parse_and_remove_key_and_arrayIndex matches "key" with array indexing
whismanoid 17:371f0c5956da 423 *
whismanoid 17:371f0c5956da 424 * @return true if keyword was found in buffer
whismanoid 17:371f0c5956da 425 * @param[in] key string value to match
whismanoid 17:371f0c5956da 426 * @param[out] valueBuf buffer is populated with the substring between key= and the first space delimiter (or end of string)
whismanoid 17:371f0c5956da 427 * @param[in] valueBufLen limits the size of valueBuf
whismanoid 17:371f0c5956da 428 *
whismanoid 17:371f0c5956da 429 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 17:371f0c5956da 430 * @post chSeparator is populated with the separator character '=' or '?' following key=, or null character if no separator
whismanoid 17:371f0c5956da 431 * @post *pindex is populated with the array index value
whismanoid 17:371f0c5956da 432 *
whismanoid 17:371f0c5956da 433 */
whismanoid 17:371f0c5956da 434 bool CmdLine::parse_and_remove_key_and_arrayIndex(const char *key, size_t *pindex, char *valueBuf, size_t valueBufLen)
whismanoid 17:371f0c5956da 435 {
whismanoid 17:371f0c5956da 436 // WIP #347 - parse_and_remove_key_and_arrayIndex(const char *key, size_t *pindex, char *valueBuf, int valueBufLen) based on parse_and_remove_key
whismanoid 17:371f0c5956da 437 // Work in Progress
whismanoid 17:371f0c5956da 438 // match (key) "[" (index:[0-9]+) "]"
whismanoid 17:371f0c5956da 439 //
whismanoid 17:371f0c5956da 440 bool verbose = false;
whismanoid 17:371f0c5956da 441 if (verbose) { serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\")...", key); }
whismanoid 17:371f0c5956da 442 // match key inside buf[]?
whismanoid 17:371f0c5956da 443 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 17:371f0c5956da 444 {
whismanoid 17:371f0c5956da 445 if (buf[idxSearch] == '\0') {
whismanoid 17:371f0c5956da 446 if (verbose) { serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") no match", key); }
whismanoid 17:371f0c5956da 447 return false; /* no match */
whismanoid 17:371f0c5956da 448 }
whismanoid 17:371f0c5956da 449 if (match_is_case_sensitive) {
whismanoid 17:371f0c5956da 450 // case-sensitive string comparison
whismanoid 17:371f0c5956da 451 if (buf[idxSearch] != key[0]) { continue; }
whismanoid 17:371f0c5956da 452 }
whismanoid 17:371f0c5956da 453 else {
whismanoid 17:371f0c5956da 454 // case-insensitive string comparison using toupper()
whismanoid 17:371f0c5956da 455 if (toupper(buf[idxSearch]) != toupper(key[0])) { continue; }
whismanoid 17:371f0c5956da 456 }
whismanoid 17:371f0c5956da 457 // possible match; compare buf[idxSearch..] to key[0..]
whismanoid 17:371f0c5956da 458 unsigned int idxKey = idxSearch; // test whether buf[idxKey..] == key[0..]
whismanoid 17:371f0c5956da 459 unsigned int idxBL = 0; // find such that buf[idxBL] == '[' // bracket left
whismanoid 17:371f0c5956da 460 unsigned int idxBR = 0; // find such that buf[idxBR] == ']' // bracket right
whismanoid 17:371f0c5956da 461 unsigned int idxSeparator = idxSearch; // test whether key=value pair
whismanoid 17:371f0c5956da 462 unsigned int idxSpace = idxSearch; // end of key=value word
whismanoid 17:371f0c5956da 463 for (unsigned int offset = 0; offset < strlen(key); offset++)
whismanoid 17:371f0c5956da 464 {
whismanoid 17:371f0c5956da 465 if (verbose) {
whismanoid 17:371f0c5956da 466 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") searching idxKey=%d + offset=%d buf[%d]='%c'",
whismanoid 17:371f0c5956da 467 key, idxKey, offset, (idxKey + offset), buf[idxKey + offset]);
whismanoid 17:371f0c5956da 468 }
whismanoid 17:371f0c5956da 469 //
whismanoid 17:371f0c5956da 470 // WIP #347 - handle brackets idxBL '[' idxBR ']'
whismanoid 17:371f0c5956da 471 if (buf[idxKey + offset] == '[') {
whismanoid 17:371f0c5956da 472 idxBL = idxKey + offset;
whismanoid 17:371f0c5956da 473 if (verbose) {
whismanoid 17:371f0c5956da 474 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") found '[' at index idxBL=%d", key, idxBL);
whismanoid 17:371f0c5956da 475 }
whismanoid 17:371f0c5956da 476 }
whismanoid 17:371f0c5956da 477 //
whismanoid 17:371f0c5956da 478 // WIP #347 - handle brackets idxBL '[' idxBR ']'
whismanoid 17:371f0c5956da 479 //if (buf[idxKey + offset] == ']') {
whismanoid 17:371f0c5956da 480 // idxBR = idxKey + offset;
whismanoid 17:371f0c5956da 481 // if (verbose) {
whismanoid 17:371f0c5956da 482 // serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") found ']' at index idxBR=%d", key, idxBR);
whismanoid 17:371f0c5956da 483 // }
whismanoid 17:371f0c5956da 484 //}
whismanoid 17:371f0c5956da 485 //
whismanoid 17:371f0c5956da 486 if (match_is_case_sensitive) {
whismanoid 17:371f0c5956da 487 // case-sensitive string comparison
whismanoid 17:371f0c5956da 488 if (buf[idxKey + offset] != key[offset]) { idxKey = 0; break; }
whismanoid 17:371f0c5956da 489 }
whismanoid 17:371f0c5956da 490 else {
whismanoid 17:371f0c5956da 491 // case-insensitive string comparison using toupper()
whismanoid 17:371f0c5956da 492 if (toupper(buf[idxKey + offset]) != toupper(key[offset])) { idxKey = 0; break; }
whismanoid 17:371f0c5956da 493 }
whismanoid 17:371f0c5956da 494 idxSpace = idxKey + offset + 1; // assume next char is a word break
whismanoid 17:371f0c5956da 495 idxSeparator = idxKey + offset + 1; // assume next char is a separator
whismanoid 17:371f0c5956da 496 //
whismanoid 17:371f0c5956da 497 if ((buf[idxSeparator] != '=') && (buf[idxSeparator] != '?') && (buf[idxSeparator] != '[') && (buf[idxSeparator] != ']')) { idxSeparator = 0; }
whismanoid 17:371f0c5956da 498 }
whismanoid 17:371f0c5956da 499 if (idxKey == 0) continue; // no match at idxSearch but keep searching
whismanoid 17:371f0c5956da 500 // ASSERT buf[idxKey..] == key[0..]
whismanoid 17:371f0c5956da 501 while ((buf[idxSpace] != ' ') && idxSpace < indexOfNextEmptyCell) { idxSpace++; }
whismanoid 17:371f0c5956da 502 if (verbose) {
whismanoid 17:371f0c5956da 503 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") match at index %d length %d, '=' index %d, ' ' index %d",
whismanoid 17:371f0c5956da 504 key, idxKey, strlen(key), idxSeparator, idxSpace);
whismanoid 17:371f0c5956da 505 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") buf[idxSeparator=%d]='%c'",
whismanoid 17:371f0c5956da 506 key, idxSeparator, buf[idxSeparator]);
whismanoid 17:371f0c5956da 507 //~ serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") idxBL=%d idxBR=%d",
whismanoid 17:371f0c5956da 508 //~ key, idxBL, idxBR);
whismanoid 17:371f0c5956da 509 //~ serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") [ index= %s ]", key, buf[idxBracket+1] );
whismanoid 17:371f0c5956da 510 }
whismanoid 17:371f0c5956da 511 // WIP #347 - handle brackets idxBL '[' idxBR ']'
whismanoid 17:371f0c5956da 512 if (buf[idxSeparator] == '[') {
whismanoid 17:371f0c5956da 513 idxBL = idxSeparator;
whismanoid 17:371f0c5956da 514 if (verbose) {
whismanoid 17:371f0c5956da 515 if (verbose) {
whismanoid 17:371f0c5956da 516 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") key[... continuing search from idxKey=%d + offset=%d buf[%d]='%c'",
whismanoid 17:371f0c5956da 517 key, idxKey, idxSeparator + 1, (idxKey + idxSeparator + 1), buf[idxKey + idxSeparator + 1]);
whismanoid 17:371f0c5956da 518 }
whismanoid 17:371f0c5956da 519 //~ serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") idxBL=%d idxBR=%d",
whismanoid 17:371f0c5956da 520 //~ key, idxBL, idxBR);
whismanoid 17:371f0c5956da 521 //~ serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") [ index= %s ]", key, buf[idxBracket+1] );
whismanoid 17:371f0c5956da 522 }
whismanoid 17:371f0c5956da 523 for (unsigned int idxBRsearch = idxBL + 1; (buf[idxBRsearch] != '\0'); idxBRsearch++)
whismanoid 17:371f0c5956da 524 {
whismanoid 17:371f0c5956da 525 if (verbose) {
whismanoid 17:371f0c5956da 526 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") key[... searching buf[idxBRsearch=%d]='%c'",
whismanoid 17:371f0c5956da 527 key, idxBRsearch, buf[idxBRsearch]);
whismanoid 17:371f0c5956da 528 }
whismanoid 17:371f0c5956da 529 if (buf[idxBRsearch] == '=') {
whismanoid 17:371f0c5956da 530 idxSeparator = idxBRsearch;
whismanoid 17:371f0c5956da 531 idxSpace = idxBRsearch;
whismanoid 17:371f0c5956da 532 break;
whismanoid 17:371f0c5956da 533 }
whismanoid 17:371f0c5956da 534 else if (buf[idxBRsearch] == ']') {
whismanoid 17:371f0c5956da 535 idxBR = idxBRsearch;
whismanoid 17:371f0c5956da 536 idxSeparator = idxBRsearch;
whismanoid 17:371f0c5956da 537 idxSpace = idxBRsearch;
whismanoid 17:371f0c5956da 538 //~ break;
whismanoid 17:371f0c5956da 539 }
whismanoid 17:371f0c5956da 540 else if (buf[idxBRsearch] == '?') {
whismanoid 17:371f0c5956da 541 idxSeparator = idxBRsearch;
whismanoid 17:371f0c5956da 542 idxSpace = idxBRsearch;
whismanoid 17:371f0c5956da 543 break;
whismanoid 17:371f0c5956da 544 }
whismanoid 17:371f0c5956da 545 else {
whismanoid 17:371f0c5956da 546 idxSeparator = 0;
whismanoid 17:371f0c5956da 547 //~ break;
whismanoid 17:371f0c5956da 548 }
whismanoid 17:371f0c5956da 549 }
whismanoid 17:371f0c5956da 550 if (verbose) {
whismanoid 17:371f0c5956da 551 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") idxBL=%d idxBR=%d",
whismanoid 17:371f0c5956da 552 key, idxBL, idxBR);
whismanoid 17:371f0c5956da 553 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") buf[idxSeparator=%d]='%c'",
whismanoid 17:371f0c5956da 554 key, idxSeparator, buf[idxSeparator]);
whismanoid 17:371f0c5956da 555 //~ serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") [ index= %s ]", key, buf[idxBracket+1] );
whismanoid 17:371f0c5956da 556 }
whismanoid 17:371f0c5956da 557 int arrayIndexValue = strtoul(&(buf[idxBL + 1]), NULL, 10); // default radix decimal
whismanoid 17:371f0c5956da 558 if (verbose) {
whismanoid 17:371f0c5956da 559 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") arrayIndexValue=%d",
whismanoid 17:371f0c5956da 560 key, arrayIndexValue);
whismanoid 17:371f0c5956da 561 }
whismanoid 17:371f0c5956da 562 if (pindex != NULL) {
whismanoid 17:371f0c5956da 563 *pindex = arrayIndexValue;
whismanoid 17:371f0c5956da 564 }
whismanoid 17:371f0c5956da 565 }
whismanoid 17:371f0c5956da 566 if (idxSeparator != 0) {
whismanoid 17:371f0c5956da 567 // found key=value: copy buf[idxSeparator+1..' '-1] into valueBuf[0..valueBufLen-1]
whismanoid 17:371f0c5956da 568 chSeparator = buf[idxSeparator];
whismanoid 17:371f0c5956da 569 for (unsigned int offset = 0; offset < valueBufLen - 1; offset++)
whismanoid 17:371f0c5956da 570 {
whismanoid 17:371f0c5956da 571 if (buf[idxSeparator + 1 + offset] == ' ') break;
whismanoid 17:371f0c5956da 572 valueBuf[offset] = buf[idxSeparator + 1 + offset];
whismanoid 17:371f0c5956da 573 valueBuf[offset + 1] = '\0';
whismanoid 17:371f0c5956da 574 }
whismanoid 17:371f0c5956da 575 } else {
whismanoid 17:371f0c5956da 576 // found key but no =value: valueBuf[] = ""
whismanoid 17:371f0c5956da 577 chSeparator = '\0';
whismanoid 17:371f0c5956da 578 valueBuf[0] = '\0';
whismanoid 17:371f0c5956da 579 }
whismanoid 17:371f0c5956da 580 // on successful match, the key=value should be deleted from cmdbuf
whismanoid 17:371f0c5956da 581 if (verbose) {
whismanoid 17:371f0c5956da 582 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") *pindex=%d buf=\"%s\" valueBuf=\"%s\" before deleting key",
whismanoid 17:371f0c5956da 583 key, *pindex, buf, valueBuf);
whismanoid 17:371f0c5956da 584 }
whismanoid 17:371f0c5956da 585 for (unsigned int offset = 0; offset < indexOfNextEmptyCell; offset++)
whismanoid 17:371f0c5956da 586 {
whismanoid 17:371f0c5956da 587 unsigned int idxCopyDst = idxKey + offset;
whismanoid 17:371f0c5956da 588 unsigned int idxCopySrc = idxSpace + 1 + offset;
whismanoid 17:371f0c5956da 589 if (idxCopyDst > indexOfNextEmptyCell) break;
whismanoid 19:b14922500968 590 // cmdLine final key=value arg wrongly included in parse_byteCount_byteList_ buffer #358
whismanoid 19:b14922500968 591 // if (idxCopySrc > indexOfNextEmptyCell) break;
whismanoid 19:b14922500968 592 if (idxCopySrc > indexOfNextEmptyCell)
whismanoid 19:b14922500968 593 {
whismanoid 19:b14922500968 594 buf[idxCopyDst] = ' ';
whismanoid 19:b14922500968 595 continue;
whismanoid 19:b14922500968 596 }
whismanoid 17:371f0c5956da 597 buf[idxCopyDst] = buf[idxCopySrc];
whismanoid 17:371f0c5956da 598 }
whismanoid 17:371f0c5956da 599 if (verbose) {
whismanoid 17:371f0c5956da 600 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") *pindex=%d buf=\"%s\" valueBuf=\"%s\" after deleting key",
whismanoid 17:371f0c5956da 601 key, *pindex, buf, valueBuf);
whismanoid 17:371f0c5956da 602 serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") returning true: valueBuf=\"%s\"", key, valueBuf);
whismanoid 17:371f0c5956da 603 }
whismanoid 17:371f0c5956da 604 return true;
whismanoid 17:371f0c5956da 605 }
whismanoid 17:371f0c5956da 606 if (verbose) { serial().printf("\r\n parse_and_remove_key_and_arrayIndex(\"%s\") no match", key); }
whismanoid 0:4d7de8b5c800 607 return false; // no match
whismanoid 0:4d7de8b5c800 608 }
whismanoid 0:4d7de8b5c800 609
whismanoid 0:4d7de8b5c800 610 /** CmdLine::parse_frequency_Hz matches "key"=digits
whismanoid 0:4d7de8b5c800 611 *
whismanoid 0:4d7de8b5c800 612 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 613 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 614 * @param[out] frequency_Hz updated from value string if match "key"=value,
whismanoid 0:4d7de8b5c800 615 * optional suffix kHz or MHz scales the value by 1000 or 10000000
whismanoid 0:4d7de8b5c800 616 *
whismanoid 0:4d7de8b5c800 617 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 618 *
whismanoid 0:4d7de8b5c800 619 */
whismanoid 0:4d7de8b5c800 620 bool CmdLine::parse_frequency_Hz(const char *key, uint32_t& frequency_Hz)
whismanoid 0:4d7de8b5c800 621 {
whismanoid 0:4d7de8b5c800 622 char valueBuf[16];
whismanoid 0:4d7de8b5c800 623 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 624 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 625 {
whismanoid 0:4d7de8b5c800 626 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 627 // parse cmdLine arg (SCLK=\d+(kHZ|MHZ)?)? --> g_SPI_SCLK_Hz
whismanoid 0:4d7de8b5c800 628 frequency_Hz = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 629 if (strstr(valueBuf, "M")) {
whismanoid 0:4d7de8b5c800 630 frequency_Hz = frequency_Hz * 1000000;
whismanoid 0:4d7de8b5c800 631 }
whismanoid 0:4d7de8b5c800 632 if (strstr(valueBuf, "k")) {
whismanoid 0:4d7de8b5c800 633 frequency_Hz = frequency_Hz * 1000;
whismanoid 0:4d7de8b5c800 634 }
whismanoid 0:4d7de8b5c800 635 return true;
whismanoid 0:4d7de8b5c800 636 }
whismanoid 0:4d7de8b5c800 637 return false; // no match
whismanoid 0:4d7de8b5c800 638 }
whismanoid 0:4d7de8b5c800 639
whismanoid 4:700b71cd3bd2 640 /** CmdLine::parse_interval_usec matches "key"=digits
whismanoid 4:700b71cd3bd2 641 *
whismanoid 4:700b71cd3bd2 642 * @return true if keyword was found in buffer
whismanoid 4:700b71cd3bd2 643 * @param[in] key string value to match
whismanoid 4:700b71cd3bd2 644 * @param[out] interval_usec updated from value string if match "key"=value,
whismanoid 4:700b71cd3bd2 645 * optional suffix Hz kHz or MHz 1/x inverts and scales the value
whismanoid 4:700b71cd3bd2 646 * optional suffix s or ms or msec or us or usec scales the value
whismanoid 4:700b71cd3bd2 647 *
whismanoid 4:700b71cd3bd2 648 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 4:700b71cd3bd2 649 *
whismanoid 4:700b71cd3bd2 650 */
whismanoid 4:700b71cd3bd2 651 bool CmdLine::parse_interval_usec(const char *key, us_timestamp_t& interval_usec)
whismanoid 4:700b71cd3bd2 652 {
whismanoid 4:700b71cd3bd2 653 char valueBuf[32];
whismanoid 4:700b71cd3bd2 654 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 4:700b71cd3bd2 655 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 4:700b71cd3bd2 656 {
whismanoid 4:700b71cd3bd2 657 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 4:700b71cd3bd2 658 interval_usec = strtoul(valueBuf, NULL, 10);
whismanoid 4:700b71cd3bd2 659 if (strstr(valueBuf, "ms")) {
whismanoid 4:700b71cd3bd2 660 interval_usec = interval_usec * 1000;
whismanoid 5:1a14de1c4d7b 661 } else if (strstr(valueBuf, "us")) {
whismanoid 5:1a14de1c4d7b 662 // avoid matching "s" which is a subset of "us"
whismanoid 5:1a14de1c4d7b 663 // interval_usec = interval_usec * 1;
whismanoid 4:700b71cd3bd2 664 } else if (strstr(valueBuf, "s")) {
whismanoid 4:700b71cd3bd2 665 interval_usec = interval_usec * 1000000;
whismanoid 4:700b71cd3bd2 666 } else if (strstr(valueBuf, "MHz")) {
whismanoid 4:700b71cd3bd2 667 interval_usec = 1. / interval_usec;
whismanoid 4:700b71cd3bd2 668 } else if (strstr(valueBuf, "kHz")) {
whismanoid 4:700b71cd3bd2 669 interval_usec = 1000. / interval_usec;
whismanoid 4:700b71cd3bd2 670 } else if (strstr(valueBuf, "Hz")) {
whismanoid 4:700b71cd3bd2 671 interval_usec = 1000000. / interval_usec;
whismanoid 4:700b71cd3bd2 672 }
whismanoid 4:700b71cd3bd2 673 return true;
whismanoid 4:700b71cd3bd2 674 }
whismanoid 4:700b71cd3bd2 675 return false; // no match
whismanoid 4:700b71cd3bd2 676 }
whismanoid 4:700b71cd3bd2 677
whismanoid 0:4d7de8b5c800 678 /** CmdLine::parse_flag matches "key"=0 or 1
whismanoid 0:4d7de8b5c800 679 *
whismanoid 0:4d7de8b5c800 680 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 681 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 682 * @param[out] nFlagVar variable to be updated by setting or clearing bits specified by nFlagBitMask.
whismanoid 0:4d7de8b5c800 683 * If match "key"=0 then the nFlagBitMask bits in nFlagVar are cleared.
whismanoid 0:4d7de8b5c800 684 * If match "key"=1 then the nFlagBitMask bits in nFlagVar are set.
whismanoid 0:4d7de8b5c800 685 * @param[in] nFlagBitMask bit mask contains binary 1 in the bit to be controlled by the key=value setting
whismanoid 0:4d7de8b5c800 686 *
whismanoid 0:4d7de8b5c800 687 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 688 *
whismanoid 0:4d7de8b5c800 689 */
whismanoid 0:4d7de8b5c800 690 bool CmdLine::parse_flag(const char *key, uint8_t& nFlagVar, const uint8_t nFlagBitMask)
whismanoid 0:4d7de8b5c800 691 {
whismanoid 0:4d7de8b5c800 692 char valueBuf[16];
whismanoid 0:4d7de8b5c800 693 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 694 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 695 {
whismanoid 0:4d7de8b5c800 696 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 697 // parse cmdLine arg (CPHA=\d)? --> g_SPI_dataMode | SPI_MODE1
whismanoid 0:4d7de8b5c800 698 int x = strtoul(valueBuf, NULL, 10);
whismanoid 0:4d7de8b5c800 699 if (x)
whismanoid 0:4d7de8b5c800 700 {
whismanoid 0:4d7de8b5c800 701 nFlagVar |= nFlagBitMask;
whismanoid 0:4d7de8b5c800 702 }
whismanoid 0:4d7de8b5c800 703 else
whismanoid 0:4d7de8b5c800 704 {
whismanoid 0:4d7de8b5c800 705 nFlagVar &= ~nFlagBitMask;
whismanoid 0:4d7de8b5c800 706 }
whismanoid 0:4d7de8b5c800 707 return true;
whismanoid 0:4d7de8b5c800 708 }
whismanoid 0:4d7de8b5c800 709 return false; // no match
whismanoid 0:4d7de8b5c800 710 }
whismanoid 0:4d7de8b5c800 711
whismanoid 0:4d7de8b5c800 712 /** CmdLine::parse_byte_hex matches "key"=value
whismanoid 0:4d7de8b5c800 713 *
whismanoid 0:4d7de8b5c800 714 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 715 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 716 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 717 *
whismanoid 0:4d7de8b5c800 718 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 719 *
whismanoid 2:0f702da53f2a 720 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 721 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 722 */
whismanoid 0:4d7de8b5c800 723 bool CmdLine::parse_byte_hex(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 724 {
whismanoid 0:4d7de8b5c800 725 char valueBuf[16];
whismanoid 0:4d7de8b5c800 726 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 727 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 728 {
whismanoid 0:4d7de8b5c800 729 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 730 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 731 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 732 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 733 {
whismanoid 0:4d7de8b5c800 734 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 735 return true;
whismanoid 0:4d7de8b5c800 736 }
whismanoid 0:4d7de8b5c800 737 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 738 {
whismanoid 0:4d7de8b5c800 739 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 740 return true;
whismanoid 0:4d7de8b5c800 741 }
whismanoid 2:0f702da53f2a 742 nByteVar = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 0:4d7de8b5c800 743 return true;
whismanoid 0:4d7de8b5c800 744 }
whismanoid 0:4d7de8b5c800 745 return false; // no match
whismanoid 0:4d7de8b5c800 746 }
whismanoid 0:4d7de8b5c800 747
whismanoid 0:4d7de8b5c800 748 /** CmdLine::parse_byte_dec matches "key"=value
whismanoid 0:4d7de8b5c800 749 *
whismanoid 0:4d7de8b5c800 750 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 751 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 752 * @param[out] nByteVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 753 *
whismanoid 0:4d7de8b5c800 754 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 755 *
whismanoid 2:0f702da53f2a 756 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 757 * Default number conversion radix is base-10 decimal.
whismanoid 0:4d7de8b5c800 758 */
whismanoid 0:4d7de8b5c800 759 bool CmdLine::parse_byte_dec(const char *key, uint8_t& nByteVar)
whismanoid 0:4d7de8b5c800 760 {
whismanoid 0:4d7de8b5c800 761 char valueBuf[16];
whismanoid 0:4d7de8b5c800 762 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 763 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 764 {
whismanoid 0:4d7de8b5c800 765 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 766 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 767 // TODO1: parse_byte_dec take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 768 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 769 {
whismanoid 0:4d7de8b5c800 770 nByteVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 771 return true;
whismanoid 0:4d7de8b5c800 772 }
whismanoid 0:4d7de8b5c800 773 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 774 {
whismanoid 0:4d7de8b5c800 775 nByteVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 776 return true;
whismanoid 0:4d7de8b5c800 777 }
whismanoid 2:0f702da53f2a 778 nByteVar = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 779 return true;
whismanoid 0:4d7de8b5c800 780 }
whismanoid 0:4d7de8b5c800 781 return false; // no match
whismanoid 0:4d7de8b5c800 782 }
whismanoid 0:4d7de8b5c800 783
whismanoid 17:371f0c5956da 784 /** CmdLine::parse_byte_dec matches "key"=value with array indexing
whismanoid 17:371f0c5956da 785 *
whismanoid 17:371f0c5956da 786 * @return true if keyword was found in buffer
whismanoid 17:371f0c5956da 787 * @param[in] key string value to match
whismanoid 17:371f0c5956da 788 * @param[out] nByteVar array base pointer
whismanoid 17:371f0c5956da 789 * @param[in] arrayIndexLimit array index limit
whismanoid 17:371f0c5956da 790 *
whismanoid 17:371f0c5956da 791 * @post on successful match, array[index] is updated from value string, and key=value substring is deleted from cmdbuf
whismanoid 17:371f0c5956da 792 *
whismanoid 17:371f0c5956da 793 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 17:371f0c5956da 794 * Default number conversion radix is base-10 decimal.
whismanoid 17:371f0c5956da 795 */
whismanoid 17:371f0c5956da 796 bool CmdLine::parse_uint8_dec_arrayUpdate(const char *key, uint8_t* nBytearray, size_t arrayIndexLimit)
whismanoid 17:371f0c5956da 797 {
whismanoid 17:371f0c5956da 798 // WIP #347 - parse_uint8_dec_arrayUpdate(const char *key, /*array=*/&AINcode[0], /*arrayIndexLimit=*/10) based on parse_uint8_dec(key, pValue)
whismanoid 17:371f0c5956da 799 // Work in progress
whismanoid 17:371f0c5956da 800 // if (parse_and_remove_key_and_arrayIndex(key, &index, valueBuf, sizeof(valueBuf)))
whismanoid 17:371f0c5956da 801 // match (key) "[" (index:[0-9]+) "]"
whismanoid 17:371f0c5956da 802 // nBytearray[index] = strtoul(valueBuf + 0, NULL, _radix_);
whismanoid 17:371f0c5956da 803 //
whismanoid 17:371f0c5956da 804 char valueBuf[16];
whismanoid 17:371f0c5956da 805 size_t index;
whismanoid 17:371f0c5956da 806 // bool parse_and_remove_key_and_arrayIndex(const char *key, size_t *pindex, char *valueBuf, size_t valueBufLen);
whismanoid 17:371f0c5956da 807 if (parse_and_remove_key_and_arrayIndex(key, &index, valueBuf, sizeof(valueBuf)))
whismanoid 17:371f0c5956da 808 {
whismanoid 17:371f0c5956da 809 if (index >= arrayIndexLimit) { index = arrayIndexLimit-1; }
whismanoid 17:371f0c5956da 810 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 17:371f0c5956da 811 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 17:371f0c5956da 812 // TODO1: parse_byte_dec take hex prefix 0x 0X or suffix $ h H
whismanoid 17:371f0c5956da 813 if (valueBuf[0] == '$')
whismanoid 17:371f0c5956da 814 {
whismanoid 17:371f0c5956da 815 nBytearray[index] = strtoul(valueBuf + 1, NULL, 16);
whismanoid 17:371f0c5956da 816 return true;
whismanoid 17:371f0c5956da 817 }
whismanoid 17:371f0c5956da 818 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 17:371f0c5956da 819 {
whismanoid 17:371f0c5956da 820 nBytearray[index] = strtoul(valueBuf + 2, NULL, 16);
whismanoid 17:371f0c5956da 821 return true;
whismanoid 17:371f0c5956da 822 }
whismanoid 17:371f0c5956da 823 nBytearray[index] = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 17:371f0c5956da 824 return true;
whismanoid 17:371f0c5956da 825 }
whismanoid 17:371f0c5956da 826 return false; // no match
whismanoid 17:371f0c5956da 827 }
whismanoid 17:371f0c5956da 828
whismanoid 0:4d7de8b5c800 829 /** CmdLine::parse_uint16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 830 *
whismanoid 0:4d7de8b5c800 831 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 832 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 833 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 834 *
whismanoid 0:4d7de8b5c800 835 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 836 *
whismanoid 2:0f702da53f2a 837 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 838 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 839 */
whismanoid 0:4d7de8b5c800 840 bool CmdLine::parse_uint16_hex(const char *key, uint16_t& uint16Var)
whismanoid 0:4d7de8b5c800 841 {
whismanoid 0:4d7de8b5c800 842 char valueBuf[16];
whismanoid 0:4d7de8b5c800 843 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 844 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 845 {
whismanoid 0:4d7de8b5c800 846 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 847 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 848 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 849 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 850 {
whismanoid 0:4d7de8b5c800 851 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 852 return true;
whismanoid 0:4d7de8b5c800 853 }
whismanoid 0:4d7de8b5c800 854 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 855 {
whismanoid 0:4d7de8b5c800 856 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 857 return true;
whismanoid 0:4d7de8b5c800 858 }
whismanoid 2:0f702da53f2a 859 uint16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 860 return true;
whismanoid 2:0f702da53f2a 861 }
whismanoid 2:0f702da53f2a 862 return false; // no match
whismanoid 2:0f702da53f2a 863 }
whismanoid 2:0f702da53f2a 864
whismanoid 2:0f702da53f2a 865 /** CmdLine::parse_uint16_dec matches "key"=value
whismanoid 2:0f702da53f2a 866 *
whismanoid 2:0f702da53f2a 867 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 868 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 869 * @param[out] uint16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 870 *
whismanoid 2:0f702da53f2a 871 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 872 *
whismanoid 2:0f702da53f2a 873 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 874 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 875 */
whismanoid 2:0f702da53f2a 876 bool CmdLine::parse_uint16_dec(const char *key, uint16_t& uint16Var)
whismanoid 2:0f702da53f2a 877 {
whismanoid 2:0f702da53f2a 878 char valueBuf[16];
whismanoid 2:0f702da53f2a 879 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 880 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 881 {
whismanoid 2:0f702da53f2a 882 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 883 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 884 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 885 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 886 {
whismanoid 2:0f702da53f2a 887 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 888 return true;
whismanoid 2:0f702da53f2a 889 }
whismanoid 2:0f702da53f2a 890 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 891 {
whismanoid 2:0f702da53f2a 892 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 893 return true;
whismanoid 2:0f702da53f2a 894 }
whismanoid 2:0f702da53f2a 895 uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 896 return true;
whismanoid 0:4d7de8b5c800 897 }
whismanoid 0:4d7de8b5c800 898 return false; // no match
whismanoid 0:4d7de8b5c800 899 }
whismanoid 0:4d7de8b5c800 900
whismanoid 17:371f0c5956da 901
whismanoid 17:371f0c5956da 902 /** CmdLine::parse_uint16_dec_arrayUpdate matches "key"=value with array indexing
whismanoid 17:371f0c5956da 903 *
whismanoid 17:371f0c5956da 904 * @return true if keyword was found in buffer
whismanoid 17:371f0c5956da 905 * @param[in] key string value to match
whismanoid 17:371f0c5956da 906 * @param[out] uint16Var array base pointer
whismanoid 17:371f0c5956da 907 * @param[in] arrayIndexLimit array index limit
whismanoid 17:371f0c5956da 908 *
whismanoid 17:371f0c5956da 909 * @post on successful match, array[index] is updated from value string, and key=value substring is deleted from cmdbuf
whismanoid 17:371f0c5956da 910 *
whismanoid 17:371f0c5956da 911 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 17:371f0c5956da 912 * Default number conversion radix is base-10 decimal.
whismanoid 17:371f0c5956da 913 */
whismanoid 17:371f0c5956da 914 bool CmdLine::parse_uint16_dec_arrayUpdate(const char *key, uint16_t* uint16array, size_t arrayIndexLimit)
whismanoid 17:371f0c5956da 915 {
whismanoid 17:371f0c5956da 916 // WIP #347 - parse_uint16_dec_arrayUpdate(/*name=*/"AINcode", /*array=*/&AINcode[0], /*arrayIndexLimit=*/10) based on parse_uint16_dec(key, pValue)
whismanoid 17:371f0c5956da 917 // Work in progress
whismanoid 17:371f0c5956da 918 //
whismanoid 17:371f0c5956da 919 char valueBuf[16];
whismanoid 17:371f0c5956da 920 size_t index;
whismanoid 17:371f0c5956da 921 // bool parse_and_remove_key_and_arrayIndex(const char *key, size_t *pindex, char *valueBuf, size_t valueBufLen);
whismanoid 17:371f0c5956da 922 if (parse_and_remove_key_and_arrayIndex(key, &index, valueBuf, sizeof(valueBuf)))
whismanoid 17:371f0c5956da 923 {
whismanoid 17:371f0c5956da 924 if (index >= arrayIndexLimit) { index = arrayIndexLimit-1; }
whismanoid 17:371f0c5956da 925 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 17:371f0c5956da 926 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 17:371f0c5956da 927 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 17:371f0c5956da 928 if (valueBuf[0] == '$')
whismanoid 17:371f0c5956da 929 {
whismanoid 17:371f0c5956da 930 uint16array[index] = strtoul(valueBuf + 1, NULL, 16);
whismanoid 17:371f0c5956da 931 return true;
whismanoid 17:371f0c5956da 932 }
whismanoid 17:371f0c5956da 933 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 17:371f0c5956da 934 {
whismanoid 17:371f0c5956da 935 uint16array[index] = strtoul(valueBuf + 2, NULL, 16);
whismanoid 17:371f0c5956da 936 return true;
whismanoid 17:371f0c5956da 937 }
whismanoid 17:371f0c5956da 938 uint16array[index] = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 17:371f0c5956da 939 return true;
whismanoid 17:371f0c5956da 940 }
whismanoid 17:371f0c5956da 941 return false; // no match
whismanoid 17:371f0c5956da 942 }
whismanoid 17:371f0c5956da 943
whismanoid 13:abedfe18f924 944 /** CmdLine::parse_int_dec matches "key"=value
whismanoid 13:abedfe18f924 945 *
whismanoid 13:abedfe18f924 946 * @return true if keyword was found in buffer
whismanoid 13:abedfe18f924 947 * @param[in] key string value to match
whismanoid 13:abedfe18f924 948 * @param[out] intVar updated from value string if match "key"=value
whismanoid 13:abedfe18f924 949 *
whismanoid 13:abedfe18f924 950 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 13:abedfe18f924 951 *
whismanoid 13:abedfe18f924 952 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 13:abedfe18f924 953 * Default number conversion radix is base-10 decimal.
whismanoid 13:abedfe18f924 954 */
whismanoid 13:abedfe18f924 955 bool CmdLine::parse_int_dec(const char *key, int& intVar)
whismanoid 13:abedfe18f924 956 {
whismanoid 13:abedfe18f924 957 char valueBuf[16];
whismanoid 13:abedfe18f924 958 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 13:abedfe18f924 959 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 13:abedfe18f924 960 {
whismanoid 13:abedfe18f924 961 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 13:abedfe18f924 962 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 13:abedfe18f924 963 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 13:abedfe18f924 964 if (valueBuf[0] == '$')
whismanoid 13:abedfe18f924 965 {
whismanoid 13:abedfe18f924 966 intVar = strtoul(valueBuf + 1, NULL, 16);
whismanoid 13:abedfe18f924 967 return true;
whismanoid 13:abedfe18f924 968 }
whismanoid 13:abedfe18f924 969 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 13:abedfe18f924 970 {
whismanoid 13:abedfe18f924 971 intVar = strtoul(valueBuf + 2, NULL, 16);
whismanoid 13:abedfe18f924 972 return true;
whismanoid 13:abedfe18f924 973 }
whismanoid 13:abedfe18f924 974 intVar = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 13:abedfe18f924 975 return true;
whismanoid 13:abedfe18f924 976 }
whismanoid 13:abedfe18f924 977 return false; // no match
whismanoid 13:abedfe18f924 978 }
whismanoid 13:abedfe18f924 979
whismanoid 0:4d7de8b5c800 980 /** CmdLine::parse_int16_hex matches "key"=value
whismanoid 0:4d7de8b5c800 981 *
whismanoid 0:4d7de8b5c800 982 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 983 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 984 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 985 *
whismanoid 0:4d7de8b5c800 986 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 987 *
whismanoid 2:0f702da53f2a 988 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 989 * Default number conversion radix is base-16 hexadecimal.
whismanoid 0:4d7de8b5c800 990 */
whismanoid 0:4d7de8b5c800 991 bool CmdLine::parse_int16_hex(const char *key, int16_t& int16Var)
whismanoid 0:4d7de8b5c800 992 {
whismanoid 0:4d7de8b5c800 993 char valueBuf[16];
whismanoid 0:4d7de8b5c800 994 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 995 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 996 {
whismanoid 0:4d7de8b5c800 997 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 0:4d7de8b5c800 998 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 0:4d7de8b5c800 999 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 0:4d7de8b5c800 1000 if (valueBuf[0] == '$')
whismanoid 0:4d7de8b5c800 1001 {
whismanoid 0:4d7de8b5c800 1002 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 0:4d7de8b5c800 1003 return true;
whismanoid 0:4d7de8b5c800 1004 }
whismanoid 0:4d7de8b5c800 1005 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 0:4d7de8b5c800 1006 {
whismanoid 0:4d7de8b5c800 1007 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 0:4d7de8b5c800 1008 return true;
whismanoid 0:4d7de8b5c800 1009 }
whismanoid 2:0f702da53f2a 1010 int16Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 2:0f702da53f2a 1011 return true;
whismanoid 2:0f702da53f2a 1012 }
whismanoid 2:0f702da53f2a 1013 return false; // no match
whismanoid 2:0f702da53f2a 1014 }
whismanoid 2:0f702da53f2a 1015
whismanoid 2:0f702da53f2a 1016 /** CmdLine::parse_int16_dec matches "key"=value
whismanoid 2:0f702da53f2a 1017 *
whismanoid 2:0f702da53f2a 1018 * @return true if keyword was found in buffer
whismanoid 2:0f702da53f2a 1019 * @param[in] key string value to match
whismanoid 2:0f702da53f2a 1020 * @param[out] int16Var updated from value string if match "key"=value
whismanoid 2:0f702da53f2a 1021 *
whismanoid 2:0f702da53f2a 1022 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 2:0f702da53f2a 1023 *
whismanoid 2:0f702da53f2a 1024 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 2:0f702da53f2a 1025 * Default number conversion radix is base-10 decimal.
whismanoid 2:0f702da53f2a 1026 */
whismanoid 2:0f702da53f2a 1027 bool CmdLine::parse_int16_dec(const char *key, int16_t& int16Var)
whismanoid 2:0f702da53f2a 1028 {
whismanoid 2:0f702da53f2a 1029 char valueBuf[16];
whismanoid 2:0f702da53f2a 1030 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 2:0f702da53f2a 1031 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 2:0f702da53f2a 1032 {
whismanoid 2:0f702da53f2a 1033 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 2:0f702da53f2a 1034 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 2:0f702da53f2a 1035 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 2:0f702da53f2a 1036 if (valueBuf[0] == '$')
whismanoid 2:0f702da53f2a 1037 {
whismanoid 2:0f702da53f2a 1038 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 2:0f702da53f2a 1039 return true;
whismanoid 2:0f702da53f2a 1040 }
whismanoid 2:0f702da53f2a 1041 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 2:0f702da53f2a 1042 {
whismanoid 2:0f702da53f2a 1043 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 2:0f702da53f2a 1044 return true;
whismanoid 2:0f702da53f2a 1045 }
whismanoid 2:0f702da53f2a 1046 int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 0:4d7de8b5c800 1047 return true;
whismanoid 0:4d7de8b5c800 1048 }
whismanoid 0:4d7de8b5c800 1049 return false; // no match
whismanoid 0:4d7de8b5c800 1050 }
whismanoid 0:4d7de8b5c800 1051
whismanoid 9:12e37800ecdd 1052 /** CmdLine::parse_uint32_hex matches "key"=value
whismanoid 9:12e37800ecdd 1053 *
whismanoid 9:12e37800ecdd 1054 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 1055 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 1056 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 1057 *
whismanoid 9:12e37800ecdd 1058 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 1059 *
whismanoid 9:12e37800ecdd 1060 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1061 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1062 */
whismanoid 9:12e37800ecdd 1063 bool CmdLine::parse_uint32_hex(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 1064 {
whismanoid 9:12e37800ecdd 1065 char valueBuf[16];
whismanoid 9:12e37800ecdd 1066 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 1067 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 1068 {
whismanoid 9:12e37800ecdd 1069 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 1070 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 1071 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 1072 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 1073 {
whismanoid 9:12e37800ecdd 1074 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 1075 return true;
whismanoid 9:12e37800ecdd 1076 }
whismanoid 9:12e37800ecdd 1077 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 1078 {
whismanoid 9:12e37800ecdd 1079 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 1080 return true;
whismanoid 9:12e37800ecdd 1081 }
whismanoid 9:12e37800ecdd 1082 uint32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 1083 return true;
whismanoid 9:12e37800ecdd 1084 }
whismanoid 9:12e37800ecdd 1085 return false; // no match
whismanoid 9:12e37800ecdd 1086 }
whismanoid 9:12e37800ecdd 1087
whismanoid 9:12e37800ecdd 1088 /** CmdLine::parse_uint32_dec matches "key"=value
whismanoid 9:12e37800ecdd 1089 *
whismanoid 9:12e37800ecdd 1090 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 1091 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 1092 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 1093 *
whismanoid 9:12e37800ecdd 1094 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 1095 *
whismanoid 9:12e37800ecdd 1096 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1097 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 1098 */
whismanoid 9:12e37800ecdd 1099 bool CmdLine::parse_uint32_dec(const char *key, uint32_t& uint32Var)
whismanoid 9:12e37800ecdd 1100 {
whismanoid 9:12e37800ecdd 1101 char valueBuf[16];
whismanoid 9:12e37800ecdd 1102 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 1103 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 1104 {
whismanoid 9:12e37800ecdd 1105 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 1106 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 1107 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 1108 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 1109 {
whismanoid 9:12e37800ecdd 1110 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 1111 return true;
whismanoid 9:12e37800ecdd 1112 }
whismanoid 9:12e37800ecdd 1113 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 1114 {
whismanoid 9:12e37800ecdd 1115 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 1116 return true;
whismanoid 9:12e37800ecdd 1117 }
whismanoid 9:12e37800ecdd 1118 uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 1119 return true;
whismanoid 9:12e37800ecdd 1120 }
whismanoid 9:12e37800ecdd 1121 return false; // no match
whismanoid 9:12e37800ecdd 1122 }
whismanoid 9:12e37800ecdd 1123
whismanoid 9:12e37800ecdd 1124 /** CmdLine::parse_int32_hex matches "key"=value
whismanoid 9:12e37800ecdd 1125 *
whismanoid 9:12e37800ecdd 1126 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 1127 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 1128 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 1129 *
whismanoid 9:12e37800ecdd 1130 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 1131 *
whismanoid 9:12e37800ecdd 1132 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1133 * Default number conversion radix is base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1134 */
whismanoid 9:12e37800ecdd 1135 bool CmdLine::parse_int32_hex(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 1136 {
whismanoid 9:12e37800ecdd 1137 char valueBuf[16];
whismanoid 9:12e37800ecdd 1138 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 1139 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 1140 {
whismanoid 9:12e37800ecdd 1141 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 1142 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 1143 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 1144 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 1145 {
whismanoid 9:12e37800ecdd 1146 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 1147 return true;
whismanoid 9:12e37800ecdd 1148 }
whismanoid 9:12e37800ecdd 1149 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 1150 {
whismanoid 9:12e37800ecdd 1151 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 1152 return true;
whismanoid 9:12e37800ecdd 1153 }
whismanoid 9:12e37800ecdd 1154 int32Var = strtoul(valueBuf, NULL, 16); // default radix hex
whismanoid 9:12e37800ecdd 1155 return true;
whismanoid 9:12e37800ecdd 1156 }
whismanoid 9:12e37800ecdd 1157 return false; // no match
whismanoid 9:12e37800ecdd 1158 }
whismanoid 9:12e37800ecdd 1159
whismanoid 9:12e37800ecdd 1160 /** CmdLine::parse_int32_dec matches "key"=value
whismanoid 9:12e37800ecdd 1161 *
whismanoid 9:12e37800ecdd 1162 * @return true if keyword was found in buffer
whismanoid 9:12e37800ecdd 1163 * @param[in] key string value to match
whismanoid 9:12e37800ecdd 1164 * @param[out] int32Var updated from value string if match "key"=value
whismanoid 9:12e37800ecdd 1165 *
whismanoid 9:12e37800ecdd 1166 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 9:12e37800ecdd 1167 *
whismanoid 9:12e37800ecdd 1168 * Prefix '$' or '0x' or '0X' selects radix base-16 hexadecimal.
whismanoid 9:12e37800ecdd 1169 * Default number conversion radix is base-10 decimal.
whismanoid 9:12e37800ecdd 1170 */
whismanoid 9:12e37800ecdd 1171 bool CmdLine::parse_int32_dec(const char *key, int32_t& int32Var)
whismanoid 9:12e37800ecdd 1172 {
whismanoid 9:12e37800ecdd 1173 char valueBuf[16];
whismanoid 9:12e37800ecdd 1174 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 9:12e37800ecdd 1175 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 9:12e37800ecdd 1176 {
whismanoid 9:12e37800ecdd 1177 // ASSERT: buf[matched_index] contains '=' followed by value
whismanoid 9:12e37800ecdd 1178 // parse cmdLine arg (CMD=\d)? --> g_I2C_command_regAddress
whismanoid 9:12e37800ecdd 1179 // TODO1: parse_byte_hex take hex prefix 0x 0X or suffix $ h H
whismanoid 9:12e37800ecdd 1180 if (valueBuf[0] == '$')
whismanoid 9:12e37800ecdd 1181 {
whismanoid 9:12e37800ecdd 1182 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 9:12e37800ecdd 1183 return true;
whismanoid 9:12e37800ecdd 1184 }
whismanoid 9:12e37800ecdd 1185 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 9:12e37800ecdd 1186 {
whismanoid 9:12e37800ecdd 1187 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 9:12e37800ecdd 1188 return true;
whismanoid 9:12e37800ecdd 1189 }
whismanoid 9:12e37800ecdd 1190 int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 9:12e37800ecdd 1191 return true;
whismanoid 9:12e37800ecdd 1192 }
whismanoid 9:12e37800ecdd 1193 return false; // no match
whismanoid 9:12e37800ecdd 1194 }
whismanoid 9:12e37800ecdd 1195
whismanoid 16:592df067fe14 1196 /** CmdLine::parse_double_or_int16 matches "key"=value
whismanoid 16:592df067fe14 1197 *
whismanoid 16:592df067fe14 1198 * Value will be parsed as double if it contains "." or "V",
whismanoid 16:592df067fe14 1199 * Otherwise,
whismanoid 16:592df067fe14 1200 * Value will be parsed as hexadecimal if it begins with "$" or "0x",
whismanoid 16:592df067fe14 1201 * Otherwise,
whismanoid 16:592df067fe14 1202 * Value will be parsed as decimal
whismanoid 16:592df067fe14 1203 *
whismanoid 16:592df067fe14 1204 * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double
whismanoid 16:592df067fe14 1205 * @param[in] key string value to match
whismanoid 16:592df067fe14 1206 * @param[out] doubleVar updated from value string if match "key"=xx.xxx
whismanoid 16:592df067fe14 1207 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 16:592df067fe14 1208 *
whismanoid 16:592df067fe14 1209 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 16:592df067fe14 1210 *
whismanoid 16:592df067fe14 1211 */
whismanoid 16:592df067fe14 1212 uint8_t CmdLine::parse_double_or_int16(const char *key, double& doubleVar, int16_t& int16Var)
whismanoid 16:592df067fe14 1213 {
whismanoid 16:592df067fe14 1214 char valueBuf[16];
whismanoid 16:592df067fe14 1215 char *end; // buffer required by strtof
whismanoid 16:592df067fe14 1216 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 16:592df067fe14 1217 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 16:592df067fe14 1218 {
whismanoid 16:592df067fe14 1219 for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++)
whismanoid 16:592df067fe14 1220 {
whismanoid 16:592df067fe14 1221 switch(valueBuf[index])
whismanoid 16:592df067fe14 1222 {
whismanoid 16:592df067fe14 1223 case '.': case 'V': case 'v':
whismanoid 16:592df067fe14 1224 // valueBuf contains decimal "." or letter "V"
whismanoid 18:2c3c44a34d81 1225 doubleVar = strtod(valueBuf, &end);
whismanoid 16:592df067fe14 1226 return 2; // 2: parsed as double
whismanoid 16:592df067fe14 1227 }
whismanoid 16:592df067fe14 1228 }
whismanoid 16:592df067fe14 1229 if (valueBuf[0] == '$')
whismanoid 16:592df067fe14 1230 {
whismanoid 16:592df067fe14 1231 int16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 16:592df067fe14 1232 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1233 }
whismanoid 16:592df067fe14 1234 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 16:592df067fe14 1235 {
whismanoid 16:592df067fe14 1236 int16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 16:592df067fe14 1237 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1238 }
whismanoid 16:592df067fe14 1239 int16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 16:592df067fe14 1240 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1241 }
whismanoid 16:592df067fe14 1242 return 0; // no match
whismanoid 16:592df067fe14 1243 }
whismanoid 16:592df067fe14 1244
whismanoid 16:592df067fe14 1245 /** CmdLine::parse_double_or_uint16 matches "key"=value
whismanoid 16:592df067fe14 1246 *
whismanoid 16:592df067fe14 1247 * Value will be parsed as double if it contains "." or "V",
whismanoid 16:592df067fe14 1248 * Otherwise,
whismanoid 16:592df067fe14 1249 * Value will be parsed as hexadecimal if it begins with "$" or "0x",
whismanoid 16:592df067fe14 1250 * Otherwise,
whismanoid 16:592df067fe14 1251 * Value will be parsed as decimal
whismanoid 16:592df067fe14 1252 *
whismanoid 16:592df067fe14 1253 * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double
whismanoid 16:592df067fe14 1254 * @param[in] key string value to match
whismanoid 16:592df067fe14 1255 * @param[out] doubleVar updated from value string if match "key"=xx.xxx
whismanoid 16:592df067fe14 1256 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 16:592df067fe14 1257 *
whismanoid 16:592df067fe14 1258 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 16:592df067fe14 1259 *
whismanoid 16:592df067fe14 1260 */
whismanoid 16:592df067fe14 1261 uint8_t CmdLine::parse_double_or_uint16(const char *key, double& doubleVar, uint16_t& uint16Var)
whismanoid 16:592df067fe14 1262 {
whismanoid 16:592df067fe14 1263 char valueBuf[16];
whismanoid 16:592df067fe14 1264 char *end; // buffer required by strtof
whismanoid 16:592df067fe14 1265 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 16:592df067fe14 1266 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 16:592df067fe14 1267 {
whismanoid 16:592df067fe14 1268 for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++)
whismanoid 16:592df067fe14 1269 {
whismanoid 16:592df067fe14 1270 switch(valueBuf[index])
whismanoid 16:592df067fe14 1271 {
whismanoid 16:592df067fe14 1272 case '.': case 'V': case 'v':
whismanoid 16:592df067fe14 1273 // valueBuf contains decimal "." or letter "V"
whismanoid 18:2c3c44a34d81 1274 doubleVar = strtod(valueBuf, &end);
whismanoid 16:592df067fe14 1275 return 2; // 2: parsed as double
whismanoid 16:592df067fe14 1276 }
whismanoid 16:592df067fe14 1277 }
whismanoid 16:592df067fe14 1278 if (valueBuf[0] == '$')
whismanoid 16:592df067fe14 1279 {
whismanoid 16:592df067fe14 1280 uint16Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 16:592df067fe14 1281 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1282 }
whismanoid 16:592df067fe14 1283 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 16:592df067fe14 1284 {
whismanoid 16:592df067fe14 1285 uint16Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 16:592df067fe14 1286 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1287 }
whismanoid 16:592df067fe14 1288 uint16Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 16:592df067fe14 1289 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1290 }
whismanoid 16:592df067fe14 1291 return 0; // no match
whismanoid 16:592df067fe14 1292 }
whismanoid 16:592df067fe14 1293
whismanoid 16:592df067fe14 1294 /** CmdLine::parse_double_or_int32 matches "key"=value
whismanoid 16:592df067fe14 1295 *
whismanoid 16:592df067fe14 1296 * Value will be parsed as double if it contains "." or "V",
whismanoid 16:592df067fe14 1297 * Otherwise,
whismanoid 16:592df067fe14 1298 * Value will be parsed as hexadecimal if it begins with "$" or "0x",
whismanoid 16:592df067fe14 1299 * Otherwise,
whismanoid 16:592df067fe14 1300 * Value will be parsed as decimal
whismanoid 16:592df067fe14 1301 *
whismanoid 16:592df067fe14 1302 * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double
whismanoid 16:592df067fe14 1303 * @param[in] key string value to match
whismanoid 16:592df067fe14 1304 * @param[out] doubleVar updated from value string if match "key"=xx.xxx
whismanoid 16:592df067fe14 1305 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 16:592df067fe14 1306 *
whismanoid 16:592df067fe14 1307 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 16:592df067fe14 1308 *
whismanoid 16:592df067fe14 1309 */
whismanoid 16:592df067fe14 1310 uint8_t CmdLine::parse_double_or_int32(const char *key, double& doubleVar, int32_t& int32Var)
whismanoid 16:592df067fe14 1311 {
whismanoid 16:592df067fe14 1312 char valueBuf[16];
whismanoid 16:592df067fe14 1313 char *end; // buffer required by strtof
whismanoid 16:592df067fe14 1314 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 16:592df067fe14 1315 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 16:592df067fe14 1316 {
whismanoid 16:592df067fe14 1317 for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++)
whismanoid 16:592df067fe14 1318 {
whismanoid 16:592df067fe14 1319 switch(valueBuf[index])
whismanoid 16:592df067fe14 1320 {
whismanoid 16:592df067fe14 1321 case '.': case 'V': case 'v':
whismanoid 16:592df067fe14 1322 // valueBuf contains decimal "." or letter "V"
whismanoid 18:2c3c44a34d81 1323 doubleVar = strtod(valueBuf, &end);
whismanoid 16:592df067fe14 1324 return 2; // 2: parsed as double
whismanoid 16:592df067fe14 1325 }
whismanoid 16:592df067fe14 1326 }
whismanoid 16:592df067fe14 1327 if (valueBuf[0] == '$')
whismanoid 16:592df067fe14 1328 {
whismanoid 16:592df067fe14 1329 int32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 16:592df067fe14 1330 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1331 }
whismanoid 16:592df067fe14 1332 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 16:592df067fe14 1333 {
whismanoid 16:592df067fe14 1334 int32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 16:592df067fe14 1335 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1336 }
whismanoid 16:592df067fe14 1337 int32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 16:592df067fe14 1338 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1339 }
whismanoid 16:592df067fe14 1340 return 0; // no match
whismanoid 16:592df067fe14 1341 }
whismanoid 16:592df067fe14 1342
whismanoid 16:592df067fe14 1343 /** CmdLine::parse_double_or_uint32 matches "key"=value
whismanoid 16:592df067fe14 1344 *
whismanoid 16:592df067fe14 1345 * Value will be parsed as double if it contains "." or "V",
whismanoid 16:592df067fe14 1346 * Otherwise,
whismanoid 16:592df067fe14 1347 * Value will be parsed as hexadecimal if it begins with "$" or "0x",
whismanoid 16:592df067fe14 1348 * Otherwise,
whismanoid 16:592df067fe14 1349 * Value will be parsed as decimal
whismanoid 16:592df067fe14 1350 *
whismanoid 16:592df067fe14 1351 * @return 0 if keyword not found in buffer; 1: parsed as integer; 2: parsed as double
whismanoid 16:592df067fe14 1352 * @param[in] key string value to match
whismanoid 16:592df067fe14 1353 * @param[out] doubleVar updated from value string if match "key"=xx.xxx
whismanoid 16:592df067fe14 1354 * @param[out] uint32Var updated from value string if match "key"=value
whismanoid 16:592df067fe14 1355 *
whismanoid 16:592df067fe14 1356 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 16:592df067fe14 1357 *
whismanoid 16:592df067fe14 1358 */
whismanoid 16:592df067fe14 1359 uint8_t CmdLine::parse_double_or_uint32(const char *key, double& doubleVar, uint32_t& uint32Var)
whismanoid 16:592df067fe14 1360 {
whismanoid 16:592df067fe14 1361 char valueBuf[16];
whismanoid 16:592df067fe14 1362 char *end; // buffer required by strtof
whismanoid 16:592df067fe14 1363 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 16:592df067fe14 1364 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 16:592df067fe14 1365 {
whismanoid 16:592df067fe14 1366 for (size_t index = 0; valueBuf[index] != '\0' && index < sizeof(valueBuf); index++)
whismanoid 16:592df067fe14 1367 {
whismanoid 16:592df067fe14 1368 switch(valueBuf[index])
whismanoid 16:592df067fe14 1369 {
whismanoid 16:592df067fe14 1370 case '.': case 'V': case 'v':
whismanoid 16:592df067fe14 1371 // valueBuf contains decimal "." or letter "V"
whismanoid 18:2c3c44a34d81 1372 doubleVar = strtod(valueBuf, &end);
whismanoid 16:592df067fe14 1373 return 2; // 2: parsed as double
whismanoid 16:592df067fe14 1374 }
whismanoid 16:592df067fe14 1375 }
whismanoid 16:592df067fe14 1376 if (valueBuf[0] == '$')
whismanoid 16:592df067fe14 1377 {
whismanoid 16:592df067fe14 1378 uint32Var = strtoul(valueBuf + 1, NULL, 16);
whismanoid 16:592df067fe14 1379 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1380 }
whismanoid 16:592df067fe14 1381 if (valueBuf[0] == '0' && (valueBuf[1] == 'X' || valueBuf[1] == 'x'))
whismanoid 16:592df067fe14 1382 {
whismanoid 16:592df067fe14 1383 uint32Var = strtoul(valueBuf + 2, NULL, 16);
whismanoid 16:592df067fe14 1384 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1385 }
whismanoid 16:592df067fe14 1386 uint32Var = strtoul(valueBuf, NULL, 10); // default radix decimal
whismanoid 16:592df067fe14 1387 return 1; // 1: parsed as integer
whismanoid 16:592df067fe14 1388 }
whismanoid 16:592df067fe14 1389 return 0; // no match
whismanoid 16:592df067fe14 1390 }
whismanoid 16:592df067fe14 1391
whismanoid 7:0bda7cfee767 1392 /** CmdLine::parse_double matches "key"=value
whismanoid 7:0bda7cfee767 1393 *
whismanoid 7:0bda7cfee767 1394 * @return true if keyword was found in buffer
whismanoid 7:0bda7cfee767 1395 * @param[in] key string value to match
whismanoid 7:0bda7cfee767 1396 * @param[out] doubleVar updated from value string if match "key"=value
whismanoid 7:0bda7cfee767 1397 *
whismanoid 7:0bda7cfee767 1398 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 7:0bda7cfee767 1399 *
whismanoid 7:0bda7cfee767 1400 */
whismanoid 7:0bda7cfee767 1401 bool CmdLine::parse_double(const char *key, double& doubleVar)
whismanoid 7:0bda7cfee767 1402 {
whismanoid 7:0bda7cfee767 1403 char valueBuf[16];
whismanoid 16:592df067fe14 1404 char *end; // buffer required by strtof
whismanoid 7:0bda7cfee767 1405 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 7:0bda7cfee767 1406 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 7:0bda7cfee767 1407 {
whismanoid 18:2c3c44a34d81 1408 doubleVar = strtod(valueBuf, &end);
whismanoid 7:0bda7cfee767 1409 return true;
whismanoid 7:0bda7cfee767 1410 }
whismanoid 7:0bda7cfee767 1411 return false; // no match
whismanoid 7:0bda7cfee767 1412 }
whismanoid 7:0bda7cfee767 1413
whismanoid 0:4d7de8b5c800 1414 /** CmdLine::parse_float matches "key"=value
whismanoid 0:4d7de8b5c800 1415 *
whismanoid 0:4d7de8b5c800 1416 * @return true if keyword was found in buffer
whismanoid 0:4d7de8b5c800 1417 * @param[in] key string value to match
whismanoid 0:4d7de8b5c800 1418 * @param[out] floatVar updated from value string if match "key"=value
whismanoid 0:4d7de8b5c800 1419 *
whismanoid 0:4d7de8b5c800 1420 * @post on successful match, the key=value substring is deleted from cmdbuf
whismanoid 0:4d7de8b5c800 1421 *
whismanoid 0:4d7de8b5c800 1422 */
whismanoid 0:4d7de8b5c800 1423 bool CmdLine::parse_float(const char *key, float& floatVar)
whismanoid 0:4d7de8b5c800 1424 {
whismanoid 0:4d7de8b5c800 1425 char valueBuf[16];
whismanoid 16:592df067fe14 1426 char *end; // buffer required by strtof
whismanoid 0:4d7de8b5c800 1427 // bool parse_and_remove_key(const char *key, char *valueBuf, int valueBufLen);
whismanoid 0:4d7de8b5c800 1428 if (parse_and_remove_key(key, valueBuf, sizeof(valueBuf)))
whismanoid 0:4d7de8b5c800 1429 {
whismanoid 0:4d7de8b5c800 1430 floatVar = strtof(valueBuf, &end);
whismanoid 0:4d7de8b5c800 1431 return true;
whismanoid 0:4d7de8b5c800 1432 }
whismanoid 0:4d7de8b5c800 1433 return false; // no match
whismanoid 0:4d7de8b5c800 1434 }
whismanoid 0:4d7de8b5c800 1435
whismanoid 0:4d7de8b5c800 1436 /** CmdLine::parse_byteCount_byteList_hex matches hexadecimal digits separated by spaces.
whismanoid 0:4d7de8b5c800 1437 * The 0x / 0X prefix is optional. The numbers must be hexadecimal.
whismanoid 0:4d7de8b5c800 1438 *
whismanoid 0:4d7de8b5c800 1439 * @return true if more than one hex byte found in buffer
whismanoid 0:4d7de8b5c800 1440 * @param[out] byteCount is populated with the number of hex bytes found
whismanoid 0:4d7de8b5c800 1441 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the hex bytes found
whismanoid 0:4d7de8b5c800 1442 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 1443 *
whismanoid 0:4d7de8b5c800 1444 */
whismanoid 0:4d7de8b5c800 1445 bool CmdLine::parse_byteCount_byteList_hex(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 1446 {
whismanoid 0:4d7de8b5c800 1447 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 1448 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 1449 byteCount = 0;
whismanoid 0:4d7de8b5c800 1450 bool got_value = false;
whismanoid 0:4d7de8b5c800 1451 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 1452 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 1453 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 1454 {
whismanoid 0:4d7de8b5c800 1455 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 1456 break; // end of buffer
whismanoid 0:4d7de8b5c800 1457 }
whismanoid 0:4d7de8b5c800 1458 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 1459 {
whismanoid 0:4d7de8b5c800 1460 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 1461 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 1462 case '8': case '9':
whismanoid 0:4d7de8b5c800 1463 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 1464 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 1465 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1466 got_value = true;
whismanoid 0:4d7de8b5c800 1467 break;
whismanoid 0:4d7de8b5c800 1468 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 1469 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 1470 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 1471 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 1472 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1473 got_value = true;
whismanoid 0:4d7de8b5c800 1474 break;
whismanoid 0:4d7de8b5c800 1475 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 1476 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 1477 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 1478 temp_value = temp_value * 0x10;
whismanoid 0:4d7de8b5c800 1479 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1480 got_value = true;
whismanoid 0:4d7de8b5c800 1481 break;
whismanoid 0:4d7de8b5c800 1482 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 1483 temp_value = 0;
whismanoid 0:4d7de8b5c800 1484 break;
whismanoid 0:4d7de8b5c800 1485 case ' ': case ',':
whismanoid 0:4d7de8b5c800 1486 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1487 {
whismanoid 0:4d7de8b5c800 1488 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1489 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1490 temp_value = 0;
whismanoid 0:4d7de8b5c800 1491 got_value = false;
whismanoid 0:4d7de8b5c800 1492 }
whismanoid 0:4d7de8b5c800 1493 break;
whismanoid 0:4d7de8b5c800 1494 }
whismanoid 0:4d7de8b5c800 1495 } // for idxSearch
whismanoid 0:4d7de8b5c800 1496 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1497 {
whismanoid 0:4d7de8b5c800 1498 //serial().printf("\r\n parse_byteCount_byteList_hex mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1499 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1500 temp_value = 0;
whismanoid 0:4d7de8b5c800 1501 got_value = false;
whismanoid 0:4d7de8b5c800 1502 }
whismanoid 0:4d7de8b5c800 1503 //serial().printf("\r\n parse_byteCount_byteList_hex (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 1504 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 1505 }
whismanoid 0:4d7de8b5c800 1506
whismanoid 0:4d7de8b5c800 1507 /** CmdLine::parse_byteCount_byteList_dec matches a list of numbers, separated by spaces.
whismanoid 0:4d7de8b5c800 1508 * The 0x / 0X prefix may be used to select hexadecimal instead of decimal.
whismanoid 0:4d7de8b5c800 1509 *
whismanoid 0:4d7de8b5c800 1510 * @return true if more than one number found in buffer
whismanoid 0:4d7de8b5c800 1511 * @param[out] byteCount is populated with the number of numbers found
whismanoid 0:4d7de8b5c800 1512 * @param[out] mosiDataBuf buffer mosiDataBuf[0..byteCount-1] is populated with the numbers found
whismanoid 0:4d7de8b5c800 1513 * @param[in] mosiDataBufSize limits the number of bytes that will be used
whismanoid 0:4d7de8b5c800 1514 *
whismanoid 0:4d7de8b5c800 1515 */
whismanoid 0:4d7de8b5c800 1516 bool CmdLine::parse_byteCount_byteList_dec(size_t& byteCount, char *mosiDataBuf, size_t mosiDataBufSize)
whismanoid 0:4d7de8b5c800 1517 {
whismanoid 0:4d7de8b5c800 1518 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\")...", buf);
whismanoid 0:4d7de8b5c800 1519 // parse cmdLine hex byte list --> int byteCount; int mosiData[MAX_SPI_BYTE_COUNT];
whismanoid 0:4d7de8b5c800 1520 byteCount = 0;
whismanoid 0:4d7de8b5c800 1521 bool got_value = false;
whismanoid 0:4d7de8b5c800 1522 uint8_t nybbleValue;
whismanoid 0:4d7de8b5c800 1523 uint8_t temp_value = 0;
whismanoid 0:4d7de8b5c800 1524 uint8_t radix = 10;
whismanoid 0:4d7de8b5c800 1525 for (unsigned int idxSearch = 0; idxSearch < indexOfNextEmptyCell; idxSearch++)
whismanoid 0:4d7de8b5c800 1526 {
whismanoid 0:4d7de8b5c800 1527 if (buf[idxSearch] == '\0') {
whismanoid 0:4d7de8b5c800 1528 break; // end of buffer
whismanoid 0:4d7de8b5c800 1529 }
whismanoid 0:4d7de8b5c800 1530 switch (buf[idxSearch])
whismanoid 0:4d7de8b5c800 1531 {
whismanoid 0:4d7de8b5c800 1532 case '0': case '1': case '2': case '3':
whismanoid 0:4d7de8b5c800 1533 case '4': case '5': case '6': case '7':
whismanoid 0:4d7de8b5c800 1534 case '8': case '9':
whismanoid 0:4d7de8b5c800 1535 nybbleValue = buf[idxSearch] - '0';
whismanoid 0:4d7de8b5c800 1536 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1537 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1538 got_value = true;
whismanoid 0:4d7de8b5c800 1539 break;
whismanoid 0:4d7de8b5c800 1540 case 'a': case 'b': case 'c':
whismanoid 0:4d7de8b5c800 1541 case 'd': case 'e': case 'f':
whismanoid 0:4d7de8b5c800 1542 nybbleValue = buf[idxSearch] - 'a' + 0x0a;
whismanoid 0:4d7de8b5c800 1543 radix = 0x10;
whismanoid 0:4d7de8b5c800 1544 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1545 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1546 got_value = true;
whismanoid 0:4d7de8b5c800 1547 break;
whismanoid 0:4d7de8b5c800 1548 case 'A': case 'B': case 'C':
whismanoid 0:4d7de8b5c800 1549 case 'D': case 'E': case 'F':
whismanoid 0:4d7de8b5c800 1550 nybbleValue = buf[idxSearch] - 'A' + 0x0a;
whismanoid 0:4d7de8b5c800 1551 radix = 0x10;
whismanoid 0:4d7de8b5c800 1552 temp_value = temp_value * radix;
whismanoid 0:4d7de8b5c800 1553 temp_value = temp_value + nybbleValue;
whismanoid 0:4d7de8b5c800 1554 got_value = true;
whismanoid 0:4d7de8b5c800 1555 break;
whismanoid 0:4d7de8b5c800 1556 case 'x': case 'X':
whismanoid 0:4d7de8b5c800 1557 temp_value = 0;
whismanoid 0:4d7de8b5c800 1558 radix = 0x10;
whismanoid 0:4d7de8b5c800 1559 break;
whismanoid 0:4d7de8b5c800 1560 case ' ': case ',':
whismanoid 0:4d7de8b5c800 1561 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1562 {
whismanoid 0:4d7de8b5c800 1563 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1564 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1565 temp_value = 0;
whismanoid 0:4d7de8b5c800 1566 radix = 10;
whismanoid 0:4d7de8b5c800 1567 got_value = false;
whismanoid 0:4d7de8b5c800 1568 }
whismanoid 0:4d7de8b5c800 1569 break;
whismanoid 0:4d7de8b5c800 1570 }
whismanoid 0:4d7de8b5c800 1571 } // for idxSearch
whismanoid 0:4d7de8b5c800 1572 if ((got_value) && (byteCount < mosiDataBufSize))
whismanoid 0:4d7de8b5c800 1573 {
whismanoid 0:4d7de8b5c800 1574 //serial().printf("\r\n parse_byteCount_byteList_dec mosiDataBuf[%d] = 0x%2.2x", byteCount, temp_value);
whismanoid 0:4d7de8b5c800 1575 mosiDataBuf[byteCount++] = temp_value;
whismanoid 0:4d7de8b5c800 1576 temp_value = 0;
whismanoid 0:4d7de8b5c800 1577 got_value = false;
whismanoid 0:4d7de8b5c800 1578 }
whismanoid 0:4d7de8b5c800 1579 //serial().printf("\r\n parse_byteCount_byteList_dec (buf=\"%s\") returning: byteCount=%d", buf, byteCount);
whismanoid 0:4d7de8b5c800 1580 return (byteCount > 0);
whismanoid 0:4d7de8b5c800 1581 }
whismanoid 0:4d7de8b5c800 1582
whismanoid 0:4d7de8b5c800 1583
whismanoid 0:4d7de8b5c800 1584 // End of file