• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <utils/Tokenizer.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <utils/Log.h>
23 
24 #ifndef DEBUG_TOKENIZER
25 // Enables debug output for the tokenizer.
26 #define DEBUG_TOKENIZER 0
27 #endif
28 
29 namespace android {
30 
isDelimiter(char ch,const char * delimiters)31 static inline bool isDelimiter(char ch, const char* delimiters) {
32     return strchr(delimiters, ch) != nullptr;
33 }
34 
Tokenizer(const String8 & filename,FileMap * fileMap,char * buffer,bool ownBuffer,size_t length)35 Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
36         bool ownBuffer, size_t length) :
37         mFilename(filename), mFileMap(fileMap),
38         mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
39         mCurrent(buffer), mLineNumber(1) {
40 }
41 
~Tokenizer()42 Tokenizer::~Tokenizer() {
43     delete mFileMap;
44     if (mOwnBuffer) {
45         delete[] mBuffer;
46     }
47 }
48 
open(const String8 & filename,Tokenizer ** outTokenizer)49 status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
50     *outTokenizer = nullptr;
51 
52     int result = OK;
53     int fd = ::open(filename.string(), O_RDONLY);
54     if (fd < 0) {
55         result = -errno;
56         ALOGE("Error opening file '%s': %s", filename.string(), strerror(errno));
57     } else {
58         struct stat stat;
59         if (fstat(fd, &stat)) {
60             result = -errno;
61             ALOGE("Error getting size of file '%s': %s", filename.string(), strerror(errno));
62         } else {
63             size_t length = size_t(stat.st_size);
64 
65             FileMap* fileMap = new FileMap();
66             bool ownBuffer = false;
67             char* buffer;
68             if (fileMap->create(nullptr, fd, 0, length, true)) {
69                 fileMap->advise(FileMap::SEQUENTIAL);
70                 buffer = static_cast<char*>(fileMap->getDataPtr());
71             } else {
72                 delete fileMap;
73                 fileMap = nullptr;
74 
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];
79                 ownBuffer = true;
80                 ssize_t nrd = read(fd, buffer, length);
81                 if (nrd < 0) {
82                     result = -errno;
83                     ALOGE("Error reading file '%s': %s", filename.string(), strerror(errno));
84                     delete[] buffer;
85                     buffer = nullptr;
86                 } else {
87                     length = size_t(nrd);
88                 }
89             }
90 
91             if (!result) {
92                 *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
93             }
94         }
95         close(fd);
96     }
97     return result;
98 }
99 
fromContents(const String8 & filename,const char * contents,Tokenizer ** outTokenizer)100 status_t Tokenizer::fromContents(const String8& filename,
101         const char* contents, Tokenizer** outTokenizer) {
102     *outTokenizer = new Tokenizer(filename, nullptr,
103             const_cast<char*>(contents), false, strlen(contents));
104     return OK;
105 }
106 
getLocation() const107 String8 Tokenizer::getLocation() const {
108     String8 result;
109     result.appendFormat("%s:%d", mFilename.string(), mLineNumber);
110     return result;
111 }
112 
peekRemainderOfLine() const113 String8 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 
nextToken(const char * delimiters)126 String8 Tokenizer::nextToken(const char* delimiters) {
127 #if DEBUG_TOKENIZER
128     ALOGD("nextToken");
129 #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 
nextLine()142 void Tokenizer::nextLine() {
143 #if DEBUG_TOKENIZER
144     ALOGD("nextLine");
145 #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 
skipDelimiters(const char * delimiters)156 void Tokenizer::skipDelimiters(const char* delimiters) {
157 #if DEBUG_TOKENIZER
158     ALOGD("skipDelimiters");
159 #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
171