-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
233 lines (185 loc) · 5.27 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QStringList>
#include <QString>
#include <QVariant>
#include <QMap>
#include <QList>
#include <QVector>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
#include <iostream>
class Node
{
public:
static const QString DateTimeFormat;
enum { Invalid = -1 };
public:
Node() : mId(Invalid), mParentId(Invalid), mComments(0)
{
}
~Node()
{
if( mComments ) {
delete mComments;
}
}
void append( Node *comment )
{
if( ! mComments ) {
mComments = new QList<Node*>();
}
// reverse an order: oldest first
mComments->prepend( comment );
}
void out( QTextStream &stream )
{
stream << QDateTime::fromMSecsSinceEpoch(mCreationTime).toLocalTime().toString(DateTimeFormat) << endl;
if( mModificationTime - mCreationTime >= 10*60*3600LL ) {
stream << QDateTime::fromMSecsSinceEpoch(mModificationTime).toLocalTime().toString(DateTimeFormat) << endl;
}
stream << mText << endl;
if( mComments )
{
foreach( Node *node, *mComments )
{
stream << "Upd (" << QDateTime::fromMSecsSinceEpoch(node->mCreationTime).toLocalTime().toString(DateTimeFormat) << ")" << endl;
stream << node->mText << endl;
}
}
}
public:
qint32 mId, mParentId;
QString mText;
qint64 mCreationTime, mModificationTime;
protected:
QList<Node*> *mComments;
};
const QString Node::DateTimeFormat = "dd.MM.yyyy hh:mm";
class Space
{
public:
Space()
{
}
~Space()
{
}
void append( Node *comment )
{
mNotes.append( comment );
}
void out( QTextStream &stream )
{
stream << "Space<" << mName << ">" << endl;
stream << "// Notes: " << mNotes.size() << endl << endl;
for( QList<Node*>::Iterator p = mNotes.begin(); p != mNotes.end(); ++p ) {
(*p)->out( stream );
stream << endl;
}
stream << endl;
}
public:
QString mName;
QList<Node*> mNotes;
};
int main( int argc, char **argv )
{
if( argc < 3 || argc > 3 )
{
if( argc == 2 && (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0) )
{
std::clog << "usage: notes <database-file> <output-file>" << std::endl;
} else {
std::clog << "error: use -h or --help for usage information" << std::endl;
}
return 0;
}
QSqlDatabase database = QSqlDatabase::addDatabase( "QSQLITE" );
database.setDatabaseName( argv[1] );
if( ! database.open() ) {
std::cerr << "error: cannot open database file `" << argv[1] << "`" << std::endl;
return 1;
}
QSqlQuery query;
QList<Node> notes, comments;
QMap< qint32, Node* > noteById;
if( ! query.exec("SELECT _id, parent_id, created, timestamp, text FROM notes ORDER BY created DESC") ) {
std::cerr << "query error: " << query.lastError().text().toLatin1().data() << std::endl;
return 1;
}
while( query.next() )
{
Node node;
node.mId = query.value(0).toInt();
node.mParentId = query.value(1).toInt();
node.mCreationTime = query.value(2).toLongLong();
node.mModificationTime = query.value(3).toLongLong();
node.mText = query.value(4).toString();
if( node.mParentId == Node::Invalid )
{
notes.append( node );
noteById.insert( node.mId, & notes.back() );
} else {
comments.append( node );
}
}
// append comments to related notes
for( QList<Node>::Iterator p = comments.begin(); p != comments.end(); ++p )
{
Node &commentNode = *p;
noteById[commentNode.mParentId]->append( &commentNode );
}
if( ! query.exec("SELECT _id, stream_name FROM streams") ) {
std::cerr << "query error: " << query.lastError().text().toLatin1().data() << std::endl;
return 1;
}
QList<Space> spaces;
QMap<qint32, Space*> spaceById;
while( query.next() )
{
Space space;
space.mName = query.value(1).toString();
spaces.append( space );
spaceById.insert( query.value(0).toInt(), & spaces.back() );
}
if( ! query.exec("SELECT note_id, stream_id FROM notes_streams") ) {
std::cerr << "query error: " << query.lastError().text().toLatin1().data() << std::endl;
return 1;
}
// fetch notes location (relating to an exact spaces) information
QMultiMap<qint32, Space*> spaceByNoteId;
while( query.next() ) {
spaceByNoteId.insert( query.value(0).toInt(), spaceById[query.value(1).toInt()] );
}
// append notes to related spaces
for( QList<Node>::Iterator np = notes.begin(); np != notes.end(); ++np )
{
QList<Space*> spaces = spaceByNoteId.values( np->mId );
for( QList<Space*>::Iterator sp = spaces.begin(); sp != spaces.end(); ++sp ) {
(*sp)->append( & *np );
}
}
QFile outputFile( argv[2] );
QTextStream stream( &outputFile );
stream.setCodec( "UTF-8" );
if( ! outputFile.open(QIODevice::WriteOnly) ) {
std::cerr << "error: cannot write output file `" << argv[2] << "`" << std::endl;
return 1;
}
for( QList<Space>::Iterator p = spaces.begin(); p != spaces.end(); ++p ) {
p->out( stream );
stream << endl;
}
stream.flush();
outputFile.close();
std::cout << "Spaces: " << spaces.size() << std::endl;
std::cout << "Notes: " << notes.size() << std::endl;
std::cout << "Comments: " << comments.size() << std::endl;
return 0;
}