1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Mork Reader.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2006
20 * the Initial Developer. All Rights Reserved.
23 * Brian Ryner <bryner@brianryner.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsMorkReader_h_
40 #define nsMorkReader_h_
42 #include "nsDataHashtable.h"
43 #include "nsILineInputStream.h"
45 #include "nsAutoPtr.h"
47 // The nsMorkReader object allows a consumer to read in a mork-format
48 // file and enumerate the rows that it contains. It does not provide
49 // any functionality for modifying mork tables.
52 // http://www.mozilla.org/mailnews/arch/mork/primer.txt
53 // http://www.mozilla.org/mailnews/arch/mork/grammar.txt
54 // http://www.jwz.org/hacks/mork.pl
56 class NS_STACK_CLASS nsMorkReader
59 // This string type has built-in storage for the hex string representation
60 // of a 32-bit row id or atom map key, plus the terminating null.
61 class IDString
: public nsFixedCString
64 IDString() : fixed_string_type(mStorage
, sizeof(mStorage
), 0) {}
65 IDString(const substring_type
&str
) :
66 fixed_string_type(mStorage
, sizeof(mStorage
), 0)
72 char_type mStorage
[9];
75 // Hashtable key type that contains an IDString
76 class IDKey
: public PLDHashEntryHdr
79 typedef const nsCSubstring
& KeyType
;
80 typedef const nsCSubstring
* KeyTypePointer
;
82 IDKey(KeyTypePointer aStr
) : mStr(*aStr
) { }
83 IDKey(const IDKey
& toCopy
) : mStr(toCopy
.mStr
) { }
86 KeyType
GetKey() const { return mStr
; }
87 PRBool
KeyEquals(const KeyTypePointer aKey
) const
89 return mStr
.Equals(*aKey
);
92 static KeyTypePointer
KeyToPointer(KeyType aKey
) { return &aKey
; }
93 static PLDHashNumber
HashKey(const KeyTypePointer aKey
)
95 return HashString(*aKey
);
97 enum { ALLOW_MEMMOVE
= PR_FALSE
};
103 // A convenience typedef for an IDKey-to-string mapping.
104 typedef nsDataHashtable
<IDKey
,nsCString
> StringMap
;
106 // A convenience typdef for an IDKey-to-index mapping, used for the
107 // column index hashtable.
108 typedef nsDataHashtable
<IDKey
,PRInt32
> IndexMap
;
110 // A MorkColumn represents the data associated with a single table column.
113 MorkColumn(const nsCSubstring
&i
, const nsCSubstring
&n
)
120 // Enumerator callback type for processing table rows.
121 // A row contains cells. Each cell specifies a column id, and the value
122 // for the column for that row.
123 // rowID is the table-unique row id
124 // values contains the cell values, in an order which corresponds to
125 // the columns returned by GetColumns().
126 // You should call NormalizeValue() on any cell value that you plan to use.
127 // userData is the opaque pointer passed to EnumerateRows()
128 // The callback can return PL_DHASH_NEXT to continue enumerating,
129 // or PL_DHASH_STOP to stop.
130 typedef PLDHashOperator
131 (* RowEnumerator
)(const nsCSubstring
&rowID
,
132 const nsTArray
<nsCString
> *values
,
135 // Initialize the importer object's data structures
138 // Read in the given mork file
139 // Note: currently, only single-table mork files are supported
140 nsresult
Read(nsIFile
*aFile
);
142 // Returns the list of columns in the current table.
143 const nsTArray
<MorkColumn
>& GetColumns() const { return mColumns
; }
145 // Enumerate the rows in the current table.
146 void EnumerateRows(RowEnumerator aCallback
, void *aUserData
) const;
148 // Get the "meta row" for the table. Each table has at most one meta row,
149 // which records information about the table. Like normal rows, the
150 // meta row contains columns in the same order as returned by GetColumns().
151 // Returns null if there is no meta row for this table.
152 const nsTArray
<nsCString
>* GetMetaRow() const { return mMetaRow
; }
154 // Normalizes the cell value (resolves references to the value map).
155 // aValue is modified in-place.
156 void NormalizeValue(nsCString
&aValue
) const;
162 // Parses a line of the file which contains key/value pairs (either
163 // the column map or the value map). Additional lines are read from
164 // mStream if the line ends mid-entry. The pairs are added to aMap.
165 nsresult
ParseMap(const nsCSubstring
&aLine
, StringMap
*aMap
);
167 // Parses a line of the file which contains a table or row definition.
168 // Additional lines are read from mStream of the line ends mid-row.
169 // An entry is added to mTable using the row ID as the key, which contains
170 // a column array for the row. The supplied column hash table maps from
171 // column id to an index in mColumns.
172 nsresult
ParseTable(const nsCSubstring
&aLine
, const IndexMap
&aColumnMap
);
174 // Reads a single logical line from mStream into aLine.
175 // Any continuation lines are consumed and appended to the line.
176 nsresult
ReadLine(nsCString
&aLine
);
178 // Create a new nsCString array and fill it with the supplied number
179 // of void strings. Returns null on out-of-memory.
180 static nsTArray
<nsCString
>* NewVoidStringArray(PRInt32 aSize
);
182 nsTArray
<MorkColumn
> mColumns
;
184 nsAutoPtr
< nsTArray
<nsCString
> > mMetaRow
;
185 nsDataHashtable
< IDKey
,nsTArray
<nsCString
>* > mTable
;
186 nsCOMPtr
<nsILineInputStream
> mStream
;
187 nsCString mEmptyString
; // note: not EmptyCString() since that's not sharable
190 #endif // nsMorkReader_h_