xref: /aosp_15_r20/system/linkerconfig/modules/configwriter.cc (revision e5eeaa8e05bc25a862c0c861bda7c8a6bfb42dad)
1 /*
2  * Copyright (C) 2019 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 "linkerconfig/configwriter.h"
18 
19 #include "linkerconfig/log.h"
20 #include "linkerconfig/variables.h"
21 
22 namespace android {
23 namespace linkerconfig {
24 namespace modules {
25 
ConfigWriter()26 ConfigWriter::ConfigWriter() noexcept {
27   content_.reserve(4096);
28 }
29 
WriteVars(const std::string & var,const std::vector<std::string> & values)30 void ConfigWriter::WriteVars(const std::string& var,
31                              const std::vector<std::string>& values) {
32   bool is_first = true;
33   for (const auto& value : values) {
34     content_ += var;
35     content_ += (is_first ? " = " : " += ");
36     content_ += value;
37     content_ += "\n";
38     is_first = false;
39   }
40 }
41 
WriteVars(const std::string & var,const std::vector<std::string> & values,const std::string & suffix)42 void ConfigWriter::WriteVars(const std::string& var,
43                              const std::vector<std::string>& values,
44                              const std::string& suffix) {
45   bool is_first = true;
46   for (const auto& value : values) {
47     content_ += var;
48     content_ += (is_first ? " = " : " += ");
49     content_ += value;
50     content_ += suffix;
51     content_ += "\n";
52     content_ += var;
53     content_ += " += ";
54     content_ += value;
55     content_ += "\n";
56     is_first = false;
57   }
58 }
59 
WriteVar(const std::string & var,const std::string & value)60 void ConfigWriter::WriteVar(const std::string& var, const std::string& value) {
61   if (!value.empty()) {
62     content_ += var;
63     content_ += " = ";
64     content_ += value;
65     content_ += "\n";
66   }
67 }
68 
WriteLine(const std::string & line)69 void ConfigWriter::WriteLine(const std::string& line) {
70   content_ += line;
71   content_ += "\n";
72 }
73 
ToString()74 std::string ConfigWriter::ToString() {
75   return content_;
76 }
77 }  // namespace modules
78 }  // namespace linkerconfig
79 }  // namespace android