2 **********************************************************************
3 * Copyright (c) 2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: March 22 2004
9 **********************************************************************
16 TokenIterator::TokenIterator(TextFile
* r
) {
18 done
= haveLine
= FALSE
;
22 TokenIterator::~TokenIterator() {
25 UBool
TokenIterator::next(UnicodeString
& token
, UErrorCode
& ec
) {
26 if (done
|| U_FAILURE(ec
)) {
32 if (!reader
->readLineSkippingComments(line
, ec
)) {
40 if (!nextToken(token
, ec
)) {
42 if (U_FAILURE(ec
)) return FALSE
;
49 int32_t TokenIterator::getLineNumber() const {
50 return reader
->getLineNumber();
54 * Read the next token from 'this->line' and append it to 'token'.
55 * Tokens are separated by rule white space. Tokens may also be
56 * delimited by double or single quotes. The closing quote must match
57 * the opening quote. If a '#' is encountered, the rest of the line
58 * is ignored, unless it is backslash-escaped or within quotes.
59 * @param token the token is appended to this StringBuffer
60 * @param ec input-output error code
61 * @return TRUE if a valid token is found, or FALSE if the end
62 * of the line is reached or an error occurs
64 UBool
TokenIterator::nextToken(UnicodeString
& token
, UErrorCode
& ec
) {
65 ICU_Utility::skipWhitespace(line
, pos
, TRUE
);
66 if (pos
== line
.length()) {
69 UChar c
= line
.charAt(pos
++);
82 while (pos
< line
.length()) {
83 c
= line
.charAt(pos
); // 16-bit ok
84 if (c
== 92/*'\\'*/) {
85 UChar32 c32
= line
.unescapeAt(pos
);
87 ec
= U_MALFORMED_UNICODE_ESCAPE
;
91 } else if ((quote
!= 0 && c
== quote
) ||
92 (quote
== 0 && uprv_isRuleWhiteSpace(c
))) {
95 } else if (quote
== 0 && c
== '#') {
96 return TRUE
; // do NOT increment
103 ec
= U_UNTERMINATED_QUOTE
;