Author: dmacvicar
Date: Sun May 6 18:05:09 2007
New Revision: 5498
URL: http://svn.opensuse.org/viewcvs/zypp?rev=5498&view=rev
Log:
r5239@piscolita: duncan | 2007-05-02 19:07:55 +0200
Custom made ini parser, adapt test to compile
Added:
trunk/libzypp/zypp/parser/IniParser.cc
trunk/libzypp/zypp/parser/IniParser.h
Modified:
trunk/ (props changed)
trunk/libzypp/tests/parser/inifile/iniparser_test.cc
Modified: trunk/libzypp/tests/parser/inifile/iniparser_test.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/tests/parser/inifile/iniparser_test.cc?rev=5498&r1=5497&r2=5498&view=diff
==============================================================================
--- trunk/libzypp/tests/parser/inifile/iniparser_test.cc (original)
+++ trunk/libzypp/tests/parser/inifile/iniparser_test.cc Sun May 6 18:05:09 2007
@@ -8,7 +8,8 @@
#include
#include "zypp/base/Logger.h"
-#include "zypp/parser/inifile/iniparser.h"
+#include "zypp/base/InputStream.h"
+#include "zypp/parser/IniParser.h"
#include "zypp/Url.h"
#include "zypp/PathInfo.h"
@@ -16,24 +17,27 @@
using std::endl;
using std::string;
using namespace zypp;
+using namespace zypp::parser;
using namespace boost::unit_test;
-
-void ini_read_test(const string &dir)
+class IniTest : public IniParser
{
- dictionary *d = iniparser_new((Pathname(dir)+"/1.ini").c_str());
-
- if ( d == NULL )
- ZYPP_THROW(Exception("Failed creating dictionary"));
-
- int n = iniparser_getnsec(d);
- BOOST_CHECK_EQUAL( n, 6 );
- MIL << n << endl;
-
- for ( int i = 0; i < n; i++ )
+ virtual void consume( const std::string §ion )
{
- MIL << iniparser_getsecname(d, i) << endl;
+ MIL << section << endl;
}
+
+ virtual void consume( const std::string §ion, const std::string &key, const std::string &value )
+ {
+ MIL << section << " | " << key << " | " << value << endl;
+ }
+};
+
+void ini_read_test(const string &dir)
+{
+ InputStream is((Pathname(dir)+"/1.ini"));
+ IniTest parser;
+ parser.parse(is);
}
test_suite*
Added: trunk/libzypp/zypp/parser/IniParser.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/parser/IniParser.cc?rev=5498&view=auto
==============================================================================
--- trunk/libzypp/zypp/parser/IniParser.cc (added)
+++ trunk/libzypp/zypp/parser/IniParser.cc Sun May 6 18:05:09 2007
@@ -0,0 +1,131 @@
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/parser/IniParser.cc
+ *
+*/
+#include <iostream>
+#include <sstream>
+
+#include
+
+#include "zypp/base/Logger.h"
+#include "zypp/base/String.h"
+#include "zypp/base/IOStream.h"
+
+#include "zypp/parser/IniParser.h"
+#include "zypp/ProgressData.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+namespace parser
+{ /////////////////////////////////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////
+//
+// METHOD NAME : IniParser::IniParser
+// METHOD TYPE : Ctor
+//
+IniParser::IniParser()
+ : _line_nr(0)
+{}
+
+///////////////////////////////////////////////////////////////////
+//
+// METHOD NAME : IniParser::~IniParser
+// METHOD TYPE : Dtor
+//
+IniParser::~IniParser()
+{}
+
+void IniParser::beginParse()
+{}
+
+void IniParser::consume( const std::string §ion, const std::string &key, const std::string &value )
+{}
+
+void IniParser::consume( const std::string §ion )
+{}
+
+void IniParser::endParse()
+{}
+
+void dumpRegexpResults( const boost::smatch &what )
+{
+ for ( unsigned int k=0; k < what.size(); k++)
+ {
+ DBG << "[match "<< k << "] [" << what[k] << "]" << std::endl;
+ }
+}
+
+///////////////////////////////////////////////////////////////////
+//
+// METHOD NAME : IniParser::parse
+// METHOD TYPE : void
+//
+void IniParser::parse( const InputStream & input_r )
+{
+ boost::regex rxSection("^\\[(.+)\\]$");
+ boost::regex rxKeyValue("^(.+)[[:space:]]*=[[:space:]]*(.+)$");
+
+ MIL << "Start parsing " << input_r << endl;
+ _inputname = input_r.name();
+ beginParse();
+
+ ProgressData ticks( makeProgressData( input_r ) );
+ ticks.toMin();
+
+ iostr::EachLine line( input_r );
+ for ( ; line; line.next() )
+ {
+ std::string trimmed = str::trim(*line);
+ const char *where = trimmed.c_str(); /* Skip leading spaces */
+ if (*where==';' || *where=='#' || *where==0)
+ continue ; /* Comment lines */
+ else
+ {
+ if (*where=='[' )
+ {
+ boost::smatch what;
+ if(boost::regex_match(trimmed, what, rxSection, boost::match_extra))
+ {
+ //dumpRegexpResults(what);
+ std::string section = what[1];
+ consume(section);
+ section.swap(_current_section);
+ }
+ }
+ else
+ {
+ boost::smatch what;
+ if(boost::regex_match(trimmed, what, rxKeyValue, boost::match_extra))
+ {
+ //dumpRegexpResults(what);
+ consume( _current_section, what[1], what[2] );
+ }
+ }
+ }
+ ticks.set( input_r.stream().tellg() );
+ }
+ ticks.toMax();
+
+ endParse();
+ _inputname.clear();
+ MIL << "Done parsing " << input_r << endl;
+}
+
+/////////////////////////////////////////////////////////////////
+} // namespace parser
+///////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
Added: trunk/libzypp/zypp/parser/IniParser.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/libzypp/zypp/parser/IniParser.h?rev=5498&view=auto
==============================================================================
--- trunk/libzypp/zypp/parser/IniParser.h (added)
+++ trunk/libzypp/zypp/parser/IniParser.h Sun May 6 18:05:09 2007
@@ -0,0 +1,84 @@
+/*---------------------------------------------------------------------\
+| ____ _ __ __ ___ |
+| |__ / \ / / . \ . \ |
+| / / \ V /| _/ _/ |
+| / /__ | | | | | | |
+| /_____||_| |_| |_| |
+| |
+\---------------------------------------------------------------------*/
+/** \file zypp/parser/IniParser.h
+ *
+*/
+#ifndef ZYPP_PARSER_INIPARSER_H
+#define ZYPP_PARSER_INIPARSER_H
+
+#include <iosfwd>
+#include <string>
+#include <list>
+
+#include "zypp/base/PtrTypes.h"
+#include "zypp/base/NonCopyable.h"
+#include "zypp/base/InputStream.h"
+
+#include "zypp/parser/tagfile/ParseException.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+namespace parser
+{ /////////////////////////////////////////////////////////////////
+
+using tagfile::ParseException;
+
+///////////////////////////////////////////////////////////////////
+//
+// CLASS NAME : IniParser
+//
+/** Basic SUSEtags parser.
+ * Will replace parser/tagfile/ and parser/taggedfile/ stuff.
+*/
+class IniParser : private base::NonCopyable
+{
+public:
+ /** Default ctor */
+ IniParser();
+ /** Dtor */
+ virtual ~IniParser();
+ /** Parse the stream.
+ * \throw ParseExcetion on errors. Invoke \ref consume
+ * for each tag. \ref consume might throw other exceptions
+ * as well.
+ */
+ void parse( const InputStream & imput_r );
+
+public:
+ /** Called when start parsing. */
+ virtual void beginParse();
+ /** Called when a section is found. */
+ virtual void consume( const std::string §ion );
+ /** Called when a key value is found. */
+ virtual void consume( const std::string §ion, const std::string &key, const std::string &value );
+ /** Called when the parse is done. */
+ virtual void endParse();
+public:
+ /** Name of the current InputStream. */
+ const std::string & inputname() const
+ {
+ return _inputname;
+ }
+
+private:
+ std::string _inputname;
+ std::string _current_section;
+ int _line_nr;
+ //ProgressData _ticks;
+};
+
+/////////////////////////////////////////////////////////////////
+} // namespace parser
+///////////////////////////////////////////////////////////////////
+/////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_PARSER_INIPARSER_H
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org