michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsPluginManifestLineReader_h_ michael@0: #define nsPluginManifestLineReader_h_ michael@0: michael@0: #include "nspr.h" michael@0: #include "nsDebug.h" michael@0: michael@0: #ifdef XP_WIN michael@0: #define PLUGIN_REGISTRY_FIELD_DELIMITER '|' michael@0: #else michael@0: #define PLUGIN_REGISTRY_FIELD_DELIMITER ':' michael@0: #endif michael@0: michael@0: #define PLUGIN_REGISTRY_END_OF_LINE_MARKER '$' michael@0: michael@0: class nsPluginManifestLineReader michael@0: { michael@0: public: michael@0: nsPluginManifestLineReader() {mBase = mCur = mNext = mLimit = 0;} michael@0: ~nsPluginManifestLineReader() { if (mBase) delete[] mBase; mBase=0;} michael@0: michael@0: char* Init(uint32_t flen) michael@0: { michael@0: mBase = mCur = mNext = new char[flen + 1]; michael@0: if (mBase) { michael@0: mLimit = mBase + flen; michael@0: *mLimit = 0; michael@0: } michael@0: mLength = 0; michael@0: return mBase; michael@0: } michael@0: michael@0: bool NextLine() michael@0: { michael@0: if (mNext >= mLimit) michael@0: return false; michael@0: michael@0: mCur = mNext; michael@0: mLength = 0; michael@0: michael@0: char *lastDelimiter = 0; michael@0: while(mNext < mLimit) { michael@0: if (IsEOL(*mNext)) { michael@0: if (lastDelimiter) { michael@0: if (lastDelimiter && *(mNext - 1) != PLUGIN_REGISTRY_END_OF_LINE_MARKER) michael@0: return false; michael@0: *lastDelimiter = '\0'; michael@0: } else { michael@0: *mNext = '\0'; michael@0: } michael@0: michael@0: for (++mNext; mNext < mLimit; ++mNext) { michael@0: if (!IsEOL(*mNext)) michael@0: break; michael@0: } michael@0: return true; michael@0: } michael@0: if (*mNext == PLUGIN_REGISTRY_FIELD_DELIMITER) michael@0: lastDelimiter = mNext; michael@0: ++mNext; michael@0: ++mLength; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int ParseLine(char** chunks, int maxChunks) michael@0: { michael@0: NS_ASSERTION(mCur && maxChunks && chunks, "bad call to ParseLine"); michael@0: int found = 0; michael@0: chunks[found++] = mCur; michael@0: michael@0: if (found < maxChunks) { michael@0: for (char* cur = mCur; *cur; cur++) { michael@0: if (*cur == PLUGIN_REGISTRY_FIELD_DELIMITER) { michael@0: *cur = 0; michael@0: chunks[found++] = cur + 1; michael@0: if (found == maxChunks) michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: return found; michael@0: } michael@0: michael@0: char* LinePtr() { return mCur; } michael@0: uint32_t LineLength() { return mLength; } michael@0: michael@0: bool IsEOL(char c) {return c == '\n' || c == '\r';} michael@0: michael@0: char* mBase; michael@0: private: michael@0: char* mCur; michael@0: uint32_t mLength; michael@0: char* mNext; michael@0: char* mLimit; michael@0: }; michael@0: michael@0: #endif