ConfDataWriter.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ConfDataWriter.cpp 2548 2011-04-11 08:41:22Z yanweignss $"
00002 
00009 //============================================================================
00010 //
00011 //  This file is part of GPSTk, the GPS Toolkit.
00012 //
00013 //  The GPSTk is free software; you can redistribute it and/or modify
00014 //  it under the terms of the GNU Lesser General Public License as published
00015 //  by the Free Software Foundation; either version 2.1 of the License, or
00016 //  any later version.
00017 //
00018 //  The GPSTk is distributed in the hope that it will be useful,
00019 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 //  GNU Lesser General Public License for more details.
00022 //
00023 //  You should have received a copy of the GNU Lesser General Public
00024 //  License along with GPSTk; if not, write to the Free Software Foundation,
00025 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026 //
00027 //  Wei Yan - Chinese Academy of Sciences  2009, 2010
00028 //
00029 //============================================================================
00030 
00031 
00032 #include "ConfDataWriter.hpp"
00033 
00034 
00035 namespace gpstk
00036 {
00037 
00038            // Method to open a configuration data file to be written.
00039         void ConfDataWriter::open(const char* fn)
00040         {
00041                 FFTextStream::open(fn, std::ios::out);
00042 
00043                 writeHeader();
00044                 
00045                 return;
00046         }
00047       // Write a common header for all of the configuration data files
00048       // written by this class. 
00049         void ConfDataWriter::writeHeader()
00050         {
00051      /* 
00052            writeCommentLine("This is a configuration file written by ConfDataWriter, and it ");
00053       writeCommentLine("can be read by ConfDataReader.");
00054                 writeCommentLine("YAN Wei,Dec,8th 2009");
00055                 writeCommentLine("Enjoy!");
00056                 writeCommentLine("");
00057                 writeSeparatorLine();
00058                 */
00059       
00060         }
00061 
00062       // Write a comment line start by '#'
00063         void ConfDataWriter::writeCommentLine(const string& comment)
00064         {
00065                 formattedPutLine("# "+comment);
00066         }
00067 
00068 
00069       // Write a comment line as a separator line
00070       // @param s    char of the separator line
00071       // @param n    size of the separator line
00072         void ConfDataWriter::writeSeparatorLine(const string& s,
00073                                            const int&    n )
00074         {
00075                 writeCommentLine(string(n,s[0]));
00076         }
00077 
00078       // Write several blank lines default write one line
00079         void ConfDataWriter::writeBlankLine(const int& n)
00080         {
00081                 int nLine = (n < 1) ? 0 : n;
00082                 for(int i = 0;i < nLine; i++) 
00083       {
00084          formattedPutLine("");
00085       }
00086         }
00087       
00088       /* Write a string variable with general format
00089       *
00090       * @param var          variable name
00091       * @param val          variable value
00092       * @param varComment   variable comment 
00093       * @param valComment   value comment
00094       */
00095         void ConfDataWriter::writeVariable(const string& var,
00096                                       const string& val,
00097                                       const string& varComment,
00098                                       const string& valComment)
00099         {
00100                 string line=var;
00101 
00102       if(var.length()<variableWidth) 
00103          line = StringUtils::leftJustify(var,variableWidth);
00104 
00105                 if(varComment.length()>0) line += " , " + varComment;
00106                 
00107       line += " = " + val;
00108                 
00109       if(valComment.length() > 0) line += " , " + valComment;
00110                 
00111       formattedPutLine(line);
00112         }
00113 
00114       /* Write a double variable with general format
00115        *
00116        * @param var          variable name
00117        * @param val          variable value
00118        * @param varComment   variable comment 
00119        * @param valComment   value comment
00120        */
00121    void ConfDataWriter::writeVariable(const string& var,
00122                       const double& val,
00123                       const string& varComment,
00124                       const string& valComment)
00125    { 
00126       writeVariable(var,StringUtils::asString(val,valuePrecison),
00127                                                          varComment,valComment);
00128    }
00129 
00130       
00131 
00132            /* Write a string variable list with general format
00133        *
00134        * @param var          variable name
00135        * @param valList      variable list values
00136        * @param n            size of the variable list
00137        * @param varComment   variable comment 
00138        * @param valComment   value comment
00139        */
00140         void ConfDataWriter::writeVariableList(const string& var,
00141                                           const string  valList[],
00142                                           const int&    n,
00143                                           const string& varComment,
00144                                           const string& valComment)
00145         {
00146                 string line=var;
00147 
00148       if(var.length()<variableWidth) 
00149          line = StringUtils::leftJustify(var,variableWidth);
00150 
00151                 if(varComment.length() > 0) line += " , " + varComment;
00152 
00153                 line += " = ";
00154                 
00155                 for(int i=0;i<n;i++) line += valList[i] + " ";
00156 
00157                 
00158                 if(valComment.length() > 0) line += " , " + valComment;
00159 
00160                 formattedPutLine(line);
00161         }
00162         
00163 
00164       /* Write a string variable list with general format
00165        *
00166        * @param var          variable name
00167        * @param valList      variable list values by std::vector
00168        * @param varComment   variable comment 
00169        * @param valComment   value comment
00170        */
00171         void ConfDataWriter::writeVariableList(const string&         var,
00172                                           vector<string> valList,
00173                                           const string&         varComment,
00174                                           const string&         valComment)
00175         {
00176                 string line=var;
00177 
00178       if(var.length()<variableWidth) 
00179          line = StringUtils::leftJustify(var,variableWidth);
00180 
00181                 if(varComment.length() > 0) line += " , " + varComment;
00182 
00183                 line += " = ";
00184 
00185                 for(vector<string>::const_iterator it = valList.begin();
00186          it != valList.end();
00187          ++it )
00188                 {
00189                         line += (*it) + " ";
00190                 }
00191 
00192 
00193                 if(valComment.length() > 0) line += " , " + valComment;
00194 
00195                 formattedPutLine(line);
00196         }
00197 
00198 
00199       /* Write a int variable list with general format
00200        *
00201        * @param var          variable name
00202        * @param valList      variable list values
00203        * @param n            size of the variable list
00204        * @param varComment   variable comment 
00205        * @param valComment   value comment
00206        */
00207         void ConfDataWriter::writeVariableList(const string& var,
00208                                           const int     valList[],
00209                                           const int&    n,
00210                                           const string& varComment,
00211                                           const string& valComment )
00212         {
00213                 vector<string> vals;
00214                 for(int i = 0; i < n; i++)
00215       {
00216          vals.push_back(StringUtils::asString(valList[i]));
00217       }
00218 
00219                 writeVariableList(var, vals, varComment, valComment);
00220         }
00221 
00222       /* Write a double variable list with general format
00223        *
00224        * @param var          variable name
00225        * @param valList      variable list values
00226        * @param n            size of the variable list
00227        * @param varComment   variable comment 
00228        * @param valComment   value comment
00229        */
00230         void ConfDataWriter::writeVariableList(const string& var,
00231                                           const double valList[],
00232                                           const int&    n,
00233                                           const string& varComment,
00234                                           const string& valComment)
00235         {
00236                 vector<string> vals;
00237                 for(int i = 0; i < n; i++) 
00238       {
00239          vals.push_back(StringUtils::asString(valList[i],valuePrecison));
00240       }
00241 
00242                 writeVariableList(var, vals, varComment, valComment);
00243         }
00244         
00245         
00246       /* Write a new section with some comment
00247        *
00248        * @param name          name of the section to be written
00249        * @param comment       comment of the section to be written
00250        */
00251         void ConfDataWriter::writeSection(const string& name,
00252                                      const string& comment)
00253         {
00254       string commentCopy(comment);
00255 
00256                 if(commentCopy.length() < 1)
00257       {
00258          commentCopy = "Configuration data for '" + name + "' section";
00259       }
00260                 
00261       writeCommentLine(StringUtils::upperCase(commentCopy));
00262 
00263                 writeSeparatorLine();
00264                 
00265       formattedPutLine("[" + StringUtils::strip(name) + "]");
00266 
00267         }
00268 
00269 
00270       // Write a common tailer for all of the configuration data files
00271       // written by this class.
00272         void ConfDataWriter::writeEnd()
00273         {
00274                 writeBlankLine();
00275 
00276                 writeCommentLine("End Of the File");
00277                 
00278       writeSeparatorLine();
00279         }
00280 
00281 
00282       // Write a string line to the file.
00283         void ConfDataWriter::formattedPutLine(const std::string& sline)
00284         {
00285       // to make sure the line is less than 255 
00286                 (*this) << sline.substr(0,255) << endl;
00287         }
00288 
00289 }  // End of 'namespace gpstk'

Generated on Tue May 22 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1