# 2023-10-12 ## Adventures in writing a text editor Last night, I found out about the std::filesystem functionality. This makes my life a lot easier; I was looking at having to write portable code to do things like check permissions and getting file sizes. It's all well and good in Linux (and MacOS for that matter), but on WIndows it's a mess. Also, having to manage a bunch of #if defined (__linux__) cases isn't ideal. Here's an example of a function that this allows[1]: static bool checkAccess(std::filesystem::path path, std::filesystem::perms mode, bool checkParent) { if (!std::filesystem::exists(path) && checkParent) { auto fullPath = std::filesystem::absolute(path); auto parent = fullPath.parent_path(); auto dirEnt = std::filesystem::directory_entry(parent); auto perms = dirEnt.status().permissions(); return (perms & mode) != std::filesystem::perms::none; } return (status(path).permissions() & mode) != std::filesystem::perms::none; } I "wasted" some time writing a line ending checker, before realizing that I didn't have to because std::getline is a thing. I was thinking about how to express line endings and I think the simplest way to do that is to read 512B (which I think is how mime checking is normally done), and I'll count the number of '\r' and '\n': if they match, then it's Windows line endings. In any other case, such as when the file doesn't exist, I'm just going to default to Unix line endings. The tools I use are capable of handling these just fine, and can convert if they want. I wrote a few test sketches to look at some things; for this, Dev-C++ is pretty useful, even if it was kind of a pain to work out how to enable C++17 mode. It's a quick write-some-code and run it for Windows (my desktop that I've been working on kge a lot on runs Windows). It's not *quite* as nice as `make ` on the Unix systems, but it gets the job done. I didn't get around to writing the Frame yet, but I did some thinking about it on the drive into work. Constructing a new Buffer with a file path doesn't automatically load the file - although it could, I guess, as I could mark the underlying File class as read only. I don't think any of the standard library code I'm using throws exceptions, so on second thought, it probably makes sense. I can also do a first-pass at line-ending detection. ### Stomping on files When I added that, I noticed that files were suddenly getting stomped on. [1] https://kls.ai6ua.net/B78QZ