2 * Copyright (C) 2010 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _UTILS_TOKENIZER_H
18 #define _UTILS_TOKENIZER_H
21 #include <utils/Errors.h>
22 #include <utils/FileMap.h>
23 #include <utils/String8.h>
28 * A simple tokenizer for loading and parsing ASCII text files line by line.
31 Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
32 bool ownBuffer, size_t length);
38 * Opens a file and maps it into memory.
40 * Returns NO_ERROR and a tokenizer for the file, if successful.
41 * Otherwise returns an error and sets outTokenizer to NULL.
43 static status_t open(const String8& filename, Tokenizer** outTokenizer);
46 * Prepares to tokenize the contents of a string.
48 * Returns NO_ERROR and a tokenizer for the string, if successful.
49 * Otherwise returns an error and sets outTokenizer to NULL.
51 static status_t fromContents(const String8& filename,
52 const char* contents, Tokenizer** outTokenizer);
55 * Returns true if at the end of the file.
57 inline bool isEof() const { return mCurrent == getEnd(); }
60 * Returns true if at the end of the line or end of the file.
62 inline bool isEol() const { return isEof() || *mCurrent == '\n'; }
65 * Gets the name of the file.
67 inline String8 getFilename() const { return mFilename; }
70 * Gets a 1-based line number index for the current position.
72 inline int32_t getLineNumber() const { return mLineNumber; }
75 * Formats a location string consisting of the filename and current line number.
76 * Returns a string like "MyFile.txt:33".
78 String8 getLocation() const;
81 * Gets the character at the current position.
82 * Returns null at end of file.
84 inline char peekChar() const { return isEof() ? '\0' : *mCurrent; }
87 * Gets the remainder of the current line as a string, excluding the newline character.
89 String8 peekRemainderOfLine() const;
92 * Gets the character at the current position and advances past it.
93 * Returns null at end of file.
95 inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); }
98 * Gets the next token on this line stopping at the specified delimiters
99 * or the end of the line whichever comes first and advances past it.
100 * Also stops at embedded nulls.
101 * Returns the token or an empty string if the current character is a delimiter
102 * or is at the end of the line.
104 String8 nextToken(const char* delimiters);
107 * Advances to the next line.
108 * Does nothing if already at the end of the file.
113 * Skips over the specified delimiters in the line.
114 * Also skips embedded nulls.
116 void skipDelimiters(const char* delimiters);
119 Tokenizer(const Tokenizer& other); // not copyable
127 const char* mCurrent;
130 inline const char* getEnd() const { return mBuffer + mLength; }
134 } // namespace android
136 #endif // _UTILS_TOKENIZER_H