Skip to content

Commit

Permalink
[impl] save to file
Browse files Browse the repository at this point in the history
  • Loading branch information
n-taka committed Sep 29, 2020
1 parent c759557 commit 6327cbf
Show file tree
Hide file tree
Showing 8 changed files with 322 additions and 51 deletions.
260 changes: 222 additions & 38 deletions UnPoseSymmetricMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,91 @@
#define DLLEXPORT __attribute__((visibility("default")))
#endif

namespace
{
void readCache(std::filesystem::path &metaPath, web::json::value &meta, std::filesystem::path &cachePath, web::json::value &cache)
{
////
// initialize
////
metaPath.clear();
meta = web::json::value();
cachePath.clear();
cache = web::json::value();

////
// parse meta
////
const std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
metaPath = tmpDir;
metaPath.append("UnPoseSymmetricMeta.json");

if (std::filesystem::exists(metaPath))
{
// json for metadata exists
std::ifstream metaIn(metaPath);
meta = web::json::value::parse(metaIn);
metaIn.close();
}
else
{
// json for metadata does NOT exist
// default path for cache
cachePath = tmpDir;
cachePath.append("UnPoseSymmetricCache.json");
std::string pStr = cachePath.string();
utility::string_t v(pStr.begin(), pStr.end());
meta[_XPLATSTR("path")] = web::json::value(v);
// write metadata to file
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();
}

////
// parse cache
////
cachePath = std::filesystem::path(meta[_XPLATSTR("path")].as_string());
if (std::filesystem::exists(cachePath))
{
// json for cache exists
std::ifstream cacheIn(cachePath);
cache = web::json::value::parse(cacheIn);
cacheIn.close();
}
else
{
// json for cache does NOT exist
// empty json
cache = web::json::value();

// write cache to file
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
}
}
} // namespace

extern "C" DLLEXPORT float version(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
return 1.0f;
}

