File: directory-io.md

package info (click to toggle)
glaze 6.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,948 kB
  • sloc: cpp: 121,839; sh: 99; ansic: 26; makefile: 13
file content (24 lines) | stat: -rw-r--r-- 1,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Directory Serialization and Deserialization

Glaze supports reading and writing from an entire directory of files into a map like object.

`#include "glaze/glaze.hpp"` or specifically include: 

- `#include "glaze/file/read_directory.hpp"` for read support
- `#include "glaze/file/write_directory.hpp"` for write support

The key to the map is the path to the individual file and the value is any C++ type.

The code below demonstrates writing out a map of two file paths and associated structs to two separate files. We then read the created directory into a default structure of the same form.

```c++
std::map<std::filesystem::path, my_struct> files{{"./dir/alpha.json", {}}, {"./dir/beta.json", {.i = 0}}};
expect(not glz::write_directory(files, "./dir"));

std::map<std::filesystem::path, my_struct> input{};
expect(not glz::read_directory(input, "./dir"));
expect(input.size() == 2);
expect(input.contains("./dir/alpha.json"));
expect(input.contains("./dir/beta.json"));
expect(input["./dir/beta.json"].i == 0);
```