|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsManifestLineReader_h__ |
|
7 #define nsManifestLineReader_h__ |
|
8 |
|
9 #include "nspr.h" |
|
10 #include "nsDebug.h" |
|
11 |
|
12 class nsManifestLineReader |
|
13 { |
|
14 public: |
|
15 nsManifestLineReader() : mBase(nullptr) {} |
|
16 ~nsManifestLineReader() {} |
|
17 |
|
18 void Init(char* base, uint32_t flen) |
|
19 { |
|
20 mBase = mCur = mNext = base; |
|
21 mLength = 0; |
|
22 mLimit = base + flen; |
|
23 } |
|
24 |
|
25 bool NextLine() |
|
26 { |
|
27 if(mNext >= mLimit) |
|
28 return false; |
|
29 |
|
30 mCur = mNext; |
|
31 mLength = 0; |
|
32 |
|
33 while(mNext < mLimit) |
|
34 { |
|
35 if(IsEOL(*mNext)) |
|
36 { |
|
37 *mNext = '\0'; |
|
38 for(++mNext; mNext < mLimit; ++mNext) |
|
39 if(!IsEOL(*mNext)) |
|
40 break; |
|
41 return true; |
|
42 } |
|
43 ++mNext; |
|
44 ++mLength; |
|
45 } |
|
46 return false; |
|
47 } |
|
48 |
|
49 int ParseLine(char** chunks, int* lengths, int maxChunks) |
|
50 { |
|
51 NS_ASSERTION(mCur && maxChunks && chunks, "bad call to ParseLine"); |
|
52 int found = 0; |
|
53 chunks[found++] = mCur; |
|
54 |
|
55 if(found < maxChunks) |
|
56 { |
|
57 char *lastchunk = mCur; |
|
58 int *lastlength = lengths; |
|
59 for(char* cur = mCur; *cur; cur++) |
|
60 { |
|
61 if(*cur == ',') |
|
62 { |
|
63 *cur = 0; |
|
64 // always fill in the previous chunk's length |
|
65 *lastlength++ = cur - lastchunk; |
|
66 chunks[found++] = lastchunk = cur+1; |
|
67 if(found == maxChunks) |
|
68 break; |
|
69 } |
|
70 } |
|
71 // crazy pointer math - calculate the length of the final chunk |
|
72 *lastlength = (mCur + mLength) - lastchunk; |
|
73 } |
|
74 return found; |
|
75 } |
|
76 |
|
77 char* LinePtr() {return mCur;} |
|
78 uint32_t LineLength() {return mLength;} |
|
79 |
|
80 bool IsEOL(char c) {return c == '\n' || c == '\r';} |
|
81 private: |
|
82 char* mCur; |
|
83 uint32_t mLength; |
|
84 char* mNext; |
|
85 char* mBase; |
|
86 char* mLimit; |
|
87 }; |
|
88 |
|
89 #endif |