Пример #1
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;
}
Пример #2
0
void XML::CreateRoot(MAUtil::String filename)
{
	MAUtil::String root("<root filename=\"");
	
	root.append(filename.c_str(), filename.length());
	root.append("\">", 2);

	maFileWrite(file, root.c_str(), root.size());
}
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;
}
Пример #4
0
	/**
	 * 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;
	}
Пример #5
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);
	}
Пример #6
0
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;
}
Пример #7
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;
	}
Пример #8
0
void XML::WriteNode(Data data)
{

	MAUtil::String coords_tag = "<Point>\n";

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	coords_tag = "	<Coords>\n";

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	//Write the Lan and Lon values.
	coords_tag = "		<Lat>";
	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	MAUtil::String data2;
	data2 = MAUtil::doubleToString(data.loc.lat);

	maFileWrite(file, data2.c_str(), data2.size());

	coords_tag = "</Lat>\n";
	maFileWrite(file, coords_tag.c_str(), coords_tag.size());


	coords_tag = "		<Lon>";
	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	data2 = MAUtil::doubleToString(data.loc.lon);

	maFileWrite(file, data2.c_str(), data2.size());

	coords_tag = "</Lon>\n";
	maFileWrite(file, coords_tag.c_str(), coords_tag.size());
	//
	coords_tag = "	</Coords>\n";

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());


	if(data.imagePath.size() > 0)
	{
		_ActivityIndicator->show();

		byte *tdata;
		int dataLen;
		MAUtil::String img_tag = "	<Image>";


		lprintfln("Image file: %s", data.imagePath.c_str());

		maFileWrite(file, img_tag.c_str(), img_tag.size());
		//maFileWrite(file, data.imagePath.c_str(), data.imagePath.size());
		//Write the data to the xml

		if(!writeDataToXML(data.imagePath))
		{
			maPanic(0, "Couldn't locate the image file.");
		}

		//maFileWrite(file, tdata, dataLen);
		//End of data writting.

		img_tag = "</Image>\n";

		maFileWrite(file, img_tag.c_str(), img_tag.size());

		_ActivityIndicator->hide();
	}


	if(data.videoPath.size() > 0)
	{
		_ActivityIndicator->show();

		byte *tdata;
		int dataLen;
		MAUtil::String vid_tag = "	<Video>";

		maFileWrite(file, vid_tag.c_str(), vid_tag.size());
		//maFileWrite(file, data.videoPath.c_str(), data.videoPath.size());
		//Write the data to the xml

		lprintfln("Image file: %s", data.videoPath.c_str());

		if(!writeDataToXML(data.videoPath))
		{
			maPanic(0, "Couldn't locate the video file.");
		}

		//maFileWrite(file, tdata, dataLen);
		//End of data writting.

		vid_tag = "</Video>\n";

		maFileWrite(file, vid_tag.c_str(), vid_tag.size());

		_ActivityIndicator->hide();
	}

	if(data.text.size() > 0)
	{
		MAUtil::String text_tag = "	<Text>";

		maFileWrite(file, text_tag.c_str(), text_tag.size());
		maFileWrite(file, data.text.c_str(), data.text.size());

		text_tag = "</Text>\n";

		maFileWrite(file, text_tag.c_str(), text_tag.size());
	}


	//Write the date time.
	coords_tag = "	<DateTime>";

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	coords_tag = dateToString(data.time);

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	coords_tag = "</DateTime>\n";

	maFileWrite(file, coords_tag.c_str(), coords_tag.size());

	//End of date time.

	coords_tag = "</Point>";
	maFileWrite(file, coords_tag.c_str(), coords_tag.size());
}
Пример #9
0
void XML::CloseRoot()
{
	MAUtil::String root("</root>");
	maFileWrite(file, root.c_str(), root.size());
}
Пример #10
0
void PrintConsole(const wchar_t *str)
{
	int length, pos = 0;
	wchar_t* line;

	if (gConsoleLogging)
	{
		static const char prefix[] = "PrintConsole: ";
		maWriteLog(prefix, strlen(prefix));
		length = wcslen(str);
		if (length > 0)
		{
#ifdef MAPIP
			char buf8[length * MB_LEN_MAX];
#else
			char* buf8 = (char*)malloc(length * MB_LEN_MAX);
#endif
			int len8;
			//maWriteLog(str, length*sizeof(wchar_t));
			//convert to utf-8
			memset(buf8, 0, length * MB_LEN_MAX);
			len8 = wcstombs(buf8, str, length * MB_LEN_MAX);
			maWriteLog(buf8, len8);
			if (str[length - 1] != '\n')
				maWriteLog("\n", 1);
#ifndef MAPIP
			free(buf8);
#endif
		}
	}
	if(gConsoleFile > 0) {
		int res = maFileWrite(gConsoleFile, str, wcslen(str));
		if(res < 0) {
			maPanic(res, "PrintConsole maFileWrite");
		}
	}

	if (!sConsole.initialized)
		InitConsole();

	if (sConsole.postponedLineFeed)
	{
		FeedLine();
		sConsole.postponedLineFeed = 0;
	}

	while (str[pos] != '\0')
	{
		if (str[pos] == '\r')
			sConsole.cursorPos.x = 0;
		else if (str[pos] == '\n')
		{
			if (str[pos + 1] == '\0')
				sConsole.postponedLineFeed = 1;
			else
				FeedLine();
		}
		else
		{
			line = sConsole.lines[(sConsole.cursorPos.y + sConsole.firstLine) %
				sConsole.height].line;
			line[sConsole.cursorPos.x++] = str[pos];

			if (sConsole.cursorPos.x >= CONSOLE_WIDTH)
			{
				if (str[pos + 1] == '\0')
					sConsole.postponedLineFeed = 1;
				else
					FeedLine();
			}
		}

		pos++;
	}

	if(gConsoleForceDisplay)
		DisplayConsole();
}