initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 |
| 4 | * |
| 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/ |
| 9 | * |
| 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 |
| 13 | * License. |
| 14 | * |
| 15 | * The Original Code is the Mork Reader. |
| 16 | * |
| 17 | * The Initial Developer of the Original Code is |
| 18 | * Google Inc. |
| 19 | * Portions created by the Initial Developer are Copyright (C) 2006 |
| 20 | * the Initial Developer. All Rights Reserved. |
| 21 | * |
| 22 | * Contributor(s): |
| 23 | * Brian Ryner <[email protected]> (original author) |
| 24 | * |
| 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. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | |
| 39 | // Source: |
| 40 | // http://mxr.mozilla.org/firefox/source/db/morkreader/nsMorkReader.h |
| 41 | |
[email protected] | 9fbf402 | 2011-04-10 02:04:13 | [diff] [blame] | 42 | #ifndef CHROME_BROWSER_IMPORTER_MORK_READER_H_ |
| 43 | #define CHROME_BROWSER_IMPORTER_MORK_READER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include <fstream> |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 46 | #include <map> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | #include <string> |
| 48 | #include <vector> |
| 49 | |
| 50 | #include "base/basictypes.h" |
[email protected] | e74b81d | 2009-07-20 22:18:42 | [diff] [blame] | 51 | |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 52 | class FilePath; |
[email protected] | f6061aed | 2009-10-06 16:28:57 | [diff] [blame] | 53 | class ImporterBridge; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
| 55 | // The nsMorkReader object allows a consumer to read in a mork-format |
| 56 | // file and enumerate the rows that it contains. It does not provide |
| 57 | // any functionality for modifying mork tables. |
[email protected] | 9fbf402 | 2011-04-10 02:04:13 | [diff] [blame] | 58 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | // References: |
| 60 | // http://www.mozilla.org/mailnews/arch/mork/primer.txt |
| 61 | // http://www.mozilla.org/mailnews/arch/mork/grammar.txt |
| 62 | // http://www.jwz.org/hacks/mork.pl |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | class MorkReader { |
| 64 | public: |
| 65 | // The IDString type has built-in storage for the hex string representation |
| 66 | // of a 32-bit row id or atom map key, plus the terminating null. |
| 67 | // We use STL string here so that is can be operated with STL containers. |
| 68 | typedef std::string IDString; |
| 69 | |
| 70 | // Lists the contents of a series of columns. |
| 71 | typedef std::vector<std::string> ColumnDataList; |
| 72 | |
| 73 | // A MorkColumn describes a column of the table. |
| 74 | struct MorkColumn { |
[email protected] | f25b54a14 | 2011-03-24 21:58:02 | [diff] [blame] | 75 | MorkColumn(const IDString& i, const std::string& n) : id(i), name(n) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
| 77 | IDString id; |
| 78 | std::string name; |
| 79 | }; |
| 80 | typedef std::vector<MorkColumn> MorkColumnList; |
| 81 | |
| 82 | // The key for each row is the identifier for it, and the data is a pointer |
| 83 | // to an array for each column. |
| 84 | typedef std::map<IDString, ColumnDataList*> RowMap; |
| 85 | |
| 86 | typedef RowMap::const_iterator iterator; |
| 87 | |
| 88 | MorkReader(); |
| 89 | ~MorkReader(); |
| 90 | |
| 91 | // Read in the given mork file. Returns true on success. |
| 92 | // Note: currently, only single-table mork files are supported |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 93 | bool Read(const FilePath& filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
| 95 | // Returns the list of columns in the current table. |
| 96 | const MorkColumnList& columns() const { return columns_; } |
| 97 | |
| 98 | // Get the "meta row" for the table. Each table has at most one meta row, |
| 99 | // which records information about the table. Like normal rows, the |
| 100 | // meta row contains columns in the same order as returned by columns(). |
| 101 | // Returns null if there is no meta row for this table. |
| 102 | const ColumnDataList& meta_row() const { return meta_row_; } |
| 103 | |
| 104 | // Normalizes the cell value (resolves references to the value map). |
| 105 | // |value| is modified in-place. |
| 106 | void NormalizeValue(std::string* value) const; |
| 107 | |
| 108 | // Allow iteration over the table cells using STL iterators. The iterator's |
| 109 | // |first| will be the row ID, and the iterator's |second| will be a |
| 110 | // pointer to a ColumnDataList containing the cell data. |
| 111 | iterator begin() const { return table_.begin(); } |
| 112 | iterator end() const { return table_.end(); } |
| 113 | |
| 114 | private: |
| 115 | // A convenience typedef for an ID-to-string mapping. |
| 116 | typedef std::map<IDString, std::string> StringMap; |
| 117 | |
| 118 | // A convenience typdef for an ID-to-index mapping, used for the column index |
| 119 | // hashtable. |
| 120 | typedef std::map<IDString, int> IndexMap; |
| 121 | |
| 122 | // Parses a line of the file which contains key/value pairs (either |
| 123 | // the column map or the value map). The starting line is parsed starting at |
| 124 | // the given index. Additional lines are read from stream_ if the line ends |
| 125 | // mid-entry. The pairs are added to the map. |
| 126 | bool ParseMap(const std::string& first_line, |
| 127 | size_t start_index, |
| 128 | StringMap* map); |
| 129 | |
| 130 | // Parses a line of the file which contains a table or row definition, |
| 131 | // starting at the given offset within the line. Additional lines are read |
| 132 | // from |stream_| of the line ends mid-row. An entry is added to |table_| |
| 133 | // using the row ID as the key, which contains a column array for the row. |
| 134 | // The supplied column hash table maps from column id to an index in |
| 135 | // |columns_|. |
| 136 | void ParseTable(const std::string& first_line, |
| 137 | size_t start_index, |
| 138 | const IndexMap* column_map); |
| 139 | |
| 140 | // Reads a single logical line from mStream into aLine. |
| 141 | // Any continuation lines are consumed and appended to the line. |
| 142 | bool ReadLine(std::string* line); |
| 143 | |
| 144 | std::ifstream stream_; |
| 145 | |
| 146 | // Lists the names of the columns for the table. |
| 147 | MorkColumnList columns_; |
| 148 | |
| 149 | // Maps hex string IDs to the corrsponding names. |
| 150 | StringMap value_map_; |
| 151 | |
| 152 | // The data of the columns in the meta row. |
| 153 | ColumnDataList meta_row_; |
| 154 | |
| 155 | // The contents of the mork database. This array pointer is owned by this |
| 156 | // class and must be deleted. |
| 157 | RowMap table_; |
| 158 | }; |
| 159 | |
| 160 | // ImportHistoryFromFirefox2 is the main entry point to the importer. |
[email protected] | 750d1df0 | 2010-02-22 18:05:00 | [diff] [blame] | 161 | void ImportHistoryFromFirefox2(const FilePath& file, ImporterBridge* bridge); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | |
[email protected] | 9fbf402 | 2011-04-10 02:04:13 | [diff] [blame] | 163 | #endif // CHROME_BROWSER_IMPORTER_MORK_READER_H_ |