2 * Copyright (C) 2011, Jamie Thompson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include "CSVSymbianEventLogParser.h"
25 #include <QStringList>
27 using namespace EventParsers;
32 inline bool operator()(const QPair<char, uint> &a, const QPair<char, uint> &b) const
34 return b.second < a.second;
38 iEventParser *CSVSymbianEventLogParser::IsValid(QFile &eventFile)
40 qDebug() << "Checking if a CSV call log file...";
42 QByteArray firstLineContent(eventFile.readLine());
44 if(firstLineContent.length() > 0)
46 // Count the non-alphanumeric characters used
47 QHash<char, uint> counts;
48 foreach(char c, firstLineContent)
51 QList<QPair<char, uint> > orderedCounts;
52 orderedCounts.reserve(counts.size());
53 foreach(char c, counts.keys())
54 if(!QChar(c).isLetterOrNumber())
55 orderedCounts.append(QPair<char, uint>(c, counts.value(c)));
57 qSort(orderedCounts.begin(), orderedCounts.end(), SortByValueDesc());
59 // Work around Q_FOREACH macro limitation when dealing with
60 // multi-typed templates (comma issue)
61 typedef QPair<char, uint> bodge;
62 foreach(bodge count, orderedCounts)
63 qDebug() << count.first << " = " << count.second;
66 // No-one would be mad enough to use quotation marks or apostrophes
67 // as their delimiter,but just in case, check the second most
68 // frequent character is present thr right number of times for
69 // the qutation marks to be present on every column heading (two
70 // per heading, less one as they're seperators)
71 if((orderedCounts.value(0).first == '"' || orderedCounts.value(0).first == '\'')
72 && ((orderedCounts.value(0).second / 2) - 1 == orderedCounts.value(1).second ))
75 delim = orderedCounts.value(1).first;
78 delim = orderedCounts.value(0).first;
80 // Check we have the essential fields we need, and grab their
82 QStringList requiredHeadings;
83 requiredHeadings << "etype" << "etime" << "remote"
84 << "direction" << "duration" << "number";
86 EventParsers::CSVSymbianEventLogParser::ColumnIndicesHash headingPositions;
87 headingPositions.reserve(requiredHeadings.count());
89 QStringList headings(QString(firstLineContent).split(delim));
90 for(QStringList::size_type i(0); i < headings.count(); ++i)
92 QRegExp content("^[\"\']?(\\w*)?[\"\']?$");
93 content.indexIn(headings.value(i).trimmed());
94 QString heading(content.cap(1));
95 qDebug() << headings.value(i) << " : " << heading;
97 // Check over the required headings
98 foreach(QString requiredHeading, requiredHeadings)
100 if(heading.toLower() == requiredHeading)
102 headingPositions[requiredHeading] = i;
103 requiredHeadings.removeOne(requiredHeading);
108 // If we found all of the required headings, continue
109 if(requiredHeadings.count() == 0)
111 return new EventParsers::CSVSymbianEventLogParser(eventFile.fileName(), headingPositions);
118 CSVSymbianEventLogParser::CSVSymbianEventLogParser(const QString &filename, const ColumnIndicesHash &columns)
122 EventTypes::EventFromFileList CSVSymbianEventLogParser::ParseFile(QFile &eventFile, const QList<uint> &recordsToReturn)
124 qDebug() << "CSV Parsing NYI!";
125 return EventTypes::EventFromFileList();