blob: c3ec1652b292a04fedda6db7f037113136263408 [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
Steven Moreland377adea2022-10-08 05:06:52 +000024#ifndef DEBUG_TOKENIZER
Jeff Brown647925d2010-11-10 16:03:06 -080025// Enables debug output for the tokenizer.
26#define DEBUG_TOKENIZER 0
Steven Moreland377adea2022-10-08 05:06:52 +000027#endif
Jeff Brown647925d2010-11-10 16:03:06 -080028
29namespace android {
30
31static inline bool isDelimiter(char ch, const char* delimiters) {
Yi Konge1731a42018-07-16 18:11:34 -070032 return strchr(delimiters, ch) != nullptr;
Jeff Brown647925d2010-11-10 16:03:06 -080033}
34
Jeff Brown2c1627d2012-04-17 18:19:50 -070035Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
36 bool ownBuffer, size_t length) :
Jeff Brown1d618d62010-12-02 13:50:46 -080037 mFilename(filename), mFileMap(fileMap),
Jeff Brown2c1627d2012-04-17 18:19:50 -070038 mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
39 mCurrent(buffer), mLineNumber(1) {
Jeff Brown647925d2010-11-10 16:03:06 -080040}
41
42Tokenizer::~Tokenizer() {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000043 delete mFileMap;
Jeff Brown2c1627d2012-04-17 18:19:50 -070044 if (mOwnBuffer) {
Jeff Brown1d618d62010-12-02 13:50:46 -080045 delete[] mBuffer;
Jeff Brownd36ec3a2010-11-19 13:13:07 -080046 }
Jeff Brown647925d2010-11-10 16:03:06 -080047}
48
49status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
Yi Konge1731a42018-07-16 18:11:34 -070050 *outTokenizer = nullptr;
Jeff Brown647925d2010-11-10 16:03:06 -080051
Elliott Hughes643268f2018-10-08 11:10:11 -070052 int result = OK;
Jeff Brown647925d2010-11-10 16:03:06 -080053 int fd = ::open(filename.string(), O_RDONLY);
54 if (fd < 0) {
55 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070056 ALOGE("Error opening file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080057 } else {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080058 struct stat stat;
59 if (fstat(fd, &stat)) {
Jeff Brown647925d2010-11-10 16:03:06 -080060 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070061 ALOGE("Error getting size of file '%s': %s", filename.string(), strerror(errno));
Jeff Brown647925d2010-11-10 16:03:06 -080062 } else {
63 size_t length = size_t(stat.st_size);
Jeff Brown647925d2010-11-10 16:03:06 -080064
Jeff Brown1d618d62010-12-02 13:50:46 -080065 FileMap* fileMap = new FileMap();
Jeff Brown2c1627d2012-04-17 18:19:50 -070066 bool ownBuffer = false;
Jeff Brown1d618d62010-12-02 13:50:46 -080067 char* buffer;
Yi Konge1731a42018-07-16 18:11:34 -070068 if (fileMap->create(nullptr, fd, 0, length, true)) {
Jeff Brown1d618d62010-12-02 13:50:46 -080069 fileMap->advise(FileMap::SEQUENTIAL);
70 buffer = static_cast<char*>(fileMap->getDataPtr());
71 } else {
Narayan Kamath6832a7a2015-02-23 15:43:35 +000072 delete fileMap;
Yi Konge1731a42018-07-16 18:11:34 -070073 fileMap = nullptr;
Jeff Brown1d618d62010-12-02 13:50:46 -080074
75 // Fall back to reading into a buffer since we can't mmap files in sysfs.
76 // The length we obtained from stat is wrong too (it will always be 4096)
77 // so we must trust that read will read the entire file.
78 buffer = new char[length];
Jeff Brown2c1627d2012-04-17 18:19:50 -070079 ownBuffer = true;
Jeff Brown1d618d62010-12-02 13:50:46 -080080 ssize_t nrd = read(fd, buffer, length);
81 if (nrd < 0) {
82 result = -errno;
Elliott Hughes6ed68cc2015-06-30 08:22:24 -070083 ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno));
Jeff Brown1d618d62010-12-02 13:50:46 -080084 delete[] buffer;
Yi Konge1731a42018-07-16 18:11:34 -070085 buffer = nullptr;
Jeff Brown1d618d62010-12-02 13:50:46 -080086 } else {
87 length = size_t(nrd);
Jeff Brown647925d2010-11-10 16:03:06 -080088 }
89 }
Jeff Brown1d618d62010-12-02 13:50:46 -080090
91 if (!result) {
Jeff Brown2c1627d2012-04-17 18:19:50 -070092 *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
Jeff Brownd36ec3a2010-11-19 13:13:07 -080093 }
Jeff Brown647925d2010-11-10 16:03:06 -080094 }
95 close(fd);
96 }
97 return result;
98}
99
Jeff Brown2c1627d2012-04-17 18:19:50 -0700100status_t Tokenizer::fromContents(const String8& filename,
101 const char* contents, Tokenizer** outTokenizer) {
Yi Konge1731a42018-07-16 18:11:34 -0700102 *outTokenizer = new Tokenizer(filename, nullptr,
Jeff Brown2c1627d2012-04-17 18:19:50 -0700103 const_cast<char*>(contents), false, strlen(contents));
104 return OK;
105}
106
Jeff Brown647925d2010-11-10 16:03:06 -0800107String8 Tokenizer::getLocation() const {
108 String8 result;
109 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
110 return result;
111}
112
113String8 Tokenizer::peekRemainderOfLine() const {
114 const char* end = getEnd();
115 const char* eol = mCurrent;
116 while (eol != end) {
117 char ch = *eol;
118 if (ch == '\n') {
119 break;
120 }
121 eol += 1;
122 }
123 return String8(mCurrent, eol - mCurrent);
124}
125
126String8 Tokenizer::nextToken(const char* delimiters) {
127#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000128 ALOGD("nextToken");
Jeff Brown647925d2010-11-10 16:03:06 -0800129#endif
130 const char* end = getEnd();
131 const char* tokenStart = mCurrent;
132 while (mCurrent != end) {
133 char ch = *mCurrent;
134 if (ch == '\n' || isDelimiter(ch, delimiters)) {
135 break;
136 }
137 mCurrent += 1;
138 }
139 return String8(tokenStart, mCurrent - tokenStart);
140}
141
142void Tokenizer::nextLine() {
143#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000144 ALOGD("nextLine");
Jeff Brown647925d2010-11-10 16:03:06 -0800145#endif
146 const char* end = getEnd();
147 while (mCurrent != end) {
148 char ch = *(mCurrent++);
149 if (ch == '\n') {
150 mLineNumber += 1;
151 break;
152 }
153 }
154}
155
156void Tokenizer::skipDelimiters(const char* delimiters) {
157#if DEBUG_TOKENIZER
Steve Blockeb095332011-12-20 16:23:08 +0000158 ALOGD("skipDelimiters");
Jeff Brown647925d2010-11-10 16:03:06 -0800159#endif
160 const char* end = getEnd();
161 while (mCurrent != end) {
162 char ch = *mCurrent;
163 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
164 break;
165 }
166 mCurrent += 1;
167 }
168}
169
170} // namespace android