blob: 6464ae17a2888dec9aec122c9ccc809e82f3fb02 [file] [log] [blame]
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
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 <iostream>
18#include <sstream>
19
20#include "Utilities.h"
21
22using namespace std;
23
24const char LEGAL_NOTICE[] =
25 "/*\n"
26 " * Copyright (C) 2015 The Android Open Source Project\n"
27 " *\n"
28 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
29 " * you may not use this file except in compliance with the License.\n"
30 " * You may obtain a copy of the License at\n"
31 " *\n"
32 " * http://www.apache.org/licenses/LICENSE-2.0\n"
33 " *\n"
34 " * Unless required by applicable law or agreed to in writing, software\n"
35 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
36 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
37 " * See the License for the specific language governing permissions and\n"
38 " * limitations under the License.\n"
39 " */\n\n";
40
41const char AUTO_GENERATED_WARNING[] =
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070042 "Don't edit this file! It is auto-generated by frameworks/rs/api/generate.sh.";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070043
44string capitalize(const string& source) {
45 int length = source.length();
46 string result;
47 bool capitalize = true;
48 for (int s = 0; s < length; s++) {
49 if (source[s] == '_') {
50 capitalize = true;
51 } else if (capitalize) {
52 result += toupper(source[s]);
53 capitalize = false;
54 } else {
55 result += source[s];
56 }
57 }
58 return result;
59}
60
61void trimSpaces(string* s) {
62 const size_t end = s->find_last_not_of(" ");
63 if (end == string::npos) {
64 // All spaces
65 s->erase();
66 return;
67 } else {
68 s->erase(end + 1);
69 }
70 const size_t start = s->find_first_not_of(" ");
71 if (start > 0) {
72 s->erase(0, start);
73 }
74}
75
76string stringReplace(string s, string match, string rep) {
77 while (1) {
78 // This is not efficient but we don't care, as this program runs very rarely.
79 size_t p = s.find(match);
80 if (p == string::npos) break;
81
82 s.erase(p, match.size());
83 s.insert(p, rep);
84 }
85 return s;
86}
87
88bool charRemoved(char c, string* s) {
89 size_t p = s->find(c);
90 if (p != string::npos) {
91 s->erase(p, 1);
92 return true;
93 }
94 return false;
95}
96
97string stripHtml(const string& html) {
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -070098 string in = stringReplace(html, "<li>", "- ");
99 string out;
100 for (size_t start = 0; start < in.size(); start++) {
101 size_t lt = in.find('<', start);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700102 if (lt == string::npos) {
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700103 out += in.substr(start);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700104 break;
105 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700106 out += in.substr(start, lt - start);
107 if (isalpha(in[lt + 1]) || in[lt + 1] == '/') {
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700108 // It's an HTML tag. Search for the end.
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700109 start = in.find('>', lt + 1);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700110 if (start == string::npos) {
111 break;
112 }
113 } else {
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700114 out += '<';
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700115 }
116 }
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700117 out = stringReplace(out, "&gt;", ">");
118 out = stringReplace(out, "&lt;", "<");
119 out = stringReplace(out, "&nbsp;", " ");
120 return out;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700121}
122
123string hashString(const string& s) {
124 long hash = 0;
125 for (size_t i = 0; i < s.length(); i++) {
126 hash = hash * 43 + s[i];
127 }
128 stringstream stream;
129 stream << "0x" << std::hex << hash << "l";
130 return stream.str();
131}
132
133bool testAndSet(const string& flag, set<string>* set) {
134 if (set->find(flag) == set->end()) {
135 set->insert(flag);
136 return false;
137 }
138 return true;
139}
140
141double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) {
142 /* Double has only 52 bits of precision (53 implied). So for longs, we want
143 * to create smaller values to avoid a round up. Same for floats and halfs.
144 */
145 int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize);
146 unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits))
147 << lowZeroBits;
148 return (double)l;
149}
150
151// Opens the stream. Reports an error if it can't.
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700152bool GeneratedFile::start(const string& directory, const string& name) {
153 const string path = directory + "/" + name;
154 open(path.c_str(), ios::out | ios::trunc);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700155 if (!is_open()) {
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700156 cerr << "Error. Can't open the output file: " << path << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700157 return false;
158 }
159 return true;
160}
161
162void GeneratedFile::writeNotices() {
163 *this << LEGAL_NOTICE;
164 *this << "// " << AUTO_GENERATED_WARNING << "\n\n";
165}
166
167void GeneratedFile::increaseIndent() {
168 mIndent.append(string(TAB_SIZE, ' '));
169}
170
171void GeneratedFile::decreaseIndent() {
172 mIndent.erase(0, TAB_SIZE);
173}