sf::Vector3i ParseVector3i(const std::string theValue, const sf::Vector3i theDefault) { sf::Vector3i anResult = theDefault; // Try to find the first comma size_t anComma1Offset = theValue.find_first_of(',', 0); if(anComma1Offset != std::string::npos) { Int32 anX = ParseInt32(theValue.substr(0, anComma1Offset), theDefault.x); // Try to find the next comma size_t anComma2Offset = theValue.find_first_of(',',anComma1Offset+1); if(anComma2Offset != std::string::npos) { Int32 anY = ParseInt32(theValue.substr(anComma1Offset+1, anComma2Offset), theDefault.y); Int32 anZ = ParseInt32(theValue.substr(anComma2Offset+1), theDefault.z); // Now that all 3 values have been parsed, return the Vector3f found anResult.x = anX; anResult.y = anY; anResult.z = anZ; } } // Return the result found or theDefault assigned above return anResult; }
sf::IntRect ParseIntRect(const std::string theValue, const sf::IntRect theDefault) { sf::IntRect anResult = theDefault; // Try to find the first comma size_t anComma1Offset = theValue.find_first_of(','); if(anComma1Offset != std::string::npos) { sf::Int32 anLeft = ParseInt32(theValue.substr(0,anComma1Offset), theDefault.left); // Try to find the next comma size_t anComma2Offset = theValue.find_first_of(',',anComma1Offset+1); if(anComma2Offset != std::string::npos) { sf::Int32 anTop = ParseInt32(theValue.substr(anComma1Offset+1,anComma2Offset), theDefault.top); // Try to find the next comma size_t anComma3Offset = theValue.find_first_of(',',anComma2Offset+1); if(anComma3Offset != std::string::npos) { // Get the width and height values sf::Int32 anWidth = ParseInt32(theValue.substr(anComma2Offset+1,anComma3Offset), theDefault.width); sf::Int32 anHeight = ParseInt32(theValue.substr(anComma3Offset+1), theDefault.height); // Now that all 4 values have been parsed, return the color found anResult.left = anLeft; anResult.top = anTop; anResult.width = anWidth; anResult.height = anHeight; } } } // Return the result found or theDefault assigned above return anResult; }
TInt TPkgParser::ParseLineL(class CFileObjectHolder* &aFileList, char* line, TUint32& aTotalDataSize) { char *save_line = line; TInt ret = 0; while(line && isspace(*line) ){ //strip whitespace ++line; } if(*line == '\n' || *line == '\0'){ ret = 1; } else if (!line) { //We need a valid string. ret = 0; } else if (line[0] == '!') { /* This is the line with the total number of */ /* bytes to read. */ if((ret = ParseUint32(iTotalDataSize, ++line))){ aTotalDataSize = iTotalDataSize; } ret = 1; } else if(line[0] == '#'){ //skip comment lines. //end function ret = 1; } else if(line[0] == '@'){ //version line ret = ParseInt32(iVersion, ++line); } else { ret = ParseDataLineL(aFileList, line); } delete save_line; return ret; }
bool RaceTags::isCounterIncrementOrDecrementUpdate(const VarsInfo::VarData& var, int op_id) const { int read_cmd = VarsInfo::getCommandIdForVarReadInEventAction(var, op_id); int write_cmd = VarsInfo::getCommandIdForVarWriteInEventAction(var, op_id); if (read_cmd == -1 || write_cmd == -1) return false; if (read_cmd > write_cmd) return false; // Parse the read value. const char* read_value = getValueOfReadOrWrite(op_id, read_cmd); if (read_value == NULL) return false; int read_int; if (!ParseInt32(read_value, &read_int)) return false; // Parse the written value. const char* write_value = getValueOfReadOrWrite(op_id, write_cmd); if (write_value == NULL) return false; int write_int; if (!ParseInt32(write_value, &write_int)) return false; return abs(read_int - write_int) == 1; }
int UniValue::get_int() const { if (typ != VNUM) throw std::runtime_error("JSON value is not an integer as expected"); int32_t retval; if (!ParseInt32(getValStr(), &retval)) throw std::runtime_error("JSON integer out of range"); return retval; }
sf::Vector2i ParseVector2i(const std::string theValue, const sf::Vector2i theDefault) { sf::Vector2i anResult = theDefault; // Try to find the first comma size_t anCommaOffset = theValue.find_first_of(','); if(anCommaOffset != std::string::npos) { Int32 anX = ParseInt32(theValue.substr(0,anCommaOffset), theDefault.x); Int32 anY = ParseInt32(theValue.substr(anCommaOffset+1), theDefault.y); // Now that both values have been parsed, return the vector found anResult.x = anX; anResult.y = anY; } // Return the result found or theDefault assigned above return anResult; }
bool RaceTags::isValueTypeReadOrNull(int op_id, int cmd_id) const { const char* read_value = getValueOfReadOrWrite(op_id, cmd_id); if (read_value == NULL) return false; int read_int; if (ParseInt32(read_value, &read_int)) return true; return (strcmp(read_value, "undefined") == 0 || strcmp(read_value, "NULL") == 0 || strcmp(read_value, "true") == 0 || strcmp(read_value, "false") == 0); }
bool CHDKeyStore::DeriveKey(const CHDPubKey hdPubKey, CKey& keyOut) const { //this methode required no locking std::string chainPath = hdPubKey.chainPath; std::vector<std::string> pathFragments; boost::split(pathFragments, chainPath, boost::is_any_of("/")); CExtKey extKey; CExtKey parentKey; BOOST_FOREACH(std::string fragment, pathFragments) { bool harden = false; if (*fragment.rbegin() == '\'') { harden = true; fragment = fragment.substr(0,fragment.size()-1); } if (fragment == "m") { CExtKey bip32MasterKey; CKeyingMaterial masterSeed; // get master seed if (!GetMasterSeed(hdPubKey.chainHash, masterSeed)) return false; if (masterSeed.size() == BIP32_EXTKEY_SIZE) { //if the seed size matches the BIP32_EXTKEY_SIZE, we assume its a encoded ext priv key bip32MasterKey.Decode(&masterSeed[0]); } else bip32MasterKey.SetMaster(&masterSeed[0], masterSeed.size()); parentKey = bip32MasterKey; } else if (fragment == "c") { return false; } else { CExtKey childKey; int32_t nIndex; if (!ParseInt32(fragment,&nIndex)) return false; parentKey.Derive(childKey, (harden ? 0x80000000 : 0)+nIndex); parentKey = childKey; } }
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) { size_t colon = in.find_last_of(':'); // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator bool fHaveColon = colon != in.npos; bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos); if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) { int32_t n; if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) { in = in.substr(0, colon); portOut = n; } } if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']') hostOut = in.substr(1, in.size()-2); else hostOut = in; }
// Reads and returns a 32-bit integer stored in the environment // variable corresponding to the given flag; if it isn't set or // doesn't represent a valid 32-bit integer, returns default_value. Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) { const std::string env_var = FlagToEnvVar(flag); const char* const string_value = posix::GetEnv(env_var.c_str()); if (string_value == NULL) { // The environment variable is not set. return default_value; } Int32 result = default_value; if (!ParseInt32(Message() << "Environment variable " << env_var, string_value, &result)) { printf("The default value %s is used.\n", (Message() << default_value).GetString().c_str()); fflush(stdout); return default_value; } return result; }
// static status_t M3UParser::parseMetaData( const AString &line, sp<AMessage> *meta, const char *key) { ssize_t colonPos = line.find(":"); if (colonPos < 0) { return ERROR_MALFORMED; } int32_t x; status_t err = ParseInt32(line.c_str() + colonPos + 1, &x); if (err != OK) { return err; } if (meta->get() == NULL) { *meta = new AMessage; } (*meta)->setInt32(key, x); return OK; }
static bool BinaryParserParam(BinaryParser *self, const char *keyword, char *value) { if (CompareKeyword(keyword, "COL")) { BinaryParam(&self->fields, &self->nfield, value, self->preserve_blanks, false); if (self->fields[self->nfield - 1].character) self->fields[self->nfield - 1].str = palloc(self->fields[self->nfield - 1].len * MAX_CONVERSION_GROWTH + 1); } else if (CompareKeyword(keyword, "PRESERVE_BLANKS")) { self->preserve_blanks = ParseBoolean(value); } else if (CompareKeyword(keyword, "STRIDE")) { ASSERT_ONCE(self->rec_len == 0); self->rec_len = ParseInt32(value, 1); } else if (CompareKeyword(keyword, "SKIP") || CompareKeyword(keyword, "OFFSET")) { ASSERT_ONCE(self->offset < 0); self->offset = ParseInt64(value, 0); } else if (CompareKeyword(keyword, "FILTER")) { ASSERT_ONCE(!self->filter.funcstr); self->filter.funcstr = pstrdup(value); } else return false; /* unknown parameter */ return true; }
static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart) { if (!CheckWarmup(req)) return false; std::string param; const RetFormat rf = ParseDataFormat(param, strURIPart); std::vector<std::string> uriParts; if (param.length() > 1) { std::string strUriParams = param.substr(1); boost::split(uriParts, strUriParams, boost::is_any_of("/")); } // throw exception in case of an empty request std::string strRequestMutable = req->ReadBody(); if (strRequestMutable.length() == 0 && uriParts.size() == 0) return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request"); bool fInputParsed = false; bool fCheckMemPool = false; std::vector<COutPoint> vOutPoints; // parse/deserialize input // input-format = output-format, rest/getutxos/bin requires binary input, gives binary output, ... if (uriParts.size() > 0) { //inputs is sent over URI scheme (/rest/getutxos/checkmempool/txid1-n/txid2-n/...) if (uriParts.size() > 0 && uriParts[0] == "checkmempool") fCheckMemPool = true; for (size_t i = (fCheckMemPool) ? 1 : 0; i < uriParts.size(); i++) { uint256 txid; int32_t nOutput; std::string strTxid = uriParts[i].substr(0, uriParts[i].find("-")); std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1); if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid)) return RESTERR(req, HTTP_BAD_REQUEST, "Parse error"); txid.SetHex(strTxid); vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput)); } if (vOutPoints.size() > 0) fInputParsed = true; else return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request"); } switch (rf) { case RF_HEX: { // convert hex to bin, continue then with bin part std::vector<unsigned char> strRequestV = ParseHex(strRequestMutable); strRequestMutable.assign(strRequestV.begin(), strRequestV.end()); } case RF_BINARY: { try { //deserialize only if user sent a request if (strRequestMutable.size() > 0) { if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed"); CDataStream oss(SER_NETWORK, PROTOCOL_VERSION); oss << strRequestMutable; oss >> fCheckMemPool; oss >> vOutPoints; } } catch (const std::ios_base::failure& e) { // abort in case of unreadable binary data return RESTERR(req, HTTP_BAD_REQUEST, "Parse error"); } break; } case RF_JSON: { if (!fInputParsed) return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request"); break; } default: { return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")"); } } // limit max outpoints if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS) return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size())); // check spentness and form a bitmap (as well as a JSON capable human-readable string representation) std::vector<unsigned char> bitmap; std::vector<CCoin> outs; std::string bitmapStringRepresentation; std::vector<bool> hits; bitmap.resize((vOutPoints.size() + 7) / 8); { LOCK2(cs_main, mempool.cs); CCoinsView viewDummy; CCoinsViewCache view(&viewDummy); CCoinsViewCache& viewChain = *pcoinsTip; CCoinsViewMemPool viewMempool(&viewChain, mempool); if (fCheckMemPool) view.SetBackend(viewMempool); // switch cache backend to db+mempool in case user likes to query mempool for (size_t i = 0; i < vOutPoints.size(); i++) { bool hit = false; Coin coin; if (view.GetCoin(vOutPoints[i], coin) && !mempool.isSpent(vOutPoints[i])) { hit = true; outs.emplace_back(std::move(coin)); } hits.push_back(hit); bitmapStringRepresentation.append(hit ? "1" : "0"); // form a binary string representation (human-readable for json output) bitmap[i / 8] |= ((uint8_t)hit) << (i % 8); } } switch (rf) { case RF_BINARY: { // serialize data // use exact same output as mentioned in Bip64 CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs; std::string ssGetUTXOResponseString = ssGetUTXOResponse.str(); req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteReply(HTTP_OK, ssGetUTXOResponseString); return true; } case RF_HEX: { CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); ssGetUTXOResponse << chainActive.Height() << chainActive.Tip()->GetBlockHash() << bitmap << outs; std::string strHex = HexStr(ssGetUTXOResponse.begin(), ssGetUTXOResponse.end()) + "\n"; req->WriteHeader("Content-Type", "text/plain"); req->WriteReply(HTTP_OK, strHex); return true; } case RF_JSON: { UniValue objGetUTXOResponse(UniValue::VOBJ); // pack in some essentials // use more or less the same output as mentioned in Bip64 objGetUTXOResponse.push_back(Pair("chainHeight", chainActive.Height())); objGetUTXOResponse.push_back(Pair("chaintipHash", chainActive.Tip()->GetBlockHash().GetHex())); objGetUTXOResponse.push_back(Pair("bitmap", bitmapStringRepresentation)); UniValue utxos(UniValue::VARR); for (const CCoin& coin : outs) { UniValue utxo(UniValue::VOBJ); utxo.push_back(Pair("height", (int32_t)coin.nHeight)); utxo.push_back(Pair("value", ValueFromAmount(coin.out.nValue))); // include the script in a json output UniValue o(UniValue::VOBJ); ScriptPubKeyToUniv(coin.out.scriptPubKey, o, true); utxo.push_back(Pair("scriptPubKey", o)); utxos.push_back(utxo); } objGetUTXOResponse.push_back(Pair("utxos", utxos)); // return json string std::string strJSON = objGetUTXOResponse.write() + "\n"; req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strJSON); return true; } default: { return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")"); } } // not reached return true; // continue to process further HTTP reqs on this cxn }