xref: /aosp_15_r20/frameworks/rs/script_api/Utilities.h (revision e1eccf28f96817838ad6867f7f39d2351ec11f56)
1*e1eccf28SAndroid Build Coastguard Worker /*
2*e1eccf28SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*e1eccf28SAndroid Build Coastguard Worker  *
4*e1eccf28SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1eccf28SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1eccf28SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1eccf28SAndroid Build Coastguard Worker  *
8*e1eccf28SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1eccf28SAndroid Build Coastguard Worker  *
10*e1eccf28SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1eccf28SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1eccf28SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1eccf28SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1eccf28SAndroid Build Coastguard Worker  * limitations under the License.
15*e1eccf28SAndroid Build Coastguard Worker  */
16*e1eccf28SAndroid Build Coastguard Worker 
17*e1eccf28SAndroid Build Coastguard Worker #ifndef ANDROID_RS_API_GENERATOR_UTILITIES_H
18*e1eccf28SAndroid Build Coastguard Worker #define ANDROID_RS_API_GENERATOR_UTILITIES_H
19*e1eccf28SAndroid Build Coastguard Worker 
20*e1eccf28SAndroid Build Coastguard Worker #include <fstream>
21*e1eccf28SAndroid Build Coastguard Worker #include <set>
22*e1eccf28SAndroid Build Coastguard Worker #include <string>
23*e1eccf28SAndroid Build Coastguard Worker 
24*e1eccf28SAndroid Build Coastguard Worker // Capitalizes and removes underscores.  E.g. converts "native_log" to NativeLog.
25*e1eccf28SAndroid Build Coastguard Worker std::string capitalize(const std::string& source);
26*e1eccf28SAndroid Build Coastguard Worker 
27*e1eccf28SAndroid Build Coastguard Worker // Trim trailing and leading spaces from a string.
28*e1eccf28SAndroid Build Coastguard Worker void trimSpaces(std::string* s);
29*e1eccf28SAndroid Build Coastguard Worker 
30*e1eccf28SAndroid Build Coastguard Worker // Replaces in string s all occurences of match with rep.
31*e1eccf28SAndroid Build Coastguard Worker std::string stringReplace(std::string s, std::string match, std::string rep);
32*e1eccf28SAndroid Build Coastguard Worker 
33*e1eccf28SAndroid Build Coastguard Worker // Removes the character from present. Returns true if the string contained the character.
34*e1eccf28SAndroid Build Coastguard Worker bool charRemoved(char c, std::string* s);
35*e1eccf28SAndroid Build Coastguard Worker 
36*e1eccf28SAndroid Build Coastguard Worker // Removes HTML references from the string.
37*e1eccf28SAndroid Build Coastguard Worker std::string stripHtml(const std::string& html);
38*e1eccf28SAndroid Build Coastguard Worker 
39*e1eccf28SAndroid Build Coastguard Worker // Returns a string that's an hexadecimal constant of the hash of the string.
40*e1eccf28SAndroid Build Coastguard Worker std::string hashString(const std::string& s);
41*e1eccf28SAndroid Build Coastguard Worker 
42*e1eccf28SAndroid Build Coastguard Worker // Adds a string to a set.  Return true if it was already in.
43*e1eccf28SAndroid Build Coastguard Worker bool testAndSet(const std::string& flag, std::set<std::string>* set);
44*e1eccf28SAndroid Build Coastguard Worker 
45*e1eccf28SAndroid Build Coastguard Worker /* Returns a double that should be able to be converted to an integer of size
46*e1eccf28SAndroid Build Coastguard Worker  * numberOfIntegerBits.
47*e1eccf28SAndroid Build Coastguard Worker  */
48*e1eccf28SAndroid Build Coastguard Worker double maxDoubleForInteger(int numberOfIntegerBits, int mantissaSize);
49*e1eccf28SAndroid Build Coastguard Worker 
50*e1eccf28SAndroid Build Coastguard Worker /* Creates an " __attribute__((...))" tag.  If userAttribute starts with '=', we don't
51*e1eccf28SAndroid Build Coastguard Worker  * use the additionalAttribute.  An empty string will be returned if there are no attributes.
52*e1eccf28SAndroid Build Coastguard Worker  */
53*e1eccf28SAndroid Build Coastguard Worker std::string makeAttributeTag(const std::string& userAttribute,
54*e1eccf28SAndroid Build Coastguard Worker                              const std::string& additionalAttribute, unsigned int deprecatedApiLevel,
55*e1eccf28SAndroid Build Coastguard Worker                              const std::string& deprecatedMessage);
56*e1eccf28SAndroid Build Coastguard Worker 
57*e1eccf28SAndroid Build Coastguard Worker /* This class is used to generate one source file.  There will be one instance
58*e1eccf28SAndroid Build Coastguard Worker  * for each generated file.
59*e1eccf28SAndroid Build Coastguard Worker  */
60*e1eccf28SAndroid Build Coastguard Worker class GeneratedFile : public std::ofstream {
61*e1eccf28SAndroid Build Coastguard Worker private:
62*e1eccf28SAndroid Build Coastguard Worker     std::string mIndent;  // The correct spacing at the beginning of each line.
63*e1eccf28SAndroid Build Coastguard Worker     const int TAB_SIZE = 4;
64*e1eccf28SAndroid Build Coastguard Worker 
65*e1eccf28SAndroid Build Coastguard Worker public:
66*e1eccf28SAndroid Build Coastguard Worker     // Opens the stream.  Reports an error if it can't.
67*e1eccf28SAndroid Build Coastguard Worker     bool start(const std::string& directory, const std::string& name);
68*e1eccf28SAndroid Build Coastguard Worker 
69*e1eccf28SAndroid Build Coastguard Worker     // Write copyright notice & auto-generated warning in Java/C style comments.
70*e1eccf28SAndroid Build Coastguard Worker     void writeNotices();
71*e1eccf28SAndroid Build Coastguard Worker 
72*e1eccf28SAndroid Build Coastguard Worker     void increaseIndent();               // Increases the new line indentation by 4.
73*e1eccf28SAndroid Build Coastguard Worker     void decreaseIndent();               // Decreases the new line indentation by 4.
74*e1eccf28SAndroid Build Coastguard Worker     void comment(const std::string& s);  // Outputs a multiline comment.
75*e1eccf28SAndroid Build Coastguard Worker 
76*e1eccf28SAndroid Build Coastguard Worker     // Starts a control block.  This works both for Java and C++.
startBlock()77*e1eccf28SAndroid Build Coastguard Worker     void startBlock() {
78*e1eccf28SAndroid Build Coastguard Worker         *this << " {\n";
79*e1eccf28SAndroid Build Coastguard Worker         increaseIndent();
80*e1eccf28SAndroid Build Coastguard Worker     }
81*e1eccf28SAndroid Build Coastguard Worker 
82*e1eccf28SAndroid Build Coastguard Worker     // Ends a control block.
83*e1eccf28SAndroid Build Coastguard Worker     void endBlock(bool addSemicolon = false) {
84*e1eccf28SAndroid Build Coastguard Worker         decreaseIndent();
85*e1eccf28SAndroid Build Coastguard Worker         indent() << "}" << (addSemicolon ? ";" : "") << "\n";
86*e1eccf28SAndroid Build Coastguard Worker     }
87*e1eccf28SAndroid Build Coastguard Worker 
88*e1eccf28SAndroid Build Coastguard Worker     /* Indents the line.  By returning *this, we can use like this:
89*e1eccf28SAndroid Build Coastguard Worker      *  mOut.ident() << "a = b;\n";
90*e1eccf28SAndroid Build Coastguard Worker      */
indent()91*e1eccf28SAndroid Build Coastguard Worker     std::ofstream& indent() {
92*e1eccf28SAndroid Build Coastguard Worker         *this << mIndent;
93*e1eccf28SAndroid Build Coastguard Worker         return *this;
94*e1eccf28SAndroid Build Coastguard Worker     }
95*e1eccf28SAndroid Build Coastguard Worker 
indentPlus()96*e1eccf28SAndroid Build Coastguard Worker     std::ofstream& indentPlus() {
97*e1eccf28SAndroid Build Coastguard Worker         *this << mIndent << std::string(2 * TAB_SIZE, ' ');
98*e1eccf28SAndroid Build Coastguard Worker         return *this;
99*e1eccf28SAndroid Build Coastguard Worker     }
100*e1eccf28SAndroid Build Coastguard Worker };
101*e1eccf28SAndroid Build Coastguard Worker 
102*e1eccf28SAndroid Build Coastguard Worker extern const char AUTO_GENERATED_WARNING[];
103*e1eccf28SAndroid Build Coastguard Worker 
104*e1eccf28SAndroid Build Coastguard Worker #endif  // ANDROID_RS_API_GENERATOR_UTILITIES_H
105