HAL  v4.5.0-299-g9ee015483
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
project_manager.cpp
Go to the documentation of this file.
2 
10 
11 #include <filesystem>
12 #include <fstream>
13 #include <iostream>
14 #include <rapidjson/filereadstream.h>
15 #include <rapidjson/stringbuffer.h>
16 
17 #define PRETTY_JSON_OUTPUT 0
18 #if PRETTY_JSON_OUTPUT == 1
19 #include "rapidjson/prettywriter.h"
20 #else
21 #include "rapidjson/writer.h"
22 #endif
23 
25 
26 namespace hal
27 {
28  ProjectManager* ProjectManager::inst = nullptr;
29 
30  const std::string ProjectManager::s_project_file = ".project.json";
31 
32  ProjectManager::ProjectManager() : m_project_status(ProjectStatus::NONE)
33  {
34  ;
35  }
36 
38  {
39  if (!inst)
40  inst = new ProjectManager;
41  return inst;
42  }
43 
44  void ProjectManager::register_serializer(const std::string& tagname, ProjectSerializer* serializer)
45  {
46  if (m_serializer.find(tagname) == m_serializer.end())
47  m_serializer[tagname] = serializer;
48  else
49  {
50  log_warning("project_manager", "serializer '{}' already registered.", tagname);
51  }
52  }
53 
54  void ProjectManager::unregister_serializer(const std::string& tagname)
55  {
56  auto it = m_serializer.find(tagname);
57  if (it != m_serializer.end())
58  m_serializer.erase(it);
59  }
60 
61  std::string ProjectManager::get_filename(const std::string& tagname)
62  {
63  auto it = m_filename.find(tagname);
64  if (it == m_filename.end())
65  return std::string();
66  return it->second;
67  }
68 
69  bool ProjectManager::open_project(const std::string& path)
70  {
71  if (!path.empty())
72  m_proj_dir = ProjectDirectory(path);
73  if (!std::filesystem::exists(m_proj_dir))
74  {
75  log_warning("project_manager", "cannot open project '{}', path doesn't exist.", path);
76  return false;
77  }
78  if (deserialize())
79  {
80  m_project_status = ProjectStatus::OPENED;
81  return true;
82  }
83  return false;
84  }
85 
86  void ProjectManager::set_project_directory(const std::string& path)
87  {
88  m_proj_dir = ProjectDirectory(path);
89  }
90 
92  {
93  return m_proj_dir;
94  }
95 
96  bool ProjectManager::create_project_directory(const std::string& path)
97  {
98  std::error_code errCode;
99  m_proj_dir = ProjectDirectory(path);
100  if (std::filesystem::exists(m_proj_dir))
101  return false;
102  bool success = std::filesystem::create_directory(m_proj_dir, errCode);
103  if (errCode)
104  {
105  log_warning("project_manager", "cannot create directory '{}': '{}'", m_proj_dir.string(), errCode.message());
106  return false;
107  }
108  if (!success)
109  return false;
110  m_netlist_file = m_proj_dir.get_default_filename(".hal");
111 
112  m_proj_dir.create_zip_warning();
113 
114  std::filesystem::create_directory(m_proj_dir.get_filename("py"));
115  std::filesystem::create_directory(m_proj_dir.get_filename(ProjectDirectory::s_shadow_dir));
116 
117  return serialize_to_projectfile(false);
118  }
119 
121  {
122  if (m_proj_dir.empty())
123  return false;
124  return std::filesystem::remove_all(m_proj_dir);
125  }
126 
128  {
129  m_project_status = stat;
130  }
131 
133  {
134  return m_project_status;
135  }
136 
137  void ProjectManager::set_gate_library_path(const std::string& glpath)
138  {
139  m_gatelib_path = glpath;
140  }
141 
142  bool ProjectManager::serialize_project(Netlist* netlist, bool shadow)
143  {
144  if (!netlist)
145  return false;
146 
147  m_netlist_save = netlist;
148  const GateLibrary* gl = m_netlist_save->get_gate_library();
149  if (gl)
150  m_gatelib_path = gl->get_path().string();
151  if (shadow)
152  m_netlist_file = m_proj_dir.get_shadow_filename(".hal");
153  else
154  m_netlist_file = m_proj_dir.get_default_filename(".hal");
155 
156  if (!netlist_serializer::serialize_to_file(m_netlist_save, m_netlist_file))
157  return false;
158 
159  if (!serialize_external(shadow))
160  return false;
161 
162  if (!serialize_to_projectfile(shadow))
163  return false;
164 
165  // a regular save writes to the project directory anyway, so this is where projects created before
166  // the warning existed get it; autosave writes only to the shadow directory and leaves it alone
167  if (!shadow)
168  m_proj_dir.create_zip_warning();
169 
170  return true;
171  }
172 
174  {
175  std::filesystem::path filename(m_proj_dir);
176  filename.append(m_netlist_file);
177  return filename.string();
178  }
179 
180  std::unique_ptr<Netlist>& ProjectManager::get_netlist()
181  {
182  return m_netlist_load;
183  }
184 
186  {
187  std::filesystem::path projFilePath(m_proj_dir);
188  projFilePath.append(s_project_file);
189 
190  FILE* fp = fopen(projFilePath.string().c_str(), "r");
191  if (fp == NULL)
192  return;
193 
194  char buffer[65536];
195  rapidjson::FileReadStream frs(fp, buffer, sizeof(buffer));
196  rapidjson::Document doc;
197  doc.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(frs);
198  fclose(fp);
199 
200  if (doc.HasMember("netlist"))
201  {
202  rapidjson::Value::MemberIterator netlistMember = doc.FindMember("netlist");
203  std::string netlistFilename = netlistMember->value.GetString();
204  int n = ProjectDirectory::s_shadow_dir.size() + 1;
205  if (netlistFilename.substr(0, n) == ProjectDirectory::s_shadow_dir + '/')
206  {
207  netlistFilename.erase(0, n);
208  netlistMember->value.SetString(netlistFilename.c_str(), doc.GetAllocator());
209 
210  std::ofstream of(projFilePath);
211  if (!of.good())
212  return;
213 
214  rapidjson::StringBuffer strbuf;
215 #if PRETTY_JSON_OUTPUT == 1
216  rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(strbuf);
217 #else
218  rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
219 #endif
220  doc.Accept(writer);
221  of << strbuf.GetString();
222  of.close();
223  }
224  }
225  }
226 
227  bool ProjectManager::deserialize()
228  {
229  std::filesystem::path projFilePath(m_proj_dir);
230  projFilePath.append(s_project_file);
231 
232  FILE* fp = fopen(projFilePath.string().c_str(), "r");
233  if (fp == NULL)
234  {
235  log_error("project_manager", "cannot open project file '{}'.", projFilePath.string());
236  return false;
237  }
238 
239  char buffer[65536];
240  rapidjson::FileReadStream frs(fp, buffer, sizeof(buffer));
241  rapidjson::Document doc;
242  doc.ParseStream<0, rapidjson::UTF8<>, rapidjson::FileReadStream>(frs);
243  fclose(fp);
244 
245  if (doc.HasMember("gate_library"))
246  {
247  m_gatelib_path = doc["gate_library"].GetString();
248  std::filesystem::path gatelibPath(m_gatelib_path);
249  if (gatelibPath.is_relative())
250  gatelibPath = m_proj_dir / gatelibPath;
251 
252  if (doc.HasMember("netlist"))
253  {
254  m_netlist_file = doc["netlist"].GetString();
255  std::filesystem::path netlistPath(m_proj_dir);
256  netlistPath.append(m_netlist_file);
257  m_netlist_load = netlist_factory::load_netlist(netlistPath,gatelibPath);
258  if (!m_netlist_load)
259  {
260  log_error("project_manager", "cannot load netlist {}.", netlistPath.string());
261  return false;
262  }
263  }
264  else
265  {
266  log_error("project_manager", "no 'netlist' token found in project file {}.", projFilePath.string());
267  return false;
268  }
269  }
270  else
271  {
272  log_error("netlist", "'gate_library' token not found in project file '{}'.", projFilePath.string());
273  return false;
274  }
275 
276 
277  if (doc.HasMember("serializer"))
278  {
279  for (auto it = doc["serializer"].MemberBegin(); it != doc["serializer"].MemberEnd(); ++it)
280  {
281  m_filename[it->name.GetString()] = it->value.GetString();
282  }
283  }
284 
285  for (auto it = m_serializer.begin(); it != m_serializer.end(); ++it)
286  {
287  it->second->deserialize(m_netlist_load.get(), m_proj_dir);
288  }
289  return true;
290  }
291 
292  bool ProjectManager::serialize_external(bool shadow)
293  {
294  if (!m_netlist_save)
295  return false;
296 
297  m_filename.clear();
298 
299  for (auto it = m_serializer.begin(); it != m_serializer.end(); ++it)
300  {
301  std::string relfile = it->second->serialize(m_netlist_save, shadow ? m_proj_dir.get_filename(ProjectDirectory::s_shadow_dir) : m_proj_dir, shadow);
302  if (relfile.empty())
303  continue;
304  m_filename[it->first] = relfile;
305  }
306  return true;
307  }
308 
309  bool ProjectManager::serialize_to_projectfile(bool shadow) const
310  {
311  std::filesystem::path projFilePath = m_proj_dir.get_filename(s_project_file);
312  if (shadow)
313  {
314  std::filesystem::path shadowPath = m_proj_dir.get_filename(ProjectDirectory::s_shadow_dir);
315  std::filesystem::create_directory(shadowPath);
316  projFilePath = shadowPath;
317  projFilePath.append(s_project_file);
318  }
319 
320  JsonWriteDocument doc;
321  doc["serialization_format_version"] = SERIALIZATION_FORMAT_VERSION;
322  doc["netlist"] = m_proj_dir.get_relative_file_path(m_netlist_file).string();
323  doc["gate_library"] = m_proj_dir.get_relative_file_path(m_gatelib_path).string();
324 
325  if (!m_filename.empty())
326  {
327  JsonWriteObject& serial = doc.add_object("serializer");
328  for (auto it = m_filename.begin(); it != m_filename.end(); ++it)
329  {
330  if (it->second.empty())
331  continue;
332  serial[it->first] = it->second;
333  }
334  serial.close();
335  }
336  return doc.serialize(projFilePath.string());
337  }
338 
339  void ProjectManager::dump() const
340  {
341  for (auto it = m_filename.begin(); it != m_filename.end(); ++it)
342  {
343  std::cout << "serializer: <" << it->first << "> <" << it->second << ">" << std::endl;
344  }
345  }
346 } // namespace hal
std::filesystem::path get_path() const
const GateLibrary * get_gate_library() const
Definition: netlist.cpp:134
std::filesystem::path get_filename(const std::string &relative_filename) const
std::filesystem::path get_relative_file_path(const std::string &filename) const
static const std::string s_shadow_dir
std::filesystem::path get_shadow_filename(const std::string &extension=std::string()) const
std::filesystem::path get_default_filename(const std::string &extension=std::string()) const
static ProjectManager * instance()
void set_project_status(ProjectStatus status)
bool create_project_directory(const std::string &path)
void unregister_serializer(const std::string &tagname)
bool serialize_project(Netlist *netlist, bool shadow=false)
std::unique_ptr< Netlist > & get_netlist()
void register_serializer(const std::string &tagname, ProjectSerializer *serializer)
void set_gate_library_path(const std::string &gl_path)
std::string get_filename(const std::string &serializer_name)
ProjectStatus get_project_status() const
void restore_project_file_from_autosave()
static const std::string s_project_file
std::string get_netlist_filename() const
bool open_project(const std::string &path="")
const ProjectDirectory & get_project_directory() const
void set_project_directory(const std::string &path)
#define log_error(channel,...)
Definition: log.h:78
#define log_warning(channel,...)
Definition: log.h:76
std::unique_ptr< Netlist > load_netlist(const std::filesystem::path &netlist_file, const std::filesystem::path &gate_library_file=std::filesystem::path())
Create a netlist from the given file.
bool serialize_to_file(const Netlist *netlist, const std::filesystem::path &hal_file)
Definition: defines.h:45
std::error_code error_code
Definition: defines.h:46
const int SERIALIZATION_FORMAT_VERSION
This file contains various functions to create and load netlists.