1 //==============================================================================
2 //
3 //  This file is part of GPSTk, the GPS Toolkit.
4 //
5 //  The GPSTk is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 3.0 of the License, or
8 //  any later version.
9 //
10 //  The GPSTk is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with GPSTk; if not, write to the Free Software Foundation,
17 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 //  This software was developed by Applied Research Laboratories at the
20 //  University of Texas at Austin.
21 //  Copyright 2004-2020, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 //  This software was developed by Applied Research Laboratories at the
28 //  University of Texas at Austin, under contract to an agency or agencies
29 //  within the U.S. Department of Defense. The U.S. Government retains all
30 //  rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 //  Pursuant to DoD Directive 523024
33 //
34 //  DISTRIBUTION STATEMENT A: This software has been approved for public
35 //                            release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 /**
40 * @file ConfDataWriter.cpp
41 * Class to write configuration data files.
42 */
43 
44 #include "ConfDataWriter.hpp"
45 
46 using namespace std;
47 
48 namespace gpstk
49 {
50 
51 	   // Method to open a configuration data file to be written.
open(const char * fn)52 	void ConfDataWriter::open(const char* fn)
53 	{
54 		FFTextStream::open(fn, std::ios::out);
55 
56 		writeHeader();
57 
58 		return;
59 	}
60       // Write a common header for all of the configuration data files
61       // written by this class.
writeHeader()62 	void ConfDataWriter::writeHeader()
63 	{
64      /*
65 	   writeCommentLine("This is a configuration file written by ConfDataWriter, and it ");
66       writeCommentLine("can be read by ConfDataReader.");
67 		writeCommentLine("YAN Wei,Dec,8th 2009");
68 		writeCommentLine("Enjoy!");
69 		writeCommentLine("");
70 		writeSeparatorLine();
71 		*/
72 
73 	}
74 
75       // Write a comment line start by '#'
writeCommentLine(const string & comment)76 	void ConfDataWriter::writeCommentLine(const string& comment)
77 	{
78 		formattedPutLine("# "+comment);
79 	}
80 
81 
82       // Write a comment line as a separator line
83       // @param s    char of the separator line
84       // @param n    size of the separator line
writeSeparatorLine(const string & s,const int & n)85 	void ConfDataWriter::writeSeparatorLine(const string& s,
86                                            const int&    n )
87 	{
88 		writeCommentLine(string(n,s[0]));
89 	}
90 
91       // Write several blank lines default write one line
writeBlankLine(const int & n)92 	void ConfDataWriter::writeBlankLine(const int& n)
93 	{
94 		int nLine = (n < 1) ? 0 : n;
95 		for(int i = 0;i < nLine; i++)
96       {
97          formattedPutLine("");
98       }
99 	}
100 
101       /* Write a string variable with general format
102       *
103       * @param var          variable name
104       * @param val          variable value
105       * @param varComment   variable comment
106       * @param valComment   value comment
107       */
writeVariable(const string & var,const string & val,const string & varComment,const string & valComment)108 	void ConfDataWriter::writeVariable(const string& var,
109                                       const string& val,
110                                       const string& varComment,
111                                       const string& valComment)
112 	{
113 		string line=var;
114 
115       if(int(var.length())<variableWidth)
116          line = StringUtils::leftJustify(var,variableWidth);
117 
118 		if(varComment.length()>0) line += " , " + varComment;
119 
120       line += " = " + val;
121 
122       if(valComment.length() > 0) line += " , " + valComment;
123 
124       formattedPutLine(line);
125 	}
126 
127       /* Write a double variable with general format
128        *
129        * @param var          variable name
130        * @param val          variable value
131        * @param varComment   variable comment
132        * @param valComment   value comment
133        */
writeVariable(const string & var,const double & val,const string & varComment,const string & valComment)134    void ConfDataWriter::writeVariable(const string& var,
135                       const double& val,
136                       const string& varComment,
137                       const string& valComment)
138    {
139       writeVariable(var,StringUtils::asString(val,valuePrecison),
140                                                          varComment,valComment);
141    }
142 
143 
144 
145 	   /* Write a string variable list with general format
146        *
147        * @param var          variable name
148        * @param valList      variable list values
149        * @param n            size of the variable list
150        * @param varComment   variable comment
151        * @param valComment   value comment
152        */
writeVariableList(const string & var,const string valList[],const int & n,const string & varComment,const string & valComment)153 	void ConfDataWriter::writeVariableList(const string& var,
154                                           const string  valList[],
155                                           const int&    n,
156                                           const string& varComment,
157                                           const string& valComment)
158 	{
159 		string line=var;
160 
161       if(int(var.length())<variableWidth)
162          line = StringUtils::leftJustify(var,variableWidth);
163 
164 		if(varComment.length() > 0) line += " , " + varComment;
165 
166 		line += " = ";
167 
168 		for(int i=0;i<n;i++) line += valList[i] + " ";
169 
170 
171 		if(valComment.length() > 0) line += " , " + valComment;
172 
173 		formattedPutLine(line);
174 	}
175 
176 
177       /* Write a string variable list with general format
178        *
179        * @param var          variable name
180        * @param valList      variable list values by std::vector
181        * @param varComment   variable comment
182        * @param valComment   value comment
183        */
writeVariableList(const string & var,vector<string> valList,const string & varComment,const string & valComment)184 	void ConfDataWriter::writeVariableList(const string&         var,
185                                           vector<string> valList,
186                                           const string&         varComment,
187                                           const string&         valComment)
188 	{
189 		string line=var;
190 
191       if(int(var.length())<variableWidth)
192          line = StringUtils::leftJustify(var,variableWidth);
193 
194 		if(varComment.length() > 0) line += " , " + varComment;
195 
196 		line += " = ";
197 
198 		for(vector<string>::const_iterator it = valList.begin();
199          it != valList.end();
200          ++it )
201 		{
202 			line += (*it) + " ";
203 		}
204 
205 
206 		if(valComment.length() > 0) line += " , " + valComment;
207 
208 		formattedPutLine(line);
209 	}
210 
211 
212       /* Write a int variable list with general format
213        *
214        * @param var          variable name
215        * @param valList      variable list values
216        * @param n            size of the variable list
217        * @param varComment   variable comment
218        * @param valComment   value comment
219        */
writeVariableList(const string & var,const int valList[],const int & n,const string & varComment,const string & valComment)220 	void ConfDataWriter::writeVariableList(const string& var,
221                                           const int     valList[],
222                                           const int&    n,
223                                           const string& varComment,
224                                           const string& valComment )
225 	{
226 		vector<string> vals;
227 		for(int i = 0; i < n; i++)
228       {
229          vals.push_back(StringUtils::asString(valList[i]));
230       }
231 
232 		writeVariableList(var, vals, varComment, valComment);
233 	}
234 
235       /* Write a double variable list with general format
236        *
237        * @param var          variable name
238        * @param valList      variable list values
239        * @param n            size of the variable list
240        * @param varComment   variable comment
241        * @param valComment   value comment
242        */
writeVariableList(const string & var,const double valList[],const int & n,const string & varComment,const string & valComment)243 	void ConfDataWriter::writeVariableList(const string& var,
244                                           const double valList[],
245                                           const int&    n,
246                                           const string& varComment,
247                                           const string& valComment)
248 	{
249 		vector<string> vals;
250 		for(int i = 0; i < n; i++)
251       {
252          vals.push_back(StringUtils::asString(valList[i],valuePrecison));
253       }
254 
255 		writeVariableList(var, vals, varComment, valComment);
256 	}
257 
258 
259       /* Write a new section with some comment
260        *
261        * @param name          name of the section to be written
262        * @param comment       comment of the section to be written
263        */
writeSection(const string & name,const string & comment)264 	void ConfDataWriter::writeSection(const string& name,
265                                      const string& comment)
266 	{
267       string commentCopy(comment);
268 
269 		if(commentCopy.length() < 1)
270       {
271          commentCopy = "Configuration data for '" + name + "' section";
272       }
273 
274       writeCommentLine(StringUtils::upperCase(commentCopy));
275 
276 		writeSeparatorLine();
277 
278       formattedPutLine("[" + StringUtils::strip(name) + "]");
279 
280 	}
281 
282 
283       // Write a common tailer for all of the configuration data files
284       // written by this class.
writeEnd()285 	void ConfDataWriter::writeEnd()
286 	{
287 		writeBlankLine();
288 
289 		writeCommentLine("End Of the File");
290 
291       writeSeparatorLine();
292 	}
293 
294 
295       // Write a string line to the file.
formattedPutLine(const std::string & sline)296 	void ConfDataWriter::formattedPutLine(const std::string& sline)
297 	{
298       // to make sure the line is less than 255
299 		(*this) << sline.substr(0,255) << endl;
300 	}
301 
302 }  // End of 'namespace gpstk'
303