Line data Source code
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 0 : nsPluginManifestLineReader() {mBase = mCur = mNext = mLimit = 0;}
24 0 : ~nsPluginManifestLineReader() { if (mBase) delete[] mBase; mBase=0;}
25 :
26 0 : char* Init(uint32_t flen)
27 : {
28 0 : mBase = mCur = mNext = new char[flen + 1];
29 0 : if (mBase) {
30 0 : mLimit = mBase + flen;
31 0 : *mLimit = 0;
32 : }
33 0 : mLength = 0;
34 0 : return mBase;
35 : }
36 :
37 0 : bool NextLine()
38 : {
39 0 : if (mNext >= mLimit)
40 0 : return false;
41 :
42 0 : mCur = mNext;
43 0 : mLength = 0;
44 :
45 0 : char *lastDelimiter = 0;
46 0 : while(mNext < mLimit) {
47 0 : if (IsEOL(*mNext)) {
48 0 : if (lastDelimiter) {
49 0 : if (lastDelimiter && *(mNext - 1) != PLUGIN_REGISTRY_END_OF_LINE_MARKER)
50 0 : return false;
51 0 : *lastDelimiter = '\0';
52 : } else {
53 0 : *mNext = '\0';
54 : }
55 :
56 0 : for (++mNext; mNext < mLimit; ++mNext) {
57 0 : if (!IsEOL(*mNext))
58 0 : break;
59 : }
60 0 : return true;
61 : }
62 0 : if (*mNext == PLUGIN_REGISTRY_FIELD_DELIMITER)
63 0 : lastDelimiter = mNext;
64 0 : ++mNext;
65 0 : ++mLength;
66 : }
67 0 : return false;
68 : }
69 :
70 0 : int ParseLine(char** chunks, int maxChunks)
71 : {
72 0 : NS_ASSERTION(mCur && maxChunks && chunks, "bad call to ParseLine");
73 0 : int found = 0;
74 0 : chunks[found++] = mCur;
75 :
76 0 : if (found < maxChunks) {
77 0 : for (char* cur = mCur; *cur; cur++) {
78 0 : if (*cur == PLUGIN_REGISTRY_FIELD_DELIMITER) {
79 0 : *cur = 0;
80 0 : chunks[found++] = cur + 1;
81 0 : if (found == maxChunks)
82 0 : break;
83 : }
84 : }
85 : }
86 0 : return found;
87 : }
88 :
89 0 : char* LinePtr() { return mCur; }
90 0 : uint32_t LineLength() { return mLength; }
91 :
92 0 : 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
|