• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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.
25 std::string capitalize(const std::string& source);
26 
27 // Trim trailing and leading spaces from a string.
28 void trimSpaces(std::string* s);
29 
30 // Replaces in string s all occurences of match with rep.
31 std::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.
34 bool charRemoved(char c, std::string* s);
35 
36 // Removes HTML references from the string.
37 std::string stripHtml(const std::string& html);
38 
39 // Returns a string that's an hexadecimal constant of the hash of the string.
40 std::string hashString(const std::string& s);
41 
42 // Adds a string to a set.  Return true if it was already in.
43 bool 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  */
48 double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize);
49 
50 /* Creates an " __attribute__((...))" tag.  If userAttribute starts with '=', we don't
51  * use the additionalAttribute.  An empty string will be returned if there are no attributes.
52  */
53 std::string makeAttributeTag(const std::string& userAttribute,
54                              const std::string& additionalAttribute, unsigned int deprecatedApiLevel,
55                              const std::string& deprecatedMessage);
56 
57 /* This class is used to generate one source file.  There will be one instance
58  * for each generated file.
59  */
60 class GeneratedFile : public std::ofstream {
61 private:
62     std::string mIndent;  // The correct spacing at the beginning of each line.
63     const int TAB_SIZE = 4;
64 
65 public:
66     // Opens the stream.  Reports an error if it can't.
67     bool start(const std::string& directory, const std::string& name);
68 
69     // Write copyright notice & auto-generated warning in Java/C style comments.
70     void writeNotices();
71 
72     void increaseIndent();               // Increases the new line indentation by 4.
73     void decreaseIndent();               // Decreases the new line indentation by 4.
74     void comment(const std::string& s);  // Outputs a multiline comment.
75 
76     // Starts a control block.  This works both for Java and C++.
startBlock()77     void startBlock() {
78         *this << " {\n";
79         increaseIndent();
80     }
81 
82     // Ends a control block.
83     void endBlock(bool addSemicolon = false) {
84         decreaseIndent();
85         indent() << "}" << (addSemicolon ? ";" : "") << "\n";
86     }
87 
88     /* Indents the line.  By returning *this, we can use like this:
89      *  mOut.ident() << "a = b;\n";
90      */
indent()91     std::ofstream& indent() {
92         *this << mIndent;
93         return *this;
94     }
95 
indentPlus()96     std::ofstream& indentPlus() {
97         *this << mIndent << std::string(2 * TAB_SIZE, ' ');
98         return *this;
99     }
100 };
101 
102 extern const char AUTO_GENERATED_WARNING[];
103 
104 #endif  // ANDROID_RS_API_GENERATOR_UTILITIES_H
105