Confdump Agent  1.4.0
FixedTable.hpp
Go to the documentation of this file.
00001 /* 
00002  * Confdump-Agent - Dump static and runtime system configuration
00003  * Copyright (C) 2009-2012  Straton IT, SAS
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef INCLUDED_CONFDUMP_SYSTEM_FIXEDTABLE_HPP
00019 #define INCLUDED_CONFDUMP_SYSTEM_FIXEDTABLE_HPP
00020 
00021 #include <iosfwd>
00022 #include <vector>
00023 #include <string>
00024 #include <QByteArray>
00025 #include <QString>
00026 #include <QStringList>
00027 #include "boost/shared_ptr.hpp"
00028 
00029 #include "Confdump/Result.hpp"
00030 
00031 namespace Confdump
00032 {
00033 
00034 namespace System
00035 {
00036 
00037 class ManagedElement;
00038 
00052 class FixedTable
00053 {
00054 public:
00055     enum ParsingMode
00056     {
00057         DetectWidthsFromHeader,
00058         UseSpecifiedHeaders
00059     };
00060 
00061     enum Platform
00062     {
00063         Windows,
00064         Linux
00065     };
00069     class Specification
00070     {
00071     public:
00072         Specification();
00073         ~Specification();
00074 
00076         bool hasHeader() const { return true; }
00078         ParsingMode parsingMode() const { return UseSpecifiedHeaders; }
00079 
00081         Platform platform() const { return Linux; }
00083 
00084         struct ColumnHead
00085         {
00087             std::string name;
00089             unsigned skip;
00091             unsigned width;
00092 
00093             ColumnHead() : skip( 0 ), width( 0 ) {}
00094         };
00096         Specification& addColumnHead( const std::string &name, unsigned skip, unsigned width );
00097         typedef std::vector<ColumnHead>::const_iterator ColumnHeadIterator;
00098         ColumnHeadIterator columnHeadsBegin() const;
00099         ColumnHeadIterator columnHeadsEnd() const;
00100 
00101     private:
00102         std::vector<ColumnHead> columnHeads_;
00103     };
00104 
00105     FixedTable();
00106     ~FixedTable();
00107 
00109     template<class ElementT>
00110     void readAllInto( QByteArray byteArray, std::vector< boost::shared_ptr< ElementT > > &elements );
00111 
00113     void setFieldTranslations( const std::map<std::string, std::string> &translations );
00114 
00116     void setSpecification( const Specification &spec );
00117 
00118 private:
00119     std::vector<std::string> header_;
00120     std::vector<std::string::size_type> columnPositions_;
00121     Specification specification_;
00122     std::map<std::string, std::string> fieldTranslations_;
00123 
00124     void readHeaderFrom( const QString Headers );
00125     bool readLineFrom( const QString line, boost::shared_ptr<ManagedElement> element );
00126     void detectColumnPositionsFromLine( const std::string &line );
00127     void checkColumnPositionsFromSpecification( const std::string &line );
00128     void splitLineOnColumnPositions( const std::string &line, std::vector<std::string> &columns );
00129 };
00130 
00131 template<class ElementT>
00132 void FixedTable::readAllInto( QByteArray byteArray, std::vector< boost::shared_ptr< ElementT > > &elements )
00133 {
00134     QString fileContent(QString::fromLocal8Bit(byteArray));
00135     QStringList lines;
00136     QString patern("");
00137     if( specification_.platform() == Linux)
00138         patern= "\n";
00139 
00140     lines = fileContent.split(patern);
00141 
00142     if ( specification_.hasHeader() )
00143         readHeaderFrom( lines[0] );
00144 
00145     for(int i = 1;i < lines.size()-1; i++)
00146     {
00147         boost::shared_ptr<ElementT> element( new ElementT() );
00148         readLineFrom( lines[i], element );
00149         elements.push_back( element );
00150     }
00151 }
00152 
00153 }
00154 }
00155 
00156 #endif /* INCLUDED_CONFDUMP_SYSTEM_FIXEDTABLE_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends