tools svg2tvg: support converting multiple files.
authorHermet Park <chuneon.park@samsung.com>
Wed, 27 Oct 2021 11:53:01 +0000 (20:53 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Thu, 28 Oct 2021 06:19:31 +0000 (15:19 +0900)
Usage:
   svg2tvg [SVG file] or [SVG folder]

Examples:
    $ svg2tvg input.svg
    $ svg2tvg svgfolder

src/bin/svg2tvg/svg2tvg.cpp

index 79ba653..4924aac 100644 (file)
  */
 
 #include <iostream>
+#include <vector>
 #include <thorvg.h>
-#include <array>
+#include <dirent.h>
 
 using namespace std;
 using namespace tvg;
 
-   
+
 void helpMsg()
 {
-    cout<<"Usage: \n   svg2tvg [SVG file]\n\nExamples: \n    $ svg2tvg input.svg\n\n";
+    cout<<"Usage: \n   svg2tvg [SVG file] or [SVG folder]\n\nExamples: \n    $ svg2tvg input.svg\n    $ svg2tvg svgfolder\n\n";
 }
 
+
 bool convert(string& in, string& out)
 {
     if (Initializer::init(CanvasEngine::Sw, 0) != Result::Success) return false;
@@ -50,47 +52,111 @@ bool convert(string& in, string& out)
 }
 
 
-int main(int argc, char **argv)
+void convert(string& svgName)
 {
-    //No Input SVG
-    if (argc < 2) {
-        helpMsg();
-        return 0;
-    }
+    //Get tvg file
+    auto tvgName = svgName;
+    tvgName.replace(tvgName.length() - 3, 3, "tvg");
 
-    auto input = argv[1];
+    if (convert(svgName, tvgName)) {
+        cout<<"Generated TVG file : "<< tvgName << endl;
+    } else {
+        cout<<"Failed Converting TVG file : "<< svgName << endl;
+    }
+}
 
-    array<char, 5000> memory;
 
+char* getpath(const char* input)
+{
+    static char buf[PATH_MAX];
 #ifdef _WIN32
-    input = _fullpath(memory.data(), input, memory.size());
+    return fullpath(buf, input, PATH_MAX);
 #else
-    input = realpath(input, memory.data());
+    return realpath(input, buf);
 #endif
+}
 
-    //Verify svg file.
-    if (!input) {
-        helpMsg();
-        return 0;
-    }
 
-    string svgName(input);
+bool validate(string& svgName)
+{
     string extn = ".svg";
 
     if (svgName.size() <= extn.size() || svgName.substr(svgName.size() - extn.size()) != extn) {
+        cout << "Error: \"" << svgName << "\" is invalid." << endl;
+        return false;
+    }
+    return true;
+}
+
+
+void directory(const string& path, DIR* dir)
+{
+    //List directories
+    while (auto entry = readdir(dir)) {
+        if (*entry->d_name == '.' || *entry->d_name == '$') continue;
+        if (entry->d_type == DT_DIR) {
+            string subpath = string(path);
+#ifdef _WIN32
+            subpath += '\\';
+#else
+            subpath += '/';
+#endif
+            subpath += entry->d_name;
+
+            //open directory
+            if (auto subdir = opendir(subpath.c_str())) {
+                cout << "Sub Directory: \"" << subpath << "\"" << endl;
+                directory(subpath, subdir);
+                closedir(dir);
+            }
+
+        } else {
+            string svgName(entry->d_name);
+            if (!validate(svgName)) continue;
+            svgName = string(path);
+#ifdef _WIN32
+            svgName += '\\';
+#else
+            svgName += '/';
+#endif
+            svgName += entry->d_name;
+
+            convert(svgName);
+        }
+    }
+}
+
+
+
+int main(int argc, char **argv)
+{
+    //Collect input files
+    vector<const char*> inputs;
+
+    for (int i = 1; i < argc; ++i) {
+        inputs.push_back(argv[i]);
+    }
+
+    //No Input SVG
+    if (inputs.empty()) {
         helpMsg();
         return 0;
     }
 
-    //Get tvg file.
-    auto tvgName = svgName.substr(svgName.find_last_of("/\\") + 1);
-    tvgName.replace(tvgName.length() - 3, 3, "tvg");
+    for (auto input : inputs) {
 
-    //Convert!
-    if (convert(svgName, tvgName)) {
-        cout<<"Generated TVG file : "<< tvgName << endl;
-    } else {
-        cout<<"Failed Converting TVG file : "<< tvgName << endl;
+        auto path = getpath(input);
+
+        if (auto dir = opendir(path)) {
+            //load from directory
+            cout << "Directory: \"" << path << "\"" << endl;
+            directory(path, dir);
+            closedir(dir);
+        } else {
+            string svgName(input);
+            if (!validate(svgName)) continue;
+            convert(svgName);
+        }
     }
 
     return 0;