michael@0: /* michael@0: * Copyright (C) 2010 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #define LOG_TAG "Tokenizer" michael@0: #include "cutils_log.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include "Tokenizer.h" michael@0: michael@0: // Enables debug output for the tokenizer. michael@0: #define DEBUG_TOKENIZER 0 michael@0: michael@0: michael@0: namespace android { michael@0: michael@0: static inline bool isDelimiter(char ch, const char* delimiters) { michael@0: return strchr(delimiters, ch) != NULL; michael@0: } michael@0: michael@0: Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, michael@0: bool ownBuffer, size_t length) : michael@0: mFilename(filename), mFileMap(fileMap), michael@0: mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length), michael@0: mCurrent(buffer), mLineNumber(1) { michael@0: } michael@0: michael@0: Tokenizer::~Tokenizer() { michael@0: if (mFileMap) { michael@0: mFileMap->release(); michael@0: } michael@0: if (mOwnBuffer) { michael@0: delete[] mBuffer; michael@0: } michael@0: } michael@0: michael@0: status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) { michael@0: *outTokenizer = NULL; michael@0: michael@0: int result = NO_ERROR; michael@0: int fd = ::open(filename.string(), O_RDONLY); michael@0: if (fd < 0) { michael@0: result = -errno; michael@0: ALOGE("Error opening file '%s', %s.", filename.string(), strerror(errno)); michael@0: } else { michael@0: struct stat stat; michael@0: if (fstat(fd, &stat)) { michael@0: result = -errno; michael@0: ALOGE("Error getting size of file '%s', %s.", filename.string(), strerror(errno)); michael@0: } else { michael@0: size_t length = size_t(stat.st_size); michael@0: michael@0: FileMap* fileMap = new FileMap(); michael@0: bool ownBuffer = false; michael@0: char* buffer; michael@0: if (fileMap->create(NULL, fd, 0, length, true)) { michael@0: fileMap->advise(FileMap::SEQUENTIAL); michael@0: buffer = static_cast(fileMap->getDataPtr()); michael@0: } else { michael@0: fileMap->release(); michael@0: fileMap = NULL; michael@0: michael@0: // Fall back to reading into a buffer since we can't mmap files in sysfs. michael@0: // The length we obtained from stat is wrong too (it will always be 4096) michael@0: // so we must trust that read will read the entire file. michael@0: buffer = new char[length]; michael@0: ownBuffer = true; michael@0: ssize_t nrd = read(fd, buffer, length); michael@0: if (nrd < 0) { michael@0: result = -errno; michael@0: ALOGE("Error reading file '%s', %s.", filename.string(), strerror(errno)); michael@0: delete[] buffer; michael@0: buffer = NULL; michael@0: } else { michael@0: length = size_t(nrd); michael@0: } michael@0: } michael@0: michael@0: if (!result) { michael@0: *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length); michael@0: } michael@0: } michael@0: close(fd); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: status_t Tokenizer::fromContents(const String8& filename, michael@0: const char* contents, Tokenizer** outTokenizer) { michael@0: *outTokenizer = new Tokenizer(filename, NULL, michael@0: const_cast(contents), false, strlen(contents)); michael@0: return OK; michael@0: } michael@0: michael@0: String8 Tokenizer::getLocation() const { michael@0: String8 result; michael@0: result.appendFormat("%s:%d", mFilename.string(), mLineNumber); michael@0: return result; michael@0: } michael@0: michael@0: String8 Tokenizer::peekRemainderOfLine() const { michael@0: const char* end = getEnd(); michael@0: const char* eol = mCurrent; michael@0: while (eol != end) { michael@0: char ch = *eol; michael@0: if (ch == '\n') { michael@0: break; michael@0: } michael@0: eol += 1; michael@0: } michael@0: return String8(mCurrent, eol - mCurrent); michael@0: } michael@0: michael@0: String8 Tokenizer::nextToken(const char* delimiters) { michael@0: #if DEBUG_TOKENIZER michael@0: ALOGD("nextToken"); michael@0: #endif michael@0: const char* end = getEnd(); michael@0: const char* tokenStart = mCurrent; michael@0: while (mCurrent != end) { michael@0: char ch = *mCurrent; michael@0: if (ch == '\n' || isDelimiter(ch, delimiters)) { michael@0: break; michael@0: } michael@0: mCurrent += 1; michael@0: } michael@0: return String8(tokenStart, mCurrent - tokenStart); michael@0: } michael@0: michael@0: void Tokenizer::nextLine() { michael@0: #if DEBUG_TOKENIZER michael@0: ALOGD("nextLine"); michael@0: #endif michael@0: const char* end = getEnd(); michael@0: while (mCurrent != end) { michael@0: char ch = *(mCurrent++); michael@0: if (ch == '\n') { michael@0: mLineNumber += 1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void Tokenizer::skipDelimiters(const char* delimiters) { michael@0: #if DEBUG_TOKENIZER michael@0: ALOGD("skipDelimiters"); michael@0: #endif michael@0: const char* end = getEnd(); michael@0: while (mCurrent != end) { michael@0: char ch = *mCurrent; michael@0: if (ch == '\n' || !isDelimiter(ch, delimiters)) { michael@0: break; michael@0: } michael@0: mCurrent += 1; michael@0: } michael@0: } michael@0: michael@0: } // namespace android