00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "text/StringTokenizer.h"
00025 #include "text/strings.h"
00026 #include <iostream>
00027
00028 using namespace std;
00029
00030 namespace Tanl {
00031 namespace Text {
00032
00033 StringTokenizer::StringTokenizer(istream& is, char const* delim) :
00034 count(0),
00035 lineNumber(0),
00036 is(&is),
00037 tok(0),
00038 toklen(0),
00039 rest(0),
00040 end(0),
00041 delim(delim)
00042 { }
00043
00044 StringTokenizer::StringTokenizer(char const* s, char const* end,
00045 char const* delim) :
00046 count(0),
00047 lineNumber(0),
00048 is(0),
00049 tok(0),
00050 toklen(0),
00051 rest(s),
00052 end(end ? end : s + ::strlen(s)),
00053 delim(delim)
00054 { }
00055
00056 char const* StringTokenizer::next()
00057 {
00058 if (hasNext()) {
00059 char const* res = tok;
00060 tok = 0;
00061 return res;
00062 }
00063 return 0;
00064 }
00065
00066 char const* StringTokenizer::hasNext()
00067 {
00068 if (tok)
00069 return tok;
00070 count += toklen;
00071 char const* start = rest;
00072 if (rest && rest < end) {
00073 if ((tok = next_token(rest, end, delim))) {
00074 toklen = rest - tok;
00075 count += tok - start;
00076 return tok;
00077 }
00078 } else if (is) {
00079 if (count) count++;
00080 while (is->getline(line, MAX_LINE_LEN)) {
00081 lineNumber++;
00082 start = rest = line;
00083 end = rest + is->gcount() - 1;
00084 if ((tok = next_token(rest, end, delim))) {
00085 toklen = rest - tok;
00086 count += tok - start;
00087 return tok;
00088 }
00089 }
00090 }
00091 count += end - start;
00092 return 0;
00093 }
00094
00095 }
00096 }