/src/libmwaw/src/lib/WriteNowEntry.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
2 | | |
3 | | /* libmwaw |
4 | | * Version: MPL 2.0 / LGPLv2+ |
5 | | * |
6 | | * The contents of this file are subject to the Mozilla Public License Version |
7 | | * 2.0 (the "License"); you may not use this file except in compliance with |
8 | | * the License or as specified alternatively below. You may obtain a copy of |
9 | | * the License at http://www.mozilla.org/MPL/ |
10 | | * |
11 | | * Software distributed under the License is distributed on an "AS IS" basis, |
12 | | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 | | * for the specific language governing rights and limitations under the |
14 | | * License. |
15 | | * |
16 | | * Major Contributor(s): |
17 | | * Copyright (C) 2002 William Lachance (wrlach@gmail.com) |
18 | | * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net) |
19 | | * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
20 | | * Copyright (C) 2006, 2007 Andrew Ziem |
21 | | * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr) |
22 | | * |
23 | | * |
24 | | * All Rights Reserved. |
25 | | * |
26 | | * For minor contributions see the git repository. |
27 | | * |
28 | | * Alternatively, the contents of this file may be used under the terms of |
29 | | * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
30 | | * in which case the provisions of the LGPLv2+ are applicable |
31 | | * instead of those above. |
32 | | */ |
33 | | |
34 | | /* |
35 | | * entry for WriteNow |
36 | | */ |
37 | | #ifndef WRITE_NOW_ENTRY |
38 | | # define WRITE_NOW_ENTRY |
39 | | |
40 | | #include <iostream> |
41 | | #include <map> |
42 | | #include <string> |
43 | | |
44 | | #include "libmwaw_internal.hxx" |
45 | | #include "MWAWEntry.hxx" |
46 | | |
47 | | /** class to store entry in a WriteNow document */ |
48 | | struct WriteNowEntry final : public MWAWEntry { |
49 | | //! construtor |
50 | | WriteNowEntry() |
51 | 7.55M | : MWAWEntry() |
52 | 7.55M | , m_fileType(-1) |
53 | 7.55M | { |
54 | 30.2M | for (auto &val : m_val) val=0; |
55 | 7.55M | } |
56 | 11.2M | WriteNowEntry(WriteNowEntry const &)=default; |
57 | 642k | WriteNowEntry &operator=(WriteNowEntry const &)=default; |
58 | 25.7k | WriteNowEntry &operator=(WriteNowEntry &&)=default; |
59 | | //! destructor |
60 | | ~WriteNowEntry() final; |
61 | | //! returns true if this entry store a zone |
62 | | bool isZoneType() const |
63 | 82.2k | { |
64 | 82.2k | return m_fileType == 4 || m_fileType == 6; |
65 | 82.2k | } |
66 | | //! returns true if this is a zone |
67 | | bool isZone() const |
68 | 45.9k | { |
69 | 45.9k | return isZoneType() && valid(); |
70 | 45.9k | } |
71 | | //! operator<< |
72 | | friend std::ostream &operator<<(std::ostream &o, WriteNowEntry const &entry) |
73 | 0 | { |
74 | 0 | if (entry.type().length()) { |
75 | 0 | o << entry.type(); |
76 | 0 | if (entry.id() >= 0) o << "[" << entry.id() << "]"; |
77 | 0 | o << "="; |
78 | 0 | } |
79 | 0 | o << "["; |
80 | 0 | switch (entry.m_fileType) { |
81 | 0 | case 0x4: |
82 | 0 | o << "zone,"; |
83 | 0 | break; |
84 | 0 | case 0x6: |
85 | 0 | o << "zone2,"; |
86 | 0 | break; |
87 | 0 | case 0xc: |
88 | 0 | o << "none/data,"; |
89 | 0 | break; |
90 | 0 | default: |
91 | 0 | o << "#type=" << entry.m_fileType << ","; |
92 | 0 | } |
93 | 0 | for (int i = 0; i < 4; i++) { |
94 | 0 | if (entry.m_val[i]) o << "v" << i << "=" << std::hex << entry.m_val[i] << std::dec << ","; |
95 | 0 | } |
96 | 0 | o << "],"; |
97 | 0 | return o; |
98 | 0 | } |
99 | | //! the file entry id |
100 | | int m_fileType; |
101 | | //! other values |
102 | | int m_val[4]; |
103 | | }; |
104 | | |
105 | | /** the manager of the entries */ |
106 | | struct WriteNowEntryManager { |
107 | | WriteNowEntryManager() |
108 | 84.5k | : m_posMap() |
109 | 84.5k | , m_typeMap() {} |
110 | | |
111 | | //! return an entry for a position |
112 | | WriteNowEntry get(long pos) const |
113 | 0 | { |
114 | 0 | auto it = m_posMap.find(pos); |
115 | 0 | if (it == m_posMap.end()) |
116 | 0 | return WriteNowEntry(); |
117 | 0 | return it->second; |
118 | 0 | } |
119 | | |
120 | | //! add a new entry |
121 | | bool add(WriteNowEntry const &entry) |
122 | 2.27M | { |
123 | 2.27M | if (!entry.valid()) return false; |
124 | 786k | if (m_posMap.find(entry.begin()) != m_posMap.end()) { |
125 | 44.4k | MWAW_DEBUG_MSG(("WriteNowEntryManager:add: an entry for this position already exists\n")); |
126 | 44.4k | return false; |
127 | 44.4k | } |
128 | 741k | auto it = m_posMap.insert(std::pair<long, WriteNowEntry>(entry.begin(), entry)).first; |
129 | 741k | m_typeMap.insert |
130 | 741k | (std::multimap<std::string, WriteNowEntry const *>::value_type(entry.type(), &(it->second))); |
131 | 741k | return true; |
132 | 786k | } |
133 | | |
134 | | //! reset the data |
135 | | void reset() |
136 | 27.0k | { |
137 | 27.0k | m_posMap.clear(); |
138 | 27.0k | m_typeMap.clear(); |
139 | 27.0k | } |
140 | | //! the list of entries by position |
141 | | std::map<long, WriteNowEntry> m_posMap; |
142 | | //! the list of entries |
143 | | std::multimap<std::string, WriteNowEntry const *> m_typeMap; |
144 | | }; |
145 | | |
146 | | #endif |