File I/O
This page documents the file and path utilities in Capy.
Code snippets assume using namespace boost::capy; is in effect.
|
file
A platform-independent file handle:
file f("data.txt", file_mode::read);
// Read data
std::vector<char> buf(1024);
std::size_t n = f.read(buf.data(), buf.size());
// Write data
f.write(data.data(), data.size());
// Query and seek
std::uint64_t sz = f.size();
std::uint64_t pos = f.pos();
f.seek(100);
File Modes
| Mode | Description |
|---|---|
|
Open for reading (must exist) |
|
Create or truncate for writing |
|
Open for appending (create if needed) |
|
Open for both reading and writing |
path
An owning, mutable path string with UTF-8 encoding:
path p("C:/Users/data.txt");
// Decomposition
path_view dir = p.parent_path(); // "C:/Users"
path_view name = p.filename(); // "data.txt"
path_view stem = p.stem(); // "data"
path_view ext = p.extension(); // ".txt"
// Modification
p /= "subdir"; // Append with separator
p.replace_extension(".json");
// Native conversion (Windows)
std::wstring native = p.native_wstring();
Internal Format
Paths use forward slashes internally, regardless of platform:
path p("C:\\Users\\data.txt"); // Input with backslashes
std::cout << p.string(); // "C:/Users/data.txt"
This enables cross-platform serialization. Native format conversion happens at API boundaries.
Validation
Paths are validated at construction time:
// Throws system_error on invalid path
path p("\0invalid"); // Embedded null
// Non-throwing alternative
auto result = try_parse_path(input);
if (result)
use(*result);
else
handle_error(result.error());
Decomposition Reference
| Method | Example Input | Result |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path_view
A non-owning reference to a valid path string:
void process(path_view p)
{
// Decomposition works the same
path_view name = p.filename();
// Convert to owning path if needed
path owned(p);
}
Path views are validated at construction, just like paths. All decomposition methods return path_view pointing into the original storage.
Path Generation
// Normalize: remove "." and "..", collapse separators
path normal = p.lexically_normal();
// Relative path from base to target
path rel = target.lexically_relative(base);
// Same as relative, but returns target if not possible
path prox = target.lexically_proximate(base);
Iteration
Iterate over path components:
path p("C:/foo/bar");
for (path_view component : p)
{
// "C:", "/", "foo", "bar"
}
// Or iterate as string_view
for (std::string_view segment : p.segments())
{
// Same components as string_view
}
Summary
| Class | Purpose |
|---|---|
|
Platform-independent file handle |
|
File open mode enumeration |
|
Owning UTF-8 path string |
|
Non-owning path reference |
|
Non-throwing path parsing |
Next Steps
-
Compression — Brotli and ZLib support