summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2012-04-17 18:19:50 -0700
committerAlex Ray <aray@google.com>2013-07-30 13:56:59 -0700
commit2c1627dc49994f83a636efd1970825b519bd93cb (patch)
tree831b26e950ee3072af10b2a119e526925ce51628
parent52d59e2c48a1563ea4f7e9fef6dc3611a5d74aaa (diff)
downloadsystem_core-2c1627dc49994f83a636efd1970825b519bd93cb.zip
system_core-2c1627dc49994f83a636efd1970825b519bd93cb.tar.gz
system_core-2c1627dc49994f83a636efd1970825b519bd93cb.tar.bz2
Support tokenizing arbitrary content.
Bug: 6110399 Change-Id: I37be63b68934fd451e6dffbf7d6079553619c0a3
-rw-r--r--include/utils/Tokenizer.h13
-rw-r--r--libs/utils/Tokenizer.cpp20
2 files changed, 28 insertions, 5 deletions
diff --git a/include/utils/Tokenizer.h b/include/utils/Tokenizer.h
index c7db5fb..bb25f37 100644
--- a/include/utils/Tokenizer.h
+++ b/include/utils/Tokenizer.h
@@ -28,7 +28,8 @@ 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);
+ Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
+ bool ownBuffer, size_t length);
public:
~Tokenizer();
@@ -42,6 +43,15 @@ public:
static status_t open(const String8& filename, Tokenizer** outTokenizer);
/**
+ * Prepares to tokenize the contents of a string.
+ *
+ * Returns NO_ERROR and a tokenizer for the string, if successful.
+ * Otherwise returns an error and sets outTokenizer to NULL.
+ */
+ static status_t fromContents(const String8& filename,
+ const char* contents, Tokenizer** outTokenizer);
+
+ /**
* Returns true if at the end of the file.
*/
inline bool isEof() const { return mCurrent == getEnd(); }
@@ -111,6 +121,7 @@ private:
String8 mFilename;
FileMap* mFileMap;
char* mBuffer;
+ bool mOwnBuffer;
size_t mLength;
const char* mCurrent;
diff --git a/libs/utils/Tokenizer.cpp b/libs/utils/Tokenizer.cpp
index efda2bf..7067533 100644
--- a/libs/utils/Tokenizer.cpp
+++ b/libs/utils/Tokenizer.cpp
@@ -35,15 +35,18 @@ static inline bool isDelimiter(char ch, const char* delimiters) {
return strchr(delimiters, ch) != NULL;
}
-Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer, size_t length) :
+Tokenizer::Tokenizer(const String8& filename, FileMap* fileMap, char* buffer,
+ bool ownBuffer, size_t length) :
mFilename(filename), mFileMap(fileMap),
- mBuffer(buffer), mLength(length), mCurrent(buffer), mLineNumber(1) {
+ mBuffer(buffer), mOwnBuffer(ownBuffer), mLength(length),
+ mCurrent(buffer), mLineNumber(1) {
}
Tokenizer::~Tokenizer() {
if (mFileMap) {
mFileMap->release();
- } else {
+ }
+ if (mOwnBuffer) {
delete[] mBuffer;
}
}
@@ -65,6 +68,7 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
size_t length = size_t(stat.st_size);
FileMap* fileMap = new FileMap();
+ bool ownBuffer = false;
char* buffer;
if (fileMap->create(NULL, fd, 0, length, true)) {
fileMap->advise(FileMap::SEQUENTIAL);
@@ -77,6 +81,7 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
// The length we obtained from stat is wrong too (it will always be 4096)
// so we must trust that read will read the entire file.
buffer = new char[length];
+ ownBuffer = true;
ssize_t nrd = read(fd, buffer, length);
if (nrd < 0) {
result = -errno;
@@ -89,7 +94,7 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
}
if (!result) {
- *outTokenizer = new Tokenizer(filename, fileMap, buffer, length);
+ *outTokenizer = new Tokenizer(filename, fileMap, buffer, ownBuffer, length);
}
}
close(fd);
@@ -97,6 +102,13 @@ status_t Tokenizer::open(const String8& filename, Tokenizer** outTokenizer) {
return result;
}
+status_t Tokenizer::fromContents(const String8& filename,
+ const char* contents, Tokenizer** outTokenizer) {
+ *outTokenizer = new Tokenizer(filename, NULL,
+ const_cast<char*>(contents), false, strlen(contents));
+ return OK;
+}
+
String8 Tokenizer::getLocation() const {
String8 result;
result.appendFormat("%s:%d", mFilename.string(), mLineNumber);