update-ids.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <algorithm>
  23. #include <cctype>
  24. #include <exception>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <regex>
  28. #include <string>
  29. #include <unordered_set>
  30. #include <vector>
  31. #include <utility>
  32. #include <cassert>
  33. #include <string.h>
  34. #include <unicode/ucal.h>
  35. #include "../../../src/common/TimeZones.h"
  36. using std::exception;
  37. using std::cerr;
  38. using std::cout;
  39. using std::endl;
  40. using std::ofstream;
  41. using std::string;
  42. using std::unordered_set;
  43. using std::vector;
  44. using std::min_element;
  45. using std::pair;
  46. void writeString(ofstream& stream, const char* str, bool includeNullByte)
  47. {
  48. stream.write(str, strlen(str) + (includeNullByte ? 1 : 0));
  49. }
  50. int run(int argc, const char* argv[])
  51. {
  52. if (argc != 4)
  53. {
  54. cerr << "Syntax: " << argv[0] << " <ids.dat file> <TimeZones.h file> <TimeZoneIds.h file>" << endl;
  55. return 1;
  56. }
  57. const unsigned MAX_ID = 65535;
  58. unordered_set<string> zonesSet;
  59. vector<string> zonesVector;
  60. for (unsigned i = 0; i < sizeof(BUILTIN_TIME_ZONE_LIST) / sizeof(BUILTIN_TIME_ZONE_LIST[0]); ++i)
  61. {
  62. zonesSet.insert(BUILTIN_TIME_ZONE_LIST[i]);
  63. zonesVector.push_back(BUILTIN_TIME_ZONE_LIST[i]);
  64. }
  65. UErrorCode icuErrorCode = U_ZERO_ERROR;
  66. const char* databaseVersion = ucal_getTZDataVersion(&icuErrorCode);
  67. cout << "Database version: " << databaseVersion << endl;
  68. UEnumeration* uenum = ucal_openTimeZones(&icuErrorCode);
  69. assert(uenum);
  70. int32_t length;
  71. char buffer[512];
  72. while (const UChar* str = uenum_unext(uenum, &length, &icuErrorCode))
  73. {
  74. for (unsigned i = 0; i <= length; ++i)
  75. buffer[i] = (char) str[i];
  76. if (zonesSet.find(buffer) == zonesSet.end())
  77. {
  78. cout << "New time zone included: " << buffer << "(" << (MAX_ID - zonesVector.size()) << ")." << endl;
  79. zonesVector.push_back(buffer);
  80. }
  81. }
  82. uenum_close(uenum);
  83. ofstream datStream, headerStream, idsHeaderStream;
  84. datStream.open(argv[1], std::fstream::out | std::fstream::trunc | std::fstream::binary);
  85. headerStream.open(argv[2], std::fstream::out | std::fstream::trunc);
  86. idsHeaderStream.open(argv[3], std::fstream::out | std::fstream::trunc);
  87. uint8_t byte;
  88. // file signature
  89. writeString(datStream, "FBTZ", true);
  90. // file format version
  91. const unsigned fileVersion = 1;
  92. byte = fileVersion & 0xFF;
  93. datStream.write((char*) &byte, 1);
  94. byte = (fileVersion >> 8) & 0xFF;
  95. datStream.write((char*) &byte, 1);
  96. // time zone database version
  97. writeString(datStream, databaseVersion, true);
  98. // count
  99. byte = zonesVector.size() & 0xFF;
  100. datStream.write((char*) &byte, 1);
  101. byte = (zonesVector.size() >> 8) & 0xFF;
  102. datStream.write((char*) &byte, 1);
  103. unsigned index = MAX_ID;
  104. for (const auto zone : zonesVector)
  105. {
  106. // null terminated name
  107. datStream.write(zone.c_str(), zone.length() + 1);
  108. --index;
  109. }
  110. datStream.close();
  111. writeString(headerStream,
  112. "// The content of this file is generated with help of update-ids utility Do not edit.\n\n",
  113. false);
  114. writeString(idsHeaderStream,
  115. "#ifndef FIREBIRD_TIME_ZONES_H\n"
  116. "#define FIREBIRD_TIME_ZONES_H\n\n",
  117. false);
  118. writeString(idsHeaderStream,
  119. "// The content of this file is generated with help of update-ids utility Do not edit.\n\n",
  120. false);
  121. sprintf(buffer, "static const char* BUILTIN_TIME_ZONE_VERSION = \"%s\";\n\n", databaseVersion);
  122. writeString(headerStream, buffer, false);
  123. writeString(headerStream,
  124. "// Do not change order of items in this array! The index corresponds to a TimeZone ID, which must be fixed!\n",
  125. false);
  126. writeString(headerStream, "static const char* BUILTIN_TIME_ZONE_LIST[] = {\n", false);
  127. index = MAX_ID;
  128. const std::regex plusNumberRegex("\\+([[:digit:]])");
  129. const std::regex minusNumberRegex("\\-([[:digit:]])");
  130. for (const auto zone : zonesVector)
  131. {
  132. auto zoneDef = zone;
  133. zoneDef = std::regex_replace(zoneDef, plusNumberRegex, "_plus_$1");
  134. zoneDef = std::regex_replace(zoneDef, minusNumberRegex, "_minus_$1");
  135. std::replace(zoneDef.begin(), zoneDef.end(), '/', '_');
  136. std::replace(zoneDef.begin(), zoneDef.end(), '-', '_');
  137. std::transform(zoneDef.begin(), zoneDef.end(), zoneDef.begin(),
  138. [](unsigned char c) { return std::tolower(c); });
  139. zoneDef = "fb_tzid_" + zoneDef;
  140. sprintf(buffer, "\t\"%s\"%s\t// %s - %u\n",
  141. zone.c_str(), (zone == zonesVector.back() ? "" : ","), zoneDef.c_str(), index);
  142. writeString(headerStream, buffer, false);
  143. sprintf(buffer, "#define %-45s%u /* %s */\n", zoneDef.c_str(), index, zone.c_str());
  144. writeString(idsHeaderStream, buffer, false);
  145. --index;
  146. }
  147. writeString(headerStream, "};\n", false);
  148. writeString(idsHeaderStream,
  149. "\n#endif /* FIREBIRD_TIME_ZONES_H */\n",
  150. false);
  151. headerStream.close();
  152. idsHeaderStream.close();
  153. return 0;
  154. }
  155. int main(int argc, const char* argv[])
  156. {
  157. try
  158. {
  159. return run(argc, argv);
  160. }
  161. catch (const exception& e)
  162. {
  163. cerr << e.what() << '\n';
  164. }
  165. }