Skip to content

Commit

Permalink
Cleanup std::filesystem::path usage (#1011)
Browse files Browse the repository at this point in the history
  • Loading branch information
xelatihy authored Aug 15, 2020
1 parent bf7319e commit fc63d17
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 94 deletions.
11 changes: 5 additions & 6 deletions apps/yimageproc/yimageproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,11 @@ int main(int argc, const char* argv[]) {
auto error = ""s;

// load
auto ext = path(filename).extension().string();
auto basename = path(filename).stem().string();
auto ioerror = ""s;
auto img = image<vec4f>{};
if (ext == ".ypreset") {
if (!make_image_preset(basename, img, ioerror)) print_fatal(ioerror);
auto ioerror = ""s;
auto img = image<vec4f>{};
if (path{filename}.extension().string() == ".ypreset") {
if (!make_image_preset(path{filename}.stem().string(), img, ioerror))
print_fatal(ioerror);
} else {
if (!load_image(filename, img, ioerror)) print_fatal(ioerror);
}
Expand Down
10 changes: 5 additions & 5 deletions apps/yimageview/yimageview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ void update_display(app_state* app) {
void load_image_async(app_states* apps, const string& filename) {
auto app = apps->states.emplace_back(new app_state{});
app->filename = filename;
app->outname = path(filename).replace_extension(".display.png").string();
app->name = path(filename).filename().string();
app->outname = path{filename}.replace_extension(".display.png").string();
app->name = path{filename}.filename().string();
app->exposure = apps->exposure;
app->filmic = apps->filmic;
app->params = apps->params;
Expand Down Expand Up @@ -164,8 +164,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
}
continue_line(win);
if (draw_filedialog_button(win, "save", apps->selected && apps->selected->ok,
"save image", save_path, true, path(save_path).parent_path().string(),
path(save_path).filename().string(),
"save image", save_path, true, path{save_path}.parent_path().string(),
path{save_path}.filename().string(),
"*.png;*.jpg;*.tga;*.bmp;*.hdr;*.exr")) {
auto app = apps->selected;
app->outname = save_path;
Expand Down Expand Up @@ -227,7 +227,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
end_header(win);
}
if (begin_header(win, "inspect")) {
draw_label(win, "image", path(app->filename).filename().string());
draw_label(win, "image", path{app->filename}.filename().string());
draw_label(win, "filename", app->filename);
draw_label(win, "outname", app->outname);
draw_label(win, "image",
Expand Down
6 changes: 2 additions & 4 deletions apps/ymeshproc/ymeshproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,9 @@ int main(int argc, const char* argv[]) {
// load mesh
auto ioerror = ""s;
print_progress("load mesh", 0, 1);
auto ext = path(filename).extension().string();
auto basename = path(filename).stem().string();
if (ext == ".ypreset") {
if (path{filename}.extension().string() == ".ypreset") {
if (!make_mesh_preset(triangles, positions, normals, texcoords, colors,
basename, ioerror))
path{filename}.stem().string(), ioerror))
print_fatal(ioerror);
} else {
if (!load_mesh(filename, triangles, positions, normals, texcoords, colors,
Expand Down
16 changes: 8 additions & 8 deletions apps/ysceneitrace/ysceneitrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ void reset_display(app_state* app) {
void load_scene_async(app_states* apps, const string& filename,
const string& camera_name = "", bool add_skyenv = false) {
auto app = apps->states.emplace_back(new app_state{});
app->name = path(filename).filename().string() + " [loading]";
app->name = path{filename}.filename().string() + " [loading]";
app->filename = filename;
app->imagename = path(filename).replace_extension(".png").string();
app->outname = path(filename).replace_extension(".edited.yaml").string();
app->imagename = path{filename}.replace_extension(".png").string();
app->outname = path{filename}.replace_extension(".edited.yaml").string();
app->params = apps->params;
app->status = "load";
app->loader = std::async(std::launch::async, [app, camera_name,
Expand Down Expand Up @@ -462,8 +462,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
}
continue_line(win);
if (draw_filedialog_button(win, "save", apps->selected && apps->selected->ok,
"save", save_path, true, path(save_path).parent_path().string(),
path(save_path).filename().string(), "*.yaml;*.obj;*.pbrt")) {
"save", save_path, true, path{save_path}.parent_path().string(),
path{save_path}.filename().string(), "*.yaml;*.obj;*.pbrt")) {
auto app = apps->selected;
app->outname = save_path;
save_scene(app->outname, app->ioscene, app->error);
Expand All @@ -472,8 +472,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
continue_line(win);
if (draw_filedialog_button(win, "save image",
apps->selected && apps->selected->ok, "save image", save_path, true,
path(save_path).parent_path().string(),
path(save_path).filename().string(),
path{save_path}.parent_path().string(),
path{save_path}.filename().string(),
"*.png;*.jpg;*.tga;*.bmp;*.hdr;*.exr")) {
auto app = apps->selected;
app->outname = save_path;
Expand Down Expand Up @@ -527,7 +527,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
end_header(win);
}
if (begin_header(win, "inspect")) {
draw_label(win, "scene", path(app->filename).filename().string());
draw_label(win, "scene", path{app->filename}.filename().string());
draw_label(win, "filename", app->filename);
draw_label(win, "outname", app->outname);
draw_label(win, "imagename", app->imagename);
Expand Down
19 changes: 9 additions & 10 deletions apps/ysceneproc/ysceneproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,9 +796,9 @@ bool make_preset(scene_model* scene, const string& type, string& error) {
}

void make_dir(const string& dirname) {
if (exists(path(dirname))) return;
if (exists(path{dirname})) return;
try {
create_directories(path(dirname));
create_directories(path{dirname});
} catch (...) {
print_fatal("cannot create directory " + dirname);
}
Expand All @@ -822,14 +822,13 @@ int main(int argc, const char* argv[]) {
parse_cli(cli, argc, argv);

// load scene
auto ext = path(filename).extension().string();
auto basename = path(filename).stem().string();
auto scene_guard = std::make_unique<scene_model>();
auto scene = scene_guard.get();
auto ioerror = ""s;
if (ext == ".ypreset") {
if (path{filename}.extension().string() == ".ypreset") {
print_progress("make preset", 0, 1);
if (!make_preset(scene, basename, ioerror)) print_fatal(ioerror);
if (!make_preset(scene, path{filename}.stem().string(), ioerror))
print_fatal(ioerror);
print_progress("make preset", 1, 1);
} else {
if (!load_scene(filename, scene, ioerror, print_progress))
Expand All @@ -853,16 +852,16 @@ int main(int argc, const char* argv[]) {
}

// tesselate if needed
if (path(output).extension() != ".json") {
if (path{output}.extension() != ".json") {
tesselate_shapes(scene, print_progress);
}

// make a directory if needed
make_dir(path(output).parent_path().string());
make_dir(path{output}.parent_path().string());
if (!scene->shapes.empty())
make_dir((path(output).parent_path() / "shapes").string());
make_dir((path{output}.parent_path() / "shapes").string());
if (!scene->textures.empty())
make_dir((path(output).parent_path() / "textures").string());
make_dir((path{output}.parent_path() / "textures").string());

// save scene
if (!save_scene(output, scene, ioerror, print_progress)) print_fatal(ioerror);
Expand Down
12 changes: 6 additions & 6 deletions apps/yscenetrace/yscenetrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int main(int argc, const char* argv[]) {
const image<vec4f>& render, int sample, int samples) {
if (!save_batch) return;
auto ext = "-s" + std::to_string(sample + samples) +
path(imfilename).extension().string();
auto outfilename = path(imfilename).replace_extension(ext).string();
path{imfilename}.extension().string();
auto outfilename = path{imfilename}.replace_extension(ext).string();
auto ioerror = ""s;
print_progress("save image", sample, samples);
if (!save_image(outfilename, render, ioerror)) print_fatal(ioerror);
Expand All @@ -120,10 +120,10 @@ int main(int argc, const char* argv[]) {
const int feature_samples = 8;
std::string feature_ext = "exr"s;

auto imext = path(imfilename).extension().string();
auto imext = path{imfilename}.extension().string();
if (imext != "hdr" && is_hdr_filename(imext)) feature_ext = imext;

auto base_name = path(imfilename).filename().replace_extension("").string();
auto base_name = path{imfilename}.filename().replace_extension("").string();

auto fparams = params;
fparams.bounces = feature_bounces;
Expand All @@ -132,7 +132,7 @@ int main(int argc, const char* argv[]) {
// render denoise albedo
fparams.sampler = trace_sampler_type::albedo;
auto albedo = trace_image(scene, camera, fparams, print_progress);
auto albedo_filename = path(imfilename)
auto albedo_filename = path{imfilename}
.replace_filename(base_name + "-albedo")
.replace_extension(feature_ext)
.string();
Expand All @@ -144,7 +144,7 @@ int main(int argc, const char* argv[]) {
// render denoise normals
fparams.sampler = trace_sampler_type::normal;
auto normal = trace_image(scene, camera, fparams, print_progress);
auto normal_filename = path(imfilename)
auto normal_filename = path{imfilename}
.replace_filename(base_name + "-normal")
.replace_extension(feature_ext)
.string();
Expand Down
12 changes: 6 additions & 6 deletions apps/ysceneview/ysceneview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ void load_scene_async(
app_states* apps, const string& filename, const string& camera_name = "") {
auto app = apps->states.emplace_back(new app_state{});
app->filename = filename;
app->imagename = path(filename).replace_extension(".png").string();
app->outname = path(filename).replace_extension(".edited.yaml").string();
app->name = path(app->filename).filename().string();
app->imagename = path{filename}.replace_extension(".png").string();
app->outname = path{filename}.replace_extension(".edited.yaml").string();
app->name = path{app->filename}.filename().string();
app->drawgl_prms = apps->drawgl_prms;
app->status = "load";
app->loader = std::async(std::launch::async, [app, camera_name]() {
Expand Down Expand Up @@ -422,8 +422,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
}
continue_line(win);
if (draw_filedialog_button(win, "save", apps->selected && apps->selected->ok,
"save", save_path, true, path(save_path).parent_path().string(),
path(save_path).filename().string(), "*.yaml;*.obj;*.pbrt")) {
"save", save_path, true, path{save_path}.parent_path().string(),
path{save_path}.filename().string(), "*.yaml;*.obj;*.pbrt")) {
auto app = apps->selected;
app->outname = save_path;
save_scene(app->outname, app->ioscene, app->error);
Expand Down Expand Up @@ -472,7 +472,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
end_header(win);
}
if (begin_header(win, "inspect")) {
draw_label(win, "scene", path(app->filename).filename().string());
draw_label(win, "scene", path{app->filename}.filename().string());
draw_label(win, "filename", app->filename);
draw_label(win, "outname", app->outname);
draw_label(win, "imagename", app->imagename);
Expand Down
7 changes: 3 additions & 4 deletions apps/yshapeproc/yshapeproc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,9 @@ int main(int argc, const char* argv[]) {
// load mesh
auto ioerror = ""s;
print_progress("load shape", 0, 1);
auto ext = path(filename).extension().string();
auto basename = path(filename).stem().string();
if (ext == ".ypreset") {
if (!make_shape_preset(shape, basename, ioerror)) print_fatal(ioerror);
if (path{filename}.extension().string() == ".ypreset") {
if (!make_shape_preset(shape, path{filename}.stem().string(), ioerror))
print_fatal(ioerror);
} else {
if (!load_shape(filename, shape, ioerror, facevarying))
print_fatal(ioerror);
Expand Down
12 changes: 6 additions & 6 deletions apps/yshapeview/yshapeview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ void load_shape_async(
app_states* apps, const string& filename, const string& camera_name = "") {
auto app = apps->states.emplace_back(new app_state{});
app->filename = filename;
app->imagename = path(filename).replace_extension(".png").string();
app->outname = path(filename).replace_extension(".edited.obj").string();
app->name = path(app->filename).filename().string();
app->imagename = path{filename}.replace_extension(".png").string();
app->outname = path{filename}.replace_extension(".edited.obj").string();
app->name = path{app->filename}.filename().string();
app->drawgl_prms = apps->drawgl_prms;
app->status = "load";
app->loader = std::async(std::launch::async, [app, camera_name]() {
Expand Down Expand Up @@ -233,8 +233,8 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
}
continue_line(win);
if (draw_filedialog_button(win, "save", apps->selected && apps->selected->ok,
"save", save_path, true, path(save_path).parent_path().string(),
path(save_path).filename().string(), "*.ply;*.obj")) {
"save", save_path, true, path{save_path}.parent_path().string(),
path{save_path}.filename().string(), "*.ply;*.obj")) {
auto app = apps->selected;
app->outname = save_path;
auto ok = save_shape(app->outname, *app->ioshape, app->error);
Expand Down Expand Up @@ -297,7 +297,7 @@ void draw_widgets(gui_window* win, app_states* apps, const gui_input& input) {
end_header(win);
}
if (begin_header(win, "inspect")) {
draw_label(win, "shape", path(app->filename).filename().string());
draw_label(win, "shape", path{app->filename}.filename().string());
draw_label(win, "filename", app->filename);
draw_label(win, "outname", app->outname);
draw_label(win, "imagename", app->imagename);
Expand Down
32 changes: 16 additions & 16 deletions libs/yocto/yocto_modelio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ bool load_obj(const string& filename, obj_model* obj, string& error,
if (std::find(mtllibs.begin(), mtllibs.end(), mtllib) == mtllibs.end()) {
mtllibs.push_back(mtllib);
if (!load_mtl(
(path(filename).parent_path() / mtllib).string(), obj, error))
(path{filename}.parent_path() / mtllib).string(), obj, error))
return dependent_error();
for (auto material : obj->materials)
material_map[material->name] = material;
Expand Down Expand Up @@ -1973,8 +1973,8 @@ bool load_obj(const string& filename, obj_model* obj, string& error,
if (geom_only) return true;

// load extensions
auto extfilename = path(filename).replace_extension(".objx");
if (exists(path(extfilename))) {
auto extfilename = path{filename}.replace_extension(".objx");
if (exists(path{extfilename})) {
if (!load_objx(extfilename.string(), obj, error)) return dependent_error();
}

Expand Down Expand Up @@ -2278,7 +2278,7 @@ inline void format_obj_value(string& str, const obj_vertex& value) {
// save material library
if (!obj->materials.empty()) {
if (!format_obj_values(fs, "mtllib {}\n\n",
(path(filename).filename().replace_extension(".mtl").string())))
(path{filename}.filename().replace_extension(".mtl").string())))
return write_error();
}

Expand Down Expand Up @@ -2326,7 +2326,7 @@ inline void format_obj_value(string& str, const obj_vertex& value) {
// save mtl
if (!obj->materials.empty()) {
if (!save_mtl(
path(filename).replace_extension(".mtl").string(), obj, error))
path{filename}.replace_extension(".mtl").string(), obj, error))
return dependent_error();
}

Expand All @@ -2335,7 +2335,7 @@ inline void format_obj_value(string& str, const obj_vertex& value) {
std::any_of(obj->shapes.begin(), obj->shapes.end(),
[](auto shape) { return !shape->instances.empty(); })) {
if (!save_objx(
path(filename).replace_extension(".objx").string(), obj, error))
path{filename}.replace_extension(".objx").string(), obj, error))
return dependent_error();
}

Expand Down Expand Up @@ -3567,18 +3567,18 @@ inline pair<vec3f, vec3f> get_subsurface(const string& name) {
auto filenames = vector<string>{};
if (!parse_pbrt_value(str, filename)) return false;
if (!str.data()) return false;
auto filenamep = path(filename).filename();
if (path(filenamep).extension() == ".spd") {
filenamep = path(filenamep).replace_extension("").string();
auto filenamep = path{filename}.filename();
if (path{filenamep}.extension() == ".spd") {
filenamep = path{filenamep}.replace_extension("").string();
if (filenamep == "SHPS") {
value.value3f = {1, 1, 1};
} else if (path(filenamep).extension() == ".eta") {
} else if (path{filenamep}.extension() == ".eta") {
auto eta =
get_etak(path(filenamep).replace_extension("").string()).first;
get_etak(path{filenamep}.replace_extension("").string()).first;
value.value3f = {eta.x, eta.y, eta.z};
} else if (path(filenamep).extension() == ".k") {
} else if (path{filenamep}.extension() == ".k") {
auto k =
get_etak(path(filenamep).replace_extension("").string()).second;
get_etak(path{filenamep}.replace_extension("").string()).second;
value.value3f = {k.x, k.y, k.z};
} else {
return false;
Expand Down Expand Up @@ -4725,7 +4725,7 @@ struct pbrt_context {
} else if (cmd == "Include") {
auto includename = ""s;
if (!parse_param(str, includename)) return parse_error();
if (!load_pbrt((path(filename).parent_path() / includename).string(),
if (!load_pbrt((path{filename}.parent_path() / includename).string(),
pbrt, error, ctx, material_map, named_materials, named_textures,
named_mediums, ply_dirname))
return dependent_error();
Expand Down Expand Up @@ -4768,7 +4768,7 @@ bool load_pbrt(const string& filename, pbrt_model* pbrt, string& error) {
auto named_materials = unordered_map<string, pbrt_material>{{"", {}}};
auto named_mediums = unordered_map<string, pbrt_medium>{{"", {}}};
auto named_textures = unordered_map<string, pbrt_texture>{{"", {}}};
auto dirname = path(filename).parent_path().string();
auto dirname = path{filename}.parent_path().string();
if (dirname != "") dirname += "/";
if (!load_pbrt(filename, pbrt, error, ctx, material_map, named_materials,
named_textures, named_mediums, dirname))
Expand Down Expand Up @@ -5044,7 +5044,7 @@ bool save_pbrt(
add_normals(ply, shape->normals);
add_texcoords(ply, shape->texcoords);
add_triangles(ply, shape->triangles);
if (!save_ply((path(filename).parent_path() / shape->filename_).string(),
if (!save_ply((path{filename}.parent_path() / shape->filename_).string(),
ply, error))
return dependent_error();
}
Expand Down
Loading

0 comments on commit fc63d17

Please sign in to comment.