flathash.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2015 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include <sstream>
  18. #include <string>
  19. #include "flatbuffers/hash.h"
  20. #include <stdio.h>
  21. enum OutputFormat {
  22. kDecimal,
  23. kHexadecimal,
  24. kHexadecimal0x
  25. };
  26. int main(int argc, char* argv[]) {
  27. const char* name = argv[0];
  28. if (argc <= 1) {
  29. printf("%s HASH [OPTION]... STRING... [-- STRING...]\n", name);
  30. printf("Available hashing algorithms:\n 32 bit:\n");
  31. size_t size = sizeof(flatbuffers::kHashFunctions32) /
  32. sizeof(flatbuffers::kHashFunctions32[0]);
  33. for (size_t i = 0; i < size; ++i) {
  34. printf(" * %s\n", flatbuffers::kHashFunctions32[i].name);
  35. }
  36. printf(" 64 bit:\n");
  37. size = sizeof(flatbuffers::kHashFunctions64) /
  38. sizeof(flatbuffers::kHashFunctions64[0]);
  39. for (size_t i = 0; i < size; ++i) {
  40. printf(" * %s\n", flatbuffers::kHashFunctions64[i].name);
  41. }
  42. printf(
  43. " -d Output hash in decimal.\n"
  44. " -x Output hash in hexadecimal.\n"
  45. " -0x Output hash in hexadecimal and prefix with 0x.\n"
  46. " -c Append the string to the output in a c-style comment.\n");
  47. return 0;
  48. }
  49. const char* hash_algorithm = argv[1];
  50. flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
  51. flatbuffers::FindHashFunction32(hash_algorithm);
  52. flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 =
  53. flatbuffers::FindHashFunction64(hash_algorithm);
  54. if (!hash_function32 && !hash_function64) {
  55. printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
  56. return 0;
  57. }
  58. OutputFormat output_format = kHexadecimal;
  59. bool annotate = false;
  60. bool escape_dash = false;
  61. for (int i = 2; i < argc; i++) {
  62. const char* arg = argv[i];
  63. if (!escape_dash && arg[0] == '-') {
  64. std::string opt = arg;
  65. if (opt == "-d") output_format = kDecimal;
  66. else if (opt == "-x") output_format = kHexadecimal;
  67. else if (opt == "-0x") output_format = kHexadecimal0x;
  68. else if (opt == "-c") annotate = true;
  69. else if (opt == "--") escape_dash = true;
  70. else printf("Unrecognized argument: \"%s\"\n", arg);
  71. } else {
  72. std::stringstream ss;
  73. if (output_format == kDecimal) {
  74. ss << std::dec;
  75. } else if (output_format == kHexadecimal) {
  76. ss << std::hex;
  77. } else if (output_format == kHexadecimal0x) {
  78. ss << std::hex;
  79. ss << "0x";
  80. }
  81. if (hash_function32)
  82. ss << hash_function32(arg);
  83. else if (hash_function64)
  84. ss << hash_function64(arg);
  85. if (annotate)
  86. ss << " /* \"" << arg << "\" */";
  87. ss << "\n";
  88. std::cout << ss.str();
  89. }
  90. }
  91. return 0;
  92. }