blob: 19dadf08d55791381f1109c17b166477017e0c11 [file] [log] [blame]
Jeff Browna3477c82010-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>
25#include <sys/mman.h>
26#include <utils/Log.h>
27#include <utils/Tokenizer.h>
28
29// Enables debug output for the tokenizer.
30#define DEBUG_TOKENIZER 0
31
32
33namespace android {
34
35static inline bool isDelimiter(char ch, const char* delimiters) {
36 return strchr(delimiters, ch) != NULL;
37}
38
39
40Tokenizer::Tokenizer(const String8& filename, const char* buffer, size_t length) :
41 mFilename(filename), mBuffer(buffer), mLength(length),
42 mCurrent(buffer), mLineNumber(1) {
43}
44
45Tokenizer::~Tokenizer() {
46 munmap((void*)mBuffer, mLength);
47}
48
49status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
50 *outTokenizer = NULL;
51
52 int result = NO_ERROR;
53 int fd = ::open(filename.string(), O_RDONLY);
54 if (fd < 0) {
55 result = -errno;
56 LOGE("Error opening file '%s', %s.", filename.string(), strerror(errno));
57 } else {
58 struct stat64 stat;
59 if (fstat64(fd, &stat)) {
60 result = -errno;
61 LOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno));
62 } else {
63 size_t length = size_t(stat.st_size);
64 void* buffer = mmap(NULL, length, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
65 if (buffer == MAP_FAILED) {
66 result = -errno;
67 LOGE("Error mapping file '%s', %s.", filename.string(), strerror(errno));
68 } else {
69 if (madvise(buffer, length, MADV_SEQUENTIAL)) {
70 LOGW("Error calling madvise for mmapped file '%s', %s.", filename.string(),
71 strerror(errno));
72 }
73
74 *outTokenizer = new Tokenizer(filename, static_cast<const char*>(buffer), length);
75 if (!*outTokenizer) {
76 result = NO_MEMORY;
77 LOGE("Error allocating tokenizer for file=%s.", filename.string());
78 munmap(buffer, length);
79 }
80 }
81 }
82 close(fd);
83 }
84 return result;
85}
86
87String8 Tokenizer::getLocation() const {
88 String8 result;
89 result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
90 return result;
91}
92
93String8 Tokenizer::peekRemainderOfLine() const {
94 const char* end = getEnd();
95 const char* eol = mCurrent;
96 while (eol != end) {
97 char ch = *eol;
98 if (ch == '\n') {
99 break;
100 }
101 eol += 1;
102 }
103 return String8(mCurrent, eol - mCurrent);
104}
105
106String8 Tokenizer::nextToken(const char* delimiters) {
107#if DEBUG_TOKENIZER
108 LOGD("nextToken");
109#endif
110 const char* end = getEnd();
111 const char* tokenStart = mCurrent;
112 while (mCurrent != end) {
113 char ch = *mCurrent;
114 if (ch == '\n' || isDelimiter(ch, delimiters)) {
115 break;
116 }
117 mCurrent += 1;
118 }
119 return String8(tokenStart, mCurrent - tokenStart);
120}
121
122void Tokenizer::nextLine() {
123#if DEBUG_TOKENIZER
124 LOGD("nextLine");
125#endif
126 const char* end = getEnd();
127 while (mCurrent != end) {
128 char ch = *(mCurrent++);
129 if (ch == '\n') {
130 mLineNumber += 1;
131 break;
132 }
133 }
134}
135
136void Tokenizer::skipDelimiters(const char* delimiters) {
137#if DEBUG_TOKENIZER
138 LOGD("skipDelimiters");
139#endif
140 const char* end = getEnd();
141 while (mCurrent != end) {
142 char ch = *mCurrent;
143 if (ch == '\n' || !isDelimiter(ch, delimiters)) {
144 break;
145 }
146 mCurrent += 1;
147 }
148}
149
150} // namespace android