Beispiel #1
0
	/**
	 * 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;
	}
Beispiel #2
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;
}
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;
}
Beispiel #4
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;
}
Beispiel #5
0
	/**
	 * @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);
}
	/**
	 * \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;
		}
	}