blob: 80ebb0d4c4cfe0f6110d5f900695fb3e5be16a0f [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[] =
42 "Don't edit this file! It is auto-generated by frameworks/rs/api/gen_runtime.";
43
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) {
98 string s;
99 for (size_t start = 0; start < html.size(); start++) {
100 size_t lt = html.find('<', start);
101 if (lt == string::npos) {
102 s += html.substr(start);
103 break;
104 }
105 s += html.substr(start, lt - start);
106 if (isalpha(html[lt + 1]) || html[lt + 1] == '/') {
107 // It's an HTML tag. Search for the end.
108 start = html.find('>', lt + 1);
109 if (start == string::npos) {
110 break;
111 }
112 } else {
113 s += '<';
114 }
115 }
116 s = stringReplace(s, "&gt;", ">");
117 s = stringReplace(s, "&lt;", "<");
118 s = stringReplace(s, "&nbsp;", " ");
119 return s;
120}
121
122string hashString(const string& s) {
123 long hash = 0;
124 for (size_t i = 0; i < s.length(); i++) {
125 hash = hash * 43 + s[i];
126 }
127 stringstream stream;
128 stream << "0x" << std::hex << hash << "l";
129 return stream.str();
130}
131
132bool testAndSet(const string& flag, set<string>* set) {
133 if (set->find(flag) == set->end()) {
134 set->insert(flag);
135 return false;
136 }
137 return true;
138}
139
140double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize) {
141 /* Double has only 52 bits of precision (53 implied). So for longs, we want
142 * to create smaller values to avoid a round up. Same for floats and halfs.
143 */
144 int lowZeroBits = max(0, numberOfIntegerBits - mantissaSize);
145 unsigned long l = (0xffffffffffffffff >> (64 - numberOfIntegerBits + lowZeroBits))
146 << lowZeroBits;
147 return (double)l;
148}
149
150// Opens the stream. Reports an error if it can't.
151bool GeneratedFile::start(const string& name) {
152 open(name.c_str(), ios::out | ios::trunc);
153 if (!is_open()) {
154 cerr << "Error. Can't open the output file: " << name << "\n";
155 return false;
156 }
157 return true;
158}
159
160void GeneratedFile::writeNotices() {
161 *this << LEGAL_NOTICE;
162 *this << "// " << AUTO_GENERATED_WARNING << "\n\n";
163}
164
165void GeneratedFile::increaseIndent() {
166 mIndent.append(string(TAB_SIZE, ' '));
167}
168
169void GeneratedFile::decreaseIndent() {
170 mIndent.erase(0, TAB_SIZE);
171}