StringArray getMultiConfigItem(const ConfigMap& configItems, const String &itemName, const StringArray& defRetVal, const char* tokenizeSeparator) { ConfigMap::const_iterator item = configItems.find(itemName); if (item != configItems.end()) { StringArray rv; rv.reserve(item->second.size()); for (size_t i = 0; i < item->second.size(); ++i) { if (tokenizeSeparator) { StringArray tokenizedValue(item->second[i].value.tokenize(tokenizeSeparator)); rv.insert(rv.end(), tokenizedValue.begin(), tokenizedValue.end()); } else { rv.push_back(item->second[i].value); } } return rv; } else { return defRetVal; } }
// ------------------------------------------------------------------- // GetObject // ------------------------------------------------------------------- boost::shared_ptr<void> XMLNode::GetObject(boost::shared_ptr<string> path) { std::vector<string> bits; boost::shared_ptr<XMLNode> currentNode = shared_from_this(); boost::shared_ptr<XMLNodeList> currentNodeList = boost::shared_ptr<XMLNodeList>(); bool listMode = false; boost::shared_ptr<void> ob; /* boost::regex re(">"); boost::sregex_token_iterator i(path->begin(), path->end(), re, -1); boost::sregex_token_iterator j; while (i != j) { string tokenizedValue = *i; bits.push_back(tokenizedValue.substr(1, tokenizedValue.size() - 1)); i++; } */ char* pChunk; char delims[] = ">"; pChunk = strtok ((char*)path->c_str(), delims); while (pChunk != NULL) { string tokenizedValue(pChunk); bits.push_back(tokenizedValue.substr(1, tokenizedValue.size() - 1)); pChunk = strtok (NULL, delims); } for (unsigned long int i = 0; i < bits.size(); i++) { if (listMode) { list<boost::shared_ptr<XMLNode> >::iterator iteratorNodeList = currentNodeList->begin(); std::advance(iteratorNodeList, atoi(bits[i].c_str())); currentNode = *iteratorNodeList; ob = currentNode; listMode = false; } else { map<string, tagXMLObject>::iterator iteratorNode = currentNode->find(bits[i]); tagXMLObject xmlObject = iteratorNode->second; if (xmlObject.type == XMLObjectType_XMLNodeList) { currentNodeList = (boost::static_pointer_cast<XMLNodeList>)(xmlObject.XMLObjectInstance); listMode = true; } else { // reached a leaf node/attribute if (i != (bits.size() - 1)) { // unexpected leaf node string actualPath = ""; for (unsigned long j = 0; j <= i; j++) { actualPath = actualPath + ">" + bits[j]; } //Debug.Log("xml path search truncated. Wanted: " + path + " got: " + actualPath); } return ob; } } } if (listMode) return currentNodeList; else return currentNode; }
// ------------------------------------------------------------------- // UploadValuesAsync // ------------------------------------------------------------------- void SFSWebClient::UploadValuesAsync (boost::shared_ptr<string> uriHost, unsigned short int uriPort, boost::shared_ptr<string> paramName, boost::shared_ptr<string> encodedData) { boost::shared_ptr<TCPClient> client = boost::shared_ptr<TCPClient>(); try { client = boost::shared_ptr<TCPClient>(new TCPClient(boostIoService)); boost::shared_ptr<IPAddress> address (new IPAddress(IPAddress::IPADDRESSTYPE_IPV4, *uriHost)); client->SynchConnect(address, uriPort); } catch (exception e) { boost::shared_ptr<string> messageException (new string(e.what())); boost::shared_ptr<string> message (new string("Http error creating http connection: " + (*messageException))); OnHttpResponse()->Invoke(true, message); try { client->Shutdown(); } catch (exception e) { boost::shared_ptr<string> messageException (new string(e.what())); boost::shared_ptr<string> message (new string("Error during http scocket shutdown: " + (*messageException))); OnHttpResponse()->Invoke(true, message); } return; } try { string data = (*paramName) + "=" + (*encodedData); boost::shared_ptr<wstring> dataAsNative (new wstring()); dataAsNative->assign(data.begin(), data.end()); boost::shared_ptr<string> dataAsUtf8 (new string()); WStrToUtf8(dataAsNative, dataAsUtf8); string sb; sb.append("POST /" + (*BBClient::BB_SERVLET) + " HTTP/1.0\r\n"); sb.append("Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"); boost::shared_ptr<string> valueContentLength (new string()); boost::shared_ptr<string> format (new string ("Content-Length: %ld\r\n")); StringFormatter<long int> (valueContentLength, format, dataAsUtf8->size()); sb.append(*valueContentLength); sb.append("\r\n"); sb.append(data); boost::shared_ptr<vector<unsigned char> > outputData (new vector<unsigned char>(sb.begin(), sb.end())); outputData->push_back('\0'); client->SynchWrite(outputData); string responseFromServer; do { boost::shared_ptr<vector<unsigned char> > receive = client->SynchRead(); if (receive->size() == 0) { break; } boost::shared_ptr<string> src (new string(receive->begin(), receive->end())); boost::shared_ptr<wstring> dest (new wstring()); Utf8toWStr(src, dest); responseFromServer.append(dest->begin(), dest->end()); } while (true); // Remove http header and trim whitespaces at the end std::vector<string> parts; /* boost::regex re("\\r\\n\\r\\n"); boost::sregex_token_iterator i(responseFromServer.begin(), responseFromServer.end(), re, -1); boost::sregex_token_iterator j; while (i != j) { parts.push_back(*i++); } */ const char* chunkCurrent = responseFromServer.c_str(); const char* chunkNext = chunkCurrent; while ((chunkNext = strstr(chunkCurrent, "\r\n\r\n")) != NULL) { string tokenizedValue (chunkCurrent, chunkNext - chunkCurrent); parts.push_back(tokenizedValue); chunkCurrent = chunkNext + 4; } string tokenizedValueLast (chunkCurrent); if (tokenizedValueLast.length() > 0) { parts.push_back(tokenizedValueLast); } if ((responseFromServer.size() > 4) && (responseFromServer.compare(responseFromServer.size() - 4, 4, "\r\n\r\n") == 0)) { parts.push_back(""); } if (parts.size() < 2) { OnHttpResponse()->Invoke(true, boost::shared_ptr<string>(new string ("Error during http response: connection closed by remote side"))); return; } string charsToTrim = " "; string dataPayload = parts[1].substr(0, parts[1].find_last_not_of(charsToTrim) + 1); // Sending to the higher layer boost::shared_ptr<string> payload (new string(dataPayload)); OnHttpResponse()->Invoke(false, payload); } catch (exception e) { boost::shared_ptr<string> messageException (new string(e.what())); boost::shared_ptr<string> message (new string("Error during http request: " + (*messageException))); OnHttpResponse()->Invoke(true, message); } try { client->Shutdown(); } catch (exception e) { boost::shared_ptr<string> messageException (new string(e.what())); boost::shared_ptr<string> message (new string("Error during http scocket shutdown: " + (*messageException))); OnHttpResponse()->Invoke(true, message); } }