如果您使用内存映射文件,您可能会减少实际的磁盘访问(写访问)。如果匹配行位于“后期”块中,则可以避免(重新)写入任何前面的块。
以下代码
- 使用 boost 是为了不依赖于平台
- 允许比匹配的目标行更短和更长的替换
- 就地调整文件大小
- 演示通过正则表达式查找行
- 已经过很好的测试(在 linux 上),在此处发布之前
有关基本原理,请参阅内联注释。代码:
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/regex.hpp>
#include <boost/filesystem.hpp>
using namespace boost;
const auto MAX_INCREASE = 1024; // allow 1 kilobyte room to grow
void process(
filesystem::path const& spec,
std::string const& pattern,
std::string const& replace)
{
// get current size of file on disk
auto const cur_size = file_size(spec);
// map, with MAX_INCREASE room to spare
iostreams::mapped_file mmap(
spec.native(),
iostreams::mapped_file::readwrite,
cur_size+MAX_INCREASE);
// find the line matching 'pattern'
char *bof = mmap.data();
char *eof = bof + cur_size; // don't read beyond cur_size!
regex re(pattern);
match_results<char*> match;
if (regex_search(bof, eof, match, re))
{
// replace the matched contents!
auto b = match[0].first,
e = match[0].second;
std::cout << "Matching line: '" << std::string(b, e) << "'\n";
// figure out whether we need to grow/shrink
auto delta = (b + replace.size()) - e;
std::cout << "Delta: " << delta << "\n";
if (delta < 0)
{
// shrinking
std::copy(replace.begin(), replace.end(), b); // replacement
std::copy(e, eof, e + delta); // shift back
resize_file(filesystem::path(spec), cur_size + delta);
}
else if (delta < MAX_INCREASE)
{
// growing
resize_file(filesystem::path(spec), cur_size + delta);
std::copy_backward(b, eof, eof + delta); // shift onwards
std::copy(replace.begin(), replace.end(), b); // insert replacement
}
else
{
// error handling (MAX_INCREASE exceeded)
}
}
// TODO error handling (no match)?
}
int main()
{
process("input.txt", "^int .*?$", "void foo()\n// mmap was here");
//process("input.txt", "^int .*?$", "");
}