]> Git trees. - libqmvoc.git/commitdiff
Make the reader detection code a little cleaner.
authorFrederik Gladhorn <gladhorn@kde.org>
Tue, 18 Sep 2007 13:21:08 +0000 (13:21 +0000)
committerFrederik Gladhorn <gladhorn@kde.org>
Tue, 18 Sep 2007 13:21:08 +0000 (13:21 +0000)
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=713990

keduvocdocument/keduvocdocument.cpp

index aaaea591c80e6ad3cded1717f34f053af7264d5f..3144a4ab8a19b7e99fc5c14ee5f0a39219cf8de4 100644 (file)
@@ -178,49 +178,47 @@ void KEduVocDocument::insertEntry( KEduVocExpression *expression, int index )
 KEduVocDocument::FileType KEduVocDocument::detectFileType( const QString &fileName )
 {
     QIODevice * f = KFilterDev::deviceForFile( fileName );
-    if ( !f->open( QIODevice::ReadOnly ) )
+    if ( !f->open( QIODevice::ReadOnly ) ) {
+        kDebug() << "Warning, could not open QIODevice for file: " << fileName;
         return Csv;
-
-    QDataStream is( f );
-
-    qint8 c1, c2, c3, c4, c5;
-    is >> c1
-    >> c2
-    >> c3
-    >> c4
-    >> c5;  // guess filetype by first x bytes
+    }
 
     QTextStream ts( f );
-    QString line;
+    QString line1;
     QString line2;
 
-    line = ts.readLine();
-    if ( !ts.atEnd() )
+    line1 = ts.readLine();
+    if ( !ts.atEnd() ) {
         line2 = ts.readLine();
-    line.prepend( c5 );
-    line.prepend( c4 );
-    line.prepend( c3 );
-    line.prepend( c2 );
-    line.prepend( c1 );
-
-    if ( !is.device()->isOpen() )
-        return KvdNone;
+    }
 
     f->close();
-    if ( c1 == '<' && c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l' ) {
-        if ( line2.indexOf( "pauker", 0 ) >  0 )
+    if ( line1.startsWith(QString::fromLatin1("<?xml")) ) {
+        if ( line2.indexOf( "pauker", 0 ) >  0 ) {
             return Pauker;
-        else if ( line2.indexOf( "xdxf", 0 ) >  0 )
+        }
+        if ( line2.indexOf( "xdxf", 0 ) >  0 ) {
             return Xdxf;
-        else
+        } else {
             return Kvtml;
+        }
     }
 
-    if ( line == WQL_IDENT )
+    if ( line1 == WQL_IDENT ) {
         return Wql;
+    }
 
-    if ( c1 == '"' && ( line.contains( '"' ) == 1 || line.contains( QRegExp( "\",[0-9]" ) ) ) )
+    /*
+    Vokabeln.de files:
+    The header seems to be something like this:
+    "Name
+    Lang1 - Lang2",123,234,456
+    */
+    if ( line1.startsWith(QChar::fromLatin1('"')) &&
+        ((line1.contains(QRegExp( "\",[0-9]" ))) ||
+        (line2.contains(QRegExp( "\",[0-9]" ))))) {
         return Vokabeln;
+    }
 
     return Csv;
 }