/** * Read a text string from a file. * @return true on success, false on error. */ bool FileUtil::readTextFromFile( const MAUtil::String& filePath, MAUtil::String& inText) { MAHandle file = openFileForReading(filePath); if (file < 0) { return false; } int size = maFileSize(file); if (size < 1) { return false; } // Allocate buffer with space for a null termination character. char* buffer = (char*) malloc(sizeof(char) * (size + 1)); int result = maFileRead(file, buffer, size); maFileClose(file); buffer[size] = 0; inText = buffer; return result == 0; }
// Reads a log file from a s60v3 debug runtime. static bool tryToRead() { MAUtil::String filename = "C:/Data/msrlogold.txt"; printf("Open '%s'\n", filename.c_str()); MAHandle file = maFileOpen(filename.c_str(), MA_ACCESS_READ); if(file < 0) { printf("Error %i\n", file); return false; } int res = maFileExists(file); MAASSERT(res >= 0); if(!res) { printf("File does not exist.\n"); return false; } int size = maFileSize(file); printf("Size: %i\n", size); MAASSERT(size >= 0); static char data[32*1024]; MAASSERT(size < (int)sizeof(data)); res = maFileRead(file, data, size); MAASSERT(res == 0); data[32] = 0; printf("%s\n", data); printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done.\n"); return true; }
/** * Read a data object from a file. * @param filePath Full path of file to read. * @param inPlaceholder Placeholder handle for data object. * @return true on success, false on error. */ bool FileUtil::readDataFromFile( const MAUtil::String& filePath, MAHandle inPlaceholder) { MAHandle file = openFileForReading(filePath); if (file < 0) { return false; } int size = maFileSize(file); if (size < 1) { return false; } int result = maCreateData(inPlaceholder, size); if (RES_OK != result) { return false; } result = maFileReadToData(file, inPlaceholder, 0, size); maFileClose(file); return result == 0; }
/** * Get size of a file. * @return File size on success, <0 on error. */ static int FileGetSize(const String& path) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } int size = maFileSize(file); maFileClose(file); return size; }
char *Controller::readSource(const char *fileName) { char *buffer = NULL; bool networkFile = strstr(fileName, "://"); const char *delim = strchr(fileName, '?'); int len = strlen(fileName); int endIndex = delim ? (delim - fileName) : len; if (delim && !networkFile) { strcpy(opt_command, delim + 1); } _mainBas = false; trace("readSource %s %d %s", fileName, endIndex, opt_command); if (networkFile) { buffer = readConnection(fileName); } else if (strncasecmp("main.bas", fileName, endIndex) == 0) { // load as resource int len = maGetDataSize(MAIN_BAS); buffer = (char *)tmp_alloc(len + 1); maReadData(MAIN_BAS, buffer, 0, len); buffer[len] = '\0'; _mainBas = true; } else { // load from file system MAHandle handle = maFileOpen(fileName, MA_ACCESS_READ); if (maFileExists(handle)) { int len = maFileSize(handle); buffer = (char *)tmp_alloc(len + 1); maFileRead(handle, buffer, len); buffer[len] = '\0'; } maFileClose(handle); } if (buffer == NULL) { buffer = (char *)tmp_alloc(strlen(ERROR_BAS) + 1); strcpy(buffer, ERROR_BAS); } delete [] _programSrc; len = strlen(buffer); _programSrc = new char[len + 1]; strncpy(_programSrc, buffer, len); _programSrc[len] = 0; logPrint("Opened: %s %d bytes\n", fileName, len); return buffer; }
/** * Truncate a file. * @return New file length on success, <0 on error. */ static int FileTruncate(const String& path, int size) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } int exists = maFileExists(file); if (1 != exists) { // Error. maFileClose(file); return -1; } int fileSize = maFileSize(file); if (fileSize < 0) { // Error. maFileClose(file); return -1; } if (fileSize < size) { // No need to truncate, return current file size. maFileClose(file); return fileSize; } int result = maFileTruncate(file, size); maFileClose(file); if (0 == result) { // Success, return truncated size. return size; } // Error. return -1; }
/** * @return >0 on success, <0 on error. */ static int FileRead(const String& path, String& data) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } int size = maFileSize(file); if (size < 0) { return -1; } char* buf = (char*) malloc(size + 1); if (NULL == buf) { return -1; } int result = maFileRead(file, buf, size); maFileClose(file); if (result < 0) { free(buf); return -1; } buf[size] = 0; data = buf; free(buf); return 1; }
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); }
int XML::getSize() { return maFileSize(XML::file); }
int XML::getRemaining(MAHandle file) { return maFileSize(file) - maFileTell(file); }
/** * Loads an image from a file and returns the handle to it. * * @param imagePath relative path to the image file. */ MAHandle ResourceMessageHandler::loadImageResource(const char* imagePath) { if (!getFileUtil()) { return 0; } // Get the current apllication directory path. String appPath = getFileUtil()->getAppPath(); // Construct image path. char completePath[2048]; sprintf(completePath, "%s%s", appPath.c_str(), imagePath); // Load the image and create a data handle from it. MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ); if (imageFile < 0) { return 0; } int fileSize = maFileSize(imageFile); if (fileSize < 1) { return 0; } // Create buffer to hold file data. MAHandle fileData = maCreatePlaceholder(); int result = maCreateData(fileData, fileSize); if (RES_OK != result) { maDestroyPlaceholder(fileData); return 0; } // Read data from file. result = maFileReadToData(imageFile, fileData, 0, fileSize); maFileClose(imageFile); if (result < 0) { maDestroyPlaceholder(fileData); return 0; } // Create image. MAHandle imageHandle = maCreatePlaceholder(); result = maCreateImageFromData( imageHandle, fileData, 0, maGetDataSize(fileData)); maDestroyPlaceholder(fileData); if (RES_OK != result) { maDestroyPlaceholder(imageHandle); return 0; } // Return the handle to the loaded image. return imageHandle; }
/** * Copy a file. Overwrites the destination file. * @return 0 on success <0 on error. */ static int FileCopyFile( const String& sourcePath, const String& destinationPath) { // Open source file. MAHandle sourceFile = maFileOpen(sourcePath.c_str(), MA_ACCESS_READ_WRITE); if (sourceFile < 0) { return -1; } // Check that source file exists. int exists = maFileExists(sourceFile); if (1 != exists) { maFileClose(sourceFile); return -1; } // Get and check source size. int fileSize = maFileSize(sourceFile); if (fileSize < 0) { maFileClose(sourceFile); return -1; } // Create data object for source data to copy. MAHandle data = maCreatePlaceholder(); int createDataResult = maCreateData(data, fileSize); if (RES_OK != createDataResult) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } int readResult = maFileReadToData(sourceFile, data, 0, fileSize); if (readResult < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // This deletes the destination file if it already exists. FileDeleteFile(destinationPath); // Create destination file. bool createSuccess = FileCreatePath(destinationPath); if (!createSuccess) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Open destination file. MAHandle destinationFile = maFileOpen(destinationPath.c_str(), MA_ACCESS_READ_WRITE); if (destinationFile < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Write data to destination file. int writeResult = maFileWriteFromData(destinationFile, data, 0, fileSize); if (writeResult < 0) { maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); return -1; } // Close files and free data object. maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); // Success. return 0; }
/** * \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; } }