Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 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 <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 Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <utils/Tokenizer.h> |
| 27 | |
| 28 | // Enables debug output for the tokenizer. |
| 29 | #define DEBUG_TOKENIZER 0 |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | static inline bool isDelimiter(char ch, const char* delimiters) { |
| 35 | return strchr(delimiters, ch) != NULL; |
| 36 | } |
| 37 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 38 | Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, size_t length) : |
| 39 | mFilename(filename), mFileMap(fileMap), |
| 40 | mBuffer(buffer), mLength(length), mCurrent(buffer), mLineNumber(1) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | Tokenizer::~Tokenizer() { |
Jeff Brown | a6210d0 | 2010-11-19 13:13:07 -0800 | [diff] [blame] | 44 | if (mFileMap) { |
| 45 | mFileMap->release(); |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 46 | } else { |
| 47 | delete[] mBuffer; |
Jeff Brown | a6210d0 | 2010-11-19 13:13:07 -0800 | [diff] [blame] | 48 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | status_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 Brown | a6210d0 | 2010-11-19 13:13:07 -0800 | [diff] [blame] | 60 | struct stat stat; |
| 61 | if (fstat(fd, &stat)) { |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 62 | 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 Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 66 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 67 | FileMap* fileMap = new FileMap(); |
| 68 | char* buffer; |
| 69 | if (fileMap->create(NULL, fd, 0, length, true)) { |
| 70 | fileMap->advise(FileMap::SEQUENTIAL); |
| 71 | buffer = static_cast<char*>(fileMap->getDataPtr()); |
| 72 | } else { |
| 73 | fileMap->release(); |
| 74 | fileMap = NULL; |
| 75 | |
| 76 | // Fall back to reading into a buffer since we can't mmap files in sysfs. |
| 77 | // The length we obtained from stat is wrong too (it will always be 4096) |
| 78 | // so we must trust that read will read the entire file. |
| 79 | buffer = new char[length]; |
| 80 | ssize_t nrd = read(fd, buffer, length); |
| 81 | if (nrd < 0) { |
| 82 | result = -errno; |
| 83 | LOGE("Error reading file '%s', %s.", filename.string(), strerror(errno)); |
| 84 | delete[] buffer; |
| 85 | buffer = NULL; |
| 86 | } else { |
| 87 | length = size_t(nrd); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 90 | |
| 91 | if (!result) { |
| 92 | *outTokenizer = new Tokenizer(filename, fileMap, buffer, length); |
Jeff Brown | a6210d0 | 2010-11-19 13:13:07 -0800 | [diff] [blame] | 93 | } |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 94 | } |
| 95 | close(fd); |
| 96 | } |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | String8 Tokenizer::getLocation() const { |
| 101 | String8 result; |
| 102 | result.appendFormat("%s:%d", mFilename.string(), mLineNumber); |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | String8 Tokenizer::peekRemainderOfLine() const { |
| 107 | const char* end = getEnd(); |
| 108 | const char* eol = mCurrent; |
| 109 | while (eol != end) { |
| 110 | char ch = *eol; |
| 111 | if (ch == '\n') { |
| 112 | break; |
| 113 | } |
| 114 | eol += 1; |
| 115 | } |
| 116 | return String8(mCurrent, eol - mCurrent); |
| 117 | } |
| 118 | |
| 119 | String8 Tokenizer::nextToken(const char* delimiters) { |
| 120 | #if DEBUG_TOKENIZER |
| 121 | LOGD("nextToken"); |
| 122 | #endif |
| 123 | const char* end = getEnd(); |
| 124 | const char* tokenStart = mCurrent; |
| 125 | while (mCurrent != end) { |
| 126 | char ch = *mCurrent; |
| 127 | if (ch == '\n' || isDelimiter(ch, delimiters)) { |
| 128 | break; |
| 129 | } |
| 130 | mCurrent += 1; |
| 131 | } |
| 132 | return String8(tokenStart, mCurrent - tokenStart); |
| 133 | } |
| 134 | |
| 135 | void Tokenizer::nextLine() { |
| 136 | #if DEBUG_TOKENIZER |
| 137 | LOGD("nextLine"); |
| 138 | #endif |
| 139 | const char* end = getEnd(); |
| 140 | while (mCurrent != end) { |
| 141 | char ch = *(mCurrent++); |
| 142 | if (ch == '\n') { |
| 143 | mLineNumber += 1; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Tokenizer::skipDelimiters(const char* delimiters) { |
| 150 | #if DEBUG_TOKENIZER |
| 151 | LOGD("skipDelimiters"); |
| 152 | #endif |
| 153 | const char* end = getEnd(); |
| 154 | while (mCurrent != end) { |
| 155 | char ch = *mCurrent; |
| 156 | if (ch == '\n' || !isDelimiter(ch, delimiters)) { |
| 157 | break; |
| 158 | } |
| 159 | mCurrent += 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | } // namespace android |