1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gonk/libui/Tokenizer.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* 1.5 + * Copyright (C) 2010 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef _UTILS_TOKENIZER_H 1.21 +#define _UTILS_TOKENIZER_H 1.22 + 1.23 +#include <assert.h> 1.24 +#include <utils/Errors.h> 1.25 +#include <utils/FileMap.h> 1.26 +#include <utils/String8.h> 1.27 + 1.28 +namespace android { 1.29 + 1.30 +/** 1.31 + * A simple tokenizer for loading and parsing ASCII text files line by line. 1.32 + */ 1.33 +class Tokenizer { 1.34 + Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, 1.35 + bool ownBuffer, size_t length); 1.36 + 1.37 +public: 1.38 + ~Tokenizer(); 1.39 + 1.40 + /** 1.41 + * Opens a file and maps it into memory. 1.42 + * 1.43 + * Returns NO_ERROR and a tokenizer for the file, if successful. 1.44 + * Otherwise returns an error and sets outTokenizer to NULL. 1.45 + */ 1.46 + static status_t open(const String8& filename, Tokenizer** outTokenizer); 1.47 + 1.48 + /** 1.49 + * Prepares to tokenize the contents of a string. 1.50 + * 1.51 + * Returns NO_ERROR and a tokenizer for the string, if successful. 1.52 + * Otherwise returns an error and sets outTokenizer to NULL. 1.53 + */ 1.54 + static status_t fromContents(const String8& filename, 1.55 + const char* contents, Tokenizer** outTokenizer); 1.56 + 1.57 + /** 1.58 + * Returns true if at the end of the file. 1.59 + */ 1.60 + inline bool isEof() const { return mCurrent == getEnd(); } 1.61 + 1.62 + /** 1.63 + * Returns true if at the end of the line or end of the file. 1.64 + */ 1.65 + inline bool isEol() const { return isEof() || *mCurrent == '\n'; } 1.66 + 1.67 + /** 1.68 + * Gets the name of the file. 1.69 + */ 1.70 + inline String8 getFilename() const { return mFilename; } 1.71 + 1.72 + /** 1.73 + * Gets a 1-based line number index for the current position. 1.74 + */ 1.75 + inline int32_t getLineNumber() const { return mLineNumber; } 1.76 + 1.77 + /** 1.78 + * Formats a location string consisting of the filename and current line number. 1.79 + * Returns a string like "MyFile.txt:33". 1.80 + */ 1.81 + String8 getLocation() const; 1.82 + 1.83 + /** 1.84 + * Gets the character at the current position. 1.85 + * Returns null at end of file. 1.86 + */ 1.87 + inline char peekChar() const { return isEof() ? '\0' : *mCurrent; } 1.88 + 1.89 + /** 1.90 + * Gets the remainder of the current line as a string, excluding the newline character. 1.91 + */ 1.92 + String8 peekRemainderOfLine() const; 1.93 + 1.94 + /** 1.95 + * Gets the character at the current position and advances past it. 1.96 + * Returns null at end of file. 1.97 + */ 1.98 + inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); } 1.99 + 1.100 + /** 1.101 + * Gets the next token on this line stopping at the specified delimiters 1.102 + * or the end of the line whichever comes first and advances past it. 1.103 + * Also stops at embedded nulls. 1.104 + * Returns the token or an empty string if the current character is a delimiter 1.105 + * or is at the end of the line. 1.106 + */ 1.107 + String8 nextToken(const char* delimiters); 1.108 + 1.109 + /** 1.110 + * Advances to the next line. 1.111 + * Does nothing if already at the end of the file. 1.112 + */ 1.113 + void nextLine(); 1.114 + 1.115 + /** 1.116 + * Skips over the specified delimiters in the line. 1.117 + * Also skips embedded nulls. 1.118 + */ 1.119 + void skipDelimiters(const char* delimiters); 1.120 + 1.121 +private: 1.122 + Tokenizer(const Tokenizer& other); // not copyable 1.123 + 1.124 + String8 mFilename; 1.125 + FileMap* mFileMap; 1.126 + char* mBuffer; 1.127 + bool mOwnBuffer; 1.128 + size_t mLength; 1.129 + 1.130 + const char* mCurrent; 1.131 + int32_t mLineNumber; 1.132 + 1.133 + inline const char* getEnd() const { return mBuffer + mLength; } 1.134 + 1.135 +}; 1.136 + 1.137 +} // namespace android 1.138 + 1.139 +#endif // _UTILS_TOKENIZER_H