forked from guangmingwan/rs97-txtReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·150 lines (137 loc) · 3.59 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
/*
*
*
*
*
*/
#if QT_VERSION >= 0x050000
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#else
#include <QApplication>
#include <QDialog>
#endif
#include "qtxtreader.h"
#include <iostream>
#include <QDir>
#include <QFile>
#include <QTextCodec>
#include <QtPlugin>
#include <QDebug>
#include <QDesktopWidget>
#include "charsetdetect.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <wchar.h>
using namespace std;
#define Q_OS_DARWIN
//Q_IMPORT_PLUGIN(qwslinuxinputkbddriver);
#define BUFFER_SIZE 4096
const char *detect_charset(const char *fileName)
{
FILE *fd = fopen(fileName, "r");
csd_t csd = csd_open();
if (csd == (csd_t)-1)
{
qDebug("csd_open failed: %s", fileName);
return NULL;
}
int size;
char buf[BUFFER_SIZE] = {0};
while ((size = fread(buf, 1, sizeof(buf), fd)) != 0)
{
int result = csd_consider(csd, buf, size);
if (result < 0)
{
qDebug("csd_consider failed");
return NULL;
}
else if (result > 0)
{
// Already have enough data
break;
}
}
const char *result = csd_close(csd);
if (result == NULL)
{
qDebug("Unknown character set");
return NULL;
}
else
{
qDebug("%s", result);
return result;
}
}
int main(int argc, char **argv)
{
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
//this->setFont(QFont("wenquanyi", 12, QFont::Bold));
QApplication app(argc, argv);
#ifdef __APPLE__
QString appPath = QDir::currentPath();
qDebug("appPath %s", qPrintable(appPath));
app.setStyleSheet(
"QTextEdit"
"{"
"background-image: url(" + appPath + "/bin/background.png);"
//"background2:rgb(253, 230, 224);"
"}");
#else
app.setStyleSheet(
"QTextEdit"
"{"
"background-image: url(./background.png);"
//"background2:rgb(253, 230, 224);"
"}");
#endif
qtxtReader *reader = new qtxtReader;
//QGridLayout *gl = new QGridLayout(dialog);
//gl->addWidget(rte,0,0,1,1);
reader->setWindowTitle(QObject::tr("rs97 text reader"));
#ifdef __APPLE__
reader->setFixedSize(320, 240);
#else
QDesktopWidget desktop;
QRect rec = desktop.geometry();
int height = rec.height();
int width = rec.width();
reader->setMinimumWidth(width);
reader->setMinimumHeight(height);
reader->setFixedSize(width, height);
#endif
reader->show();
#ifdef __APPLE__
#else
QApplication::setOverrideCursor(Qt::BlankCursor); //Hide the mouse
#endif
if (argc > 1)
{
QString fileName = QString::fromUtf8(argv[1]);
//QTextCodec *codec = QTextCodec::codecForName("utf-8");
QFile file(argv[1]);
const char *code = detect_charset(argv[1]);
const wchar_t *encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
if (file.open(QIODevice::ReadOnly))
{
reader->fileName = argv[1];
reader->setWindowTitle(fileName);
QTextStream stream(&file);
stream.setCodec(code);
reader->setCode(code);
QString str = stream.readAll();
qDebug("open file : %ls", encodedName);
reader->setText(str);
}
else
{
QString str = QString("open file fail:%1 \n %2").arg(fileName, file.errorString());
reader->setText(str);
}
}
return app.exec();
}