bool getArgs(const std::string& uri, Arguments* args) { assert(args); std::string arg_name; std::istringstream istream(uri); skipCharacter(istream, '/'); skipCharacter(istream, '?'); // skip '/?' or '//?' characters. Aimp UControl sends '//?' for some reason - handle it. while ( istream.good() ) { getline(istream, arg_name, '='); if ( !istream.good() ) { return false; } getline(istream, (*args)[arg_name], '&'); } return istream.eof(); // treat operation successfull if we have read till end of stream. }
OSReturn_t Parser::parseVariableName(uchar_t* pName, std::size_t siz) { OSReturn_t r; r = skipCharacter('"'); if (r != OSReturn::OS_OK) return r; bool isTooLarge; isTooLarge = false; for (; hasMoreContent(); advanceCurrent()) { uchar_t ch; ch = getCurrentChar(); if (ch == '"') { // Skip over terminating " advanceCurrent(); break; } if (siz > 1) { *pName++ = ch; *pName = '\0'; --siz; } else { isTooLarge = true; } } r = skipCharacter(':'); if (r != OSReturn::OS_OK) return r; if (isTooLarge) return OSReturn::OS_NOT_ENOUGH_SPACE; return OSReturn::OS_OK; }
OSReturn_t Parser::parseByteArray(uint8_t* pByteArray, std::size_t size, std::size_t *pLength) { OSReturn_t r; uint32_t n_32, i; uchar_t ch; ch = skipSpaces(); r = skipCharacter('['); if (r != OSReturn::OS_OK) return r; for (i = 0; hasMoreContent(); i++) { if (i == size) return OSReturn::OS_NOT_ENOUGH_SPACE; r = parseNumber(n_32); if (r != OSReturn::OS_OK) return r; pByteArray[i] = (uint8_t) n_32; r = skipCharacter(','); if (r != OSReturn::OS_OK) break; } r = skipCharacter(']'); if (r != OSReturn::OS_OK) return r; *pLength = i + 1; return OSReturn::OS_OK; }
static int interpretNumber (int *number, const unsigned char **bytes, int *count) { int ok = skipCharacter('0', bytes, count); *number = 0; while (*count) { static unsigned char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; const unsigned char *digit = memchr(digits, **bytes, sizeof(digits)); if (!digit) break; *number = (*number * 10) + (digit - digits); ok = 1; ++*bytes, --*count; } return ok; }
OSReturn_t Parser::parseString(uchar_t* string, std::size_t siz) { OSReturn_t r; uchar_t ch; ch = skipSpaces(); r = skipCharacter('"'); if (r != OSReturn::OS_OK) return r; bool isTooLarge; isTooLarge = false; for (; hasMoreContent(); advanceCurrent()) { ch = getCurrentChar(); if (ch == '"') { // Skip over terminating " advanceCurrent(); break; } if (siz > 1) { *string++ = ch; *string = '\0'; --siz; } else { isTooLarge = true; } } if (isTooLarge) return OSReturn::OS_NOT_ENOUGH_SPACE; return OSReturn::OS_OK; }
static int identifyDisplay (BrailleDisplay *brl) { static const unsigned char identify[] = {'I', CR}; if (writeBytes(brl, identify, sizeof(identify))) { if (serialAwaitInput(serialDevice, 1000)) { unsigned char identity[0X100]; size_t length; if (readBytes(identity, sizeof(identity), &length)) { static const unsigned char prefix[] = {'b', 'r', 'a', 'u', 'd', 'i', ' '}; if ((length >= sizeof(prefix)) && (memcmp(identity, prefix, sizeof(prefix)) == 0)) { const unsigned char *bytes = memchr(identity, ',', length); if (bytes) { int count = length - (bytes - identity); int cells; ++bytes, --count; skipCharacter(' ', &bytes, &count); if (interpretNumber(&cells, &bytes, &count)) { if (!count) { logMessage(LOG_INFO, "Detected: %.*s", (int)length, identity); brl->textColumns = cells; brl->textRows = 1; return 1; } } } } logUnexpectedPacket(identity, length); } } } return 0; }