extern "C" DLLEXPORT float initialize(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
std::filesystem::path cache = std::filesystem::temp_directory_path();
cache.append("UnPoseSymmetric.json");

if (std::filesystem::exists(cache))
const std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
std::filesystem::path metaPath = tmpDir;
metaPath.append("UnPoseSymmetricMeta.json");
if (std::filesystem::exists(metaPath))
{
std::filesystem::remove_all(metaPath);
}
std::filesystem::path cachePath = tmpDir;
cachePath.append("UnPoseSymmetricCache.json");
if (std::filesystem::exists(cachePath))
{
// erase older cache (for accidental collision in subtool name)
std::filesystem::remove_all(cache);
std::filesystem::remove_all(cachePath);
}

return 1.0f;
Expand All @@ -41,16 +112,9 @@ extern "C" DLLEXPORT float initialize(char *someText, double optValue, char *out
extern "C" DLLEXPORT float isCached(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
// find and parse tmp file
web::json::value json;
std::filesystem::path cache = std::filesystem::temp_directory_path();
cache.append("UnPoseSymmetric.json");

if (std::filesystem::exists(cache))
{
std::ifstream ifs(cache);
json = web::json::value::parse(ifs);
ifs.close();
}
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);

// parse parameters
#if defined(_WIN32) || defined(_WIN64)
Expand All @@ -66,7 +130,7 @@ extern "C" DLLEXPORT float isCached(char *someText, double optValue, char *outpu
key = utility::string_t(fileName.begin(), fileName.end());
}

if (json.has_field(key))
if (cache.has_field(key))
{
return 1.0f;
}
Expand All @@ -85,16 +149,9 @@ extern "C" DLLEXPORT float getCachedRotation(char *someText, double optValue, ch
} loader;

// find and parse tmp file
web::json::value json;
std::filesystem::path cache = std::filesystem::temp_directory_path();
cache.append("UnPoseSymmetric.json");

if (std::filesystem::exists(cache))
{
std::ifstream ifs(cache);
json = web::json::value::parse(ifs);
ifs.close();
}
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);

// parse parameters
#if defined(_WIN32) || defined(_WIN64)
Expand All @@ -114,15 +171,15 @@ extern "C" DLLEXPORT float getCachedRotation(char *someText, double optValue, ch
Eigen::Matrix<double, 1, 3> translateXYZ, invTranslateXYZ;
double extraRotX = 0;
double invExtraRotX = 0;
if (json.has_field(key))
if (cache.has_field(key))
{
// hit
eulerZYX << json[key][_XPLATSTR("rotZ")].as_double(), json[key][_XPLATSTR("rotY")].as_double(), json[key][_XPLATSTR("rotX")].as_double();
invEulerZYX << json[key][_XPLATSTR("invRotZ")].as_double(), json[key][_XPLATSTR("invRotY")].as_double(), json[key][_XPLATSTR("invRotX")].as_double();
translateXYZ << json[key][_XPLATSTR("offX")].as_double(), json[key][_XPLATSTR("offY")].as_double(), json[key][_XPLATSTR("offZ")].as_double();
invTranslateXYZ << json[key][_XPLATSTR("invOffX")].as_double(), json[key][_XPLATSTR("invOffY")].as_double(), json[key][_XPLATSTR("invOffZ")].as_double();
extraRotX = json[key][_XPLATSTR("extraRotX")].as_double();
invExtraRotX = json[key][_XPLATSTR("invExtraRotX")].as_double();
eulerZYX << cache[key][_XPLATSTR("rotZ")].as_double(), cache[key][_XPLATSTR("rotY")].as_double(), cache[key][_XPLATSTR("rotX")].as_double();
invEulerZYX << cache[key][_XPLATSTR("invRotZ")].as_double(), cache[key][_XPLATSTR("invRotY")].as_double(), cache[key][_XPLATSTR("invRotX")].as_double();
translateXYZ << cache[key][_XPLATSTR("offX")].as_double(), cache[key][_XPLATSTR("offY")].as_double(), cache[key][_XPLATSTR("offZ")].as_double();
invTranslateXYZ << cache[key][_XPLATSTR("invOffX")].as_double(), cache[key][_XPLATSTR("invOffY")].as_double(), cache[key][_XPLATSTR("invOffZ")].as_double();
extraRotX = cache[key][_XPLATSTR("extraRotX")].as_double();
invExtraRotX = cache[key][_XPLATSTR("invExtraRotX")].as_double();
}
else
{
Expand Down Expand Up @@ -205,10 +262,137 @@ extern "C" DLLEXPORT float getCachedRotation(char *someText, double optValue, ch
update[_XPLATSTR("invOffZ")] = web::json::value(-translateXYZ(2));

// write json to cache file.
json[key] = update;
std::ofstream ofs(cache);
json.serialize(ofs);
ofs.close();
cache[key] = update;
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();

return 1.0f;
}

extern "C" DLLEXPORT float eraseCachedRotation(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
// find and parse tmp file
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);

// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path inputGoZFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path inputGoZFileName(tmpStr.substr(2));
#endif
utility::string_t key;
{
std::string fileName = inputGoZFileName.filename().string();
key = utility::string_t(fileName.begin(), fileName.end());
}

if (cache.has_field(key))
{
cache.erase(key);
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
return 1.0f;
}
else
{
return 0.0f;
}
}

extern "C" DLLEXPORT float loadCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
std::filesystem::path metaPath, cachePath, cachePath_;
web::json::value meta, meta_, cache, cache_;
readCache(metaPath, meta, cachePath, cache);
meta_ = meta;

// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path cacheFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path cacheFileName(tmpStr.substr(2));
#endif
std::string pStr = cacheFileName.string();
utility::string_t v(pStr.begin(), pStr.end());
meta_[_XPLATSTR("path")] = web::json::value(v);

// temporary update path for cache
{
std::ofstream metaOut(metaPath);
meta_.serialize(metaOut);
metaOut.close();
}

readCache(metaPath, meta_, cachePath_, cache_);

// write current cache to new file
{
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();
}

{
std::ofstream cacheOut(cachePath);
cache_.serialize(cacheOut);
cacheOut.close();
}
return 1.0f;
}

extern "C" DLLEXPORT float selectCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData)
{
std::filesystem::path metaPath, cachePath;
web::json::value meta, cache;
readCache(metaPath, meta, cachePath, cache);

// std::filesystem::remove_all(cachePath);

if (optValue > 0)
{
// switch to default
// erase current json
std::filesystem::remove_all(metaPath);
web::json::value cache_;
// readCache automatically create cache file in default path
readCache(metaPath, meta, cachePath, cache_);
// write current cache to new file
std::ofstream cacheOut(cachePath);
cache.serialize(cacheOut);
cacheOut.close();
}
else
{
// parse parameters
#if defined(_WIN32) || defined(_WIN64)
std::filesystem::path cacheFileName(someText);
#else
// for macOS, zsc adds meaningless prefix
std::string tmpStr(someText);
std::filesystem::path cacheFileName(tmpStr.substr(2));
#endif

std::string pStr = cacheFileName.string();
utility::string_t v(pStr.begin(), pStr.end());
meta[_XPLATSTR("path")] = web::json::value(v);
// write current meta to new file
std::ofstream metaOut(metaPath);
meta.serialize(metaOut);
metaOut.close();

// write current cache to new file
std::ofstream cacheOut(cacheFileName);
cache.serialize(cacheOut);
cacheOut.close();
}

return 1.0f;
}
6 changes: 6 additions & 0 deletions UnPoseSymmetricMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ extern "C" DLLEXPORT float initialize(char *someText, double optValue, char *out
extern "C" DLLEXPORT float isCached(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData);

extern "C" DLLEXPORT float getCachedRotation(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData);

extern "C" DLLEXPORT float eraseCachedRotation(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData);

extern "C" DLLEXPORT float loadCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData);

extern "C" DLLEXPORT float selectCacheFile(char *someText, double optValue, char *outputBuffer, int optBuffer1Size, char *pOptBuffer2, int optBuffer2Size, char **zData);
Loading

0 comments on commit 6327cbf

Please sign in to comment.