history.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. namespace knacki::cd{
  5. class AppContext;
  6. typedef char PTR_TYPE;
  7. class IOError
  8. {
  9. public:
  10. IOError(const std::string &e): what(e)
  11. {}
  12. const std::string what;
  13. };
  14. class HistoryEntry
  15. {
  16. public:
  17. HistoryEntry(PTR_TYPE index, const std::string &e);
  18. HistoryEntry(const HistoryEntry &);
  19. HistoryEntry &operator=(const HistoryEntry &o);
  20. const std::string &GetPath() const;
  21. PTR_TYPE GetIndex() const;
  22. protected:
  23. std::string path;
  24. PTR_TYPE index;
  25. };
  26. class History
  27. {
  28. public:
  29. History(const std::string &file);
  30. const std::vector<HistoryEntry> &GetEntries() const;
  31. const HistoryEntry *GetNextWorkingDirectory(const AppContext &app) const;
  32. bool IsPrevious(const HistoryEntry &) const;
  33. bool IsCurrent(const HistoryEntry &) const;
  34. protected:
  35. History() {};
  36. std::vector<HistoryEntry> hist;
  37. PTR_TYPE current;
  38. PTR_TYPE prev;
  39. void Read(std::istream &file);
  40. const HistoryEntry *GetHistoryEntry(PTR_TYPE index) const;
  41. };
  42. class HistoryRW: public History
  43. {
  44. public:
  45. HistoryRW(const std::string &file);
  46. bool AppendCwd();
  47. bool Append(const std::string &);
  48. bool Append(const HistoryEntry &);
  49. bool Flush();
  50. void Write();
  51. protected:
  52. void TruncateAfter(PTR_TYPE pos);
  53. private:
  54. const std::string path;
  55. };
  56. }