bool Facebook::extractConnectionTypeAndId(FacebookRequest *req, MAUtil::String &connectionType, MAUtil::String &id) const { const MAUtil::String path = req->getPath(); int found = path.findFirstOf('/'); if(String::npos == found) { return false; } id = path.substr(0, found); connectionType = path.substr(found+1); return (connectionType.size()>0 && id.size()>0); }
/** * Helper function that unescapes a string. */ static MAUtil::String UnescapeHelper(const MAUtil::String& str) { // The decoded string. MAUtil::String result = ""; for (int i = 0; i < str.length(); ++i) { // If the current character is the '%' escape char... if ('%' == (char) str[i]) { // Get the char value of the two digit hex value. MAUtil::String hex = str.substr(i + 1, 2); long charValue = strtol( hex.c_str(), NULL, 16); // Append to result. result += (char) charValue; // Skip over the hex chars. i += 2; } else { // Not encoded, just copy the character. result += str[i]; } } return result; }
/** * Parse the received XML, and search for title& snippet attributes. */ void MediaWiki::processSearchResults() { /* for ex, try this request for "bear" in your browser: * http://en.wikipedia.org/w/api.php?action=query&format=xml& * list=search&srwhat=text&srsearch=bear * the output looks like this: * ... * <search> * <p ns="0" title="title1" snippet="text.." size="10283" * wordcount="1324" timestamp="2011-04-12T14:25:24Z" /> * more paragraphs * </search> * ... */ // Search for each p paragraph, and get the title & snippet. MAUtil::String input = mBuffer; int openTag = input.find("<p ",0); while( openTag != -1 ) { int closeTag = input.find("/>", openTag+1); if (closeTag != -1 && closeTag > openTag) { MAUtil::String record = input.substr( openTag+2, closeTag - openTag +1); // A record, ex: <p ns="0" title="title1" snippet="text.." // size="10283" wordcount="1324" timestamp="2011-04-12T14:25:24Z" /> // Add the title if it doesn't exist yet. MAUtil::String newRecord = getTitle(record); bool canAdd(true); for (int i=0; i < mWiki->titleResults.size(); i++) { if ( mWiki->titleResults[i] == newRecord ) { canAdd = false; } } if (canAdd) { mWiki->titleResults.add(newRecord); mWiki->snippetResults.add(getSnippet(record)); } } input.remove(0,closeTag); // Get the next tag. openTag = input.find("<p ",0); } }
/* * PublishingListener overwrite * FacebookPublisher2 registers itself as a PublishingListener of the mFacebook object. This will call the * FacebookPublisher2::publishingResponseReceived, sending the response from server in the "newId" param. The newId is * the id of new created object. * @param newId - the id of the new object. * @param path - contains the id of the object on which the publish request was made and the request name. * e.g: id/feed, id/likes * This function is called when a new object is created (Album, Like, Comment, StatusMessage ect). */ void FacebookPublisher2::publishingResponseReceived(const MAUtil::String &data, const MAUtil::String &path) { MAUtil::String id; int found = data.find(":",0); if(found != MAUtil::String::npos) { found = data.find("\"", found); int last = data.findLastOf('"'); if( (found != MAUtil::String::npos) && (last != MAUtil::String::npos)) { id = data.substr(found+1, last-found); } } mListener->publishingResponseReceived(id, path); }
/** * Parse paragraph, and get the title attribute. * @param input The input paragraph. * @return The title. */ MAUtil::String MediaWiki::getTitle(MAUtil::String inputStr) { MAUtil::String title = ""; int start = inputStr.find("title=",0); if ( start != -1 ){ int end = inputStr.find(" snippet=", start+1); if ( end != -1) { // Get rid of the quotes also // ( from beginning, and end): title="text.." snippet=etc title = inputStr.substr(start+7, end - start - 8); } } // Format the title to unicode. formatToUnicode(title); // Get rid of the span tags, and keep only the content: the searchTerm. trimSpanTags(title); return title; }
/** * Parse paragraph, and get the snippet attribute. * @param input The input paragraph. * @return The snippet. */ MAUtil::String MediaWiki::getSnippet(MAUtil::String inputStr) { MAUtil::String snippet = ""; int start = inputStr.find("snippet=",0); if ( start != -1 ){ int end = inputStr.find(" size=", start+1); if ( end != -1){ // Get rid of the quotes also( from beginning, and end) snippet = inputStr.substr(start+9, end - start - 10 ); } } // Format the snippet to unicode. formatToUnicode(snippet); // Get rid of the span tags, and keep only the content: the searchTerm. trimSpanTags(snippet); // Get rid of the bold tags from end, that enclose dots. trimBoldTags(snippet); return snippet; }
void string() { MAUtil::String str = "test"; assert("String::==", str == "test"); assert("String::!=", str != "fest"); assert("String::<", !(str < "fest") && (MAUtil::String("fest") < str)); assert("String::>", !(MAUtil::String("fest") > str) && (str > "fest")); assert("String::<=", str <= "test" && str <= "west"); assert("String::>=", str >= "test" && str >= "fest"); assert("String::+", (str + "ing") == "testing"); str+="ing"; assert("String::+=", str == "testing"); assert("String::find()", str.find("ing") == 4 && str.find("1") == MAUtil::String::npos); str+=" string"; assert("String::findLastOf()", str.findLastOf('g') == 13 && str.findLastOf('1') == MAUtil::String::npos); assert("String::findFirstOf()", str.findFirstOf('g') == 6 && str.findFirstOf('1') == MAUtil::String::npos); assert("String::findFirstNotOf()", str.findFirstNotOf('t') == 1 && str.findFirstNotOf('1') == 0); str.insert(7, " MAUtil::"); assert("String::insert(string)", str == "testing MAUtil:: string"); str.remove(16, 2); assert("String::remove()", str == "testing MAUtil::tring"); str.insert(16, 'S'); assert("String::insert(char)", str == "testing MAUtil::String"); assert("String::substr()", str.substr(8, 6) == "MAUtil"); assert("String::length()", str.length() == 22); str.reserve(32); assert("String::reserve()", str == "testing MAUtil::String" && str.length() == 22); assert("String::capacity()", str.capacity() == 32); str.clear(); assert("String::clear()", str.length() == 0 && str == ""); }
void SettingsScreen::loadSettings() { MAUtil::String filename = getLocalPath() + SETTINGS_FILE_NAME; MAHandle file = maFileOpen(filename.c_str(), MA_ACCESS_READ); if(file < 0) { printf("Error opening file %i\n", file); return; } // Check if the file exists. int res = maFileExists(file); MAASSERT(res >= 0); if(!res) { printf("File does not exist.\n"); maFileClose(file); return; } // Get the file size. int size = maFileSize(file); printf("Size: %i\n", size); MAASSERT(size >= 0); // Read the file data. static char data[200]; MAASSERT(size < (int)sizeof(data)); res = maFileRead(file, data, size); MAASSERT(res == 0); // Close the file. printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done.\n"); MAUtil::String contents = data; printf("Loaded settings string %s", contents.c_str()); if (contents.findFirstOf(',', 0) <= 0) return; int commaPosition = contents.findFirstOf(',', 0); MAUtil::String appCode = contents.substr(0, commaPosition); mAppCodeBox->setText(appCode); printf("app code: %s", appCode.c_str()); int prevCommaPosition = commaPosition + 1; commaPosition = contents.findFirstOf(',', prevCommaPosition); MAUtil::String appUniq = contents.substr(prevCommaPosition, commaPosition-prevCommaPosition); mAppUniqBox->setText(appUniq); printf("app uniq: %s", appUniq.c_str()); prevCommaPosition = commaPosition + 1; commaPosition = contents.findFirstOf(',', prevCommaPosition); MAUtil::String appPwd = contents.substr(prevCommaPosition, contents.length() - prevCommaPosition); //mAppPwdBox->setText(appPwd); printf("app pwd: %s", appPwd.c_str()); //helper = CBHelper(appCode, appUniq); //helper.setPassword(appPwd); this->mScreen->initalizeHelper(appCode, appUniq, appPwd); }
/** * Parse the message. This finds the message name and * creates a dictionary with the message parameters. */ void WebViewMessage::parse(MAHandle dataHandle) { // Set message name to empty string as default. mMessageName = ""; // We must have data. if (NULL == dataHandle) { return; } // Get length of the data, it is not zero terminated. int dataSize = maGetDataSize(dataHandle); // Allocate buffer for string data. char* stringData = (char*) malloc(dataSize + 1); // Get the data. maReadData(dataHandle, stringData, 0, dataSize); // Zero terminate. stringData[dataSize] = 0; // Create String object with message. MAUtil::String messageString = stringData; // Find schema. int start = messageString.find("mosync://"); if (0 != start) { return; } // Set start of message name. start = 9; // Find end of message name. int end = messageString.find("?", start); if (MAUtil::String::npos == end) { // No params, set message name to rest of string. mMessageName = messageString.substr(start); return; } // Set message name. mMessageName = messageString.substr(start, end - start); while (1) { // Find param name. start = end + 1; end = messageString.find("=", start); if (MAUtil::String::npos == end) { // No param name found, we are done. break; } // Set param name. MAUtil::String paramName = messageString.substr(start, end - start); MAUtil::String paramValue; // Find end of param value. start = end + 1; end = messageString.find("&", start); if (MAUtil::String::npos == end) { // Last param, set param value to rest of string. paramValue = messageString.substr(start); } else { paramValue = messageString.substr(start, end - start); } // Add param to table. mMessageParams.insert(unescape(paramName), unescape(paramValue)); // If no more params we are done. if (MAUtil::String::npos == end) { break; } } // Free string data. free(stringData); }
/** * \brief This function is used for reading the settings from the settings file */ void SettingsManager::_readSettings() { char settingsFileContent[Model::BUFF_SIZE]; MAUtil::String content; int fileSize = maFileSize(_settingsFile); maFileRead(_settingsFile, settingsFileContent, fileSize); content.append(settingsFileContent, strlen(settingsFileContent)); int offset = 0; int position = content.find("|", offset); _coin = content.substr(offset, position); //read the coin offset = position + 1; position = content.find("|", offset); int day = MAUtil::stringToInteger(content.substr(offset, position - offset), 10); //read the reset day offset = position + 1; position = content.find("|", offset); MAUtil::String dateString = content.substr(offset, position - offset); //read the dateString offset = position + 1; position = content.find("|", offset); MAUtil::String binaryMask = content.substr(offset, position - offset); //read the binary mask offset = position + 1; _debtValue = MAUtil::stringToDouble(content.substr(offset, content.length() - offset)); if(binaryMask == "100") { _showAll = true; _showMonthly = false; _showFromDate = false; } else if(binaryMask == "010") { _showAll = false; _showMonthly = true; _showFromDate = false; } else if(binaryMask == "001") { _showAll = false; _showMonthly = false; _showFromDate = true; } if(_showMonthly) { _date._day = day; struct tm * dateTime = new tm; split_time(maTime(), dateTime); _date._mounth = dateTime->tm_mon + 1; _date._year = dateTime->tm_year + 1900; delete dateTime; } else if(_showFromDate) { offset = 0; position = dateString.find("-", offset); _date._year = MAUtil::stringToInteger(dateString.substr(offset, position), 10); offset = position + 1; position = dateString.find("-", offset); _date._mounth = MAUtil::stringToInteger(dateString.substr(offset, position - offset), 10); offset = position + 1; _date._day = MAUtil::stringToInteger(dateString.substr(offset, dateString.length() - offset), 10); } else if(_showAll) { _date._day = 1; _date._mounth = 1; _date._year = 1601; } }