replicant-frameworks_native/include/utils/Tokenizer.h
Jeff Brown db360642ed Improve support for external keyboards.
Use Vendor ID, Product ID and optionally the Version to
locate keymaps and configuration files for external devices.

Moved virtual key definition parsing to native code so that
EventHub can identify touch screens with virtual keys and load
the appropriate key layout file.

Cleaned up a lot of old code in EventHub.

Fixed a regression in ViewRoot's fallback event handling.

Fixed a minor bug in FileMap that caused it to try to munmap
or close invalid handled when released if the attempt to map
the file failed.

Added a couple of new String8 conveniences for formatting strings.

Modified Tokenizer to fall back to open+read when mmap fails since
we can't mmap sysfs files as needed to open the virtual key
definition files in /sys/board_properties/.

Change-Id: I6ca5e5f9547619fd082ddac47e87ce185da69ee6
2010-12-02 16:01:32 -08:00

126 lines
3.4 KiB
C++

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _UTILS_TOKENIZER_H
#define _UTILS_TOKENIZER_H
#include <assert.h>
#include <utils/Errors.h>
#include <utils/FileMap.h>
#include <utils/String8.h>
namespace android {
/**
* A simple tokenizer for loading and parsing ASCII text files line by line.
*/
class Tokenizer {
Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, size_t length);
public:
~Tokenizer();
/**
* Opens a file and maps it into memory.
*
* Returns NO_ERROR and a tokenizer for the file, if successful.
* Otherwise returns an error and sets outTokenizer to NULL.
*/
static status_t open(const String8& filename, Tokenizer** outTokenizer);
/**
* Returns true if at the end of the file.
*/
inline bool isEof() const { return mCurrent == getEnd(); }
/**
* Returns true if at the end of the line or end of the file.
*/
inline bool isEol() const { return isEof() || *mCurrent == '\n'; }
/**
* Gets the name of the file.
*/
inline String8 getFilename() const { return mFilename; }
/**
* Gets a 1-based line number index for the current position.
*/
inline int32_t getLineNumber() const { return mLineNumber; }
/**
* Formats a location string consisting of the filename and current line number.
* Returns a string like "MyFile.txt:33".
*/
String8 getLocation() const;
/**
* Gets the character at the current position.
* Returns null at end of file.
*/
inline char peekChar() const { return isEof() ? '\0' : *mCurrent; }
/**
* Gets the remainder of the current line as a string, excluding the newline character.
*/
String8 peekRemainderOfLine() const;
/**
* Gets the character at the current position and advances past it.
* Returns null at end of file.
*/
inline char nextChar() { return isEof() ? '\0' : *(mCurrent++); }
/**
* Gets the next token on this line stopping at the specified delimiters
* or the end of the line whichever comes first and advances past it.
* Also stops at embedded nulls.
* Returns the token or an empty string if the current character is a delimiter
* or is at the end of the line.
*/
String8 nextToken(const char* delimiters);
/**
* Advances to the next line.
* Does nothing if already at the end of the file.
*/
void nextLine();
/**
* Skips over the specified delimiters in the line.
* Also skips embedded nulls.
*/
void skipDelimiters(const char* delimiters);
private:
Tokenizer(const Tokenizer& other); // not copyable
String8 mFilename;
FileMap* mFileMap;
char* mBuffer;
size_t mLength;
const char* mCurrent;
int32_t mLineNumber;
inline const char* getEnd() const { return mBuffer + mLength; }
};
} // namespace android
#endif // _UTILS_TOKENIZER_H