libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/stringutil.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       stringutil.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #include <ostream>
00026 #include "stringutil.h"
00027 #include "NCstring.h"
00028 
00029 
00030 unsigned strutil::split( const std::string        line_tv,
00031                          std::vector<std::string> & words_Vtr,
00032                          const std::string        sep_tv,
00033                          const bool       singlesep_bv )
00034 {
00035     words_Vtr.clear();
00036 
00037     if ( line_tv.empty() )
00038         return words_Vtr.size();
00039 
00040     struct sepctrl
00041     {
00042         const std::string & sep_t;
00043         sepctrl( const std::string & sep_tv ) : sep_t( sep_tv ) {}
00044 
00045         // Note that '\0' ist neither Sep nor NonSep
00046         inline bool isSep( const char c )    const { return( sep_t.find( c ) != std::string::npos ); }
00047 
00048         inline bool isNonSep( const char c )    const { return( c && !isSep( c ) ); }
00049 
00050         inline void skipSep( const char *& p ) const { while ( isSep( *p ) ) ++p; }
00051 
00052         inline void skipNonSep( const char *& p ) const { while ( isNonSep( *p ) ) ++p; }
00053     };
00054 
00055     sepctrl      sep_Ci( sep_tv );
00056 
00057     const char * s_pci = line_tv.c_str();
00058 
00059     const char * c_pci = s_pci;
00060 
00061     // Start with c_pci at the beginning of the 1st field to add.
00062     // In singlesep the beginning might be equal to the next sep,
00063     // which makes an empty field before the sep.
00064     if ( !singlesep_bv && sep_Ci.isSep( *c_pci ) )
00065     {
00066         sep_Ci.skipSep( c_pci );
00067     }
00068 
00069     for ( s_pci = c_pci; *s_pci; s_pci = c_pci )
00070     {
00071         sep_Ci.skipNonSep( c_pci );
00072         words_Vtr.push_back( std::string( s_pci, c_pci - s_pci ) );
00073 
00074         if ( *c_pci )
00075         {
00076             if ( singlesep_bv )
00077             {
00078                 if ( !*( ++c_pci ) )
00079                 {
00080                     // line ends with a sep -> add the empty field behind
00081                     words_Vtr.push_back( "" );
00082                 }
00083             }
00084             else
00085                 sep_Ci.skipSep( c_pci );
00086         }
00087     }
00088 
00089     return words_Vtr.size();
00090 }
00091 
00092 namespace std
00093 {
00094     ostream & operator<<( ostream & stream, const wstring & text )
00095     {
00096         string utf8text;
00097         NCstring::RecodeFromWchar( text, "UTF-8", &utf8text );
00098 
00099         return stream << utf8text;
00100     }
00101 }
 All Classes Functions Variables