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

xtl_container.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 
00037 #ifndef _CTTL_XTL_CONTAINER_H_INCLUDED_
00038 #define _CTTL_XTL_CONTAINER_H_INCLUDED_
00039 
00040 #include "xtl_container_impl.h"
00041 #include "xtl_identity_functors.h"
00042 
00043 namespace cttl_impl {
00044 
00050 const int xtl_flag_default = 0;
00051 
00057 const int xtl_flag_runtime_find = 1;
00058 
00064 const int xtl_flag_runtime_bang_find = 2;
00065 
00075 template< typename StringT = CTTL_STD_STRING >
00076 class xtl_text_container : public xtl_text_container_impl< StringT >
00077 {
00078 public:
00079 
00081     typedef typename xtl_text_container_impl< StringT >::string_T string_T;
00082 
00084     typedef typename xtl_text_container_impl< StringT >::char_T char_T;
00085 
00097     xtl_bitflags m_flags;
00098 
00099 
00101     // Constructors                                                                 //
00103 
00104 public:
00106     xtl_text_container(
00107         xtl_bitflags::value_type flags_ = xtl_flag_default,
00108         size_t vector_size_ = xtl_identity_vector_default_size )
00109         :
00110         xtl_text_container_impl< StringT >( vector_size_ ),
00111         m_flags( flags_ )
00112     {
00113     }
00114 
00116     template< typename CharT >
00117     xtl_text_container(
00118         CharT const* pchar_,
00119         xtl_bitflags::value_type flags_ = xtl_flag_default,
00120         size_t vector_size_ = xtl_identity_vector_default_size
00121         )
00122         :
00123         xtl_text_container_impl< std::basic_string< CharT > >( pchar_, vector_size_ ),
00124         m_flags( flags_ )
00125     {
00126     }
00127 
00129     xtl_text_container(
00130         StringT const& str_,
00131         xtl_bitflags::value_type flags_ = xtl_flag_default,
00132         size_t vector_size_ = xtl_identity_vector_default_size
00133         )
00134         :
00135         xtl_text_container_impl< StringT >( str_, vector_size_ ),
00136         m_flags( flags_ )
00137     {
00138     }
00139 
00140 private:
00141     /*Copy constructor is disabled due to complexity of @c xtl_text_container object.*/
00142     explicit xtl_text_container( xtl_text_container< StringT > const& )
00143         :
00144         xtl_text_container_impl< StringT >( xtl_identity_vector_default_size )
00145     {
00146     }
00147 
00148     /*Direct assignment is disabled due to complexity of @c xtl_text_container object.*/
00149     xtl_text_container const& operator=( xtl_text_container< StringT > const& )
00150     {
00151         return *this;
00152     }
00153 
00154 public:
00159     std::vector< size_t >& identity_vector()
00160     {
00161         return this->m_identity_vector;
00162     }
00163 
00168     std::vector< size_t >& offset_stack()
00169     {
00170         return this->m_offset_stack;
00171     }
00172 
00180     size_t identity_vector_size() const
00181     {
00182         return this->m_identity_vector.size();
00183     }
00184     
00198     size_t identity_vector_size( size_t new_size_ )
00199     {
00200         if ( new_size_ ) {
00201             this->m_identity_vector.resize( new_size_ );
00202             return this->m_identity_vector.size();
00203         }
00204 
00205         this->m_identity_vector.clear();
00206         return 0;
00207     }
00208 
00222     size_t identity_vector_push_back( size_t offset_ )
00223     {
00224         this->m_identity_vector.push_back( offset_ );
00225         return this->m_identity_vector.size() - 1;
00226     }
00227 
00229 // Functions to store, access, and maintain encapsulated text
00231 public:
00233     void text( char_T const* pchar_ )
00234     {
00235         if ( pchar_ )
00236             this->m_client_str = pchar_;
00237         else
00238             this->m_client_str.clear();
00239         
00240         this->identity_zero_all();
00241     }
00242     
00244     void text( StringT const& str_ )
00245     {
00246         this->m_client_str = str_;
00247         this->identity_zero_all();
00248     }
00249 
00250 
00252     StringT const&  text() const
00253     {
00254         return this->m_client_str;
00255     }
00256 
00258     StringT& text()
00259     {
00260         return this->m_client_str;
00261     }
00262 
00264     // Functions to manipulate substrings
00266 public:
00268     void text_tolower( int from_id_, int to_id_ )
00269     {
00270         typename StringT::iterator first = this->m_client_str.begin() + identity_offset( from_id_ );
00271         typename StringT::iterator last = this->m_client_str.begin() + identity_offset( to_id_ );
00272         
00273         std::transform(
00274             first,
00275             last,
00276             first,
00277             tolower
00278             );
00279     }
00280     
00282     void text_toupper( int from_id_, int to_id_ )
00283     {
00284         typename StringT::iterator first = this->m_client_str.begin() + identity_offset( from_id_ );
00285         typename StringT::iterator last = this->m_client_str.begin() + identity_offset( to_id_ );
00286         
00287         std::transform(
00288             first,
00289             last,
00290             first,
00291             toupper
00292             );
00293     }
00294     
00295 
00297     StringT text_substring( int from_id_, int to_id_ ) const
00298     {
00299         assert( this->m_identity_vector[ from_id_ ] + this->m_identity_vector[ to_id_ ] - this->m_identity_vector[ from_id_ ] <= this->m_client_str.length() );
00300 
00301         if ( this->m_identity_vector[ from_id_ ] == this->m_identity_vector[ to_id_ ] )
00302             return StringT();
00303 
00304         return this->m_client_str.substr(
00305             this->m_identity_vector[ from_id_ ],
00306             this->m_identity_vector[ to_id_ ] - this->m_identity_vector[ from_id_ ]
00307             );
00308     }
00309 
00311     StringT text_absolute_substring( size_t from_offset_, size_t to_offset_ ) const
00312     {
00313         assert( from_offset_ + to_offset_ - from_offset_ <= this->m_client_str.length() );
00314 
00315         if ( from_offset_ == to_offset_ )
00316             return StringT();
00317 
00318         return this->m_client_str.substr(
00319             from_offset_,
00320             to_offset_ - from_offset_
00321             );
00322     }
00323 
00325     // Functions to manipulate identities
00327 
00333     size_t identity_go_bof( int id_ )
00334     {
00335         return this->m_identity_vector[ id_ ] = 0;
00336     }
00337 
00343     size_t identity_go_eof( int id_ )
00344     {
00345         return this->m_identity_vector[ id_ ] = this->m_client_str.length();
00346     }
00347 
00349     size_t identity_offset( int id_ ) const
00350     {
00351         return this->m_identity_vector[ id_ ];
00352     }
00353 
00357     size_t identity_go_offset( int id_, size_t offset_ )
00358     {
00359         /* returns old offset_ */
00360         size_t old_offset = this->m_identity_vector[ id_ ];
00361         this->m_identity_vector[ id_ ] = offset_;
00362         return old_offset;
00363     }
00364 
00370     size_t identity_add_offset( int id_, size_t offset_ )
00371     {
00372         return this->m_identity_vector[ id_ ] += offset_;
00373     }
00374 
00376     void push_offset( size_t x_ )
00377     {
00378         this->m_offset_stack.push_back( x_ );
00379     }
00380 
00386     size_t pop_offset()
00387     {
00388         size_t temp = this->m_offset_stack.back();
00389         this->m_offset_stack.pop_back();
00390         return temp;
00391     }
00392 
00400     size_t push_identity( int id_ )
00401     {
00402         this->m_offset_stack.push_back( this->m_identity_vector[ id_ ] );
00403         return this->m_offset_stack.size() - 1;
00404     }
00405 
00407     void pop_identity()
00408     {
00409         this->m_offset_stack.pop_back();
00410     }
00411 
00417     size_t pop_identity( int id_ )
00418     {
00419         this->m_identity_vector[ id_ ] = this->m_offset_stack.back();
00420         this->m_offset_stack.pop_back();
00421         return this->m_identity_vector[ id_ ];
00422     }
00423 
00425     size_t stack_size() const
00426     {
00427         return this->m_offset_stack.size();
00428     }
00429 
00431     size_t stack_offset( int sp_ ) const
00432     {
00433         return this->m_offset_stack[ sp_ ];
00434     }
00435 
00437     void stack_offset( int sp_, size_t offset_ )
00438     {
00439         this->m_offset_stack[ sp_ ] = offset_;
00440     }
00441 
00443     void identity_assign( int target_i_, int source_i_ )
00444     {
00445         this->m_identity_vector[ target_i_ ] = this->m_identity_vector[ source_i_ ];
00446     }
00447 
00455     size_t identity_get_line( int id_ ) const
00456     {
00457         return offset_2_line( identity_offset( id_ ) );
00458     }
00459 
00466     size_t identity_go_line( int id_, int line_ )
00467     {
00468         return this->m_identity_vector[ id_ ] = this->line_2_offset( line_ );
00469     }
00470 
00477     size_t identity_go_next_line( int id_ )
00478     {
00479         return this->m_identity_vector[ id_ ] = offset_2_next_line_offset( identity_offset( id_ ) );
00480     }
00481 
00488     size_t identity_go_line_home( int id_ )
00489     {
00490         return this->m_identity_vector[ id_ ] = offset_2_line_home_offset( identity_offset( id_ ) );
00491     }
00492 
00499     size_t identity_go_line_end( int id_ )
00500     {
00501         return this->m_identity_vector[ id_ ] = offset_2_line_end_offset( identity_offset( id_ ) );
00502     }
00503 
00504 };  // class xtl_text_container
00505 
00506 }   // namespace cttl_impl
00507 
00508 #endif // _CTTL_XTL_CONTAINER_H_INCLUDED_
00509 
00510 
00511 

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