blob: 311d7c69e06ed5f6dde4281f99680753c2b77d7d [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;", " ");
Jean-Luc Brouillet6386ceb2015-04-28 15:06:30 -0700120 out = stringReplace(out, "&amp;", "&");
Jean-Luc Brouillet4a730042015-04-02 16:15:25 -0700121 return out;
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700122}
123
124string hashString(const string& s) {
125 long hash = 0;
126 for (size_t i = 0; i < s.length(); i++) {
127 hash = hash * 43 + s[i];
128 }
129 stringstream stream;
130 stream << "0x" << std::hex << hash << "l";
131 return stream.str();
132}
133
134bool testAndSet(const string& flag, set<string>* set) {
135 if (set->find(flag) == set->end()) {
136 set->insert(flag);
137 return false;
138 }
139 return true;
140}
141
142double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) {
143 /* Double has only 52 bits of precision (53 implied). So for longs, we want
144 * to create smaller values to avoid a round up. Same for floats and halfs.
145 */
146 int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize);
147 unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits))
148 << lowZeroBits;
149 return (double)l;
150}
151
152// Opens the stream. Reports an error if it can't.
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700153bool GeneratedFile::start(const string& directory, const string& name) {
154 const string path = directory + "/" + name;
155 open(path.c_str(), ios::out | ios::trunc);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700156 if (!is_open()) {
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -0700157 cerr << "Error. Can't open the output file: " << path << "\n";
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -0700158 return false;
159 }
160 return true;
161}
162
163void GeneratedFile::writeNotices() {
164 *this << LEGAL_NOTICE;
165 *this << "// " << AUTO_GENERATED_WARNING << "\n\n";
166}
167
168void GeneratedFile::increaseIndent() {
169 mIndent.append(string(TAB_SIZE, ' '));
170}
171
172void GeneratedFile::decreaseIndent() {
173 mIndent.erase(0, TAB_SIZE);
174}