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