home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / strsplit.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  2KB  |  94 lines

  1.  
  2.  
  3.  
  4.  
  5. /*
  6.  *
  7.  *          Copyright (C) 1994, M. A. Sridhar
  8.  *  
  9.  *
  10.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  11.  *     to copy, modify or distribute this software  as you see fit,
  12.  *     and to use  it  for  any  purpose, provided   this copyright
  13.  *     notice and the following   disclaimer are included  with all
  14.  *     copies.
  15.  *
  16.  *                        DISCLAIMER
  17.  *
  18.  *     The author makes no warranties, either expressed or implied,
  19.  *     with respect  to  this  software, its  quality, performance,
  20.  *     merchantability, or fitness for any particular purpose. This
  21.  *     software is distributed  AS IS.  The  user of this  software
  22.  *     assumes all risks  as to its quality  and performance. In no
  23.  *     event shall the author be liable for any direct, indirect or
  24.  *     consequential damages, even if the  author has been  advised
  25.  *     as to the possibility of such damages.
  26.  *
  27.  */
  28.  
  29.  
  30.  
  31.  
  32. #ifdef __GNUC__
  33. #pragma implementation
  34. #endif
  35.  
  36.  
  37.  
  38. #include "base/strsplit.h"
  39.  
  40.  
  41. CL_StringSplitter::CL_StringSplitter (const CL_String& s, const char* fld)
  42. : _string (s), _index (-1), _fieldSeps (fld)
  43. {
  44. }
  45.  
  46. void CL_StringSplitter::Reset ()
  47. {
  48.     _index = -1;
  49. }
  50.  
  51.  
  52.  
  53. CL_String CL_StringSplitter::Next (const char* fld)
  54. {
  55.     if (_index >= _string.Length())
  56.         return "";
  57.     if (fld)
  58.         _fieldSeps = fld;
  59.     long spos = _string.NCharIndex (_fieldSeps.AsPtr(), _index+1);
  60.     if (spos < 0) {
  61.         _index = _string.Length();
  62.         return "";
  63.     }
  64.     long epos = _string.CharIndex (_fieldSeps.AsPtr(), spos);
  65.     if (epos < 0) {
  66.         _index = _string.Length()-1;
  67.         return CL_String (_string._string + spos);
  68.     }
  69.     _index = epos-1;
  70.     return CL_String (_string._string + spos, epos-spos);
  71. }
  72.  
  73. CL_String CL_StringSplitter::Remaining ()
  74. {
  75.     long l = _index+1;
  76.     _index = _string.Length()-1;
  77.     return CL_String (_string._string + l);
  78. }
  79.  
  80. CL_String CL_StringSplitter::Scan (const char* char_set)
  81. {
  82.     if (!char_set || _index >= _string.Length())
  83.         return  "";
  84.     long l = _string.NCharIndex (char_set, _index+1);
  85.     if (l > _index) {
  86.         CL_String tmp = ((CL_String&)_string) (_index, l - _index);
  87.         _index = l-1;
  88.         return tmp;
  89.     }
  90.     return "";
  91. }
  92.  
  93.  
  94.