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 SMV::buttonClicked(Widget* button) { mEditBox->hideKeyboard(); MAUtil::String str; str = mEditBox->getText(); strcpy(engine->url_path, str.c_str()); int str_c = strlen(engine->url_path); if(engine->url_path[str_c-1] != '/') { engine->url_path[str_c++] = '/'; engine->url_path[str_c] = '\0'; //mEditBox->setText(engine->url_path); } MAHandle directory = maFileOpen(engine->url_path, MA_ACCESS_READ); if (directory < 0) maMessageBox("Uwaga!", "Blad FileOpen!"); else { if (!maFileExists(directory)) maMessageBox("Uwaga!", "Katalog nie istnieje!"); else { maWidgetScreenShow(0); int res = maLocationStart(); if(res<0) maPanic(1, "No GPS available"); engine->read_conf_file(); engine->env_init(); engine->draw(); } } }
// 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; }
XML::XML(MAUtil::String filename) { MAUtil::String file = getLocalPath()+filename; file += ".xml"; fname = file; XML::file = maFileOpen(file.c_str(), MA_ACCESS_READ_WRITE); int res = maFileExists(XML::file); if(res == 1) { maFileTruncate(XML::file, 0); } else if(res == 0) { maFileCreate(XML::file); } else { maPanic(0, "Error on file creation."); } CreateRoot(filename); _ActivityIndicator = new WaitMessage("Please, wait...", "Saving data to file."); }
/** * Open a file for writing. * Create the file if it does not exist. * Note: Will truncate the file if it exists. * @return Handle to the open file, <0 on error. */ MAHandle FileUtil::openFileForWriting(const MAUtil::String& filePath) { MAHandle file = maFileOpen(filePath.c_str(), MA_ACCESS_READ_WRITE); if (file < 0) { return -1; } if (maFileExists(file)) { // If the file exists, truncate it to zero size. // We do this to prevent problems with old data // at the end of the file if the new file is // shorter than the old file. maFileTruncate(file, 0); } else { // If the file does not exist, create it. int result = maFileCreate(file); if (result < 0) { return -1; } } return file; }
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; }
/** * \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(); } }
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; }
static bool writeFile(MAHandle fh, MAHandle data, int dataOffset, int dataLen) { int exists; MAT(exists = maFileExists(fh)); if(exists) { MAT(maFileTruncate(fh, 0)); } else { MAT(maFileCreate(fh)); } MAT(maFileWriteFromData(fh, data, dataOffset, dataLen)); return true; }
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; }
/** * Open a file for reading. * @return Handle to the open file, <0 on error. */ MAHandle FileUtil::openFileForReading(const MAUtil::String& filePath) { MAHandle file = maFileOpen(filePath.c_str(), MA_ACCESS_READ); if (file < 0) { return -1; } if (!maFileExists(file)) { return -1; } return file; }
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; }
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; }
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; }
/** * 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; }
static bool deleteIfExist(const MAUtil::String& path) { MAHandle file = maFileOpen(path.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"); res = maFileDelete(file); if(res < 0) { printf("Error %i\n", res); return false; } printf("Deleted.\n"); } else { printf("File doesn't exist.\n"); } return true; }
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; }