diff options
author | jaseg <git@jaseg.de> | 2021-02-04 22:53:49 +0100 |
---|---|---|
committer | jaseg <git@jaseg.de> | 2021-02-04 22:53:49 +0100 |
commit | bd2b0127409dd2dfdd1173c740e79034812d008c (patch) | |
tree | 2e4e0e632e75a94da511859da95437bf24445f8a /svg-flatten/src | |
parent | fb9c6ac32ce578c04af8d241528c65a755e14216 (diff) | |
download | gerbolyze-bd2b0127409dd2dfdd1173c740e79034812d008c.tar.gz gerbolyze-bd2b0127409dd2dfdd1173c740e79034812d008c.tar.bz2 gerbolyze-bd2b0127409dd2dfdd1173c740e79034812d008c.zip |
svg-flatten: Be a bit smarter locating usvg
Diffstat (limited to 'svg-flatten/src')
-rw-r--r-- | svg-flatten/src/main.cpp | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/svg-flatten/src/main.cpp b/svg-flatten/src/main.cpp index 3cf9a36..53ce19b 100644 --- a/svg-flatten/src/main.cpp +++ b/svg-flatten/src/main.cpp @@ -1,6 +1,8 @@ #include <cstdlib> #include <cstdio> +#include <sys/types.h> +#include <pwd.h> #include <filesystem> #include <iostream> #include <fstream> @@ -338,30 +340,59 @@ int main(int argc, char **argv) { } string dpi_str = to_string(dpi); - const char *command_line[] = {"usvg", "--keep-named-groups", "--dpi", dpi_str.c_str(), barf.c_str(), frob.c_str(), NULL}; - struct subprocess_s subprocess; - int rc = subprocess_create(command_line, subprocess_option_inherit_environment, &subprocess); - if (rc) { - cerr << "Error calling usvg!" << endl; - return EXIT_FAILURE; + const char *command_line[] = {nullptr, "--keep-named-groups", "--dpi", dpi_str.c_str(), barf.c_str(), frob.c_str(), NULL}; + bool found_usvg = false; + int usvg_rc=-1; + for (int i=0; i<2; i++) { + const char *homedir; + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + string homedir_s(homedir); + string loc_in_home = homedir_s + "/.cargo/bin/usvg"; + + if (i == 0) { + command_line[0] = "usvg"; + } else { + command_line[0] = loc_in_home.c_str(); + } + + struct subprocess_s subprocess; + int rc = subprocess_create(command_line, subprocess_option_inherit_environment, &subprocess); + if (rc) { + cerr << "Error calling usvg!" << endl; + return EXIT_FAILURE; + } + + usvg_rc = -1; + rc = subprocess_join(&subprocess, &usvg_rc); + if (rc) { + cerr << "Error calling usvg!" << endl; + return EXIT_FAILURE; + } + + rc = subprocess_destroy(&subprocess); + if (rc) { + cerr << "Error calling usvg!" << endl; + return EXIT_FAILURE; + } + + if (usvg_rc == 255) { + continue; + } + found_usvg = true; + break; } - int usvg_rc = 0; - rc = subprocess_join(&subprocess, &usvg_rc); - if (rc) { - cerr << "Error calling usvg!" << endl; + if (!found_usvg) { + cerr << "Error: Cannot find usvg. Is it installed and in $PATH?" << endl; return EXIT_FAILURE; } + if (usvg_rc) { cerr << "usvg returned an error code: " << usvg_rc << endl; return EXIT_FAILURE; } - - rc = subprocess_destroy(&subprocess); - if (rc) { - cerr << "Error calling usvg!" << endl; - return EXIT_FAILURE; - } } VectorizerSelectorizer vec_sel(vectorizer, args["vectorizer_map"] ? args["vectorizer_map"] : ""); |