Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge IO functionality in Yocto/IO. #1275

Merged
merged 21 commits into from
Sep 6, 2021
1 change: 0 additions & 1 deletion apps/yimage/yimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include <yocto/yocto_cli.h>
#include <yocto/yocto_color.h>
#include <yocto/yocto_commonio.h>
#include <yocto/yocto_image.h>
#include <yocto/yocto_math.h>
#include <yocto/yocto_scene.h>
Expand Down
1 change: 0 additions & 1 deletion apps/ymesh/ymesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
//

#include <yocto/yocto_cli.h>
#include <yocto/yocto_commonio.h>
#include <yocto/yocto_geometry.h>
#include <yocto/yocto_image.h>
#include <yocto/yocto_math.h>
Expand Down
1 change: 0 additions & 1 deletion apps/yscene/yscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
//

#include <yocto/yocto_cli.h>
#include <yocto/yocto_commonio.h>
#include <yocto/yocto_math.h>
#include <yocto/yocto_scene.h>
#include <yocto/yocto_sceneio.h>
Expand Down
1 change: 0 additions & 1 deletion apps/yshape/yshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
//

#include <yocto/yocto_cli.h>
#include <yocto/yocto_commonio.h>
#include <yocto/yocto_geometry.h>
#include <yocto/yocto_image.h>
#include <yocto/yocto_math.h>
Expand Down
7 changes: 2 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@ Here is a list of the main libraries.
supporting area and environment illumination, microfacet GGX and subsurface
scattering, multiple importance sampling
- [Yocto/Cli](yocto/yocto_cli.md): printing utilities and command line parsing
- [Yocto/ImageIO](yocto/yocto_imageio.md): image serialization
- [Yocto/ShapeIO](yocto/yocto_shapeio.md): shape serialization
- [Yocto/SceneIO](yocto/yocto_sceneio.md): scene serialization
- [Yocto/CommonIO](yocto/yocto_commonio.md): common IO operations, path helpers,
text and binary serialization, Json data model, Json serialization
- [Yocto/SceneIO](yocto/yocto_sceneio.md): image, shape and scene serialization
- [Yocto/ModelIO](yocto/yocto_modelio.md): low-level parsing and writing for
Ply, Obj, Stl, Pbrt formats
- [Yocto/Parallel](yocto/yocto_parallel.md): concurrency utilities
- [Yocto/Json](yocto/yocto_json.md): Json data model and serialization (deprecated)

## Example Applications

Expand Down
23 changes: 0 additions & 23 deletions docs/yocto/yocto_imageio.md

This file was deleted.

94 changes: 5 additions & 89 deletions docs/yocto/yocto_commonio.md → docs/yocto/yocto_json.md
Original file line number Diff line number Diff line change
@@ -1,90 +1,8 @@
# Yocto/CommonIO: Common IO functionality

Yocto/CommonIO is a collection of utilities used in writing IO functionality,
including file IO, Json IO, and path manipulation.
Yocto/CommonIO is implemented in `yocto_commonio.h` and `yocto_commonio.cpp`,
and depends on `json.hpp` for Json serialization and number printing, and
`fast_float.h` for number parsing.

## Errors handling in IO functions

IO functions in Yocto/GL have a dual interface to support their use with and
without exceptions. IO functions with exception are written as
`load_<type>(filename, data, <options>)` and
`save_<type>(filename, data, <options>)` where `<type>` is the data type
read or written, and `<options>` is an optional list of IO options.
A convenience shortcut is provided for all loading functions that returns the
data directly, written as `data = load_<type>(filename, <options>)`.
Upon errors, an `io_error` is thrown from all IO functions.
This makes the error-handling code more uniform across the library.
`io_error` has fields for `filename` and `message` that can retrieved directly,
and a message for users that can be retrieved with the `.what()` method.
# Yocto/Json: Json functionality

```cpp
auto text = string{};
try {
load_text("input_file.txt", text); // load text
text = load_text("input_file.txt"); // alternative load
save_text("output_file.txt", text); // save text
} catch(const io_error& error) {
print_info(error.what()); exit(1); // handle error
}
```

IO functions without exceptions are written as
`load_<type>(filename, data, error, <options>)` and
`save_<type>(filename, data, error, <options>)` where `error` is a string
that contains a message if an error occurred. These functions return a boolean
flag that indicates whether the operation succeeded.

```cpp
auto text = string{};
auto error = string{};
if(!load_text("input_file.txt", text)) // load text
print_info(error); exit(1); // handle error
if(!save_text("output_file.txt", text)) // save text
print_info(error); exit(1); // handle error
```

## Text and binary serialization

Yocto/CommonIO supports reading and writing whole files of either binary
data or text. Use `load_text(filename, text)` or `text = load_text(filename)`
to load text, and `save_text(filename, text)` to save it.
Use `load_binary(filename, data)` or `data = load_binary(filename)`
to load text, and `save_binary(filename, data)` to save it.
Text is stored as a string and binary data is stored as an array of bytes.
Use without exception is supported as described above.

```cpp
auto text = string{};
load_text("input_file.txt", text); // load text
save_text("output_file.txt", text); // save text
auto text1 = load_text("input_file.txt"); // alternative load
auto data = vector<byte>{};
load_binary("input_file.bin", data); // load data
save_binary("output_file.bin", data); // save data
auto data1 = load_binary("input_file.bin"); // alternative load
```

## Path manipulation utilities

Yocto/CommonIO contains several helper function to manipulate paths. Paths
are encoded in UTF8 across the library and these functions make it easier to
handle UTF8-encoded paths across operating systems, by wrapping
`std::filesystem` with a string interface.

Use `path_dirname(filename)`, `path_extension(filename)`,
`path_filename(filename)`, `path_basename(fillename)`
to extract the directory, extension, filename and basename from a path.
Use `path_join(patha,pathb)` to joins paths and
`replace_extension(filename,ext)` to replace a path extension.
Use `path_exists(filename)` to check if a path exists and
`path_isdir(filename)` and `path_isfile(filename)` to check whether
it is a directory ot file respectively.
Use `list_directory(dirname)` to list directory contents,
`make_directory(dirname)` to create a directory, and
`path_current()` to get the current directory.
Yocto/Json is an implementation of a Json type and related IO functionality.
Yocto/Json is implemented in `yocto_json.h` and `yocto_json.cpp`,
and depends on `json.hpp` for Json serialization.

## Json data type

Expand Down Expand Up @@ -184,9 +102,7 @@ json.try_get("key", value); // get value if present

Use `load_json(filename, json)` or `json = load_json(filename)` to load json
data from a file, and `save_json(filename, json)` to save json data to a file.
Upon errors, an `io_error` is thrown from all IO functions.
See [Yocto/CommonIO](yocto_commonio.md) for discussion on error handling
and use without exceptions.
Upon errors, an `json_error` is thrown from all IO functions.

```cpp
auto json = json_value{};
Expand Down
Loading