Convert from readline to replxx format. replxx requires each history line to prepended with time line: ### YYYY-MM-DD HH:MM:SS.SSS select 1 And w/o those service lines it will load all lines from history file as one history line for suggestion. And if there are lots of lines in file it will take lots of time (getline() + tons of reallocations). NOTE: this code uses std::ifstream/std::ofstream
| 59 | /// |
| 60 | /// NOTE: this code uses std::ifstream/std::ofstream like original replxx code. |
| 61 | void convertHistoryFile(const std::string & path, replxx::Replxx & rx) |
| 62 | { |
| 63 | std::ifstream in(path); |
| 64 | if (!in) |
| 65 | { |
| 66 | rx.print("Cannot open %s reading (for conversion): %s\n", |
| 67 | path.c_str(), errnoToString(errno).c_str()); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | std::string line; |
| 72 | if (getline(in, line).bad()) |
| 73 | { |
| 74 | rx.print("Cannot read from %s (for conversion): %s\n", |
| 75 | path.c_str(), errnoToString(errno).c_str()); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | /// This is the marker of the date, no need to convert. |
| 80 | static char const REPLXX_TIMESTAMP_PATTERN[] = "### dddd-dd-dd dd:dd:dd.ddd"; |
| 81 | if (line.empty() || (line.starts_with("### ") && line.size() == strlen(REPLXX_TIMESTAMP_PATTERN))) |
| 82 | { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | std::vector<std::string> lines; |
| 87 | in.seekg(0); |
| 88 | while (getline(in, line).good()) |
| 89 | { |
| 90 | lines.push_back(line); |
| 91 | } |
| 92 | in.close(); |
| 93 | |
| 94 | size_t lines_size = lines.size(); |
| 95 | std::sort(lines.begin(), lines.end()); |
| 96 | lines.erase(std::unique(lines.begin(), lines.end()), lines.end()); |
| 97 | rx.print("The history file (%s) is in old format. %zu lines, %zu unique lines.\n", |
| 98 | path.c_str(), lines_size, lines.size()); |
| 99 | |
| 100 | std::ofstream out(path); |
| 101 | if (!out) |
| 102 | { |
| 103 | rx.print("Cannot open %s for writing (for conversion): %s\n", |
| 104 | path.c_str(), errnoToString(errno).c_str()); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | const std::string & timestamp = replxx_now_ms_str(); |
| 109 | for (const auto & out_line : lines) |
| 110 | { |
| 111 | out << "### " << timestamp << "\n" << out_line << std::endl; |
| 112 | } |
| 113 | out.close(); |
| 114 | } |
| 115 | |
| 116 | } |
| 117 |
no test coverage detected