forked from scott/threaded_network_chat
refactor: move file operations into file.cc
This commit is contained in:
43
src/file.cc
Normal file
43
src/file.cc
Normal 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;
|
||||
}
|
||||
|
||||
38
src/main.cc
38
src/main.cc
@@ -1,6 +1,4 @@
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
@@ -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<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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user