440 lines
16 KiB
C++
440 lines
16 KiB
C++
#include "files_text_document.h"
|
|
|
|
#include <objbase.h>
|
|
#include <algorithm>
|
|
#include <stdexcept>
|
|
#include <system_error>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace ks::files
|
|
{
|
|
namespace
|
|
{
|
|
[[noreturn]] void fail(const char *operation)
|
|
{
|
|
throw std::system_error(static_cast<int>(GetLastError()), std::system_category(), operation);
|
|
}
|
|
|
|
struct File
|
|
{
|
|
HANDLE handle = INVALID_HANDLE_VALUE;
|
|
~File() { if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); }
|
|
HANDLE release() { return std::exchange(handle, INVALID_HANDLE_VALUE); }
|
|
};
|
|
|
|
struct PendingSave
|
|
{
|
|
std::filesystem::path path;
|
|
~PendingSave() { if (!path.empty()) DeleteFileW(path.c_str()); }
|
|
};
|
|
|
|
HANDLE openSource(const std::filesystem::path &path, bool editable)
|
|
{
|
|
const auto sharing = FILE_SHARE_READ | FILE_SHARE_DELETE | (editable ? 0 : FILE_SHARE_WRITE);
|
|
const auto result = CreateFileW(path.c_str(), GENERIC_READ, sharing,
|
|
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
|
if (result == INVALID_HANDLE_VALUE) fail("Open file");
|
|
return result;
|
|
}
|
|
|
|
HANDLE trackReplacement(const std::filesystem::path &path)
|
|
{
|
|
// A metadata-only handle survives the rename without blocking ReplaceFile.
|
|
const auto handle=CreateFileW(path.c_str(),0,FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
|
|
nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nullptr);
|
|
if(handle==INVALID_HANDLE_VALUE) fail("Track replacement file");
|
|
return handle;
|
|
}
|
|
|
|
uint64_t fileSize(HANDLE file)
|
|
{
|
|
LARGE_INTEGER size{};
|
|
if (!GetFileSizeEx(file, &size)) fail("Read file size");
|
|
return size.QuadPart;
|
|
}
|
|
|
|
void seek(HANDLE file, uint64_t offset)
|
|
{
|
|
LARGE_INTEGER position{};
|
|
position.QuadPart = static_cast<LONGLONG>(offset);
|
|
if (!SetFilePointerEx(file, position, nullptr, FILE_BEGIN)) fail("Seek");
|
|
}
|
|
|
|
std::string read(HANDLE file, uint64_t offset, size_t length)
|
|
{
|
|
if (!length) return {};
|
|
seek(file, offset);
|
|
std::string result(length, '\0');
|
|
DWORD count{};
|
|
if (!ReadFile(file, result.data(), static_cast<DWORD>(length), &count, nullptr)) fail("Read file");
|
|
if (count != length) throw std::runtime_error("The file ended unexpectedly.");
|
|
return result;
|
|
}
|
|
|
|
void write(HANDLE file, std::string_view bytes)
|
|
{
|
|
if (bytes.empty()) return;
|
|
DWORD written{};
|
|
if (!WriteFile(file, bytes.data(), static_cast<DWORD>(bytes.size()), &written, nullptr))
|
|
fail("Write file");
|
|
if (written != bytes.size()) throw std::runtime_error("The complete block could not be written.");
|
|
}
|
|
|
|
bool sameFile(HANDLE a, HANDLE b)
|
|
{
|
|
BY_HANDLE_FILE_INFORMATION first{}, second{};
|
|
if (!GetFileInformationByHandle(a, &first) || !GetFileInformationByHandle(b, &second))
|
|
fail("Check file identity");
|
|
return first.dwVolumeSerialNumber == second.dwVolumeSerialNumber &&
|
|
first.nFileIndexHigh == second.nFileIndexHigh && first.nFileIndexLow == second.nFileIndexLow;
|
|
}
|
|
|
|
std::filesystem::path temporaryPath(const std::filesystem::path &directory)
|
|
{
|
|
GUID id{};
|
|
wchar_t name[40]{};
|
|
if (FAILED(CoCreateGuid(&id)) || !StringFromGUID2(id, name, 40))
|
|
throw std::runtime_error("Cannot create a temporary file name.");
|
|
return directory / (std::wstring(L".kswitcher-") + name + L".tmp");
|
|
}
|
|
|
|
std::filesystem::path createTemporary(const std::filesystem::path &directory, File &file, DWORD flags)
|
|
{
|
|
auto path = temporaryPath(directory);
|
|
file.handle = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, flags, nullptr);
|
|
if (file.handle == INVALID_HANDLE_VALUE) fail("Create temporary file");
|
|
return path;
|
|
}
|
|
|
|
bool isUtf16(TextEncoding encoding)
|
|
{
|
|
return encoding==TextEncoding::utf16le || encoding==TextEncoding::utf16be;
|
|
}
|
|
|
|
std::string_view signature(TextEncoding encoding)
|
|
{
|
|
switch(encoding)
|
|
{
|
|
case TextEncoding::utf16le: return "\xff\xfe";
|
|
case TextEncoding::utf16be: return "\xfe\xff";
|
|
case TextEncoding::utf8: return "\xef\xbb\xbf";
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
std::wstring decodeMultibyte(std::string_view bytes,UINT codePage,DWORD flags)
|
|
{
|
|
const int length=static_cast<int>(bytes.size());
|
|
const auto required=MultiByteToWideChar(codePage,flags,bytes.data(),length,nullptr,0);
|
|
if(!required) fail("Decode text");
|
|
std::wstring units(required,L'\0');
|
|
if(!MultiByteToWideChar(codePage,flags,bytes.data(),length,units.data(),required)) fail("Decode text");
|
|
return units;
|
|
}
|
|
|
|
std::string encode(std::string_view utf8,TextEncoding encoding)
|
|
{
|
|
if(encoding==TextEncoding::utf8 || utf8.empty()) return std::string(utf8);
|
|
const auto units=decodeMultibyte(utf8,CP_UTF8,MB_ERR_INVALID_CHARS);
|
|
if(encoding==TextEncoding::windows1252)
|
|
{
|
|
BOOL substituted{};
|
|
const int length=static_cast<int>(units.size());
|
|
const auto required=WideCharToMultiByte(1252,WC_NO_BEST_FIT_CHARS,units.data(),length,
|
|
nullptr,0,nullptr,&substituted);
|
|
if(!required) fail("Encode Windows-1252");
|
|
if(substituted) throw std::runtime_error("Some characters cannot be saved as Windows-1252. Choose a Unicode encoding.");
|
|
std::string bytes(required,'\0');
|
|
if(!WideCharToMultiByte(1252,WC_NO_BEST_FIT_CHARS,units.data(),length,bytes.data(),required,nullptr,&substituted))
|
|
fail("Encode Windows-1252");
|
|
return bytes;
|
|
}
|
|
std::string bytes(units.size()*2,'\0');
|
|
const size_t low=encoding==TextEncoding::utf16le?0:1;
|
|
for(size_t i=0;i<units.size();++i)
|
|
{
|
|
bytes[2*i+low]=static_cast<char>(units[i]&0xff);
|
|
bytes[2*i+1-low]=static_cast<char>(units[i]>>8);
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
std::string convertLineEndings(std::string_view text,TextLineEnding ending)
|
|
{
|
|
if(ending==TextLineEnding::preserve) return std::string(text);
|
|
const std::string_view replacement=ending==TextLineEnding::crlf?"\r\n":ending==TextLineEnding::lf?"\n":"\r";
|
|
std::string result;
|
|
result.reserve(text.size());
|
|
for(size_t start=0;start<text.size();)
|
|
{
|
|
const auto next=text.find_first_of("\r\n",start);
|
|
result.append(text.substr(start,next==text.npos?text.npos:next-start));
|
|
if(next==text.npos) break;
|
|
result.append(replacement);
|
|
start=next+1;
|
|
if(text[next]=='\r' && start<text.size() && text[start]=='\n') ++start;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
TextDocument::TextDocument(std::filesystem::path path,bool editable,TextEncoding encoding)
|
|
: path_(std::move(path)), editable_(editable)
|
|
{
|
|
if(path_.empty())
|
|
{
|
|
if(encoding!=TextEncoding::automatic) encoding_=encoding;
|
|
return;
|
|
}
|
|
File source{openSource(path_, editable)};
|
|
if (editable)
|
|
{
|
|
std::wstring resolved(32768, L'\0');
|
|
const auto length = GetFinalPathNameByHandleW(source.handle, resolved.data(),
|
|
static_cast<DWORD>(resolved.size()), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
|
|
if (length && length < resolved.size())
|
|
{
|
|
resolved.resize(length);
|
|
path_ = resolved;
|
|
}
|
|
}
|
|
size_ = fileSize(source.handle);
|
|
const auto prefix = read(source.handle, 0, static_cast<size_t>(std::min<uint64_t>(3, size_)));
|
|
if(encoding==TextEncoding::automatic)
|
|
{
|
|
encoding=TextEncoding::utf8;
|
|
for(const auto candidate:{TextEncoding::utf16le,TextEncoding::utf16be})
|
|
if(prefix.starts_with(signature(candidate))) encoding=candidate;
|
|
}
|
|
encoding_=encoding;
|
|
if(prefix.starts_with(signature(encoding_))) bom_=signature(encoding_).size();
|
|
if(isUtf16(encoding_) && size_%2) throw std::runtime_error("The UTF-16 file has an incomplete code unit.");
|
|
source_ = source.release();
|
|
}
|
|
|
|
TextDocument::~TextDocument()
|
|
{
|
|
if (source_ != INVALID_HANDLE_VALUE) CloseHandle(source_);
|
|
if (spool_ != INVALID_HANDLE_VALUE) CloseHandle(spool_);
|
|
}
|
|
|
|
const wchar_t *TextDocument::encodingName() const
|
|
{
|
|
switch (encoding_)
|
|
{
|
|
case TextEncoding::utf16le: return L"UTF-16 LE";
|
|
case TextEncoding::utf16be: return L"UTF-16 BE";
|
|
case TextEncoding::windows1252: return L"Windows-1252";
|
|
default: return bom_ ? L"UTF-8 BOM" : L"UTF-8";
|
|
}
|
|
}
|
|
|
|
uint64_t TextDocument::pages() const
|
|
{
|
|
return std::max<uint64_t>(1, (size_ - bom_ + pageBytes - 1) / pageBytes);
|
|
}
|
|
|
|
uint64_t TextDocument::boundary(uint64_t page) const
|
|
{
|
|
if (!page) return bom_;
|
|
if (page >= pages()) return size_;
|
|
File temporary{source_ == INVALID_HANDLE_VALUE ? openSource(path_, false) : INVALID_HANDLE_VALUE};
|
|
return boundary(page,source_ == INVALID_HANDLE_VALUE ? temporary.handle : source_);
|
|
}
|
|
|
|
uint64_t TextDocument::boundary(uint64_t page,HANDLE source) const
|
|
{
|
|
if (!page) return bom_;
|
|
if (page >= pages()) return size_;
|
|
|
|
const auto offset = bom_ + page * pageBytes;
|
|
const auto unit = isUtf16(encoding_) ? 2u : 1u;
|
|
const auto sample = read(source, offset - unit,
|
|
static_cast<size_t>(std::min<uint64_t>(4096 + unit, size_ - offset + unit)));
|
|
const auto getUnit = [&](size_t i) -> unsigned {
|
|
const auto first = static_cast<unsigned char>(sample[i]);
|
|
if (unit == 1) return first;
|
|
const auto second = static_cast<unsigned char>(sample[i + 1]);
|
|
return encoding_ == TextEncoding::utf16le ? first | (second << 8) : (first << 8) | second;
|
|
};
|
|
if (getUnit(0) == '\n') return offset;
|
|
for (size_t i = unit; i + unit < sample.size(); i += unit)
|
|
if (getUnit(i) == '\n') return offset + i;
|
|
|
|
size_t i = unit;
|
|
if (encoding_ == TextEncoding::utf8)
|
|
{
|
|
// Advance past at most one UTF-8 code point, even on malformed input.
|
|
while (i < sample.size() && i <= 3 && (getUnit(i) & 0xc0) == 0x80) ++i;
|
|
}
|
|
else if (isUtf16(encoding_))
|
|
if (const auto value = getUnit(i); value >= 0xdc00 && value <= 0xdfff) i += unit;
|
|
if (i == unit && getUnit(0) == '\r' && getUnit(i) == '\n') i += unit;
|
|
return offset + i - unit;
|
|
}
|
|
|
|
std::string TextDocument::decode(std::string bytes) const
|
|
{
|
|
if (encoding_ == TextEncoding::utf8 || bytes.empty()) return bytes;
|
|
std::wstring units;
|
|
if(encoding_==TextEncoding::windows1252) units=decodeMultibyte(bytes,1252,0);
|
|
else
|
|
{
|
|
units.resize(bytes.size() / 2);
|
|
for (size_t i = 0; i < units.size(); ++i)
|
|
{
|
|
const auto a = static_cast<unsigned char>(bytes[2 * i]);
|
|
const auto b = static_cast<unsigned char>(bytes[2 * i + 1]);
|
|
units[i] = static_cast<wchar_t>(encoding_ == TextEncoding::utf16le ? a | (b << 8) : (a << 8) | b);
|
|
}
|
|
}
|
|
const auto length = static_cast<int>(units.size());
|
|
const int required = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, units.data(), length, nullptr, 0, nullptr, nullptr);
|
|
if (!required) fail("Decode UTF-16");
|
|
std::string result(required, '\0');
|
|
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, units.data(), length, result.data(), required, nullptr, nullptr))
|
|
fail("Decode UTF-16");
|
|
return result;
|
|
}
|
|
|
|
std::string TextDocument::readPage(uint64_t page) const
|
|
{
|
|
if (const auto it = changes_.find(page); it != changes_.end())
|
|
return read(spool_, it->second.offset, it->second.size);
|
|
return readOriginal(page);
|
|
}
|
|
|
|
std::string TextDocument::readOriginal(uint64_t page) const
|
|
{
|
|
if(path_.empty()) return {};
|
|
File temporary{source_ == INVALID_HANDLE_VALUE ? openSource(path_, false) : INVALID_HANDLE_VALUE};
|
|
const auto source=source_ == INVALID_HANDLE_VALUE ? temporary.handle : source_;
|
|
const auto start = boundary(page,source);
|
|
return decode(read(source, start, static_cast<size_t>(boundary(page + 1,source) - start)));
|
|
}
|
|
|
|
void TextDocument::releaseReadHandle()
|
|
{
|
|
if(!editable_ && source_!=INVALID_HANDLE_VALUE)
|
|
CloseHandle(std::exchange(source_,INVALID_HANDLE_VALUE));
|
|
}
|
|
|
|
bool TextDocument::modifiedWith(uint64_t page, std::string_view current) const
|
|
{
|
|
const size_t changesOnPage = changes_.contains(page) ? 1 : 0;
|
|
return changes_.size() > changesOnPage || current != readOriginal(page);
|
|
}
|
|
|
|
void TextDocument::stage(uint64_t page, std::string_view utf8)
|
|
{
|
|
if (!editable_) throw std::logic_error("The file is open for viewing.");
|
|
if (utf8.size() > editLimit) throw std::runtime_error("The edit exceeds 8 MiB on this page.");
|
|
if (utf8 == readOriginal(page))
|
|
{
|
|
changes_.erase(page);
|
|
return;
|
|
}
|
|
if (spool_ == INVALID_HANDLE_VALUE)
|
|
{
|
|
wchar_t directory[32768]{};
|
|
const auto length = GetTempPathW(static_cast<DWORD>(std::size(directory)), directory);
|
|
if (!length || length >= std::size(directory)) fail("Locate temporary directory");
|
|
File file;
|
|
createTemporary(directory, file, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE);
|
|
spool_ = file.release();
|
|
}
|
|
seek(spool_, spoolSize_);
|
|
write(spool_, utf8);
|
|
changes_.insert_or_assign(page, Change{spoolSize_, utf8.size()});
|
|
spoolSize_ += utf8.size();
|
|
}
|
|
|
|
bool TextDocument::save()
|
|
{
|
|
if (!modified()) return true;
|
|
PendingSave pending;
|
|
File output;
|
|
pending.path = createTemporary(path_.parent_path(), output, FILE_ATTRIBUTE_NORMAL);
|
|
|
|
uint64_t written{};
|
|
const auto append = [&](std::string_view bytes) {
|
|
write(output.handle, bytes);
|
|
written += bytes.size();
|
|
};
|
|
const auto copyUntil = [&](uint64_t &offset, uint64_t end) {
|
|
while (offset < end)
|
|
{
|
|
const auto block = read(source_, offset, static_cast<size_t>(std::min(pageBytes, end - offset)));
|
|
append(block);
|
|
offset += block.size();
|
|
}
|
|
};
|
|
uint64_t offset{};
|
|
for (const auto &[page, change] : changes_)
|
|
{
|
|
copyUntil(offset, boundary(page));
|
|
append(encode(read(spool_, change.offset, change.size),encoding_));
|
|
offset = boundary(page + 1);
|
|
}
|
|
copyUntil(offset, size_);
|
|
if (!FlushFileBuffers(output.handle)) fail("Flush saved file");
|
|
CloseHandle(output.release());
|
|
|
|
File replacement{trackReplacement(pending.path)};
|
|
File current{openSource(path_, true)};
|
|
if (!sameFile(source_, current.handle)) throw std::runtime_error("The file was replaced outside KSwitcher.");
|
|
const auto backup = temporaryPath(path_.parent_path());
|
|
if (!ReplaceFileW(path_.c_str(), pending.path.c_str(), backup.c_str(), 0, nullptr, nullptr))
|
|
{
|
|
const auto error = GetLastError();
|
|
if (GetFileAttributesW(backup.c_str()) != INVALID_FILE_ATTRIBUTES &&
|
|
!MoveFileExW(backup.c_str(), path_.c_str(), MOVEFILE_WRITE_THROUGH))
|
|
{
|
|
const auto location = backup.u8string();
|
|
throw std::runtime_error("Could not restore the original name after a save error. The original is at " +
|
|
std::string(location.begin(), location.end()));
|
|
}
|
|
SetLastError(error);
|
|
fail("Replace original file");
|
|
}
|
|
pending.path.clear();
|
|
const auto newSource = ReOpenFile(replacement.handle, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE, 0);
|
|
DeleteFileW(backup.c_str());
|
|
CloseHandle(source_);
|
|
source_ = newSource;
|
|
size_ = written;
|
|
changes_.clear();
|
|
if (spool_ != INVALID_HANDLE_VALUE) CloseHandle(std::exchange(spool_, INVALID_HANDLE_VALUE));
|
|
spoolSize_ = 0;
|
|
return source_ != INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
bool TextDocument::saveNew(std::filesystem::path path,TextSaveFormat format)
|
|
{
|
|
if(!path_.empty() || !editable_) throw std::logic_error("The document is not a new editable file.");
|
|
if(format.encoding==TextEncoding::automatic) format.encoding=TextEncoding::utf8;
|
|
const auto bytes=encode(convertLineEndings(readPage(0),format.lineEnding),format.encoding);
|
|
const auto bom=format.bom?signature(format.encoding):std::string_view{};
|
|
PendingSave pending;
|
|
File output;
|
|
pending.path=createTemporary(path.parent_path(),output,FILE_ATTRIBUTE_NORMAL);
|
|
write(output.handle,bom);
|
|
write(output.handle,bytes);
|
|
if(!FlushFileBuffers(output.handle)) fail("Flush saved file");
|
|
CloseHandle(output.release());
|
|
File replacement{trackReplacement(pending.path)};
|
|
if(!MoveFileExW(pending.path.c_str(),path.c_str(),MOVEFILE_WRITE_THROUGH)) fail("Create new file");
|
|
pending.path.clear();
|
|
path_=std::move(path);
|
|
encoding_=format.encoding;
|
|
bom_=bom.size();
|
|
size_=bom_+bytes.size();
|
|
changes_.clear();
|
|
if(spool_!=INVALID_HANDLE_VALUE) CloseHandle(std::exchange(spool_,INVALID_HANDLE_VALUE));
|
|
spoolSize_=0;
|
|
source_=ReOpenFile(replacement.handle,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_DELETE,0);
|
|
return source_!=INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|