Replace nlohmann/json with native C++20 JSON engine, cut build RAM/time, fix 32-bit build, enable -O2 -march=native
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
/* tools.cpp — bash/read/write tools and oversized result offloading. */
|
||||
#include "bantam.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <cerrno>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
@@ -123,7 +123,11 @@ std::string read_file_or_dir(std::string_view path, int offset, int limit, int *
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
std::sort(raw_names.begin(), raw_names.end());
|
||||
for (size_t i = 1; i < raw_names.size(); i++) {
|
||||
for (size_t j = i; j > 0 && raw_names[j - 1] > raw_names[j]; j--) {
|
||||
std::swap(raw_names[j - 1], raw_names[j]);
|
||||
}
|
||||
}
|
||||
std::string b;
|
||||
for (auto &nm : raw_names) {
|
||||
std::string full = p + "/" + nm;
|
||||
@@ -138,24 +142,29 @@ std::string read_file_or_dir(std::string_view path, int offset, int limit, int *
|
||||
return b;
|
||||
}
|
||||
/* regular file */
|
||||
std::ifstream f(p, std::ios::binary);
|
||||
FILE *f = std::fopen(p.c_str(), "rb");
|
||||
if (!f) { *style = 31; return sfmt("[tool error: read %s: %s]", p.c_str(), strerror(errno)); }
|
||||
if (offset < 1) offset = 1;
|
||||
if (limit <= 0) limit = 2000;
|
||||
std::string b;
|
||||
std::string line;
|
||||
char *line_buf = nullptr;
|
||||
size_t cap = 0;
|
||||
ssize_t n;
|
||||
int line_num = 0, read_count = 0;
|
||||
while (std::getline(f, line)) {
|
||||
while ((n = getline(&line_buf, &cap, f)) >= 0) {
|
||||
line_num++;
|
||||
if (line_num < offset) continue;
|
||||
read_count++;
|
||||
while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) line.pop_back();
|
||||
std::string_view lview = utf8_truncate(line, 2000);
|
||||
size_t len = (size_t)n;
|
||||
while (len > 0 && (line_buf[len - 1] == '\n' || line_buf[len - 1] == '\r')) len--;
|
||||
std::string_view lview = utf8_truncate(std::string_view(line_buf, len), 2000);
|
||||
str_appendf(b, "%d: ", line_num);
|
||||
b.append(lview);
|
||||
b += '\n';
|
||||
if (read_count >= limit) break;
|
||||
}
|
||||
free(line_buf);
|
||||
std::fclose(f);
|
||||
if (!b.empty()) b.pop_back(); /* trailing newline */
|
||||
*style = 2;
|
||||
return b;
|
||||
@@ -180,9 +189,10 @@ std::string write_file(std::string_view path, int offset, int del_bytes,
|
||||
*err = 1;
|
||||
return sfmt("[tool error: write %s: Is a directory]", p.c_str());
|
||||
}
|
||||
std::ifstream f(p, std::ios::binary);
|
||||
if (!f) { *err = 1; return sfmt("[tool error: write %s: %s]", p.c_str(), strerror(errno)); }
|
||||
data.assign((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
|
||||
if (!read_file_str(p, data)) {
|
||||
*err = 1;
|
||||
return sfmt("[tool error: write %s: %s]", p.c_str(), strerror(errno));
|
||||
}
|
||||
} else {
|
||||
auto slash = p.find_last_of('/');
|
||||
if (slash != std::string::npos) mkdir_p(std::string_view(p).substr(0, slash));
|
||||
@@ -201,10 +211,9 @@ std::string write_file(std::string_view path, int offset, int del_bytes,
|
||||
nd.append(data, 0, prefix_len);
|
||||
nd.append(content);
|
||||
if (suffix_len) nd.append(data, end_del, suffix_len);
|
||||
{
|
||||
std::ofstream out(p, std::ios::binary | std::ios::trunc);
|
||||
if (!out) { *err = 1; return sfmt("[tool error: write %s: %s]", p.c_str(), strerror(errno)); }
|
||||
out.write(nd.data(), (std::streamsize)nd.size());
|
||||
if (!write_file_str(p, nd)) {
|
||||
*err = 1;
|
||||
return sfmt("[tool error: write %s: %s]", p.c_str(), strerror(errno));
|
||||
}
|
||||
return sfmt("Successfully wrote %zu bytes to %s", clen, p.c_str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user