Browse Source

Slightly optimised file reading and writing

pipelines/235045657
eidheim 6 years ago
parent
commit
6ad83ec811
  1. 18
      src/filesystem.cc
  2. 8
      src/source.cc
  3. 20
      src/source_base.cc
  4. 6
      src/source_language_protocol.cc

18
src/filesystem.cc

@ -7,13 +7,17 @@
//Only use on small files
std::string filesystem::read(const std::string &path) {
std::stringstream ss;
std::string str;
std::ifstream input(path, std::ofstream::binary);
if(input) {
ss << input.rdbuf();
input.seekg(0, std::ios::end);
auto size = input.tellg();
input.seekg(0, std::ios::beg);
str.reserve(size);
str.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
input.close();
}
return ss.str();
return str;
}
//Only use on small files
@ -21,11 +25,11 @@ std::vector<std::string> filesystem::read_lines(const std::string &path) {
std::vector<std::string> res;
std::ifstream input(path, std::ofstream::binary);
if(input) {
do {
res.emplace_back();
} while(getline(input, res.back()));
}
std::string line;
while(std::getline(input, line))
res.emplace_back(std::move(line));
input.close();
}
return res;
}

8
src/source.cc

@ -403,13 +403,9 @@ bool Source::View::save() {
auto end_iter = start_iter;
bool end_reached = false;
while(!end_reached) {
for(size_t c = 0; c < 131072; c++) {
if(!end_iter.forward_char()) {
if(!end_iter.forward_chars(131072))
end_reached = true;
break;
}
}
output << get_buffer()->get_text(start_iter, end_iter).c_str();
output << get_buffer()->get_text(start_iter, end_iter).raw();
start_iter = end_iter;
}
output.close();

20
src/source_base.cc

@ -109,9 +109,13 @@ bool Source::BaseView::load(bool not_undoable_action) {
if(language) {
std::ifstream input(file_path.string(), std::ofstream::binary);
if(input) {
std::stringstream ss;
ss << input.rdbuf();
Glib::ustring ustr = ss.str();
std::string str;
input.seekg(0, std::ios::end);
auto size = input.tellg();
input.seekg(0, std::ios::beg);
str.reserve(size);
str.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
Glib::ustring ustr(std::move(str));
bool valid = true;
Glib::ustring::iterator iter;
@ -136,9 +140,13 @@ bool Source::BaseView::load(bool not_undoable_action) {
else {
std::ifstream input(file_path.string(), std::ofstream::binary);
if(input) {
std::stringstream ss;
ss << input.rdbuf();
Glib::ustring ustr = ss.str();
std::string str;
input.seekg(0, std::ios::end);
auto size = input.tellg();
input.seekg(0, std::ios::beg);
str.reserve(size);
str.assign(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
Glib::ustring ustr(std::move(str));
if(ustr.validate()) {
if(get_buffer()->size() == 0)

6
src/source_language_protocol.cc

@ -190,9 +190,7 @@ void LanguageProtocol::Client::parse_server_message() {
std::string line;
while(!header_read && std::getline(server_message_stream, line)) {
if(!line.empty()) {
if(line.back() == '\r')
line.pop_back();
if(!line.empty() && line != "\r") {
if(line.compare(0, 16, "Content-Length: ") == 0) {
try {
server_message_size = static_cast<size_t>(std::stoul(line.substr(16)));
@ -201,7 +199,7 @@ void LanguageProtocol::Client::parse_server_message() {
}
}
}
if(line.empty() && server_message_size != static_cast<size_t>(-1)) {
else if(server_message_size != static_cast<size_t>(-1)) {
server_message_content_pos = server_message_stream.tellg();
server_message_size += server_message_content_pos;
header_read = true;

Loading…
Cancel
Save