blob: 04169dc3aad7d6ec36e5133b86ab2ba5c6ab7db6 [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#ifndef ANDROID_RS_API_GENERATOR_UTILITIES_H
18#define ANDROID_RS_API_GENERATOR_UTILITIES_H
19
20#include <fstream>
21#include <set>
22#include <string>
23
24// Capitalizes and removes underscores. E.g. converts "native_log" to NativeLog.
25std::string capitalize(const std::string& source);
26
27// Trim trailing and leading spaces from a string.
28void trimSpaces(std::string* s);
29
30// Replaces in string s all occurences of match with rep.
31std::string stringReplace(std::string s, std::string match, std::string rep);
32
33// Removes the character from present. Returns true if the string contained the character.
34bool charRemoved(char c, std::string* s);
35
36// Removes HTML references from the string.
37std::string stripHtml(const std::string& html);
38
39// Returns a string that's an hexadecimal constant of the hash of the string.
40std::string hashString(const std::string& s);
41
42// Adds a string to a set. Return true if it was already in.
43bool testAndSet(const std::string& flag, std::set<std::string>* set);
44
45/* Returns a double that should be able to be converted to an integer of size
46 * numberOfIntegerBits.
47 */
48double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize);
49
50/* This class is used to generate one source file. There will be one instance
51 * for each generated file.
52 */
53class GeneratedFile : public std::ofstream {
54private:
55 std::string mIndent; // The correct spacing at the beginning of each line.
56 const int TAB_SIZE = 4;
57
58public:
59 // Opens the stream. Reports an error if it can't.
Jean-Luc Brouillet62e09932015-03-22 11:14:07 -070060 bool start(const std::string& directory, const std::string& name);
Jean-Luc Brouilletc5184e22015-03-13 13:51:24 -070061
62 // Write copyright notice & auto-generated warning in Java/C style comments.
63 void writeNotices();
64
65 void increaseIndent(); // Increases the new line indentation by 4.
66 void decreaseIndent(); // Decreases the new line indentation by 4.
67 void comment(const std::string& s); // Outputs a multiline comment.
68
69 // Starts a control block. This works both for Java and C++.
70 void startBlock() {
71 *this << " {\n";
72 increaseIndent();
73 }
74
75 // Ends a control block.
76 void endBlock(bool addSemicolon = false) {
77 decreaseIndent();
78 indent() << "}" << (addSemicolon ? ";" : "") << "\n";
79 }
80
81 /* Indents the line. By returning *this, we can use like this:
82 * mOut.ident() << "a = b;\n";
83 */
84 std::ofstream& indent() {
85 *this << mIndent;
86 return *this;
87 }
88
89 std::ofstream& indentPlus() {
90 *this << mIndent << std::string(2 * TAB_SIZE, ' ');
91 return *this;
92 }
93};
94
95extern const char AUTO_GENERATED_WARNING[];
96
97#endif // ANDROID_RS_API_GENERATOR_UTILITIES_H