fxos/include/fxos/project.h

135 lines
4.1 KiB
C++

//---------------------------------------------------------------------------//
// 1100101 |_ mov #0, r4 __ //
// 11 |_ <0xb380 %5c4> / _|_ _____ ___ //
// 0110 |_ 3.50 -> 3.60 | _\ \ / _ (_-< //
// |_ base# + offset |_| /_\_\___/__/ //
//---------------------------------------------------------------------------//
// fxos/project: Project data and management
//
// This header defines the projet structures, with its binaries and analysis
// results. It also provides access to recent projects.
//---
#ifndef FXOS_PROJECT_H
#define FXOS_PROJECT_H
#include <fxos/binary.h>
#include <fxos/util/bson.h>
#include <string>
#include <vector>
#include <ctime>
namespace FxOS {
struct Project
{
/* Create an empty project with a default name and no path. */
Project();
std::string const &name() const
{
return m_name;
}
std::string const &path() const
{
return m_path;
}
/* Set a new name. The caller should also update recent projects. */
void setName(std::string const &new_name);
/* Set a new path (usually by specifying one in `ps`). The caller should
also update recent projects. */
void setPath(std::string const &new_path);
/* Whether the project can be saved (ie. has a path). */
bool canSave() const;
/* Whether the project is dirty and should be saved before closing. */
bool isDirty() const;
/* Mark the project as dirty. This should be called by commands. */
void setDirty();
/* Save; prints on stderr and returns false in case of error. */
bool save();
/* Load from a folder. */
bool load(std::string const &path);
/* Create a binary based on the provided name. If there is a conflict, the
name will get a suffix like "_0", "_1", etc. Final name is returned. */
std::string createBinary(std::string const &name = "");
/* Get a binary by name. */
Binary *getBinary(std::string const &name);
Binary const *getBinary(std::string const &name) const;
/* List of all binaries. */
std::map<std::string, Binary> const &binaries() const
{
return m_binaries;
}
/* Remove a binary from the project. */
void removeBinary(std::string const &name);
private:
/* Project name (no constraints but typically an identifier) */
std::string m_name;
/* Absolute project path in the filesystem */
std::string m_path;
/* Whether project needs a confirmation/save before closing */
bool m_dirty;
/* List of binaries in the project */
std::map<std::string, Binary> m_binaries;
BSON serializeMetadata() const;
/* Generate a new binary name. If there is a collision, adds a suffix like
"_0", "_1", etc. until a fresh name is found. */
std::string generateBinaryName(std::string const &base = "") const;
};
struct RecentProjects
{
struct RecentProjectEntry
{
/* Last known project name */
std::string name;
/* Absolute path */
std::string path;
/* Last use time */
std::time_t utime;
RecentProjectEntry() = default;
RecentProjectEntry(BSON const &);
BSON serialize() const;
};
RecentProjects() = default;
BSON serialize() const;
void deserialize(BSON const &);
/* Get list of all entries, most recent first. */
std::vector<RecentProjectEntry> const &entries() const
{
return m_entries;
}
/* Get project path from name (empty string if none found) */
std::string pathFromName(std::string const &name) const;
/* Get path of most recent project */
std::string mostRecentPath() const;
/* Remove a project from the list, if present. */
void remove(std::string const &path);
/* Create or update a project in the list. */
void touch(Project const &p);
/* Refresh list by removing projects that no longer exist. */
void refresh();
private:
std::vector<RecentProjectEntry> m_entries;
};
/* Global recent project register */
RecentProjects &recentProjects();
} /* namespace FxOS */
#endif /* FXOS_PROJECT_H */