diff --git a/tz.cpp b/tz.cpp index e3c4da7..d465481 100644 --- a/tz.cpp +++ b/tz.cpp @@ -3,6 +3,7 @@ // Copyright (c) 2015, 2016 Howard Hinnant // Copyright (c) 2015 Ville Voutilainen // Copyright (c) 2016 Alexander Kormanovsky +// Copyright (c) 2016 Jiangang Zhuang // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -126,8 +127,17 @@ #endif #ifdef _WIN32 + static CONSTDATA char folder_delimiter = '\\'; +#else + +static CONSTDATA char folder_delimiter = '/'; + +#endif + +#ifdef _WIN32 + namespace { struct task_mem_deleter @@ -176,7 +186,6 @@ get_download_folder() #else // !_WIN32 -static CONSTDATA char folder_delimiter = '/'; static std::string @@ -204,46 +213,39 @@ namespace date using namespace detail; -static std::string get_install() +static +const std::string& +get_install() { -#ifdef _WIN32 - std::string install = get_download_folder(); - install += folder_delimiter; - install += "tzdata"; -#else - std::string install = expand_path("~/Downloads/tzdata"); -#endif - return install; -} - + static const std::string install #ifndef INSTALL -static const std::string install = get_install(); +# ifdef _WIN32 + = get_download_folder() + folder_delimiter + "tzdata"; +# else + = expand_path("~/Downloads/tzdata"); +# endif #else // INSTALL -#define STRINGIZEIMP(x) #x -#define STRINGIZE(x) STRINGIZEIMP(x) +# define STRINGIZEIMP(x) #x +# define STRINGIZE(x) STRINGIZEIMP(x) -static const std::string install = STRINGIZE(INSTALL) + - std::string(1, folder_delimiter) + "tzdata"; + = STRINGIZE(INSTALL) + std::string(1, folder_delimiter) + "tzdata"; #endif // INSTALL + return install; +} + static std::string get_download_gz_file(const std::string& version) { - auto file = install + version + ".tar.gz"; + auto file = get_install() + version + ".tar.gz"; return file; } -static const std::vector files = -{ - "africa", "antarctica", "asia", "australasia", "backward", "etcetera", "europe", - "pacificnew", "northamerica", "southamerica", "systemv", "leapseconds" -}; - // These can be used to reduce the range of the database to save memory CONSTDATA auto min_year = date::year::min(); CONSTDATA auto max_year = date::year::max(); @@ -362,7 +364,7 @@ static std::string get_download_mapping_file(const std::string& version) { - auto file = install + version + "windowsZones.xml"; + auto file = get_install() + version + "windowsZones.xml"; return file; } @@ -2549,7 +2551,7 @@ static std::string get_download_tar_file(const std::string& version) { - auto file = install; + auto file = get_install(); file += folder_delimiter; file += "tzdata"; file += version; @@ -2601,7 +2603,7 @@ extract_gz_file(const std::string& version, const std::string& gz_file, cmd += "\" x \""; cmd += tar_file; cmd += "\" -o\""; - cmd += install; + cmd += get_install(); cmd += '\"'; #if USE_SHELL_API cmd = "\"" + cmd + "\""; @@ -2671,12 +2673,12 @@ bool extract_gz_file(const std::string&, const std::string& gz_file, const std::string&) { #if USE_SHELL_API - bool unzipped = std::system(("tar -xzf " + gz_file + " -C " + install).c_str()) == EXIT_SUCCESS; + bool unzipped = std::system(("tar -xzf " + gz_file + " -C " + get_install()).c_str()) == EXIT_SUCCESS; #else // !USE_SHELL_API const char prog[] = {"/usr/bin/tar"}; const char*const args[] = { - prog, "-xzf", gz_file.c_str(), "-C", install.c_str(), nullptr + prog, "-xzf", gz_file.c_str(), "-C", get_install().c_str(), nullptr }; bool unzipped = (run_program(prog, args) == EXIT_SUCCESS); #endif // !USE_SHELL_API @@ -2696,6 +2698,7 @@ remote_install(const std::string& version) auto success = false; assert(!version.empty()); + std::string install = get_install(); auto gz_file = get_download_gz_file(version); if (file_exists(gz_file)) { @@ -2742,6 +2745,7 @@ TZ_DB init_tzdb() { using namespace date; + const std::string install = get_install(); const std::string path = install + folder_delimiter; std::string line; bool continue_zone = false; @@ -2796,6 +2800,12 @@ init_tzdb() db.version = get_version(path); #endif // !AUTO_DOWNLOAD + CONSTDATA const char* files[] = + { + "africa", "antarctica", "asia", "australasia", "backward", "etcetera", "europe", + "pacificnew", "northamerica", "southamerica", "systemv", "leapseconds" + }; + for (const auto& filename : files) { std::ifstream infile(path + filename);