refactor: move file operations into file.cc

This commit is contained in:
2026-03-09 12:26:34 -05:00
parent a1ff9dd62d
commit 08b7809c34
2 changed files with 43 additions and 38 deletions

43
src/file.cc Normal file
View File

@@ -0,0 +1,43 @@
#include <algorithm>
#include <fstream>
#include <iterator>
#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<char>(f), std::istreambuf_iterator<char>(), '\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;
}

View File

@@ -1,6 +1,4 @@
#include <algorithm>
#include <fstream> #include <fstream>
#include <iterator>
#include <string> #include <string>
#include <arpa/inet.h> #include <arpa/inet.h>
@@ -174,39 +172,3 @@ file_disp(std::string p, int lnstart, int lncount)
refresh(); refresh();
return 0; 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<char>(f), std::istreambuf_iterator<char>(), '\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;
}