blob: b68a2cfa3e68c9fe4510a56c2ad51a10fd2f50f6 [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#define LOG_TAG "Tokenizer"
18
Mathias Agopian22dbf392017-02-28 15:06:51 -080019#include <utils/Tokenizer.h>
Jeff Brown647925d2010-11-10 16:03:06 -080020#include <fcntl.h>
Jeff Brown647925d2010-11-10 16:03:06 -080021#include <sys/stat.h>
Jeff Brown647925d2010-11-10 16:03:06 -080022#include <utils/Log.h>
Jeff Brown647925d2010-11-10 16:03:06 -080023
24// Enables debug output for the tokenizer.
25#define DEBUG_TOKENIZER 0
26
27
28namespace android {
29
30static inline bool isDelimiter(char ch, const char* delimiters) {
31 return strchr(delimiters, ch) != NULL;
32}
33
Jeff Brown2c1627d2012-04-17 18:19:50 -070034Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
35 bool ownBuffer, size_t length) :
Jeff Brown1d618d62010-12-02 13:50:46 -080036 mFilename(filename), mFileMap(fileMap),
Jeff Brown2c1627d2012-04-17 18:19:50 -070037 mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
38 mCurrent(buffer), mLineNumber(1) {
Jeff Brown647925d2010-11-10 16:03:06 -080039}
40
41Tokenizer::~Tokenizer() {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000042 delete mFileMap;
Jeff Brown2c1627d2012-04-17 18:19:50 -070043 if (mOwnBuffer) {
Jeff Brown1d618d62010-12-02 13:50:46 -080044 delete[] mBuffer;
Jeff Brownd36ec3a2010-11-19 13:13:07 -080045 }
Jeff Brown647925d2010-11-10 16:03:06 -080046}
47
48status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
49 *outTokenizer = NULL;
50
51 int result = NO_ERROR;
52 int fd = ::open(filename.string(), O_RDONLY);
53 if (fd < 0) {
54 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070055 ALOGE("Error opening file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080056 } else {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080057 struct stat stat;
58 if (fstat(fd, &stat)) {
Jeff Brown647925d2010-11-10 16:03:06 -080059 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070060 ALOGE("Error getting size of file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080061 } else {
62 size_t length = size_t(stat.st_size);
Jeff Brown647925d2010-11-10 16:03:06 -080063
Jeff Brown1d618d62010-12-02 13:50:46 -080064 FileMap* fileMap = new FileMap();
Jeff Brown2c1627d2012-04-17 18:19:50 -070065 bool ownBuffer = false;
Jeff Brown1d618d62010-12-02 13:50:46 -080066 char* buffer;
67 if (fileMap->create(NULL, fd, 0, length, true)) {
68 fileMap->advise(FileMap::SEQUENTIAL);
69 buffer = static_cast<char*>(fileMap->getDataPtr());
70 } else {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000071 delete fileMap;
Jeff Brown1d618d62010-12-02 13:50:46 -080072 fileMap = NULL;
73
74 // Fall back to reading into a buffer since we can't mmap files in sysfs.
75 // The length we obtained from stat is wrong too (it will always be 4096)
76 // so we must trust that read will read the entire file.
77 buffer = new char[length];
Jeff Brown2c1627d2012-04-17 18:19:50 -070078 ownBuffer = true;
Jeff Brown1d618d62010-12-02 13:50:46 -080079 ssize_t nrd = read(fd, buffer, length);
80 if (nrd < 0) {
81 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070082 ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno));
Jeff Brown1d618d62010-12-02 13:50:46 -080083 delete[] buffer;
84 buffer = NULL;
85 } else {
86 length = size_t(nrd);
Jeff Brown647925d2010-11-10 16:03:06 -080087 }
88 }
Jeff Brown1d618d62010-12-02 13:50:46 -080089
90 if (!result) {
Jeff Brown2c1627d2012-04-17 18:19:50 -070091 *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
Jeff Brownd36ec3a2010-11-19 13:13:07 -080092 }
Jeff Brown647925d2010-11-10 16:03:06 -080093 }
94 close(fd);
95 }
96 return result;
97}
98
Jeff Brown2c1627d2012-04-17 18:19:50 -070099status_t Tokenizer::fromContents(const String8& filename,
100 const char* contents, Tokenizer** outTokenizer) {
101 *outTokenizer = new Tokenizer(filename, NULL,
102 const_cast<char*>(contents), false, strlen(contents));
103 return OK;
104}
105
Jeff Brown647925d2010-11-10 16:03:06 -0800106String8 Tokenizer::getLocation() const {
107 String8 result;
108 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
109 return result;
110}
111
112String8 Tokenizer::peekRemainderOfLine() const {
113 const char* end = getEnd();
114 const char* eol = mCurrent;
115 while (eol != end) {
116 char ch = *eol;
117 if (ch == '\n') {
118 break;
119 }
120 eol += 1;
121 }
122 return String8(mCurrent, eol - mCurrent);
123}
124
125String8 Tokenizer::nextToken(const char* delimiters) {
126#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000127 ALOGD("nextToken");
Jeff Brown647925d2010-11-10 16:03:06 -0800128#endif
129 const char* end = getEnd();
130 const char* tokenStart = mCurrent;
131 while (mCurrent != end) {
132 char ch = *mCurrent;
133 if (ch == '\n' || isDelimiter(ch, delimiters)) {
134 break;
135 }
136 mCurrent += 1;
137 }
138 return String8(tokenStart, mCurrent - tokenStart);
139}
140
141void Tokenizer::nextLine() {
142#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000143 ALOGD("nextLine");
Jeff Brown647925d2010-11-10 16:03:06 -0800144#endif
145 const char* end = getEnd();
146 while (mCurrent != end) {
147 char ch = *(mCurrent++);
148 if (ch == '\n') {
149 mLineNumber += 1;
150 break;
151 }
152 }
153}
154
155void Tokenizer::skipDelimiters(const char* delimiters) {
156#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000157 ALOGD("skipDelimiters");
Jeff Brown647925d2010-11-10 16:03:06 -0800158#endif
159 const char* end = getEnd();
160 while (mCurrent != end) {
161 char ch = *mCurrent;
162 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
163 break;
164 }
165 mCurrent += 1;
166 }
167}
168
169} // namespace android