update-ids.cpp 4.3 KB

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