1: #include <iostream>
   2: #include <string>
   3: #include <vector>
   4:
   5: #if defined ( DEBUG )
   6: #define DEBUG_CODE( code_to_put_into_source ) { code_to_put_into_source }
   7: #else
   8: #define DEBUG_CODE( code_to_put_into_source )
   9: #endif
  10:
  11: #include <libintl.h>
  12: #define _(String) gettext (String)
  13: #include <locale.h> // for setlocale --> see > man 3 setlocale 
  14:
  15: using namespace std;
  16:
  17:
  18: // -----------------------------------------------------------
  19: // 
  20: // -----------------------------------------------------------
  21: template <typename Container>
  22: inline void
  23: stringtok( Container &container, std::string const & in,
  24:            const char * const delimiters = " \t\n\r\f\v" )
  25: {
  26:     const std::string::size_type len = in.length();
  27:           std::string::size_type i = 0;
  28: 
  29:     while ( i < len )
  30:         {
  31:         // eat leading whitespace
  32:         i = in.find_first_not_of( delimiters, i );
  33:         if ( i == std::string::npos )
  34:             {
  35:             return;   // nothing left but white space
  36:             }
  37: 
  38:         // find the end of the token
  39:         std::string::size_type j = in.find_first_of( delimiters, i );
  40: 
  41:         // push token
  42:         if ( j == std::string::npos )
  43:             {
  44:             container.push_back( in.substr(i) );
  45:             return;
  46:             } 
  47:         else
  48:             {
  49:             container.push_back( in.substr( i, j-i ) );
  50:             }
  51: 
  52:         // set up for next loop
  53:         i = j + 1;
  54:         }
  55: }
  56: 
  57: // -----------------------------------------------------------
  58: // 
  59: // -----------------------------------------------------------
  60: int main( void )
  61: {
  62:     // Important settings for translation BEGIN
  63:     char * lang = setlocale ( LC_MESSAGES, "" );
  64:     cerr << "Lang = " << lang << endl;
  65:     bindtextdomain ( "stringtoken", "/usr/share/locale" );
  66:     textdomain ( "stringtoken" );
  67:     // Important settings for translation END
  68:     
  69:     std::vector<std::string> vec_tokens;
  70:     const std::string string2parse = _("Today was my birthday. I am 16 years old.");
  71:     
  72:     stringtok( vec_tokens, string2parse, " \n" );
  73:     
  74:     cout << "\n\nString = " << string2parse << "\n\nTokens:\n\n";
  75:     
  76:     ostream_iterator<string> out_iter(cout, "\n" );
  77:     copy( vec_tokens.begin(), vec_tokens.end(), out_iter );
  78:     cout << endl;
  79:     
  80: return 0;   
  81: }
  82: 
  83: