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 //Only use on small files
std::string filesystem::read(const std::string &path) { std::string filesystem::read(const std::string &path) {
std::stringstream ss; std::string str;
std::ifstream input(path, std::ofstream::binary); std::ifstream input(path, std::ofstream::binary);
if(input) { 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(); input.close();
} }
return ss.str(); return str;
} }
//Only use on small files //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::vector<std::string> res;
std::ifstream input(path, std::ofstream::binary); std::ifstream input(path, std::ofstream::binary);
if(input) { if(input) {
do { std::string line;
res.emplace_back(); while(std::getline(input, line))
} while(getline(input, res.back())); res.emplace_back(std::move(line));
}
input.close(); input.close();
}
return res; return res;
} }

8
src/source.cc

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

20
src/source_base.cc

@ -109,9 +109,13 @@ bool Source::BaseView::load(bool not_undoable_action) {
if(language) { if(language) {
std::ifstream input(file_path.string(), std::ofstream::binary); std::ifstream input(file_path.string(), std::ofstream::binary);
if(input) { if(input) {
std::stringstream ss; std::string str;
ss << input.rdbuf(); input.seekg(0, std::ios::end);
Glib::ustring ustr = ss.str(); 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; bool valid = true;
Glib::ustring::iterator iter; Glib::ustring::iterator iter;
@ -136,9 +140,13 @@ bool Source::BaseView::load(bool not_undoable_action) {
else { else {
std::ifstream input(file_path.string(), std::ofstream::binary); std::ifstream input(file_path.string(), std::ofstream::binary);
if(input) { if(input) {
std::stringstream ss; std::string str;
ss << input.rdbuf(); input.seekg(0, std::ios::end);
Glib::ustring ustr = ss.str(); 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(ustr.validate()) {
if(get_buffer()->size() == 0) if(get_buffer()->size() == 0)

6
src/source_language_protocol.cc

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

Loading…
Cancel
Save