|
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 nsPluginManifestLineReader_h_ |
|
7 #define nsPluginManifestLineReader_h_ |
|
8 |
|
9 #include "nspr.h" |
|
10 #include "nsDebug.h" |
|
11 |
|
12 #ifdef XP_WIN |
|
13 #define PLUGIN_REGISTRY_FIELD_DELIMITER '|' |
|
14 #else |
|
15 #define PLUGIN_REGISTRY_FIELD_DELIMITER ':' |
|
16 #endif |
|
17 |
|
18 #define PLUGIN_REGISTRY_END_OF_LINE_MARKER '$' |
|
19 |
|
20 class nsPluginManifestLineReader |
|
21 { |
|
22 public: |
|
23 nsPluginManifestLineReader() {mBase = mCur = mNext = mLimit = 0;} |
|
24 ~nsPluginManifestLineReader() { if (mBase) delete[] mBase; mBase=0;} |
|
25 |
|
26 char* Init(uint32_t flen) |
|
27 { |
|
28 mBase = mCur = mNext = new char[flen + 1]; |
|
29 if (mBase) { |
|
30 mLimit = mBase + flen; |
|
31 *mLimit = 0; |
|
32 } |
|
33 mLength = 0; |
|
34 return mBase; |
|
35 } |
|
36 |
|
37 bool NextLine() |
|
38 { |
|
39 if (mNext >= mLimit) |
|
40 return false; |
|
41 |
|
42 mCur = mNext; |
|
43 mLength = 0; |
|
44 |
|
45 char *lastDelimiter = 0; |
|
46 while(mNext < mLimit) { |
|
47 if (IsEOL(*mNext)) { |
|
48 if (lastDelimiter) { |
|
49 if (lastDelimiter && *(mNext - 1) != PLUGIN_REGISTRY_END_OF_LINE_MARKER) |
|
50 return false; |
|
51 *lastDelimiter = '\0'; |
|
52 } else { |
|
53 *mNext = '\0'; |
|
54 } |
|
55 |
|
56 for (++mNext; mNext < mLimit; ++mNext) { |
|
57 if (!IsEOL(*mNext)) |
|
58 break; |
|
59 } |
|
60 return true; |
|
61 } |
|
62 if (*mNext == PLUGIN_REGISTRY_FIELD_DELIMITER) |
|
63 lastDelimiter = mNext; |
|
64 ++mNext; |
|
65 ++mLength; |
|
66 } |
|
67 return false; |
|
68 } |
|
69 |
|
70 int ParseLine(char** chunks, int maxChunks) |
|
71 { |
|
72 NS_ASSERTION(mCur && maxChunks && chunks, "bad call to ParseLine"); |
|
73 int found = 0; |
|
74 chunks[found++] = mCur; |
|
75 |
|
76 if (found < maxChunks) { |
|
77 for (char* cur = mCur; *cur; cur++) { |
|
78 if (*cur == PLUGIN_REGISTRY_FIELD_DELIMITER) { |
|
79 *cur = 0; |
|
80 chunks[found++] = cur + 1; |
|
81 if (found == maxChunks) |
|
82 break; |
|
83 } |
|
84 } |
|
85 } |
|
86 return found; |
|
87 } |
|
88 |
|
89 char* LinePtr() { return mCur; } |
|
90 uint32_t LineLength() { return mLength; } |
|
91 |
|
92 bool IsEOL(char c) {return c == '\n' || c == '\r';} |
|
93 |
|
94 char* mBase; |
|
95 private: |
|
96 char* mCur; |
|
97 uint32_t mLength; |
|
98 char* mNext; |
|
99 char* mLimit; |
|
100 }; |
|
101 |
|
102 #endif |