blob: 2d0e83dcdee44ab6b14101867e9e325e3845fcc9 [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
19#include <stdlib.h>
20#include <unistd.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
Jeff Brown647925d2010-11-10 16:03:06 -080025#include <utils/Log.h>
26#include <utils/Tokenizer.h>
27
28// Enables debug output for the tokenizer.
29#define DEBUG_TOKENIZER 0
30
31
32namespace android {
33
34static inline bool isDelimiter(char ch, const char* delimiters) {
35 return strchr(delimiters, ch) != NULL;
36}
37
Jeff Brown2c1627d2012-04-17 18:19:50 -070038Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
39 bool ownBuffer, size_t length) :
Jeff Brown1d618d62010-12-02 13:50:46 -080040 mFilename(filename), mFileMap(fileMap),
Jeff Brown2c1627d2012-04-17 18:19:50 -070041 mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
42 mCurrent(buffer), mLineNumber(1) {
Jeff Brown647925d2010-11-10 16:03:06 -080043}
44
45Tokenizer::~Tokenizer() {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000046 delete mFileMap;
Jeff Brown2c1627d2012-04-17 18:19:50 -070047 if (mOwnBuffer) {
Jeff Brown1d618d62010-12-02 13:50:46 -080048 delete[] mBuffer;
Jeff Brownd36ec3a2010-11-19 13:13:07 -080049 }
Jeff Brown647925d2010-11-10 16:03:06 -080050}
51
52status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
53 *outTokenizer = NULL;
54
55 int result = NO_ERROR;
56 int fd = ::open(filename.string(), O_RDONLY);
57 if (fd < 0) {
58 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070059 ALOGE("Error opening file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080060 } else {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080061 struct stat stat;
62 if (fstat(fd, &stat)) {
Jeff Brown647925d2010-11-10 16:03:06 -080063 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070064 ALOGE("Error getting size of file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080065 } else {
66 size_t length = size_t(stat.st_size);
Jeff Brown647925d2010-11-10 16:03:06 -080067
Jeff Brown1d618d62010-12-02 13:50:46 -080068 FileMap* fileMap = new FileMap();
Jeff Brown2c1627d2012-04-17 18:19:50 -070069 bool ownBuffer = false;
Jeff Brown1d618d62010-12-02 13:50:46 -080070 char* buffer;
71 if (fileMap->create(NULL, fd, 0, length, true)) {
72 fileMap->advise(FileMap::SEQUENTIAL);
73 buffer = static_cast<char*>(fileMap->getDataPtr());
74 } else {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000075 delete fileMap;
Jeff Brown1d618d62010-12-02 13:50:46 -080076 fileMap = NULL;
77
78 // Fall back to reading into a buffer since we can't mmap files in sysfs.
79 // The length we obtained from stat is wrong too (it will always be 4096)
80 // so we must trust that read will read the entire file.
81 buffer = new char[length];
Jeff Brown2c1627d2012-04-17 18:19:50 -070082 ownBuffer = true;
Jeff Brown1d618d62010-12-02 13:50:46 -080083 ssize_t nrd = read(fd, buffer, length);
84 if (nrd < 0) {
85 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070086 ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno));
Jeff Brown1d618d62010-12-02 13:50:46 -080087 delete[] buffer;
88 buffer = NULL;
89 } else {
90 length = size_t(nrd);
Jeff Brown647925d2010-11-10 16:03:06 -080091 }
92 }
Jeff Brown1d618d62010-12-02 13:50:46 -080093
94 if (!result) {
Jeff Brown2c1627d2012-04-17 18:19:50 -070095 *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
Jeff Brownd36ec3a2010-11-19 13:13:07 -080096 }
Jeff Brown647925d2010-11-10 16:03:06 -080097 }
98 close(fd);
99 }
100 return result;
101}
102
Jeff Brown2c1627d2012-04-17 18:19:50 -0700103status_t Tokenizer::fromContents(const String8& filename,
104 const char* contents, Tokenizer** outTokenizer) {
105 *outTokenizer = new Tokenizer(filename, NULL,
106 const_cast<char*>(contents), false, strlen(contents));
107 return OK;
108}
109
Jeff Brown647925d2010-11-10 16:03:06 -0800110String8 Tokenizer::getLocation() const {
111 String8 result;
112 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
113 return result;
114}
115
116String8 Tokenizer::peekRemainderOfLine() const {
117 const char* end = getEnd();
118 const char* eol = mCurrent;
119 while (eol != end) {
120 char ch = *eol;
121 if (ch == '\n') {
122 break;
123 }
124 eol += 1;
125 }
126 return String8(mCurrent, eol - mCurrent);
127}
128
129String8 Tokenizer::nextToken(const char* delimiters) {
130#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000131 ALOGD("nextToken");
Jeff Brown647925d2010-11-10 16:03:06 -0800132#endif
133 const char* end = getEnd();
134 const char* tokenStart = mCurrent;
135 while (mCurrent != end) {
136 char ch = *mCurrent;
137 if (ch == '\n' || isDelimiter(ch, delimiters)) {
138 break;
139 }
140 mCurrent += 1;
141 }
142 return String8(tokenStart, mCurrent - tokenStart);
143}
144
145void Tokenizer::nextLine() {
146#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000147 ALOGD("nextLine");
Jeff Brown647925d2010-11-10 16:03:06 -0800148#endif
149 const char* end = getEnd();
150 while (mCurrent != end) {
151 char ch = *(mCurrent++);
152 if (ch == '\n') {
153 mLineNumber += 1;
154 break;
155 }
156 }
157}
158
159void Tokenizer::skipDelimiters(const char* delimiters) {
160#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000161 ALOGD("skipDelimiters");
Jeff Brown647925d2010-11-10 16:03:06 -0800162#endif
163 const char* end = getEnd();
164 while (mCurrent != end) {
165 char ch = *mCurrent;
166 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
167 break;
168 }
169 mCurrent += 1;
170 }
171}
172
173} // namespace android