blob: b3445b797f758820ca3f9a592cf4b6fa4ad69cc3 [file] [log] [blame]
Jeff Brown6b53e8d2010-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 Brown6b53e8d2010-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 Brown90655042010-12-02 13:50:46 -080038Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, size_t length) :
39 mFilename(filename), mFileMap(fileMap),
40 mBuffer(buffer), mLength(length), mCurrent(buffer), mLineNumber(1) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -080041}
42
43Tokenizer::~Tokenizer() {
Jeff Browna6210d02010-11-19 13:13:07 -080044 if (mFileMap) {
45 mFileMap->release();
Jeff Brown90655042010-12-02 13:50:46 -080046 } else {
47 delete[] mBuffer;
Jeff Browna6210d02010-11-19 13:13:07 -080048 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080049}
50
51status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
52 *outTokenizer = NULL;
53
54 int result = NO_ERROR;
55 int fd = ::open(filename.string(), O_RDONLY);
56 if (fd < 0) {
57 result = -errno;
58 LOGE("Error opening file '%s', %s.", filename.string(), strerror(errno));
59 } else {
Jeff Browna6210d02010-11-19 13:13:07 -080060 struct stat stat;
61 if (fstat(fd, &stat)) {
Jeff Brown6b53e8d2010-11-10 16:03:06 -080062 result = -errno;
63 LOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno));
64 } else {
65 size_t length = size_t(stat.st_size);
Jeff Brown6b53e8d2010-11-10 16:03:06 -080066
Jeff Brown90655042010-12-02 13:50:46 -080067 FileMap* fileMap = new FileMap();
68 char* buffer;
69 if (fileMap->create(NULL, fd, 0, length, true)) {
70 fileMap->advise(FileMap::SEQUENTIAL);
71 buffer = static_cast<char*>(fileMap->getDataPtr());
72 } else {
73 fileMap->release();
74 fileMap = NULL;
75
76 // Fall back to reading into a buffer since we can't mmap files in sysfs.
77 // The length we obtained from stat is wrong too (it will always be 4096)
78 // so we must trust that read will read the entire file.
79 buffer = new char[length];
80 ssize_t nrd = read(fd, buffer, length);
81 if (nrd < 0) {
82 result = -errno;
83 LOGE("Error reading file '%s', %s.", filename.string(), strerror(errno));
84 delete[] buffer;
85 buffer = NULL;
86 } else {
87 length = size_t(nrd);
Jeff Brown6b53e8d2010-11-10 16:03:06 -080088 }
89 }
Jeff Brown90655042010-12-02 13:50:46 -080090
91 if (!result) {
92 *outTokenizer = new Tokenizer(filename, fileMap, buffer, length);
Jeff Browna6210d02010-11-19 13:13:07 -080093 }
Jeff Brown6b53e8d2010-11-10 16:03:06 -080094 }
95 close(fd);
96 }
97 return result;
98}
99
100String8 Tokenizer::getLocation() const {
101 String8 result;
102 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
103 return result;
104}
105
106String8 Tokenizer::peekRemainderOfLine() const {
107 const char* end = getEnd();
108 const char* eol = mCurrent;
109 while (eol != end) {
110 char ch = *eol;
111 if (ch == '\n') {
112 break;
113 }
114 eol += 1;
115 }
116 return String8(mCurrent, eol - mCurrent);
117}
118
119String8 Tokenizer::nextToken(const char* delimiters) {
120#if DEBUG_TOKENIZER
121 LOGD("nextToken");
122#endif
123 const char* end = getEnd();
124 const char* tokenStart = mCurrent;
125 while (mCurrent != end) {
126 char ch = *mCurrent;
127 if (ch == '\n' || isDelimiter(ch, delimiters)) {
128 break;
129 }
130 mCurrent += 1;
131 }
132 return String8(tokenStart, mCurrent - tokenStart);
133}
134
135void Tokenizer::nextLine() {
136#if DEBUG_TOKENIZER
137 LOGD("nextLine");
138#endif
139 const char* end = getEnd();
140 while (mCurrent != end) {
141 char ch = *(mCurrent++);
142 if (ch == '\n') {
143 mLineNumber += 1;
144 break;
145 }
146 }
147}
148
149void Tokenizer::skipDelimiters(const char* delimiters) {
150#if DEBUG_TOKENIZER
151 LOGD("skipDelimiters");
152#endif
153 const char* end = getEnd();
154 while (mCurrent != end) {
155 char ch = *mCurrent;
156 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
157 break;
158 }
159 mCurrent += 1;
160 }
161}
162
163} // namespace android