/** * \brief Constructor * * \note This also creates the settings file if it doesn't exist, otherwise * it opens it and calls the LoadSettings() function */ SettingsManager::SettingsManager() : _coin("EUR"), _showAll(true), _showFromDate(false), _showMonthly(false), _debtValue(0.0) { GUI::DeterminePlatform(); char path[Model::BUFF_SIZE]; maGetSystemProperty("mosync.path.local", path, Model::BUFF_SIZE); _settingsFileCompletePath = new MAUtil::String(path); if(false == GUI::_IPhoneOS)(*_settingsFileCompletePath) += "/"; (*_settingsFileCompletePath) += SETTINGS_FILE; _settingsFile = maFileOpen(_settingsFileCompletePath->c_str(), MA_ACCESS_READ_WRITE); if(1 == maFileExists(_settingsFile)) { LoadSettings(); maFileClose(_settingsFile); } else { maFileCreate(_settingsFile); maFileClose(_settingsFile); _date._day = 1; _date._mounth = 1; _date._year = 1601; ApplySettings(); } }
XML::~XML() { maFileClose(file); file = XML::file = maFileOpen(fname.c_str(), MA_ACCESS_READ_WRITE); maFileSeek(file, 0, MA_SEEK_END); CloseRoot(); maFileClose(file); delete _ActivityIndicator; }
// 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 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; }
/** * 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; }
static bool tryToWrite(const MAUtil::String& dir) { MAUtil::String filename = dir + "test.txt"; printf("Open '%s'\n", filename.c_str()); MAHandle file = maFileOpen(filename.c_str(), MA_ACCESS_READ_WRITE); if(file < 0) { printf("Error %i\n", file); return false; } int res = maFileExists(file); MAASSERT(res >= 0); if(res) { printf("File exists.\n"); } else { printf("Creating file...\n"); res = maFileCreate(file); if(res < 0) { printf("Error %i\n", res); return false; } } static const char data[] = "asfihu89ph4nma98fjioan9phadf89h239hdad9h89p\n"; printf("Writing %lu bytes...\n", sizeof(data)); res = maFileWrite(file, data, sizeof(data)); MAASSERT(res == 0); printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done.\n"); return true; }
static bool cleanOut(const char* dir) { printf("cleanOut(%s)\n", dir); MAHandle list = maFileListStart(dir, "*"); MAT(list); char buf[2048]; int dirLen = strlen(dir); strcpy(buf, dir); int freeBufSpace = sizeof(buf) - dirLen; while(1) { int res; MAHandle fh; char* fileName = buf + dirLen; res = maFileListNext(list, fileName, freeBufSpace); MAT_CUSTOM(res, (_res < 0 || _res >= freeBufSpace)); if(res == 0) return true; if(fileName[res-1] == '/') { if(!cleanOut(buf)) return false; } MAT(fh = maFileOpen(buf, MA_ACCESS_READ_WRITE)); res = maFileDelete(fh); MAASSERT(maFileClose(fh) == 0); MAT(res); } MAASSERT(maFileListClose(list) == 0); return true; }
/** * 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; }
MAUtil::String XML::getLocalPath() { // Do this here to work around a MoRE bug. FileLister fl; fl.start("/"); MAUtil::String path, os; // Try getting the local path. int result = getSystemProperty("mosync.path.local", path); getSystemProperty("mosync.device.OS", os); lprintfln("OS: %s", MAUtil::lowerString(os).c_str()); if(MAUtil::lowerString(os).find("android", 0) != -1) { MAHandle tfile; path = "/sdcard/Magna Carta/"; tfile = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); lprintfln("File handle: %d", tfile); lprintfln("File exists: %d", maFileExists(tfile)); if(!maFileExists(tfile)) { int result = maFileCreate(tfile); lprintfln("File create code: %d", result); } maFileClose(tfile); lprintfln("Path: %s", path.c_str()); return path; } // If it works, fine. if(result > 0) { //printf("Got local path: %i\n", result); return path; } // Otherwise, get the first root directory. fl.start(""); result = fl.next(path); //MAASSERT(result > 0); return path; }
void SettingsScreen::saveSettings() { //return; // Construct the filename. this->mScreen->initalizeHelper(mAppCodeBox->getText(), mAppUniqBox->getText(), Cloudbase::MD5(mAppPwdBox->getText()).hexdigest()); return; MAUtil::String filename = getLocalPath() + SETTINGS_FILE_NAME; // Open the file handle. printf("Open '%s'\n", filename.c_str()); MAHandle file = maFileOpen(filename.c_str(), MA_ACCESS_READ_WRITE); if(file < 0) { printf("Error opening file %i\n", file); return; } // If the file exists... int res = maFileExists(file); MAASSERT(res >= 0); if(res) { // Truncate it, deleting any old data. printf("Truncating file...\n"); res = maFileTruncate(file, 0); MAASSERT(res == 0); } else { // Otherwise, create it. printf("Creating file...\n"); res = maFileCreate(file); MAASSERT(res >= 0); } // In either case, we now have an empty file at our disposal. // Write some data. MAUtil::String settingsData = ""; settingsData += mAppCodeBox->getText(); settingsData += ","; settingsData += mAppUniqBox->getText(); settingsData += ","; settingsData += Cloudbase::MD5(mAppPwdBox->getText()).hexdigest(); //static const char data[] = strdup(settingsData.c_str()); res = maFileWrite(file, settingsData.c_str(), settingsData.length()); MAASSERT(res == 0); // Close the file. printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done.\n"); this->loadSettings(); //return true; }
/** * Create file/directory that does not exist. */ static bool FileCreate(const String& path) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return false; } int result = maFileCreate(file); maFileClose(file); return 0 == result; }
static bool FileExistsHelper(const String& path) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return false; } int exists = maFileExists(file); maFileClose(file); return 1 == exists; }
/** * 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; }
// each record has this format: { // char name[]; // null-terminated // byte data[]; // terminated by end of record. // } void setup_filesystem() { printf("setup_filesystem\n"); // first, we must find a temporary directory which we can work out of. // we'll probably want to chroot() to it, too. char newRoot[MAX_PATH] = ""; MAASSERT(makeDir(newRoot, 0, "mosync_root/", sizeof(newRoot))); int newRootLen = strlen(newRoot); MAASSERT(chdir(newRoot) == 0); MAASSERT(chroot(".") == 0); // now we have the root of a unix file-system. const MAHandle start = maFindLabel("start"); const MAHandle end = maFindLabel("end"); MAASSERT(start > 0 && end > 0); printf("%i files:\n", end - (start+1)); for(MAHandle i=start+1; i<end; i++) { char buf[MAX_PATH]; int size = maGetDataSize(i); int pathlen = MIN(MAX_PATH, size); maReadData(i, buf, 0, pathlen); const char* name = buf; int namelen = strlen(name); MAASSERT(namelen < MAX_PATH); int dataOffset = namelen+1; int dataLen = size - dataOffset; printf("%i: %s (%i bytes)\n", i, name, dataLen); bool isDirectory = name[namelen-1] == '/'; if(isDirectory) { // there can be no data in a directory. MAASSERT(dataLen == 0); MAASSERT(!mkdir(name, 0755)); } else { // Because we want to use maFileWriteFromData(), we can't use open() here. char realName[MAX_PATH]; MAHandle fh; bool res; memcpy(realName, newRoot, newRootLen); // overwrite the slash in newRoot, so we don't get double slashes. memcpy(realName + newRootLen - 1, name, namelen + 1); MAASSERT((fh = maFileOpen(realName, MA_ACCESS_READ_WRITE)) > 0); res = writeFile(fh, i, dataOffset, dataLen); MAASSERT(maFileClose(fh) >= 0); MAASSERT(res); } } }
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; }
/** * Write a text string to a file. * @return true on success, false on error. */ bool FileUtil::writeTextToFile( const MAUtil::String& filePath, const MAUtil::String& outText) { MAHandle file = openFileForWriting(filePath); if (file < 0) { return false; } int result = maFileWrite(file, outText.c_str(), outText.length()); maFileClose(file); return result == 0; }
/** * \brief This function is used for writing the settings to the settings file */ void SettingsManager::_writeSettings() { _settingsFile = maFileOpen(_settingsFileCompletePath->c_str(), MA_ACCESS_READ_WRITE); maFileTruncate(_settingsFile, 0); char buffer[Model::BUFF_SIZE]; MAUtil::String binaryMask; if(_showAll) binaryMask = "100"; else if(_showMonthly) binaryMask = "010"; else if(_showFromDate) binaryMask = "001"; sprintf(buffer, "%s|%d|%s|%s|%.2f", _coin.c_str(), _date._day, Model::DateStructToString(_date).c_str(), binaryMask.c_str(), _debtValue); maFileWrite(_settingsFile, buffer, strlen(buffer)); maFileClose(_settingsFile); }
bool XML::writeDataToXML(MAUtil::String filename) { MAHandle tfile; int dataLen = 256; //we set the dataLen to a fixed size for now. byte *data; //int fileSize; tfile = maFileOpen(filename.c_str(), MA_ACCESS_READ_WRITE); if(!maFileExists(tfile)) { lprintfln("File does not exist: %s", filename.c_str()); return false; } data = new byte[dataLen]; while(getRemaining(tfile)) { int rwResult; if(getRemaining(tfile) < 256) { dataLen = getRemaining(tfile); } rwResult = maFileRead(tfile, data, dataLen); rwResult += maFileWrite(file, data, dataLen); if(rwResult != 0) { return false; } memset(data, 0 , 256); } delete data; maFileClose(tfile); return true; }
/** * Get date of a file. * @return File date on the form "Mon Dec 19 2011 12:46:43 GMT+0100 (CET)". * Returns empty string on error. */ static String FileGetDate(const String& path) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return ""; } int date = maFileDate(file); if (date < 0) { // Error. return ""; } maFileClose(file); // Return time in format "Mon Dec 19 2011 12:46:43 GMT+0100 (CET)". return sprint_time(date); }
/** * Delete a file or directory. If the file is a * directory it must me empty. */ static int FileDeleteFile(const String& pathParam) { String path = pathParam; if (FileIsDirectory(path)) { FileMakeDirectoryPath(path); } MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } int result = maFileDelete(file); maFileClose(file); return result; }
static bool tryToMake(const char* dir) { MAHandle file = maFileOpen(dir, MA_ACCESS_READ_WRITE); MAT(file); int res = maFileExists(file); MAASSERT(res >= 0); if(res) { printf("Dir exists.\n"); // delete everything inside. return cleanOut(dir); } else { printf("Creating dir...\n"); MAT(maFileCreate(file)); } printf("Closing...\n"); res = maFileClose(file); MAASSERT(res == 0); printf("Done: %s\n", dir); return true; }
/** * Write a data object to a file. * @return true on success, false on error. */ bool FileUtil::writeDataToFile( const MAUtil::String& filePath, MAHandle outData) { MAHandle file = openFileForWriting(filePath); if (file < 0) { return false; } int result = maFileWriteFromData( file, outData, 0, maGetDataSize(outData)); maFileClose(file); return result == 0; }
/** * @return >0 on success, <0 on error. */ static int FileWrite(const String& path, const String& data, int position) { MAHandle file = maFileOpen(path.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } // TODO: Now we assume file must exist. Is that ok? //int exists = maFileExists(file); // TODO: Should we check that position is within file size? // int size = maFileSize(file); // If we start writing from the beginning of the file, we truncate // the file. Not clear what the specification says about this. // TODO: Check how this should work! It makes no sense to truncate here. // if (position == 0) // { // maFileTruncate(file, 0); // } int result = maFileSeek(file, position, MA_SEEK_SET); // New position must equal requested position. if (result != position) { return -1; } result = maFileWrite(file, data.c_str(), data.size()); if (result < 0) { return -1; } maFileClose(file); 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); }
/** * 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; }
/** * 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; }