summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/SysTokenizer.h (plain)
blob: af16f34c6b71dd1c6b57036d47f2efccfa37b34e
1/*
2 * Copyright (C) 2011 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 * @author Tellen Yu
16 * @version 1.0
17 * @date 2014/11/04
18 * @par function description:
19 * - 1 parse file use tokenizer
20 */
21
22#ifndef _SYS_TOKENIZER_H
23#define _SYS_TOKENIZER_H
24
25#include <assert.h>
26
27#define MAX_STR_LEN 4096
28
29/**
30 * A simple tokenizer for loading and parsing ASCII text files line by line.
31 */
32class SysTokenizer {
33public:
34 SysTokenizer(const char*filename, char* buffer,
35 bool ownBuffer, size_t length);
36
37 ~SysTokenizer();
38
39 /**
40 * Opens a file and maps it into memory.
41 *
42 * Returns NO_ERROR and a tokenizer for the file, if successful.
43 * Otherwise returns an error and sets outTokenizer to NULL.
44 */
45 static int open(const char*filename, SysTokenizer** outTokenizer);
46
47 /**
48 * Prepares to tokenize the contents of a string.
49 *
50 * Returns NO_ERROR and a tokenizer for the string, if successful.
51 * Otherwise returns an error and sets outTokenizer to NULL.
52 */
53 static int fromContents(const char*filename,
54 const char* contents, SysTokenizer** outTokenizer);
55
56 /**
57 * Returns true if at the end of the file.
58 */
59 inline bool isEof() const { return mCurrent == getEnd(); }
60
61 /**
62 * Returns true if at the end of the line or end of the file.
63 */
64 inline bool isEol() const { return isEof() || *mCurrent == '\n'; }
65
66 /**
67 * Gets the name of the file.
68 */
69 inline char* getFilename() const { return const_cast<char*>(mFilename); }
70
71 /**
72 * Gets a 1-based line number index for the current position.
73 */
74 inline int32_t getLineNumber() const { return mLineNumber; }
75
76 /**
77 * Formats a location string consisting of the filename and current line number.
78 * Returns a string like "MyFile.txt:33".
79 */
80 char* getLocation() const;
81
82 /**
83 * Gets the character at the current position.
84 * Returns null at end of file.
85 */
86 inline char peekChar() const { return isEof() ? '\0' : *mCurrent; }
87
88 /**
89 * Gets the remainder of the current line as a string, excluding the newline character.
90 */
91 char* peekRemainderOfLine() const;
92
93 /**
94 * Gets the character at the current position and advances past it.
95 * Returns null at end of file.
96 */
97 inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); }
98
99 /**
100 * Gets the next token on this line stopping at the specified delimiters
101 * or the end of the line whichever comes first and advances past it.
102 * Also stops at embedded nulls.
103 * Returns the token or an empty string if the current character is a delimiter
104 * or is at the end of the line.
105 */
106 char* nextToken(const char* delimiters);
107
108 /**
109 * Advances to the next line.
110 * Does nothing if already at the end of the file.
111 */
112 void nextLine();
113
114 /**
115 * Skips over the specified delimiters in the line.
116 * Also skips embedded nulls.
117 */
118 void skipDelimiters(const char* delimiters);
119
120private:
121 const char* mFilename;
122 char* mBuffer;
123 bool mOwnBuffer;
124 size_t mLength;
125
126 const char* mCurrent;
127 int32_t mLineNumber;
128
129 char mStrs[MAX_STR_LEN];
130 char mLine[MAX_STR_LEN];
131 inline const char* getEnd() const { return mBuffer + mLength; }
132
133};
134
135#endif // _SYS_TOKENIZER_H
136