Example #1
0
void fileio_do_state_save(void)
{
	Nes::Api::Machine machine( emulator );
	GtkWidget *dialog;
	char defname[512];

	defname[0] = '\0';
	strcpy(defname, rootname);
	strcat(defname, ".nst");

	dialog = gtk_file_chooser_dialog_new ("Save state (.nst)",
					      GTK_WINDOW(mainwindow),
					      GTK_FILE_CHOOSER_ACTION_SAVE,
					      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
					      NULL);

	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), defname);

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));

		std::ofstream stateFile( filename, std::ifstream::out|std::ifstream::binary );

		if (stateFile.is_open())
		{
			machine.SaveState(stateFile);
		}

		g_free (filename);
	}

	gtk_widget_destroy(dialog);
}
Example #2
0
void fileio_do_state_load(void)
{
	Nes::Api::Machine machine( emulator );
	GtkWidget *dialog;
	GtkFileFilter *filter;

	dialog = gtk_file_chooser_dialog_new ("Load state (.nst)",
					      GTK_WINDOW(mainwindow),
					      GTK_FILE_CHOOSER_ACTION_OPEN,
					      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					      NULL);

	filter = gtk_file_filter_new();
	gtk_file_filter_set_name(filter, "Nestopia save states");
	gtk_file_filter_add_pattern(filter, "*.nst");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);

	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
	{
		char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));

		std::ifstream stateFile( filename, std::ifstream::in|std::ifstream::binary );

		if (stateFile.is_open())
		{
			machine.LoadState(stateFile);
		}

		g_free (filename);
	}

	gtk_widget_destroy(dialog);
}
bool GameDatabaseManager::writeState(const QString& fileName)
{
	QFile stateFile(fileName);

	if (!stateFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
		return false;

	QDataStream out(&stateFile);
	out.setVersion(QDataStream::Qt_4_6); // don't change

	// Write magic number and version
	out << (quint32)GAME_DATABASE_STATE_MAGIC;
	out << (quint32)GAME_DATABASE_STATE_VERSION;

	// Write the number of databases
	out << (qint32)m_databases.count();

	// Write the contents of the databases
	for (const PgnDatabase* db : qAsConst(m_databases))
	{
		out << db->fileName();
		out << db->lastModified();
		out << db->displayName();
		out << (qint32)db->entries().count();

		const auto entries = db->entries();
		for (const PgnGameEntry* entry : entries)
			entry->write(out);
	}

	m_modified = false;

	return true;
}
Example #4
0
	void MainWindow::loadState() {
		QFile state_file(stateFile());
		if (not state_file.open(QIODevice::ReadOnly))
			return;

		restoreState(state_file.readAll(), FONTMATE_STATE_VERSION);
	}
Example #5
0
bool BooksDB::initDatabase() {
	if (isInitialized()) {
		return true;
	}

	if (!open()) {
		return false;
	}

	myInitialized = true;

	ZLFile stateFile(databaseDirName() + ZLibrary::FileNameDelimiter + STATE_DATABASE_NAME);
	shared_ptr<DBCommand> cmd = SQLiteFactory::createCommand(BooksDBQuery::PREINIT_DATABASE, connection(), "@stateFile", DBValue::DBTEXT);
	((DBTextValue&)*cmd->parameter("@stateFile").value()) = stateFile.physicalFilePath();
	if (!cmd->execute()) {
		myInitialized = false;
		close();
		return false;
	}

	shared_ptr<DBRunnable> runnable = new InitBooksDBRunnable(connection());
	if (!executeAsTransaction(*runnable)) {
		myInitialized = false;
		close();
		return false;
	}

	return true;
}
Example #6
0
void ClientStatus::read()
{
    std::ifstream stateFile(filename_);
    std::string value;
    std::string token;

    /* First read topicList hash */
    if (stateFile.good()) {
        std::getline(stateFile, token, '=');
        std::getline(stateFile, value);

        std::stringstream stream(value);
        std::int64_t topicId;
        std::int32_t sqn;
        while ((stream >> topicId) && (stream >> sqn)) {
            topicStates_.insert(std::make_pair(topicId, sqn));
        }
    }
Example #7
0
bool Queue::readJsonSettingsFromFile(const QString &stateFilename,
                                     bool importOnly,
                                     bool includePrograms)
{
  if (!QFile::exists(stateFilename))
    return false;

  QFile stateFile(stateFilename);
  if (!stateFile.open(QFile::ReadOnly | QFile::Text)) {
    Logger::logError(tr("Cannot read queue information from %1.")
                     .arg(stateFilename));
    return false;
  }

  // Try to read existing data in
  QByteArray inputText = stateFile.readAll();
  QJsonParseError error;
  QJsonDocument doc = QJsonDocument::fromJson(inputText, &error);
  if (error.error != QJsonParseError::NoError) {
    Logger::logError(tr("Error parsing queue state from %1: %2\n%3")
                     .arg(stateFilename)
                     .arg(tr("%1 (at offset %2)")
                          .arg(error.errorString())
                          .arg(error.offset))
                     .arg(inputText.data()));
    stateFile.close();
    return false;
  }

  if (!doc.isObject()) {
    Logger::logError(tr("Error reading queue state from %1: "
                        "root is not an object!\n%2")
                     .arg(stateFilename)
                     .arg(inputText.data()));
    stateFile.close();
    return false;
  }

  return readJsonSettings(doc.object(), importOnly, includePrograms);
}
Example #8
0
bool Program::exportSettings(const QString &fileName) const
{
  QFile stateFile(fileName);
  if (!stateFile.open(QFile::ReadWrite | QFile::Text | QFile::Truncate)) {
    Logger::logError(tr("Cannot save program information for %1 in queue %2: "
                        "Cannot open file %3.").arg(name())
                     .arg(m_queue->name()).arg(fileName));
    return false;
  }

  QJsonObject root;
  if (!this->writeJsonSettings(root, true)) {
    stateFile.close();
    return false;
  }

  // Write the data back out:
  stateFile.write(QJsonDocument(root).toJson());
  stateFile.close();

  return true;
}
Example #9
0
bool Queue::writeJsonSettingsToFile(const QString &stateFilename,
                                    bool exportOnly,
                                    bool includePrograms) const
{
  QFile stateFile(stateFilename);
  if (!stateFile.open(QFile::ReadWrite | QFile::Text | QFile::Truncate)) {
    Logger::logError(tr("Cannot save queue information for queue %1 in %2: "
                        "Cannot open file.").arg(name()).arg(stateFilename));
    return false;
  }

  QJsonObject root;
  if (!this->writeJsonSettings(root, exportOnly, includePrograms)) {
    stateFile.close();
    return false;
  }

  // Write the data back out:
  stateFile.write(QJsonDocument(root).toJson());
  stateFile.close();

  return true;
}
Example #10
0
QString Queue::queueTypeFromFile(const QString &mqqFile)
{
  QString result;
  if (!QFile::exists(mqqFile))
    return result;

  QFile stateFile(mqqFile);
  if (!stateFile.open(QFile::ReadOnly | QFile::Text))
    return result;
  QByteArray inputText = stateFile.readAll();
  stateFile.close();

  // Try to read existing data in
  QJsonParseError error;
  QJsonDocument doc = QJsonDocument::fromJson(inputText, &error);
  if (error.error != QJsonParseError::NoError || !doc.isObject())
    return result;

  if (doc.object().value("type").isString())
    result = doc.object().value("type").toString();

  return result;
}
Example #11
0
bool Program::importSettings(const QString &fileName)
{
  if (!QFile::exists(fileName))
    return false;

  QFile stateFile(fileName);
  if (!stateFile.open(QFile::ReadOnly | QFile::Text)) {
    Logger::logError(tr("Cannot read program information from %1.")
                     .arg(fileName));
    return false;
  }
  QByteArray inputText = stateFile.readAll();
  stateFile.close();

  // Try to read existing data in
  QJsonParseError error;
  QJsonDocument doc = QJsonDocument::fromJson(inputText, &error);
  if (error.error != QJsonParseError::NoError) {
    Logger::logError(tr("Error parsing program state from %1: %2\n%3")
                     .arg(fileName)
                     .arg(tr("%1 (at offset %2)")
                          .arg(error.errorString())
                          .arg(error.offset))
                     .arg(inputText.data()));
    return false;
  }

  if (!doc.isObject()) {
    Logger::logError(tr("Error reading program state from %1: "
                        "root is not an object!\n%2")
                     .arg(fileName)
                     .arg(inputText.data()));
    return false;
  }

  return readJsonSettings(doc.object(), true);
}
Example #12
0
	void MainWindow::saveState() {
		QFile state_file(stateFile());
		state_file.open(QIODevice::WriteOnly | QIODevice::Truncate);
		state_file.write(QMainWindow::saveState(FONTMATE_STATE_VERSION));
	}
bool GameDatabaseManager::readState(const QString& fileName)
{
	QFile stateFile(fileName);

	if (!stateFile.open(QIODevice::ReadOnly))
		return false;

	QDataStream in(&stateFile);
	in.setVersion(QDataStream::Qt_4_6); // don't change

	// Read and verify the magic value
	quint32 magic;
	in >> magic;

	if (magic != GAME_DATABASE_STATE_MAGIC)
	{
		qWarning("GameDatabaseManager: bad magic value in state file");
		return false;
	}

	// Read and verify the version number
	quint32 version;
	in >> version;

	if (version < GAME_DATABASE_STATE_VERSION ||
	    version > GAME_DATABASE_STATE_VERSION)
	{
		// TODO: Add backward compatibility
		qWarning("GameDatabaseManager: state file version mismatch");
		return false;
	}

	// Read the number of databases
	qint32 dbCount;
	in >> dbCount;

	// Read the contents of the databases
	QString dbFileName;
	QDateTime dbLastModified;
	QString dbDisplayName;
	QList<PgnDatabase*> readDatabases;

	for (int i = 0; i < dbCount; i++)
	{
		in >> dbFileName;
		in >> dbLastModified;
		in >> dbDisplayName;

		// Check if the database exists
		QFileInfo fileInfo(dbFileName);
		if (!fileInfo.exists())
		{
			m_modified = true;
			continue;
		}

		// Check if the database has been modified
		if (fileInfo.lastModified() > dbLastModified)
		{
			m_modified = true;
			importPgnFile(dbFileName);
			continue;
		}

		qint32 dbEntryCount;
		in >> dbEntryCount;

		// Read the entries
		QList<const PgnGameEntry*> entries;
		for (int j = 0; j < dbEntryCount; j++)
		{
			PgnGameEntry* entry = new PgnGameEntry;
			entry->read(in);
			entries << entry;
		}

		PgnDatabase* db = new PgnDatabase(dbFileName);
		db->setEntries(entries);
		db->setLastModified(dbLastModified);
		db->setDisplayName(dbDisplayName);

		readDatabases << db;
	}

	m_modified = false;

	m_databases = readDatabases;
	emit databasesReset();

	return true;
}