Ejemplo n.º 1
0
	/**
	 * 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;
	}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
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.");
}
Ejemplo n.º 4
0
	/**
	 * \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;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
	/**
	 * 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;
	}
Ejemplo n.º 9
0
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;
}