HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
python_console_history.cpp
Go to the documentation of this file.
1 #include <QDir>
2 #include <fstream>
4 
6 
7 namespace hal
8 {
9  namespace
10  {
11  // one history entry per line in file, line breaks within multi-line commands are stored as U+2029 (UTF-8)
12  const std::string sFileLineBreak = "\xE2\x80\xA9";
13 
14  std::string replaceAll(std::string text, const std::string& from, const std::string& to)
15  {
16  for (size_t pos = text.find(from); pos != std::string::npos; pos = text.find(from, pos + to.size()))
17  text.replace(pos, from.size(), to);
18  return text;
19  }
20  }
21 
22  PythonConsoleHistory::PythonConsoleHistory() : mFileName(QDir::home().filePath(".hal_history").toLocal8Bit().constData()), mHistory()
23  {
25  }
26 
28  {
29  }
30 
31  void PythonConsoleHistory::addHistory(const std::string& command)
32  {
33  mHistory.push_back(command);
34  appendToFile(command);
35  }
36 
37  const std::string& PythonConsoleHistory::getHistoryItem(const int& index) const
38  {
39  return mHistory.at(index);
40  }
41 
43  {
44  return mHistory.size();
45  }
46 
48  {
49  std::ifstream f(mFileName);
50  mHistory.clear();
51  std::string line;
52  while (std::getline(f, line))
53  {
54  mHistory.push_back(replaceAll(line, sFileLineBreak, "\n"));
55  }
56  }
57 
58  void PythonConsoleHistory::appendToFile(const std::string& command)
59  {
60  std::ofstream f(mFileName, std::ios::out | std::ios::app);
61  f << replaceAll(command, "\n", sFileLineBreak) << std::endl;
62  }
63 }
const std::string & getHistoryItem(const int &index) const
void addHistory(const std::string &command)
S to(const T &str)
Definition: defines.h:45