blob: 9251973ca1f03cc3dd20a81d55a51561aaeef24d [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
38
Jeff Brownd36ec3a2010-11-19 13:13:07 -080039Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap,
40 const char* buffer, size_t length) :
41 mFilename(filename), mFileMap(fileMap), mBuffer(buffer), mLength(length),
Jeff Brown647925d2010-11-10 16:03:06 -080042 mCurrent(buffer), mLineNumber(1) {
43}
44
45Tokenizer::~Tokenizer() {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080046 if (mFileMap) {
47 mFileMap->release();
48 }
Jeff Brown647925d2010-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 Brownd36ec3a2010-11-19 13:13:07 -080060 struct stat stat;
61 if (fstat(fd, &stat)) {
Jeff Brown647925d2010-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 Brownd36ec3a2010-11-19 13:13:07 -080066 FileMap* fileMap = new FileMap();
67 if (!fileMap->create(NULL, fd, 0, length, true)) {
68 result = NO_MEMORY;
Jeff Brown647925d2010-11-10 16:03:06 -080069 LOGE("Error mapping file '%s', %s.", filename.string(), strerror(errno));
70 } else {
Jeff Brownd36ec3a2010-11-19 13:13:07 -080071 fileMap->advise(FileMap::SEQUENTIAL);
Jeff Brown647925d2010-11-10 16:03:06 -080072
Jeff Brownd36ec3a2010-11-19 13:13:07 -080073 *outTokenizer = new Tokenizer(filename, fileMap,
74 static_cast<const char*>(fileMap->getDataPtr()), length);
Jeff Brown647925d2010-11-10 16:03:06 -080075 if (!*outTokenizer) {
76 result = NO_MEMORY;
77 LOGE("Error allocating tokenizer for file=%s.", filename.string());
Jeff Brown647925d2010-11-10 16:03:06 -080078 }
79 }
Jeff Brownd36ec3a2010-11-19 13:13:07 -080080 if (result) {
81 fileMap->release();
82 }
Jeff Brown647925d2010-11-10 16:03:06 -080083 }
84 close(fd);
85 }
86 return result;
87}
88
89String8 Tokenizer::getLocation() const {
90 String8 result;
91 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
92 return result;
93}
94
95String8 Tokenizer::peekRemainderOfLine() const {
96 const char* end = getEnd();
97 const char* eol = mCurrent;
98 while (eol != end) {
99 char ch = *eol;
100 if (ch == '\n') {
101 break;
102 }
103 eol += 1;
104 }
105 return String8(mCurrent, eol - mCurrent);
106}
107
108String8 Tokenizer::nextToken(const char* delimiters) {
109#if DEBUG_TOKENIZER
110 LOGD("nextToken");
111#endif
112 const char* end = getEnd();
113 const char* tokenStart = mCurrent;
114 while (mCurrent != end) {
115 char ch = *mCurrent;
116 if (ch == '\n' || isDelimiter(ch, delimiters)) {
117 break;
118 }
119 mCurrent += 1;
120 }
121 return String8(tokenStart, mCurrent - tokenStart);
122}
123
124void Tokenizer::nextLine() {
125#if DEBUG_TOKENIZER
126 LOGD("nextLine");
127#endif
128 const char* end = getEnd();
129 while (mCurrent != end) {
130 char ch = *(mCurrent++);
131 if (ch == '\n') {
132 mLineNumber += 1;
133 break;
134 }
135 }
136}
137
138void Tokenizer::skipDelimiters(const char* delimiters) {
139#if DEBUG_TOKENIZER
140 LOGD("skipDelimiters");
141#endif
142 const char* end = getEnd();
143 while (mCurrent != end) {
144 char ch = *mCurrent;
145 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
146 break;
147 }
148 mCurrent += 1;
149 }
150}
151
152} // namespace android