/ 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.
Expand |
Embed | Plain Text
// Enumerator that marches thru a string of strings such as "aaa\0bbb\0ccc\0\0" that has "\0" delimiters and a "\0\0" terminator. // modeled upon IEnumerator in dot.net: has Reset, MoveNext etc. class CSzEnumerator { private: LPCSTR s; // beginning of string. LPCSTR p; // enumerator. public: /*constructor*/ CSzEnumerator( LPCSTR arg=0 ) { Reset(arg); } void Reset( LPCSTR arg=0 ) { if ( arg ) { s = arg; } p = (LPCSTR)s; // handle empty string condition to prevent starting enumeration. if ( !*p ) { p = 0; } } LPCSTR Current() { return p; } void MoveNext() { p += strlen(p) + 1; if ( !*p ) { p = 0; } } }; // CSzEnumerator
You need to login to post a comment.
