Exemplo n.º 1
0
void History::parseHistory(const std::string &directory)
{
    std::vector<std::string> log_files = getLogFiles(directory);
    for(auto &file : log_files)
    {
        // First of all, we need to parse the file name itself

        // Extract hostname
        auto hostname_pos = file.find_first_of('-');
        std::string hostname = file.substr(0, hostname_pos);

        // Extract rank
        auto rank_pos = file.find_first_of('_') - 1;
        uint16_t rank  = std::stoi(file.substr(hostname_pos + 1, rank_pos));

        // Extract tick
        auto tick_pos = file.find_first_of('-', rank_pos) + 1;
        uint32_t tick = std::stoi(file.substr(tick_pos));

        this->history_states.emplace(tick, std::unique_ptr<HistoryState> (new HistoryState(hostname, rank, tick)));

        // Now parse the contents of the archive
        std::ifstream log(file);
        cereal::BinaryInputArchive archive(log);
        std::vector<std::unique_ptr<Creature>> temp_creatures;
        archive(temp_creatures);

        for(auto &creature : temp_creatures)
        {
            history_states[tick]->addCreature(creature, this->scale);
            GraphicalCreature* graphical_creature = history_states[tick]->getLastCreature();
            const std::string species = graphical_creature->getSpecies();
            graphical_creature->setPixmap(&images[species]);

            // Store the tick of this creature in 0 so compare with later during rendering
            graphical_creature->setData(0, QVariant(tick));

            // Store pointer to View in 1 so we can get the slider value later
            GraphicsView *gview = static_cast<GraphicsView*>(this->scene->views().first());
            View *view = gview->getView();
            graphical_creature->setData(1, QVariant::fromValue(view));

            this->scene->addItem(graphical_creature);
        }
    }
}
Exemplo n.º 2
0
void writeFile(const char* folderPath, const char* content)
{
	if (NULL == folderPath || strlen(folderPath) <= 0 || NULL == content || strlen(content) <= 0)
	{
		JNILOGE("Folder path or content is NULl !");
		return;
	} JNILOGI("folder path: %s", folderPath); JNILOGI("content: %s", content);

	// New folder
	DIR* fd = newFolder(folderPath);
	if (NULL == fd)
	{
		JNILOGE("New folder fail");
		return;
	}

	// Get log number , first and last log
	long long firstTime = MAX_TIME;
	long long lastTime = 0;
	int count = getLogFiles(fd, firstTime, lastTime);
	JNILOGI("firstTime is %lld ", firstTime); JNILOGI("lastTime is %lld", lastTime); JNILOGI("count is %d", count);

	// Close the folder
	if (NULL != fd)
	{
		closedir(fd);
	}

	// Get the last time file, check the size < 200K
	bool newUpdate = false;
	char lastFileFullName[200];
	if (lastTime != 0)
	{
		snprintf(lastFileFullName, sizeof(lastFileFullName), FILE_NAME_PATTERN, folderPath, lastTime);
		JNILOGI("Last time file is : %s", lastFileFullName);

		// Get file's size
		struct stat buf;
		stat(lastFileFullName, &buf);
		JNILOGI("file size: %lld", buf.st_size);
		if (buf.st_size < FILE_SIZE_MAX)
		{
			JNILOGI("newUpdate!!!");
			newUpdate = true;
		}
	}

	// File number exceed the maximum, delete the oldest file
	if (!newUpdate && count >= FILE_NUM_MAX && firstTime < MAX_TIME)
	{
		char firstFileFullName[200];
		snprintf(firstFileFullName, sizeof(firstFileFullName), FILE_NAME_PATTERN, folderPath, firstTime);
		deleteFile(firstFileFullName);
	}

	// Begin write
	char fileFullName[200];
	long long time = getCurrentTime();
	JNILOGI("current time: %lld", time);
	snprintf(fileFullName, sizeof(fileFullName), FILE_NAME_PATTERN, folderPath, time);
	if (newUpdate)
	{
		// Update the file name
		rename(lastFileFullName, fileFullName);
	}

	write(fileFullName, content);
}