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
|
/* This file is part of the KDE project
* Copyright (C) 2002 Nadeem Hasan <nhasan@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "tdefile_txt.h"
#include <kgenericfactory.h>
#include <kdebug.h>
#include <tqfile.h>
#include <tqstringlist.h>
#include <tqregexp.h>
typedef KGenericFactory<KTxtPlugin> TxtFactory;
K_EXPORT_COMPONENT_FACTORY(tdefile_txt, TxtFactory("tdefile_txt"))
KTxtPlugin::KTxtPlugin(TQObject *parent, const char *name,
const TQStringList &args) : KFilePlugin(parent, name, args)
{
kdDebug(7034) << "Text file meta info plugin\n";
makeMimeTypeInfo( "text/plain" );
}
void KTxtPlugin::makeMimeTypeInfo(const TQString& mimeType)
{
KFileMimeTypeInfo* info = addMimeTypeInfo(mimeType);
KFileMimeTypeInfo::GroupInfo* group =
addGroupInfo(info, "General", i18n("General"));
KFileMimeTypeInfo::ItemInfo* item;
item = addItemInfo(group, "Lines", i18n("Lines"), TQVariant::Int);
setAttributes(item, KFileMimeTypeInfo::Averaged);
item = addItemInfo(group, "Words", i18n("Words"), TQVariant::Int);
setAttributes(item, KFileMimeTypeInfo::Averaged);
item = addItemInfo(group, "Characters", i18n("Characters"), TQVariant::ULongLong);
setAttributes(item, KFileMimeTypeInfo::Averaged);
item = addItemInfo(group, "Format", i18n("Format"), TQVariant::String);
}
bool KTxtPlugin::readInfo(KFileMetaInfo& info, uint)
{
if ( info.path().isEmpty() ) // remote file
return false;
TQFile f(info.path());
if (!f.open(IO_ReadOnly))
return false;
bool firstline = true;
int totLines = 0;
int totWords = 0;
unsigned long long totChars = f.size();
TQString fileFormat;
TQString line;
bool skipTotals = (totChars > 100*1024); // 100K is the max we read
unsigned int bytesRead = 0;
while (!f.atEnd())
{
f.readLine(line, 4096);
int len = line.length();
// The checks below are necessary to handle embedded NULLs
// TQFile::readLine() does not handle them well
bytesRead += len;
if (bytesRead > totChars)
break;
if (len == 0)
break;
if (firstline)
{
firstline = false;
if (line[len-1]=='\n')
{
if (len>=2 && line[len-2]=='\r')
fileFormat = i18n("DOS");
else
fileFormat = i18n("UNIX");
}
else if (line[len-1]=='\r')
fileFormat = i18n("Macintosh");
if (skipTotals)
break;
}
totWords += (TQStringList::split(TQRegExp("\\s+"), line)).count();
totLines++;
}
if (fileFormat.isEmpty())
fileFormat = i18n("Unknown");
kdDebug(7034) << "Lines: " << totLines << endl;
kdDebug(7034) << "Words: " << totWords << endl;
kdDebug(7034) << "Characters: " << totChars << endl;
kdDebug(7034) << "fileFormat: " << fileFormat << endl;
KFileMetaInfoGroup group = appendGroup(info, "General");
if (!skipTotals)
{
appendItem(group, "Lines", totLines);
appendItem(group, "Words", totWords);
}
appendItem(group, "Characters", totChars);
appendItem(group, "Format", fileFormat);
return true;
}
#include "tdefile_txt.moc"
|