xpcom/ds/nsManifestLineReader.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsManifestLineReader.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsManifestLineReader_h__
    1.10 +#define nsManifestLineReader_h__
    1.11 +
    1.12 +#include "nspr.h"
    1.13 +#include "nsDebug.h"
    1.14 +
    1.15 +class nsManifestLineReader
    1.16 +{
    1.17 +public:
    1.18 +    nsManifestLineReader() : mBase(nullptr) {} 
    1.19 +    ~nsManifestLineReader() {}
    1.20 +
    1.21 +    void Init(char* base, uint32_t flen) 
    1.22 +    {
    1.23 +        mBase = mCur = mNext = base; 
    1.24 +        mLength = 0;
    1.25 +        mLimit = base + flen;
    1.26 +    }
    1.27 +
    1.28 +    bool NextLine()
    1.29 +    {
    1.30 +        if(mNext >= mLimit)
    1.31 +            return false;
    1.32 +        
    1.33 +        mCur = mNext;
    1.34 +        mLength = 0;
    1.35 +        
    1.36 +        while(mNext < mLimit)
    1.37 +        {
    1.38 +            if(IsEOL(*mNext))
    1.39 +            {
    1.40 +                *mNext = '\0';
    1.41 +                for(++mNext; mNext < mLimit; ++mNext)
    1.42 +                    if(!IsEOL(*mNext))
    1.43 +                        break;
    1.44 +                return true;
    1.45 +            }
    1.46 +            ++mNext;
    1.47 +            ++mLength;
    1.48 +        }
    1.49 +        return false;        
    1.50 +    }
    1.51 +
    1.52 +    int ParseLine(char** chunks, int* lengths, int maxChunks)
    1.53 +    {
    1.54 +        NS_ASSERTION(mCur && maxChunks && chunks, "bad call to ParseLine");
    1.55 +        int found = 0;
    1.56 +        chunks[found++] = mCur;
    1.57 +
    1.58 +        if(found < maxChunks)
    1.59 +        {
    1.60 +            char *lastchunk = mCur;
    1.61 +            int *lastlength = lengths;
    1.62 +            for(char* cur = mCur; *cur; cur++)
    1.63 +            {
    1.64 +                if(*cur == ',')
    1.65 +                {
    1.66 +                    *cur = 0;
    1.67 +                    // always fill in the previous chunk's length
    1.68 +                    *lastlength++ = cur - lastchunk;
    1.69 +                    chunks[found++] = lastchunk = cur+1;
    1.70 +                    if(found == maxChunks)
    1.71 +                        break;
    1.72 +                }
    1.73 +            }
    1.74 +            // crazy pointer math - calculate the length of the final chunk
    1.75 +            *lastlength = (mCur + mLength) - lastchunk;
    1.76 +        }
    1.77 +        return found;
    1.78 +    }
    1.79 +
    1.80 +    char*       LinePtr() {return mCur;}    
    1.81 +    uint32_t    LineLength() {return mLength;}    
    1.82 +
    1.83 +    bool        IsEOL(char c) {return c == '\n' || c == '\r';}
    1.84 +private:
    1.85 +    char*       mCur;
    1.86 +    uint32_t    mLength;
    1.87 +    char*       mNext;
    1.88 +    char*       mBase;
    1.89 +    char*       mLimit;
    1.90 +};
    1.91 +
    1.92 +#endif

mercurial