Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

xtl_container_impl.h

Go to the documentation of this file.
00001 
00002 // Common Text Transformation Library
00003 // Copyright (C) 1997-2006 by Igor Kholodov. 
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the
00017 // Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 // mailto:cttl@users.sourceforge.net
00021 // http://sourceforge.net/projects/cttl/
00023 
00036 #ifndef _CTTL_XTL_CONTAINER_IMPL_H_INCLUDED_
00037 #define _CTTL_XTL_CONTAINER_IMPL_H_INCLUDED_
00038 
00039 #include "coreutils.h"
00040 #include "xtl_bitflags.h"
00041 #include <vector>
00042 
00043 namespace cttl_impl {
00044 
00046 const int xtl_identity_vector_default_size = 0;
00047 
00057 template< typename StringT = CTTL_STD_STRING >
00058 class xtl_text_container_impl
00059 {
00060 protected:
00062     typedef StringT string_T;
00063 
00065     typedef typename string_T::value_type char_T;
00066 
00068     StringT m_client_str;
00069 
00071     std::vector< size_t > m_offset_stack;
00072 
00074     std::vector< size_t > m_identity_vector;
00075 
00077 // Constructors                                                                 //
00079 protected:
00085     explicit xtl_text_container_impl( size_t vector_size_ )
00086         :
00087         m_identity_vector( vector_size_ )
00088     {
00089         identity_zero_all();
00090     }
00091 
00093     explicit xtl_text_container_impl(
00094         char_T const* pchar_,
00095         size_t vector_size_
00096         )
00097         :
00098         m_identity_vector( vector_size_ )
00099     {
00100         identity_zero_all();
00101 
00102         if ( pchar_ )
00103             m_client_str = pchar_;
00104     }
00105 
00107     explicit xtl_text_container_impl(
00108         StringT const& str_,
00109         size_t vector_size_
00110         )
00111         :
00112         m_client_str( str_ ),
00113         m_identity_vector( vector_size_ )
00114     {
00115         identity_zero_all();
00116     }
00117 
00118 protected:
00120     void identity_zero_all()
00121     {
00122         for ( size_t i = 0; i < m_identity_vector.size(); ++i )
00123             m_identity_vector[ i ] = 0;
00124     }
00125 
00127 // Internal offset/line calculations //
00129 protected:
00130 
00146     size_t line_2_offset( int line_ ) const
00147     {
00148         size_t offset = 0;
00149         while ( --line_ ) {
00150             offset = m_client_str.find( char_T( '\n' ), offset );   // find lf
00151             if ( offset == StringT::npos )
00152                 return m_client_str.length();
00153             ++offset;   // position next to lf
00154         }
00155         return offset;
00156     }
00157     
00164     size_t offset_2_line( size_t offset_ ) const
00165     {
00166         if ( !m_client_str.length() )   // string is empty
00167             return 1;
00168         return 1 + std::count( m_client_str.begin(), m_client_str.begin() + offset_, char_T( '\n' ) );
00169     }
00170     
00177     size_t offset_2_next_line_offset( size_t offset_ ) const
00178     {
00179         offset_ = m_client_str.find( char_T( '\n' ), offset_ ); // find next lf
00180         if ( offset_ == StringT::npos )
00181             return m_client_str.length();
00182         
00183         return ++offset_;   // position next to lf
00184     }
00185     
00197     size_t offset_2_line_home_offset( size_t offset_ ) const
00198     {
00199         if ( m_client_str[ offset_ ] == char_T( '\n' ) )
00200             return offset_; // stay where you are
00201 
00202         offset_ = m_client_str.rfind( char_T( '\n' ), offset_ );    // find previous lf
00203         if ( offset_ == StringT::npos )
00204             return 0;
00205         
00206         return ++offset_;   // position next to lf
00207     }
00208     
00220     size_t offset_2_line_end_offset( size_t offset_ ) const
00221     {
00222         static const char_T end_of_line[] = { '\r', '\n', 0x00 };
00223         offset_ = m_client_str.find_first_of( end_of_line, offset_ );
00224         if ( offset_ == StringT::npos )
00225             return m_client_str.length();
00226         
00227         return offset_;
00228     }
00229 
00230 };  // class xtl_text_container_impl
00231 
00232 }//namespace cttl_impl
00233 
00234 #endif // _CTTL_XTL_CONTAINER_IMPL_H_INCLUDED_
00235 
00236 

Generated on Thu Nov 2 17:44:06 2006 for Common Text Transformation Library by  doxygen 1.3.9.1