blob: 21e58e62ce1d646d8cabcbd1dabeaab0ae5e4ac4 [file] [log] [blame]
Jeff Brown647925d2010-11-10 16:03:06 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#ifndef _UTILS_TOKENIZER_H
18#define _UTILS_TOKENIZER_H
19
20#include <assert.h>
21#include <utils/Errors.h>
Jeff Brownd36ec3a2010-11-19 13:13:07 -080022#include <utils/FileMap.h>
Jeff Brown647925d2010-11-10 16:03:06 -080023#include <utils/String8.h>
24
25namespace android {
26
27/**
28 * A simple tokenizer for loading and parsing ASCII text files line by line.
29 */
30class Tokenizer {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080031 Tokenizer(const String8& filename, FileMap* fileMap, const char* buffer, size_t length);
Jeff Brown647925d2010-11-10 16:03:06 -080032
33public:
34 ~Tokenizer();
35
36 /**
37 * Opens a file and maps it into memory.
38 *
39 * Returns NO_ERROR and a tokenizer for the file, if successful.
40 * Otherwise returns an error and sets outTokenizer to NULL.
41 */
42 static status_t open(const String8& filename, Tokenizer** outTokenizer);
43
44 /**
45 * Returns true if at the end of the file.
46 */
47 inline bool isEof() const { return mCurrent == getEnd(); }
48
49 /**
50 * Returns true if at the end of the line or end of the file.
51 */
52 inline bool isEol() const { return isEof() || *mCurrent == '\n'; }
53
54 /**
55 * Gets the name of the file.
56 */
57 inline String8 getFilename() const { return mFilename; }
58
59 /**
60 * Gets a 1-based line number index for the current position.
61 */
62 inline int32_t getLineNumber() const { return mLineNumber; }
63
64 /**
65 * Formats a location string consisting of the filename and current line number.
66 * Returns a string like "MyFile.txt:33".
67 */
68 String8 getLocation() const;
69
70 /**
71 * Gets the character at the current position.
72 * Returns null at end of file.
73 */
74 inline char peekChar() const { return isEof() ? '\0' : *mCurrent; }
75
76 /**
77 * Gets the remainder of the current line as a string, excluding the newline character.
78 */
79 String8 peekRemainderOfLine() const;
80
81 /**
82 * Gets the character at the current position and advances past it.
83 * Returns null at end of file.
84 */
85 inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); }
86
87 /**
88 * Gets the next token on this line stopping at the specified delimiters
89 * or the end of the line whichever comes first and advances past it.
90 * Also stops at embedded nulls.
91 * Returns the token or an empty string if the current character is a delimiter
92 * or is at the end of the line.
93 */
94 String8 nextToken(const char* delimiters);
95
96 /**
97 * Advances to the next line.
98 * Does nothing if already at the end of the file.
99 */
100 void nextLine();
101
102 /**
103 * Skips over the specified delimiters in the line.
104 * Also skips embedded nulls.
105 */
106 void skipDelimiters(const char* delimiters);
107
108private:
109 Tokenizer(const Tokenizer& other); // not copyable
110
111 String8 mFilename;
Jeff Brownd36ec3a2010-11-19 13:13:07 -0800112 FileMap* mFileMap;
Jeff Brown647925d2010-11-10 16:03:06 -0800113 const char* mBuffer;
114 size_t mLength;
115
116 const char* mCurrent;
117 int32_t mLineNumber;
118
119 inline const char* getEnd() const { return mBuffer + mLength; }
120
121};
122
123} // namespace android
124
125#endif // _UTILS_TOKENIZER_H