OSDN Git Service

だいぶline_counterを実装してみた感じで
authorMyun2 <myun2@nwhite.info>
Mon, 11 Oct 2010 05:48:15 +0000 (14:48 +0900)
committerMyun2 <myun2@nwhite.info>
Mon, 11 Oct 2010 05:48:15 +0000 (14:48 +0900)
roast/include/roast/lexical/string_rule.hpp
roast/include/roast/xml/roast_xml/rule_xml.hpp
roast/include/roast/xml/roast_xml/sax.hpp
roast/test/lexical_test/roast_xml/sax_test.hpp
roast/test/lexical_test/sax_test.cpp

index 322375d..efbe6d8 100644 (file)
@@ -26,13 +26,16 @@ namespace roast
                                template <typename _It, typename _Param>
                                bool analyze(_It& it, _Param& param)
                                {
-                                       if ( *it == '\r' )
-                                               param << _cr_t;
-                                       if ( *it == '\n' )
-                                               param << _lf_t;
-
                                        T t;
-                                       return t.analyze(it, param);
+                                       bool b = t.analyze(it, param);
+                                       
+                                       if ( b ){
+                                               if ( *it == '\r' )
+                                                       param << _cr_t;
+                                               else if ( *it == '\n' )
+                                                       param << _lf_t;
+                                       }
+                                       return b;
                                }
                                template <typename T, typename _Param>
                                bool generate(const T&, const _Param&){ return true; }
@@ -40,7 +43,30 @@ namespace roast
 
                        ////
 
-                       template <typename T>
+                       class line_counter_docbase
+                       {
+                       protected:
+                               unsigned int m_line_count;
+                       private:
+                               bool m_is_prev_cr;
+                       public:
+                               line_counter_docbase(){
+                                       m_line_count = 1;
+                                       m_is_prev_cr = false;
+                               }
+                               void operator << (const _lf_t_&){
+                                       if ( !m_is_prev_cr )
+                                               m_line_count++;
+                                       m_is_prev_cr = false;
+                               }
+                               void operator << (const _cr_t_&){
+                                       m_line_count++;
+                                       m_is_prev_cr = true;
+                               }
+
+                               unsigned int get_line(){ return m_line_count; }
+                       };
+                       /*template <typename T>
                        class line_counter_docbase_
                        {
                        protected:
@@ -53,7 +79,7 @@ namespace roast
                                        m_is_prev_cr = false;
                                }
                                void operator << (const _lf_t_&){
-                                       if ( m_prev_char != '\r' )
+                                       if ( m_is_prev_cr )
                                                m_line_count++;
                                        m_is_prev_cr = false;
                                }
@@ -61,7 +87,7 @@ namespace roast
                                        m_line_count++;
                                        m_is_prev_cr = true;
                                }
-                       };
+                       };*/
 
                        /////////////////////////////////////////////////////////////
 
@@ -153,7 +179,7 @@ namespace roast
                                unichar<' '>,
                                unichar<'\t'>,
                                chars::line_countable_crlf
-                       > line_countable_whitespace;
+                       > line_countable_whitespace, lc_whitespace;
                        typedef seq<line_countable_whitespace, optional_repeat<line_countable_whitespace> >
                                lc_requre__whitespace, _lcreqws, lcreqws;
                        typedef optional_repeat<line_countable_whitespace>
@@ -209,7 +235,7 @@ namespace roast
                                template <typename _It, typename _Param>
                                bool operator()(const _It& start_it, const _It& it, _Param& param)
                                {
-                                       T t;
+                                       line_countable<T> t;
                                        _It it_work = it;
                                        return t.analyze(it_work, param);
                                }
index f83c3d4..9d7911b 100644 (file)
@@ -225,22 +225,22 @@ namespace roast
                                seq<lesser, header_internal, greater>
                                header, xmldecl;
                
-                       //////////////////////////////////////////////////////
+                       ///////////////////////////////////////////////
 
-                       //      No Elements
-
-                       class noelems : public seq<lcnrqws, optional<comment>, lcnrqws> {};
+                       //      No Means (Whitespace and Comments)
+                       class nomeans : public seq<lcnrqws, optional<comment>, lcnrqws> {};
 
                        template <typename T>
-                       class noelems_ : public seq<noelems, T>{};
+                       class nomeans_ : public seq<nomeans, T>{};
+
+                       ////
 
                        //      XML Document Rule
-               
                        typedef ::roast::lexical::rule::seq
                        <
-                               rule::noelems_<rule::header>,
-                               rule::noelems_<rule::body>,
-                               rule::noelems
+                               rule::nomeans_<rule::header>,
+                               rule::nomeans_<rule::body>,
+                               rule::nomeans
                        > _rule;
                }
                
index 5277f39..dc8f149 100644 (file)
@@ -37,8 +37,8 @@ namespace roast
 
                ///////
 
-               template <typename _SaxCallback, bool _ValidateCheck=true>
-               class sax_impl_ : public _SaxCallback
+               template <typename _SaxCallback, bool _ValidateCheck=false, bool _DetailInfos=true>
+               class sax_impl_ : public _SaxCallback, public lexical::rule::line_counter_docbase
                {
                private:
                        int m_code;
@@ -59,6 +59,9 @@ namespace roast
                        sax_impl_(){ m_code = rule::pcode::_null; }
                        sax_impl_(_SaxCallback &sub) : _SaxCallback(sub){}
 
+                       void operator << (const lexical::rule::_lf_t_&){ lexical::rule::line_counter_docbase::operator <<(lexical::rule::_lf_t_()); }
+                       void operator << (const lexical::rule::_cr_t_&){ lexical::rule::line_counter_docbase::operator <<(lexical::rule::_cr_t_()); }
+
                        template <typename _It>
                        void operator << ( const ::std::pair<_It, _It> &p )
                        {
index c06a61a..1c27fd3 100644 (file)
@@ -74,5 +74,7 @@ public:
        void end_element( const sized_ccharbuf &s ){}
 };
 
-typedef sax_impl_<_test_sax_impl> test_sax_impl;
-typedef sax_impl_<_speed_test_sax_impl> speed_test_sax_impl;
\ No newline at end of file
+//typedef sax_impl_<_test_sax_impl> test_sax_impl;
+//typedef sax_impl_<_speed_test_sax_impl> speed_test_sax_impl;
+static sax_impl_<_test_sax_impl> test_sax_impl;
+static sax_impl_<_speed_test_sax_impl> speed_test_sax_impl;
index fdc6013..146b3b3 100644 (file)
@@ -21,7 +21,7 @@ int main()
 {
        try
        {
-               FILE* fp = fopen("big_xml.xml","r");
+               FILE* fp = fopen("temp.xml","r");
                char* work = new char [BUF_SIZE];
                memset(work,0,BUF_SIZE);
                fread(work,1,BUF_SIZE,fp);
@@ -35,8 +35,9 @@ int main()
 
                printf("start:%d\n", clock());
                sax sx;
-               sx.analyze((const char*)work, speed_test_sax_impl());
+               sx.analyze((const char*)work, speed_test_sax_impl);
                printf("end:%d\n", clock());
+               printf("lines:%d\n", speed_test_sax_impl.get_line());
 
                /*
                //printf(work);