GeneratePhoneNumberHeader.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh
  2. # GeneratePhoneNumberHeader.sh
  3. # libPhoneNumber
  4. #
  5. # Created by Dave MacLachlan on 2/7/17.
  6. # Copyright © 2017 Google Inc. All rights reserved.
  7. # Takes the data sets in the PhoneNumberMetaData testing files and compresses them and then
  8. # writes them into a header that we can pull into our source. The compression reduces them from 300k
  9. # of data to 44k (per architecture). It would possibly be better to have this as a resource file
  10. # that is read in from disk (because then we only pay for the size once), but that would
  11. # dramatically change how this library is currently used by clients.
  12. #
  13. # The data set used is controlled by the value of the "TESTING" macro when the code is actually
  14. # compiled.
  15. set -eu
  16. cd "${BASH_SOURCE%/*}" || exit
  17. TEMPDIR=$(mktemp -d)
  18. gzip -c "../libPhoneNumberTests/generatedJSON/PhoneNumberMetaDataForTesting.json" > "$TEMPDIR/PhoneNumberMetaDataForTesting.zip"
  19. gzip -c "../libPhoneNumberTests/generatedJSON/PhoneNumberMetaData.json" > "$TEMPDIR/PhoneNumberMetaData.zip"
  20. gzip -c "../libPhoneNumberTests/generatedJSON/ShortNumberMetaData.json" > "$TEMPDIR/ShortNumberMetaData.zip"
  21. cat > "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  22. /*****
  23. * Data Generated from GeneratePhoneNumberHeader.sh
  24. * Off of PhoneNumberMetaDataForTesting.json, PhoneNumberMetaData.json, and ShortNumberMetaData.json
  25. */
  26. #include <zlib.h>
  27. // z_const is not defined in some versions of zlib, so define it here
  28. // in case it has not been defined.
  29. #if defined(ZLIB_CONST) && !defined(z_const)
  30. # define z_const const
  31. #else
  32. # define z_const
  33. #endif
  34. #if TESTING==1
  35. z_const Bytef kPhoneNumberMetaData[] = {
  36. EOF
  37. cat "$TEMPDIR/PhoneNumberMetaDataForTesting.zip" | xxd -i >> "NBGeneratedPhoneNumberMetaData.h"
  38. cat >> "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  39. };
  40. z_const size_t kPhoneNumberMetaDataCompressedLength = sizeof(kPhoneNumberMetaData);
  41. EOF
  42. LIB_SIZE=$(stat -f%z "../libPhoneNumberTests/generatedJSON/PhoneNumberMetaDataForTesting.json")
  43. echo "z_const size_t kPhoneNumberMetaDataExpandedLength = $LIB_SIZE;" >> "NBGeneratedPhoneNumberMetaData.h"
  44. cat >> "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  45. #else // TESTING == 1
  46. z_const Bytef kPhoneNumberMetaData[] = {
  47. EOF
  48. cat "$TEMPDIR/PhoneNumberMetaData.zip" | xxd -i >> "NBGeneratedPhoneNumberMetaData.h"
  49. cat >> "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  50. };
  51. z_const size_t kPhoneNumberMetaDataCompressedLength = sizeof(kPhoneNumberMetaData);
  52. EOF
  53. LIB_SIZE=$(stat -f%z "../libPhoneNumberTests/generatedJSON/PhoneNumberMetaData.json")
  54. echo "z_const size_t kPhoneNumberMetaDataExpandedLength = $LIB_SIZE;" >> "NBGeneratedPhoneNumberMetaData.h"
  55. echo "#endif // TESTING" >> "NBGeneratedPhoneNumberMetaData.h"
  56. # ShortNumberMetadata
  57. cat >> "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  58. #if SHORT_NUMBER_SUPPORT
  59. z_const Bytef kShortNumberMetaData[] = {
  60. EOF
  61. cat "$TEMPDIR/ShortNumberMetaData.zip" | xxd -i >> "NBGeneratedPhoneNumberMetaData.h"
  62. cat >> "NBGeneratedPhoneNumberMetaData.h" <<'EOF'
  63. };
  64. z_const size_t kShortNumberMetaDataCompressedLength = sizeof(kShortNumberMetaData);
  65. EOF
  66. LIB_SIZE=$(stat -f%z "../libPhoneNumberTests/generatedJSON/ShortNumberMetaData.json")
  67. echo "z_const size_t kShortNumberMetaDataExpandedLength = $LIB_SIZE;" >> "NBGeneratedPhoneNumberMetaData.h"
  68. echo "#endif // SHORT_NUMBER_SUPPORT" >> "NBGeneratedPhoneNumberMetaData.h"
  69. rm "$TEMPDIR/PhoneNumberMetaDataForTesting.zip"
  70. rm "$TEMPDIR/PhoneNumberMetaData.zip"
  71. rm "$TEMPDIR/ShortNumberMetaData.zip"
  72. rmdir "$TEMPDIR"