blob: 7067533b10b75adbfeeca38e2a9e2085db9e68c3 [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() {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080046 if (mFileMap) {
47 mFileMap->release();
Jeff Brown2c1627d2012-04-17 18:19:50 -070048 }
49 if (mOwnBuffer) {
Jeff Brown1d618d62010-12-02 13:50:46 -080050 delete[] mBuffer;
Jeff Brownd36ec3a2010-11-19 13:13:07 -080051 }
Jeff Brown647925d2010-11-10 16:03:06 -080052}
53
54status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
55 *outTokenizer = NULL;
56
57 int result = NO_ERROR;
58 int fd = ::open(filename.string(), O_RDONLY);
59 if (fd < 0) {
60 result = -errno;
Steve Block1b781ab2012-01-06 19:20:56 +000061 ALOGE("Error opening file '%s', %s.", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080062 } else {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080063 struct stat stat;
64 if (fstat(fd, &stat)) {
Jeff Brown647925d2010-11-10 16:03:06 -080065 result = -errno;
Steve Block1b781ab2012-01-06 19:20:56 +000066 ALOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080067 } else {
68 size_t length = size_t(stat.st_size);
Jeff Brown647925d2010-11-10 16:03:06 -080069
Jeff Brown1d618d62010-12-02 13:50:46 -080070 FileMap* fileMap = new FileMap();
Jeff Brown2c1627d2012-04-17 18:19:50 -070071 bool ownBuffer = false;
Jeff Brown1d618d62010-12-02 13:50:46 -080072 char* buffer;
73 if (fileMap->create(NULL, fd, 0, length, true)) {
74 fileMap->advise(FileMap::SEQUENTIAL);
75 buffer = static_cast<char*>(fileMap->getDataPtr());
76 } else {
77 fileMap->release();
78 fileMap = NULL;
79
80 // Fall back to reading into a buffer since we can't mmap files in sysfs.
81 // The length we obtained from stat is wrong too (it will always be 4096)
82 // so we must trust that read will read the entire file.
83 buffer = new char[length];
Jeff Brown2c1627d2012-04-17 18:19:50 -070084 ownBuffer = true;
Jeff Brown1d618d62010-12-02 13:50:46 -080085 ssize_t nrd = read(fd, buffer, length);
86 if (nrd < 0) {
87 result = -errno;
Steve Block1b781ab2012-01-06 19:20:56 +000088 ALOGE("Error reading file '%s', %s.", filename.string(), strerror(errno));
Jeff Brown1d618d62010-12-02 13:50:46 -080089 delete[] buffer;
90 buffer = NULL;
91 } else {
92 length = size_t(nrd);
Jeff Brown647925d2010-11-10 16:03:06 -080093 }
94 }
Jeff Brown1d618d62010-12-02 13:50:46 -080095
96 if (!result) {
Jeff Brown2c1627d2012-04-17 18:19:50 -070097 *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
Jeff Brownd36ec3a2010-11-19 13:13:07 -080098 }
Jeff Brown647925d2010-11-10 16:03:06 -080099 }
100 close(fd);
101 }
102 return result;
103}
104
Jeff Brown2c1627d2012-04-17 18:19:50 -0700105status_t Tokenizer::fromContents(const String8& filename,
106 const char* contents, Tokenizer** outTokenizer) {
107 *outTokenizer = new Tokenizer(filename, NULL,
108 const_cast<char*>(contents), false, strlen(contents));
109 return OK;
110}
111
Jeff Brown647925d2010-11-10 16:03:06 -0800112String8 Tokenizer::getLocation() const {
113 String8 result;
114 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
115 return result;
116}
117
118String8 Tokenizer::peekRemainderOfLine() const {
119 const char* end = getEnd();
120 const char* eol = mCurrent;
121 while (eol != end) {
122 char ch = *eol;
123 if (ch == '\n') {
124 break;
125 }
126 eol += 1;
127 }
128 return String8(mCurrent, eol - mCurrent);
129}
130
131String8 Tokenizer::nextToken(const char* delimiters) {
132#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000133 ALOGD("nextToken");
Jeff Brown647925d2010-11-10 16:03:06 -0800134#endif
135 const char* end = getEnd();
136 const char* tokenStart = mCurrent;
137 while (mCurrent != end) {
138 char ch = *mCurrent;
139 if (ch == '\n' || isDelimiter(ch, delimiters)) {
140 break;
141 }
142 mCurrent += 1;
143 }
144 return String8(tokenStart, mCurrent - tokenStart);
145}
146
147void Tokenizer::nextLine() {
148#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000149 ALOGD("nextLine");
Jeff Brown647925d2010-11-10 16:03:06 -0800150#endif
151 const char* end = getEnd();
152 while (mCurrent != end) {
153 char ch = *(mCurrent++);
154 if (ch == '\n') {
155 mLineNumber += 1;
156 break;
157 }
158 }
159}
160
161void Tokenizer::skipDelimiters(const char* delimiters) {
162#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000163 ALOGD("skipDelimiters");
Jeff Brown647925d2010-11-10 16:03:06 -0800164#endif
165 const char* end = getEnd();
166 while (mCurrent != end) {
167 char ch = *mCurrent;
168 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
169 break;
170 }
171 mCurrent += 1;
172 }
173}
174
175} // namespace android