yast2-storage

AsciiFile.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) [2004-2010] Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
00020  */
00021 
00022 
00023 #ifndef ASCII_FILE_H
00024 #define ASCII_FILE_H
00025 
00026 
00027 #include <string>
00028 #include <vector>
00029 #include <algorithm>
00030 
00031 
00032 namespace storage
00033 {
00034     using std::string;
00035     using std::vector;
00036 
00037 
00038     class AsciiFile
00039     {
00040     public:
00041 
00042         explicit AsciiFile(const char* name, bool remove_empty = false);
00043         explicit AsciiFile(const string& name, bool remove_empty = false);
00044 
00045         string name() const { return Name_C; }
00046 
00047         bool reload();
00048         bool save();
00049 
00050         void logContent() const;
00051 
00052         void append( const string& Line_Cv );
00053         void append( const vector<string>& Lines_Cv );
00054         void insert( unsigned int Before_iv, const string& Line_Cv );
00055         void clear();
00056         void remove( unsigned int Start_iv, unsigned int Cnt_iv );
00057         void replace( unsigned int Start_iv, unsigned int Cnt_iv,
00058                       const string& Line_Cv );
00059         void replace( unsigned int Start_iv, unsigned int Cnt_iv,
00060                       const vector<string>& Line_Cv );
00061 
00062         const string& operator []( unsigned int Index_iv ) const;
00063         string& operator []( unsigned int Index_iv );
00064 
00065         template <class Pred>
00066         int find_if_idx(Pred pred) const
00067         {
00068             vector<string>::const_iterator it = std::find_if(Lines_C.begin(), Lines_C.end(), pred);
00069             if (it == Lines_C.end())
00070                 return -1;
00071             return std::distance(Lines_C.begin(), it);
00072         }
00073 
00074         unsigned numLines() const { return Lines_C.size(); }
00075 
00076         vector<string>& lines() { return Lines_C; }
00077         const vector<string>& lines() const { return Lines_C; }
00078 
00079     protected:
00080 
00081         void removeLastIf(string& Text_Cr, char Char_cv) const;
00082 
00083         const string Name_C;
00084         const bool remove_empty;
00085 
00086         vector<string> Lines_C;
00087 
00088     };
00089 
00090 
00091     class SysconfigFile : protected AsciiFile
00092     {
00093     public:
00094 
00095         SysconfigFile(const char* name) : AsciiFile(name) {}
00096 
00097         bool getValue(const string& key, string& value) const;
00098 
00099     };
00100 
00101 
00102     class InstallInfFile : protected AsciiFile
00103     {
00104     public:
00105 
00106         InstallInfFile(const char* name) : AsciiFile(name) {}
00107 
00108         bool getValue(const string& key, string& value) const;
00109 
00110     };
00111 
00112 }
00113 
00114 
00115 #endif