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 nsManifestLineReader_h__ michael@0: #define nsManifestLineReader_h__ michael@0: michael@0: #include "nspr.h" michael@0: #include "nsDebug.h" michael@0: michael@0: class nsManifestLineReader michael@0: { michael@0: public: michael@0: nsManifestLineReader() : mBase(nullptr) {} michael@0: ~nsManifestLineReader() {} michael@0: michael@0: void Init(char* base, uint32_t flen) michael@0: { michael@0: mBase = mCur = mNext = base; michael@0: mLength = 0; michael@0: mLimit = base + flen; 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: while(mNext < mLimit) michael@0: { michael@0: if(IsEOL(*mNext)) michael@0: { michael@0: *mNext = '\0'; michael@0: for(++mNext; mNext < mLimit; ++mNext) michael@0: if(!IsEOL(*mNext)) michael@0: break; michael@0: return true; michael@0: } michael@0: ++mNext; michael@0: ++mLength; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: int ParseLine(char** chunks, int* lengths, 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: { michael@0: char *lastchunk = mCur; michael@0: int *lastlength = lengths; michael@0: for(char* cur = mCur; *cur; cur++) michael@0: { michael@0: if(*cur == ',') michael@0: { michael@0: *cur = 0; michael@0: // always fill in the previous chunk's length michael@0: *lastlength++ = cur - lastchunk; michael@0: chunks[found++] = lastchunk = cur+1; michael@0: if(found == maxChunks) michael@0: break; michael@0: } michael@0: } michael@0: // crazy pointer math - calculate the length of the final chunk michael@0: *lastlength = (mCur + mLength) - lastchunk; 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: private: michael@0: char* mCur; michael@0: uint32_t mLength; michael@0: char* mNext; michael@0: char* mBase; michael@0: char* mLimit; michael@0: }; michael@0: michael@0: #endif