2
* Copyright (C) 2010 The Android Open Source Project
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
#define LOG_TAG "Tokenizer"
23
#include <sys/types.h>
25
#include <utils/Log.h>
26
#include <utils/Tokenizer.h>
28
// Enables debug output for the tokenizer.
29
#define DEBUG_TOKENIZER 0
34
static inline bool isDelimiter(char ch, const char* delimiters) {
35
return strchr(delimiters, ch) != NULL;
38
Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
39
bool ownBuffer, size_t length) :
40
mFilename(filename), mFileMap(fileMap),
41
mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
42
mCurrent(buffer), mLineNumber(1) {
45
Tokenizer::~Tokenizer() {
54
status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
57
int result = NO_ERROR;
58
int fd = ::open(filename.string(), O_RDONLY);
61
ALOGE("Error opening file '%s', %s.", filename.string(), strerror(errno));
64
if (fstat(fd, &stat)) {
66
ALOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno));
68
size_t length = size_t(stat.st_size);
70
FileMap* fileMap = new FileMap();
71
bool ownBuffer = false;
73
if (fileMap->create(NULL, fd, 0, length, true)) {
74
fileMap->advise(FileMap::SEQUENTIAL);
75
buffer = static_cast<char*>(fileMap->getDataPtr());
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];
85
ssize_t nrd = read(fd, buffer, length);
88
ALOGE("Error reading file '%s', %s.", filename.string(), strerror(errno));
97
*outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
105
status_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));
112
String8 Tokenizer::getLocation() const {
114
result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
118
String8 Tokenizer::peekRemainderOfLine() const {
119
const char* end = getEnd();
120
const char* eol = mCurrent;
128
return String8(mCurrent, eol - mCurrent);
131
String8 Tokenizer::nextToken(const char* delimiters) {
135
const char* end = getEnd();
136
const char* tokenStart = mCurrent;
137
while (mCurrent != end) {
139
if (ch == '\n' || isDelimiter(ch, delimiters)) {
144
return String8(tokenStart, mCurrent - tokenStart);
147
void Tokenizer::nextLine() {
151
const char* end = getEnd();
152
while (mCurrent != end) {
153
char ch = *(mCurrent++);
161
void Tokenizer::skipDelimiters(const char* delimiters) {
163
ALOGD("skipDelimiters");
165
const char* end = getEnd();
166
while (mCurrent != end) {
168
if (ch == '\n' || !isDelimiter(ch, delimiters)) {
175
} // namespace android