Debian Codegraph @

extern/icu/tzdata/update-ids.cpp

raw
1 /*
2 * The contents of this file are subject to the Initial
3 * Developer's Public License Version 1.0 (the "License");
4 * you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
6 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
7 *
8 * Software distributed under the License is distributed AS IS,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
10 * See the License for the specific language governing rights
11 * and limitations under the License.
12 *
13 * The Original Code was created by Adriano dos Santos Fernandes
14 * for the Firebird Open Source RDBMS project.
15 *
16 * Copyright (c) 2020 Adriano dos Santos Fernandes <[email protected]>
17 * and all contributors signed below.
18 *
19 * All Rights Reserved.
20 * Contributor(s): ______________________________________.
21 */
22
23 #include <algorithm>
24 #include <cctype>
25 #include <exception>
26 #include <iostream>
27 #include <fstream>
28 #include <regex>
29 #include <string>
30 #include <unordered_set>
31 #include <vector>
32 #include <utility>
33 #include <cassert>
34 #include <string.h>
35 #include <unicode/ucal.h>
36 #include "../../../src/common/TimeZones.h"
37
38 using std::exception;
39 using std::cerr;
40 using std::cout;
41 using std::endl;
42 using std::ofstream;
43 using std::string;
44 using std::unordered_set;
45 using std::vector;
46 using std::min_element;
47 using std::pair;
48
49
50 void writeString(ofstream& stream, const char* str, bool includeNullByte)
51 {
52 stream.write(str, strlen(str) + (includeNullByte ? 1 : 0));
53 }
54
55
56 int run(int argc, const char* argv[])
57 {
58 if (argc != 4)
59 {
60 cerr << "Syntax: " << argv[0] << " <ids.dat file> <TimeZones.h file> <TimeZoneIds.h file>" << endl;
61 return 1;
62 }
63
64 const unsigned MAX_ID = 65535;
65 unordered_set<string> zonesSet;
66 vector<string> zonesVector;
67
68 for (unsigned i = 0; i < sizeof(BUILTIN_TIME_ZONE_LIST) / sizeof(BUILTIN_TIME_ZONE_LIST[0]); ++i)
69 {
70 zonesSet.insert(BUILTIN_TIME_ZONE_LIST[i]);
71 zonesVector.push_back(BUILTIN_TIME_ZONE_LIST[i]);
72 }
73
74 UErrorCode icuErrorCode = U_ZERO_ERROR;
75
76 const char* databaseVersion = ucal_getTZDataVersion(&icuErrorCode);
77 cout << "Database version: " << databaseVersion << endl;
78
79 UEnumeration* uenum = ucal_openTimeZones(&icuErrorCode);
80 assert(uenum);
81
82 int32_t length;
83 char buffer[512];
84
85 while (const UChar* str = uenum_unext(uenum, &length, &icuErrorCode))
86 {
87 for (unsigned i = 0; i <= length; ++i)
88 buffer[i] = (char) str[i];
89
90 if (zonesSet.find(buffer) == zonesSet.end())
91 {
92 cout << "New time zone included: " << buffer << "(" << (MAX_ID - zonesVector.size()) << ")." << endl;
93 zonesVector.push_back(buffer);
94 }
95 }
96
97 uenum_close(uenum);
98
99 ofstream datStream, headerStream, idsHeaderStream;
100
101 datStream.open(argv[1], std::fstream::out | std::fstream::trunc | std::fstream::binary);
102 headerStream.open(argv[2], std::fstream::out | std::fstream::trunc);
103 idsHeaderStream.open(argv[3], std::fstream::out | std::fstream::trunc);
104
105 uint8_t byte;
106
107 // file signature
108 writeString(datStream, "FBTZ", true);
109
110 // file format version
111 const unsigned fileVersion = 1;
112 byte = fileVersion & 0xFF;
113 datStream.write((char*) &byte, 1);
114 byte = (fileVersion >> 8) & 0xFF;
115 datStream.write((char*) &byte, 1);
116
117 // time zone database version
118 writeString(datStream, databaseVersion, true);
119
120 // count
121 byte = zonesVector.size() & 0xFF;
122 datStream.write((char*) &byte, 1);
123 byte = (zonesVector.size() >> 8) & 0xFF;
124 datStream.write((char*) &byte, 1);
125
126 unsigned index = MAX_ID;
127
128 for (const auto zone : zonesVector)
129 {
130 // null terminated name
131 datStream.write(zone.c_str(), zone.length() + 1);
132 --index;
133 }
134
135 datStream.close();
136
137 writeString(headerStream,
138 "// The content of this file is generated with help of update-ids utility Do not edit.\n\n",
139 false);
140
141 writeString(idsHeaderStream,
142 "#ifndef FIREBIRD_TIME_ZONES_H\n"
143 "#define FIREBIRD_TIME_ZONES_H\n\n",
144 false);
145
146 writeString(idsHeaderStream,
147 "// The content of this file is generated with help of update-ids utility Do not edit.\n\n",
148 false);
149
150 sprintf(buffer, "static const char* BUILTIN_TIME_ZONE_VERSION = \"%s\";\n\n", databaseVersion);
151 writeString(headerStream, buffer, false);
152
153 writeString(headerStream,
154 "// Do not change order of items in this array! The index corresponds to a TimeZone ID, which must be fixed!\n",
155 false);
156 writeString(headerStream, "static const char* BUILTIN_TIME_ZONE_LIST[] = {\n", false);
157
158 index = MAX_ID;
159
160 const std::regex plusNumberRegex("\\+([[:digit:]])");
161 const std::regex minusNumberRegex("\\-([[:digit:]])");
162
163 for (const auto zone : zonesVector)
164 {
165 auto zoneDef = zone;
166
167 zoneDef = std::regex_replace(zoneDef, plusNumberRegex, "_plus_$1");
168 zoneDef = std::regex_replace(zoneDef, minusNumberRegex, "_minus_$1");
169 std::replace(zoneDef.begin(), zoneDef.end(), '/', '_');
170 std::replace(zoneDef.begin(), zoneDef.end(), '-', '_');
171
172 std::transform(zoneDef.begin(), zoneDef.end(), zoneDef.begin(),
173 [](unsigned char c) { return std::tolower(c); });
174
175 zoneDef = "fb_tzid_" + zoneDef;
176
177 sprintf(buffer, "\t\"%s\"%s\t// %s - %u\n",
178 zone.c_str(), (zone == zonesVector.back() ? "" : ","), zoneDef.c_str(), index);
179 writeString(headerStream, buffer, false);
180
181 sprintf(buffer, "#define %-45s%u /* %s */\n", zoneDef.c_str(), index, zone.c_str());
182 writeString(idsHeaderStream, buffer, false);
183
184 --index;
185 }
186
187 writeString(headerStream, "};\n", false);
188
189 writeString(idsHeaderStream,
190 "\n#endif /* FIREBIRD_TIME_ZONES_H */\n",
191 false);
192
193 headerStream.close();
194 idsHeaderStream.close();
195
196 return 0;
197 }
198
199
200 int main(int argc, const char* argv[])
201 {
202 try
203 {
204 return run(argc, argv);
205 }
206 catch (const exception& e)
207 {
208 cerr << e.what() << '\n';
209 }
210 }