8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 14:03:07 +01:00
firebird-mirror/extern/icu/tzdata/update-ids.cpp
Adriano dos Santos Fernandes c434814569 Improvement CORE-6308 - Make it possible to update list of time zones
(names and ids) without source code recompilation.

Update be.zip and le.zip adding ids.dat.
2020-05-17 22:39:02 -03:00

174 lines
4.3 KiB
C++

/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Adriano dos Santos Fernandes
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2020 Adriano dos Santos Fernandes <adrianosf@gmail.com>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#include <exception>
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <utility>
#include <cassert>
#include <string.h>
#include <unicode/ucal.h>
#include "../../../src/common/TimeZones.h"
using std::exception;
using std::cerr;
using std::cout;
using std::endl;
using std::ofstream;
using std::string;
using std::unordered_set;
using std::vector;
using std::min_element;
using std::pair;
void writeString(ofstream& stream, const char* str, bool includeNullByte)
{
stream.write(str, strlen(str) + (includeNullByte ? 1 : 0));
}
int run(int argc, const char* argv[])
{
if (argc != 3)
{
cerr << "Syntax: " << argv[0] << " <ids.dat file> <TimeZones.h file>" << endl;
return 1;
}
const unsigned MAX_ID = 65535;
unordered_set<string> zonesSet;
vector<string> zonesVector;
for (unsigned i = 0; i < sizeof(BUILTIN_TIME_ZONE_LIST) / sizeof(BUILTIN_TIME_ZONE_LIST[0]); ++i)
{
zonesSet.insert(BUILTIN_TIME_ZONE_LIST[i]);
zonesVector.push_back(BUILTIN_TIME_ZONE_LIST[i]);
}
UErrorCode icuErrorCode = U_ZERO_ERROR;
const char* databaseVersion = ucal_getTZDataVersion(&icuErrorCode);
cout << "Database version: " << databaseVersion << endl;
UEnumeration* uenum = ucal_openTimeZones(&icuErrorCode);
assert(uenum);
int32_t length;
char buffer[512];
while (const UChar* str = uenum_unext(uenum, &length, &icuErrorCode))
{
for (unsigned i = 0; i <= length; ++i)
buffer[i] = (char) str[i];
if (zonesSet.find(buffer) == zonesSet.end())
{
cout << "New time zone included: " << buffer << "(" << (MAX_ID - zonesVector.size()) << ")." << endl;
zonesVector.push_back(buffer);
}
}
uenum_close(uenum);
ofstream datStream, headerStream;
datStream.open(argv[1], std::fstream::out | std::fstream::trunc | std::fstream::binary);
headerStream.open(argv[2], std::fstream::out | std::fstream::trunc);
uint8_t byte;
// file signature
writeString(datStream, "FBTZ", true);
// file format version
const unsigned fileVersion = 1;
byte = fileVersion & 0xFF;
datStream.write((char*) &byte, 1);
byte = (fileVersion >> 8) & 0xFF;
datStream.write((char*) &byte, 1);
// time zone database version
writeString(datStream, databaseVersion, true);
// count
byte = zonesVector.size() & 0xFF;
datStream.write((char*) &byte, 1);
byte = (zonesVector.size() >> 8) & 0xFF;
datStream.write((char*) &byte, 1);
unsigned index = MAX_ID;
for (const auto zone : zonesVector)
{
// null terminated name
datStream.write(zone.c_str(), zone.length() + 1);
--index;
}
datStream.close();
writeString(headerStream,
"// The content of this file is generated with help of update-ids utility Do not edit.\n\n",
false);
sprintf(buffer, "static const char* BUILTIN_TIME_ZONE_VERSION = \"%s\";\n\n", databaseVersion);
writeString(headerStream, buffer, false);
writeString(headerStream,
"// Do not change order of items in this array! The index corresponds to a TimeZone ID, which must be fixed!\n",
false);
writeString(headerStream, "static const char* BUILTIN_TIME_ZONE_LIST[] = {\n", false);
index = MAX_ID;
for (const auto zone : zonesVector)
{
sprintf(buffer, "\t\"%s\"%s\t// %u\n", zone.c_str(), (zone == zonesVector.back() ? "" : ","), index);
writeString(headerStream, buffer, false);
--index;
}
writeString(headerStream, "};\n", false);
headerStream.close();
return 0;
}
int main(int argc, const char* argv[])
{
try
{
return run(argc, argv);
}
catch(const exception& e)
{
std::cerr << e.what() << '\n';
}
}