move file scope static variables into functions to deal with static initialization order problem

This commit is contained in:
Jiangang (Jeff) Zhuang
2016-12-02 19:11:17 -05:00
committed by Howard Hinnant
parent 8a3aeb566b
commit 64ea0a5bc3

68
tz.cpp
View File

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