summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-reader/Bkmks.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-reader/Bkmks.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-reader/Bkmks.cpp240
1 files changed, 240 insertions, 0 deletions
diff --git a/noncore/apps/opie-reader/Bkmks.cpp b/noncore/apps/opie-reader/Bkmks.cpp
new file mode 100644
index 0000000..92ed69f
--- a/dev/null
+++ b/noncore/apps/opie-reader/Bkmks.cpp
@@ -0,0 +1,240 @@
1#include <qmessagebox.h>
2
3#include "Bkmks.h"
4
5#include "StyleConsts.h"
6#include "Markups.h"
7#include "my_list.h"
8#include "version.h"
9
10const unsigned long BkmkFile::magic = ((unsigned long)'q' << 24) | ((unsigned long)'t' << 16) | ((unsigned long)'r' << 8) | ((unsigned long)BKMKTYPE);
11
12
13Bkmk::Bkmk(const tchar* _nm, const tchar* _anno, unsigned int _p) : m_position(_p)
14{
15 int len = ustrlen(_nm)+1;
16 m_name = new tchar[len];
17 for (int i = 0; i < len; i++) m_name[i] = _nm[i];
18
19 if (_anno == NULL)
20 {
21 m_anno = new tchar[1];
22 m_anno[0] = 0;
23 }
24 else
25 {
26 len = ustrlen(_anno)+1;
27 m_anno = new tchar[len];
28 for (int i = 0; i < len; i++) m_anno[i] = _anno[i];
29 }
30}
31
32Bkmk::~Bkmk()
33{
34 if (m_name != NULL) delete [] m_name;
35 m_name = NULL;
36 if (m_anno != NULL) delete [] m_anno;
37 m_anno = NULL;
38}
39
40Bkmk& Bkmk::operator=(const Bkmk& rhs)
41{
42 if (m_name != NULL)
43 {
44 delete [] m_name;
45 m_name = NULL;
46 }
47 if (m_anno != NULL)
48 {
49 delete [] m_anno;
50 m_anno = NULL;
51 }
52 if (rhs.m_name != NULL)
53 {
54 int len = ustrlen(rhs.m_name)+1;
55 m_name = new tchar[len];
56 for (int i = 0; i < len; i++) m_name[i] = rhs.m_name[i];
57 }
58 else
59 m_name = NULL;
60 if (rhs.m_anno != NULL)
61 {
62 int len = ustrlen(rhs.m_anno)+1;
63 m_anno = new tchar[len];
64 for (int i = 0; i < len; i++) m_anno[i] = rhs.m_anno[i];
65 }
66 else
67 m_anno = NULL;
68 m_position = rhs.m_position;
69 return *this;
70}
71
72bool Bkmk::operator==(const Bkmk& rhs)
73{
74 return (m_position == rhs.m_position && ustrcmp(m_name,rhs.m_name) == 0);
75}
76
77void Bkmk::setAnno(tchar* t)
78{
79 if (m_anno != NULL)
80 {
81 delete [] m_anno;
82 m_anno = NULL;
83 }
84 if (t != NULL)
85 {
86 int len = ustrlen(t)+1;
87 m_anno = new tchar[len];
88 for (int i = 0; i < len; i++) m_anno[i] = t[i];
89 }
90 else
91 m_anno = NULL;
92}
93
94BkmkFile::BkmkFile(const char *fnm, bool w = false)
95 :
96 wt(w), isUpgraded(false)
97{
98 if (w)
99 {
100 f = fopen(fnm, "wb");
101 }
102 else
103 {
104 f = fopen(fnm, "rb");
105 }
106}
107
108BkmkFile::~BkmkFile()
109{
110 if (f != NULL) fclose(f);
111}
112
113void BkmkFile::write(tchar* nm, tchar* an, const unsigned int& pos)
114{
115 if (f != NULL)
116 {
117 unsigned short ln = ustrlen(nm);
118 fwrite(&ln,sizeof(ln),1,f);
119 fwrite(nm,sizeof(tchar),ln,f);
120 ln = ustrlen(an);
121 fwrite(&ln,sizeof(ln),1,f);
122 if (ln > 0) fwrite(an,sizeof(tchar),ln,f);
123 fwrite(&pos,sizeof(pos),1,f);
124 }
125}
126
127void BkmkFile::write(const Bkmk& b) { write(b.name(), b.anno(), b.value()); }
128
129void BkmkFile::write(CList<Bkmk>& bl)
130{
131 if (f != NULL)
132 {
133 fwrite(&magic, sizeof(magic), 1, f);
134 for (CList<Bkmk>::iterator i = bl.begin(); i != bl.end(); i++)
135 {
136 write(*i);
137 }
138 }
139}
140
141Bkmk* BkmkFile::read()
142{
143 Bkmk* b = NULL;
144 if (f != NULL)
145 {
146 unsigned short ln;
147 if (fread(&ln,sizeof(ln),1,f) == 1)
148 {
149 b = new Bkmk;
150 b->m_name = new tchar[ln+1];
151 fread(b->m_name,sizeof(tchar),ln,f);
152 b->m_name[ln] = 0;
153
154 fread(&ln,sizeof(ln),1,f);
155 b->m_anno = new tchar[ln+1];
156 if (ln > 0) fread(b->m_anno,sizeof(tchar),ln,f);
157 b->m_anno[ln] = 0;
158 fread(&b->m_position,sizeof(b->m_position),1,f);
159 }
160 }
161 return b;
162}
163
164CList<Bkmk>* BkmkFile::readall()
165{
166 CList<Bkmk>* bl = NULL;
167 if (f != NULL)
168 {
169 unsigned long newmagic;
170 fread(&newmagic, sizeof(newmagic), 1, f);
171 if (newmagic != magic)
172 {
173 if (QMessageBox::warning(NULL, "Old bookmark file!", "Which version of QTReader\ndid you upgrade from?", "0_4*", "Any other version") == 0)
174 {
175 fseek(f,0,SEEK_SET);
176 bl = readall04();
177 }
178 else
179 {
180 fseek(f,0,SEEK_SET);
181 bl = readall03();
182 }
183 isUpgraded = true;
184 }
185 else
186 {
187 bl = readall04();
188 }
189 }
190 return bl;
191}
192
193CList<Bkmk>* BkmkFile::readall04()
194{
195 CList<Bkmk>* bl = new CList<Bkmk>;
196 while (1)
197 {
198 Bkmk* b = read();
199 if (b == NULL) break;
200 bl->push_back(*b);
201 delete b;
202 }
203 return bl;
204}
205
206CList<Bkmk>* BkmkFile::readall03()
207{
208 CList<Bkmk>* bl = new CList<Bkmk>;
209 while (1)
210 {
211 Bkmk* b = read03();
212 if (b == NULL) break;
213 bl->push_back(*b);
214 delete b;
215 }
216 return bl;
217}
218
219Bkmk* BkmkFile::read03()
220{
221 Bkmk* b = NULL;
222 if (f != NULL)
223 {
224 unsigned short ln;
225 if (fread(&ln,sizeof(ln),1,f) == 1)
226 {
227 b = new Bkmk;
228 b->m_name = new tchar[ln+1];
229 fread(b->m_name,sizeof(tchar),ln,f);
230 b->m_name[ln] = 0;
231
232 ln = 0;
233 b->m_anno = new tchar[ln+1];
234 b->m_anno[ln] = 0;
235
236 fread(&b->m_position,sizeof(b->m_position),1,f);
237 }
238 }
239 return b;
240}