unauthorizedAccept option
[dyskinesia.git] / tools / js2hif / js2hif.cpp
blobc157e6d2f0d64ea9ffc5747e6ef94218d8014e30
1 /* coded by Ketmar // Vampire Avalon (ketmar@ketmar.no-ip.org)
3 * This program is free software. It comes without any warranty, to
4 * the extent permitted by applicable law. You can redistribute it
5 * and/or modify it under the terms of the Do What The Fuck You Want
6 * To Public License, Version 2, as published by Sam Hocevar. See
7 * http://sam.zoy.org/wtfpl/COPYING for more details.
8 */
9 //#include <QtCore>
10 #include <QDebug>
12 #include <QTime>
14 #include "k8history.h"
15 #include "k8strutils.h"
17 #include "js2hif.h"
20 #define KHISTORY_DATE_FORMAT "yyyy/MM/dd HH:mm:ss"
23 static QString dateToString (const QDateTime &dt) {
24 if (dt.isNull()) return QDateTime::currentDateTime().toUTC().toString(KHISTORY_DATE_FORMAT);
25 return dt.toUTC().toString(KHISTORY_DATE_FORMAT);
29 static void dumpMsg (QFile &fo, const HistoryMessage &msg) {
30 QByteArray res;
31 // type
32 switch (msg.type) {
33 case HistoryMessage::Incoming: res.append("*>"); break;
34 case HistoryMessage::Outgoing: res.append("*<"); break;
35 case HistoryMessage::Server: res.append("*="); break;
36 case HistoryMessage::Info: res.append("**"); break;
38 res.append(' ');
39 // date
40 //res.append(QString::number(msg.date.toUTC().toTime_t()).toAscii());
41 res.append(dateToString(msg.date).toAscii());
42 res.append(' ');
43 // uni
44 res.append(msg.uni.toUtf8());
45 res.append('\n');
46 // action
47 if (!msg.action.isEmpty()) {
48 res.append(':');
49 res.append(K8Str::escapeNL(msg.action).toUtf8());
50 res.append('\n');
52 // text
53 res.append(' ');
54 //res.append(K8Str::escapeNL(msg.text).toUtf8());
55 res.append(K8Str::escapeNL(msg.text).toUtf8());
56 res.append('\n');
57 // done
58 fo.write(res);
62 int main (int argc, char *argv[]) {
63 Q_UNUSED(argc)
64 Q_UNUSED(argv)
65 QString fiName;//("dump.hif");
66 QString foName;//("dump");
67 QString myUNI;
69 if (argc < 3) {
70 qDebug() << "usage: hif2hist basename myuni";
73 fiName = QString(argv[1]);
74 foName = QString(argv[1])+".hif";
75 myUNI = argv[2];
77 QTime st;
78 //st.start();
79 //qDebug() << "time taken (ms):" << st.elapsed();
81 HistoryFile hf(fiName, myUNI);
82 qDebug() << "opening file...";
83 st.start();
84 if (!hf.open(HistoryFile::Read)) {
85 qDebug() << "can't open history file!";
86 return 1;
88 qDebug() << "time taken (ms):" << st.elapsed();
89 qDebug() << "number of messages:" << hf.count();
91 QFile fo(foName);
92 fo.remove();
93 if (!fo.open(QIODevice::WriteOnly)) {
94 qWarning() << "can't open output file";
95 return 1;
98 qDebug() << "dumping file...";
99 int mNo = 0, mTotal = 0;
100 HistoryMessage msg;
101 st.start();
102 while (hf.read(mNo++, msg)) {
103 if (!msg.valid) {
104 qDebug() << mNo-1 << "is invalid!";
105 } else {
106 ++mTotal;
107 dumpMsg(fo, msg);
108 if (mTotal%8192 == 1) {
109 fprintf(stderr, "\r%d messages processed", mTotal); fflush(stderr);
113 fprintf(stderr, "\r%d messages processed\n", mTotal); fflush(stderr);
114 qDebug() << "time taken (ms):" << st.elapsed();
115 hf.close();
116 fo.close();
118 return 0;