Your Ad Here

Posted By

jimfred on 04/27/09


Tagged


Versions (?)

CSzEnumerator


 / Published in: C++
 

This is an enumerator that marches through a string of strings such as "aaa\0bbb\0ccc\0\0" that has "\0" delimiters and a "\0\0" terminator. It's modeled upon IEnumerator in dot.net (has Reset, MoveNext etc).

To simplify, use technique in MoveNext that increments a pointer as follows: p += strlen(p) + 1; if ( !*p ) { p = 0; }

This is useful for Win32 REGMULTISZ (multi-string) values that use \0 delimiters and \0\0 terminators such as GetCommandLine() and registry APIs.

  1. // Enumerator that marches thru a string of strings such as "aaa\0bbb\0ccc\0\0" that has "\0" delimiters and a "\0\0" terminator.
  2. // modeled upon IEnumerator in dot.net: has Reset, MoveNext etc.
  3. class CSzEnumerator
  4. {
  5. private:
  6. LPCSTR s; // beginning of string.
  7. LPCSTR p; // enumerator.
  8.  
  9. public:
  10. /*constructor*/ CSzEnumerator( LPCSTR arg=0 )
  11. {
  12. Reset(arg);
  13. }
  14.  
  15. void Reset( LPCSTR arg=0 )
  16. {
  17. if ( arg ) { s = arg; }
  18.  
  19. p = (LPCSTR)s;
  20.  
  21. // handle empty string condition to prevent starting enumeration.
  22. if ( !*p ) { p = 0; }
  23. }
  24.  
  25. LPCSTR Current()
  26. {
  27. return p;
  28. }
  29.  
  30. void MoveNext()
  31. {
  32. p += strlen(p) + 1;
  33.  
  34. if ( !*p ) { p = 0; }
  35. }
  36.  
  37. }; // CSzEnumerator

Report this snippet  

You need to login to post a comment.