"Fossies" - the Fresh Open Source Software Archive 
Member "cb2bib-2.0.1/src/c2b/documentCache.cpp" (12 Feb 2021, 4929 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "documentCache.cpp" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
2.0.0_vs_2.0.1.
1 /***************************************************************************
2 * Copyright (C) 2004-2021 by Pere Constans
3 * constans@molspaces.com
4 * cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
5 * See the LICENSE file that comes with this distribution.
6 ***************************************************************************/
7 #include "documentCache.h"
8
9 #include "cb2bib_utilities.h"
10 #include "document.h"
11 #include "settings.h"
12 #include "triads.h"
13
14 #include <QDataStream>
15
16
17 documentCache::documentCache()
18 : _current(_cache), _content_type(documentContents::Complete), _cache_load_size(0), _settingsP(settings::instance()) {}
19
20 documentCache::~documentCache()
21 {
22 save();
23 }
24
25
26 void documentCache::load(const QString& fn, const documentContents::Type type)
27 {
28 _content_type = type;
29 if (_load_filename == fn)
30 return;
31 unload();
32 _load_filename = fn;
33 const QString cache_dir(_settingsP->fileName("cb2Bib/CacheDirectory"));
34 if (QFileInfo::exists(cache_dir) && QFileInfo(cache_dir).isDir())
35 _cache_filename = QDir::cleanPath(cache_dir + '/' + QFileInfo(_load_filename).fileName() + ".c2b");
36 else
37 _cache_filename = _load_filename.trimmed() + ".c2b";
38 if (QFileInfo::exists(_cache_filename))
39 {
40 QFile file(_cache_filename);
41 if (file.open(QIODevice::ReadOnly))
42 {
43 QDataStream ds(&file);
44 ds >> _cache;
45 }
46 }
47 #if defined(C2B_USE_LZSSE)
48 if (!_cache.contains("clzsse"))
49 {
50 _cache.clear();
51 _cache.insert("clzsse", QByteArray());
52 }
53 #elif defined(C2B_USE_LZ4)
54 if (!_cache.contains("clz4"))
55 {
56 _cache.clear();
57 _cache.insert("clz4", QByteArray());
58 }
59 #elif defined(C2B_USE_LZO)
60 if (!_cache.contains("clzo"))
61 {
62 _cache.clear();
63 _cache.insert("clzo", QByteArray());
64 }
65 #else
66 if (!_cache.contains("czlib"))
67 {
68 _cache.clear();
69 _cache.insert("czlib", QByteArray());
70 }
71 #endif
72 _cache_load_size = _cache.size();
73 }
74
75 void documentCache::unload()
76 {
77 // Release cache memory
78 save();
79 _cache.clear();
80 _cache_filename.clear();
81 _cache_load_size = 0;
82 _load_filename.clear();
83 }
84
85 bool documentCache::setCurrent(const QString& fn, int* pdfCounter, QString* logString, int* errorCounter)
86 {
87 const QString doc_fn(QDir::cleanPath(fn.trimmed()));
88 if (doc_fn.isEmpty())
89 {
90 _current.clear();
91 return false;
92 }
93 const QString skey("sign:" + doc_fn);
94 const QString tkey(doc_fn);
95 _current.preload(skey, tkey, _content_type);
96
97 if (!_cache.contains(tkey))
98 {
99 document doc(doc_fn, document::Complete);
100 _current._text = doc.toString();
101 if (!doc.errorString().isEmpty())
102 {
103 ++(*errorCounter);
104 (*logString) += QObject::tr("% [cb2bib] %1\n").arg(doc.errorString());
105 return false;
106 }
107 _cache.insert(doc_fn, c2bUtils::compressString(_current._text));
108 if (_content_type == documentContents::Simplified)
109 {
110 _current._text = c2bUtils::toAscii(_current._text, c2bUtils::KeepWords);
111 _current._signature = triads::asciiTextSignature(_current._text);
112 }
113 else
114 _current._signature = triads::textSignature(_current._text);
115 _cache.insert(skey, c2bUtils::stringToByteArray(_current._signature));
116 _current._is_text_loaded = true;
117 _current._is_signature_loaded = true;
118 }
119 ++(*pdfCounter);
120 return true;
121 }
122
123 bool documentCache::setCurrent(const QString& fn)
124 {
125 const QString doc_fn(QDir::cleanPath(fn.trimmed()));
126 if (doc_fn.isEmpty())
127 {
128 _current.clear();
129 return false;
130 }
131 const QString skey("sign:" + doc_fn);
132 const QString tkey(doc_fn);
133 _current.preload(skey, tkey, _content_type);
134
135 if (!_cache.contains(tkey))
136 {
137 document doc(doc_fn, document::Complete);
138 _current._text = doc.toString();
139 if (!doc.errorString().isEmpty())
140 return false;
141 _cache.insert(doc_fn, c2bUtils::compressString(_current._text));
142 if (_content_type == documentContents::Simplified)
143 {
144 _current._text = c2bUtils::toAscii(_current._text, c2bUtils::KeepWords);
145 _current._signature = triads::asciiTextSignature(_current._text);
146 }
147 else
148 _current._signature = triads::textSignature(_current._text);
149 _cache.insert(skey, c2bUtils::stringToByteArray(_current._signature));
150 _current._is_text_loaded = true;
151 _current._is_signature_loaded = true;
152 }
153 return true;
154 }
155
156 void documentCache::save()
157 {
158 if (_cache_load_size != _cache.size() && !_cache_filename.isEmpty())
159 {
160 QFile file(_cache_filename);
161 if (file.open(QIODevice::WriteOnly))
162 {
163 QDataStream ds(&file);
164 ds << _cache;
165 }
166 }
167 }