From 57f8e7ca3ba5c402b180ffde2aef547a10d2f790 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Mon, 4 Jun 2018 17:52:08 +0900 Subject: [PATCH] Correctly handle filename containing spaces for external resources. Fixes #74. --- experimental/README.md | 15 +++++++++++++++ experimental/gen.py | 34 ++++++++++++++++++++++++++++++++++ tiny_gltf_loader.h | 10 ++++++---- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 experimental/README.md create mode 100644 experimental/gen.py diff --git a/experimental/README.md b/experimental/README.md new file mode 100644 index 0000000..cf8f21e --- /dev/null +++ b/experimental/README.md @@ -0,0 +1,15 @@ +# Python script which generates C++11 code from JSON schema. + +## Requirements + +* python3 +* jsonref + +## Generate + +Run `gen.py` by specifing the path to glTF schema directory(from https://github.com/KhronosGroup/glTF.git) + +``` +$ python gen.py /path/to/glTF/specification/2.0/schema +``` + diff --git a/experimental/gen.py b/experimental/gen.py new file mode 100644 index 0000000..25b0484 --- /dev/null +++ b/experimental/gen.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys, os +import subprocess +import json +from pprint import pprint +import jsonref + +# glTF 2.0 +schema_files = [ + "glTF.schema.json" +] + +def main(): + if len(sys.argv) < 2: + print("Requires path to glTF scheme directory.") + sys.exit(-1) + + gltf_schema_dir = sys.argv[1] + + gltf_schema_filepath = os.path.join(gltf_schema_dir, schema_files[0]) + if not os.path.exists(gltf_schema_filepath): + print("File not found: {}".format(gltf_schema_filepath)) + sys.exit(-1) + + gltf_schema_uri = 'file://{}/'.format(gltf_schema_dir) + with open(gltf_schema_filepath) as schema_file: + j = jsonref.loads(schema_file.read(), base_uri=gltf_schema_uri, jsonschema=True) + pprint(j) + + + +main() diff --git a/tiny_gltf_loader.h b/tiny_gltf_loader.h index 90e02f3..6e55276 100644 --- a/tiny_gltf_loader.h +++ b/tiny_gltf_loader.h @@ -733,15 +733,17 @@ static std::string ExpandFilePath(const std::string &filepath) { } // char** w; - int ret = wordexp(filepath.c_str(), &p, 0); + // wrap filepath by quotes to avoid splitting file path when the path contains spaces(more precisely, $IFS environment variables). + std::string quoted_filepath = "\"" + filepath + "\""; + int ret = wordexp(quoted_filepath.c_str(), &p, 0); if (ret) { // err s = filepath; return s; } - // Use first element only. if (p.we_wordv) { + // Use first item. s = std::string(p.we_wordv[0]); wordfree(&p); } else { @@ -902,7 +904,7 @@ static bool LoadExternalFile(std::vector *out, std::string *err, std::string filepath = FindFile(paths, filename); if (filepath.empty()) { if (err) { - (*err) += "File not found : " + filename + "\n"; + (*err) += "File not found : \"" + filename + "\"\n"; } return false; } @@ -910,7 +912,7 @@ static bool LoadExternalFile(std::vector *out, std::string *err, std::ifstream f(filepath.c_str(), std::ifstream::binary); if (!f) { if (err) { - (*err) += "File open error : " + filepath + "\n"; + (*err) += "File open error : \"" + filepath + "\"\n"; } return false; }