From 08b7809c34bfd5e46e642e9200153094619cd778 Mon Sep 17 00:00:00 2001 From: "jazz (gitea)" Date: Mon, 9 Mar 2026 12:26:34 -0500 Subject: [PATCH] refactor: move file operations into file.cc --- src/file.cc | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.cc | 38 -------------------------------------- 2 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 src/file.cc diff --git a/src/file.cc b/src/file.cc new file mode 100644 index 0000000..51a09db --- /dev/null +++ b/src/file.cc @@ -0,0 +1,43 @@ +#include +#include +#include + +#include "chat.h" + + +int +count_lines(std::string p) +{ + int c = 0; + + std::ifstream f(p); + if (!f) + return c; + + /* Apparently this is the most efficient way to count all lines in a file. How revolting! */ + c = std::count(std::istreambuf_iterator(f), std::istreambuf_iterator(), '\n'); + + return c; +} + +int +log_append(std::string p, std::string ln) +{ + std::ofstream f; + + f.open(p, std::ios_base::app); + if (!f.is_open()) + return 1; + + f << ln << std::endl; + + f.close(); + + // "this probably would help but theres too much broken stuff right now to be sure" - scott + // bool inclnum should be added to function if we do decide to use this block: + // if (inclnum) + // line_position++; + + return 0; +} + diff --git a/src/main.cc b/src/main.cc index e76e766..b1a279f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,6 +1,4 @@ -#include #include -#include #include #include @@ -174,39 +172,3 @@ file_disp(std::string p, int lnstart, int lncount) refresh(); return 0; } - -int -count_lines(std::string p) -{ - int c = 0; - - std::ifstream f(p); - if (!f) - return c; - - /* Apparently this is the most efficient way to count all lines in a file. How revolting! */ - c = std::count(std::istreambuf_iterator(f), std::istreambuf_iterator(), '\n'); - - return c; -} - -int -log_append(std::string p, std::string ln) -{ - std::ofstream f; - - f.open(p, std::ios_base::app); - if (!f.is_open()) - return 1; - - f << ln << std::endl; - - f.close(); - - // "this probably would help but theres too much broken stuff right now to be sure" - scott - // bool inclnum should be added to function if we do decide to use this block: - // if (inclnum) - // line_position++; - - return 0; -}