diff options
author | Clifford Wolf <clifford@clifford.at> | 2014-10-12 12:11:57 +0200 |
---|---|---|
committer | Clifford Wolf <clifford@clifford.at> | 2014-10-12 12:11:57 +0200 |
commit | 0b9282a779867459fe5babfff300795c343c46ea (patch) | |
tree | 58cd6e254eeeaf2bd812306835dce98baea2a66e /kernel | |
parent | 9b4d171e37aa23adbdbad1ca1e983e4bf35604f9 (diff) | |
download | yosys-0b9282a779867459fe5babfff300795c343c46ea.tar.gz yosys-0b9282a779867459fe5babfff300795c343c46ea.tar.bz2 yosys-0b9282a779867459fe5babfff300795c343c46ea.zip |
Added make_temp_{file,dir}() and remove_directory() APIs
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/yosys.cc | 90 | ||||
-rw-r--r-- | kernel/yosys.h | 3 |
2 files changed, 93 insertions, 0 deletions
diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 50da13ae7..4af3ff9c0 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -34,6 +34,7 @@ #include <unistd.h> #include <limits.h> +#include <dirent.h> #include <errno.h> YOSYS_NAMESPACE_BEGIN @@ -204,7 +205,96 @@ int run_command(const std::string &command, std::function<void(const std::string int ret = pclose(f); if (ret < 0) return -1; +#ifdef _WIN32 + return ret; +#else return WEXITSTATUS(ret); +#endif +} + +std::string make_temp_file(std::string template_str) +{ +#ifdef _WIN32 + if (template_str.rfind("/tmp/", 0) == 0) { + char path[MAX_PATH+1]; + GetTempPath(MAX_PATH+1, path); + template_str = stringf("%s\\%s", path, template_str.c_str() + 5); + } + + size_t pos = template_str.rfind("XXXXXX"); + log_assert(pos != std::string::npos); + + while (1) { + for (int i = 0; i < 6; i++) { + static std::string y = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + static uint32_t x = 314159265 ^ time(NULL); + x ^= x << 13, x ^= x >> 17, x ^= x << 5; + template_str[pos+i] = y[x % y.size()]; + } + if (access(template_str.c_str(), F_OK) != 0) + break; + } +#else + size_t pos = template_str.rfind("XXXXXX"); + log_assert(pos != std::string::npos); + + int suffixlen = GetSize(template_str) - pos - 6; + + char *p = strdup(template_str.c_str()); + close(mkstemps(p, suffixlen)); + template_str = p; + free(p); +#endif + + return template_str; +} + +std::string make_temp_dir(std::string template_str) +{ +#ifdef _WIN32 + template_str = make_temp_file(template_str); + mkdir(template_str.c_str()); + return template_str; +#else + size_t pos = template_str.rfind("XXXXXX"); + log_assert(pos != std::string::npos); + + int suffixlen = GetSize(template_str) - pos - 6; + log_assert(suffixlen == 0); + + char *p = strdup(template_str.c_str()); + mkdtemp(p, suffixlen); + template_str = p; + free(p); + + return template_str; +#endif +} + +void remove_directory(std::string dirname) +{ +#ifdef _WIN32 + run_command(stringf("rmdir /s /q \"%s\"", dirname.c_str())); +#else + struct stat stbuf; + struct dirent **namelist; + int n = scandir(dirname.c_str(), &namelist, nullptr, alphasort); + log_assert(n >= 0); + for (int i = 0; i < n; i++) { + if (strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) { + buffer = stringf("%s/%s", dirname.c_str(), namelist[i]->d_name); + if (!stat(buffer.c_str(), &stbuf) && S_ISREG(stbuf.st_mode)) { + log("Removing `%s'.\n", buffer.c_str()); + remove(buffer.c_str()); + } else + remove_directory(buffer); + } + free(namelist[i]); + } + free(namelist); + log("Removing `%s'.\n", dirname.c_str()); + rmdir(dirname.c_str()); +#endif } int GetSize(RTLIL::Wire *wire) diff --git a/kernel/yosys.h b/kernel/yosys.h index fcf60f9f1..e579e90d9 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -88,6 +88,9 @@ std::string next_token(std::string &text, const char *sep); bool patmatch(const char *pattern, const char *string); int readsome(std::istream &f, char *s, int n); int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>()); +std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX"); +std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX"); +void remove_directory(std::string dirname); template<typename T> int GetSize(const T &obj) { return obj.size(); } int GetSize(RTLIL::Wire *wire); |