[C++] Refactor to conform to Google C++ style guide (#5608)
[platform/upstream/flatbuffers.git] / src / flathash.cpp
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
17 #include <stdio.h>
18
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22
23 #include "flatbuffers/hash.h"
24
25 enum OutputFormat { kDecimal, kHexadecimal, kHexadecimal0x };
26
27 int main(int argc, char *argv[]) {
28   const char *name = argv[0];
29   if (argc <= 1) {
30     printf("%s HASH [OPTION]... [--] STRING...\n", name);
31     printf("Available hashing algorithms:\n");
32     printf("  16 bit:\n");
33     size_t size = sizeof(flatbuffers::kHashFunctions16) /
34                   sizeof(flatbuffers::kHashFunctions16[0]);
35     for (size_t i = 0; i < size; ++i) {
36       printf("    * %s\n", flatbuffers::kHashFunctions16[i].name);
37     }
38     printf("  32 bit:\n");
39     size = sizeof(flatbuffers::kHashFunctions32) /
40            sizeof(flatbuffers::kHashFunctions32[0]);
41     for (size_t i = 0; i < size; ++i) {
42       printf("    * %s\n", flatbuffers::kHashFunctions32[i].name);
43     }
44     printf("  64 bit:\n");
45     size = sizeof(flatbuffers::kHashFunctions64) /
46            sizeof(flatbuffers::kHashFunctions64[0]);
47     for (size_t i = 0; i < size; ++i) {
48       printf("    * %s\n", flatbuffers::kHashFunctions64[i].name);
49     }
50     printf(
51         "  -d         Output hash in decimal.\n"
52         "  -x         Output hash in hexadecimal.\n"
53         "  -0x        Output hash in hexadecimal and prefix with 0x.\n"
54         "  -c         Append the string to the output in a c-style comment.\n");
55     return 1;
56   }
57
58   const char *hash_algorithm = argv[1];
59
60   flatbuffers::NamedHashFunction<uint16_t>::HashFunction hash_function16 =
61       flatbuffers::FindHashFunction16(hash_algorithm);
62   flatbuffers::NamedHashFunction<uint32_t>::HashFunction hash_function32 =
63       flatbuffers::FindHashFunction32(hash_algorithm);
64   flatbuffers::NamedHashFunction<uint64_t>::HashFunction hash_function64 =
65       flatbuffers::FindHashFunction64(hash_algorithm);
66
67   if (!hash_function16 && !hash_function32 && !hash_function64) {
68     printf("\"%s\" is not a known hash algorithm.\n", hash_algorithm);
69     return 1;
70   }
71
72   OutputFormat output_format = kHexadecimal;
73   bool annotate = false;
74   bool escape_dash = false;
75   for (int i = 2; i < argc; i++) {
76     const char *arg = argv[i];
77     if (!escape_dash && arg[0] == '-') {
78       std::string opt = arg;
79       if (opt == "-d")
80         output_format = kDecimal;
81       else if (opt == "-x")
82         output_format = kHexadecimal;
83       else if (opt == "-0x")
84         output_format = kHexadecimal0x;
85       else if (opt == "-c")
86         annotate = true;
87       else if (opt == "--")
88         escape_dash = true;
89       else
90         printf("Unrecognized argument: \"%s\"\n", arg);
91     } else {
92       std::stringstream ss;
93       if (output_format == kDecimal) {
94         ss << std::dec;
95       } else if (output_format == kHexadecimal) {
96         ss << std::hex;
97       } else if (output_format == kHexadecimal0x) {
98         ss << std::hex;
99         ss << "0x";
100       }
101       if (hash_function16)
102         ss << hash_function16(arg);
103       else if (hash_function32)
104         ss << hash_function32(arg);
105       else if (hash_function64)
106         ss << hash_function64(arg);
107
108       if (annotate) ss << " /* \"" << arg << "\" */";
109
110       ss << "\n";
111
112       std::cout << ss.str();
113     }
114   }
115   return 0;
116 }