From: Frederik Gladhorn Date: Tue, 9 Oct 2007 22:43:08 +0000 (+0000) Subject: Cleanup and improve the vokabeln reader. X-Git-Tag: v3.95.0~14 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=8167ae5701f2d54c2c931421569019d318e60d65;p=libqmvoc.git Cleanup and improve the vokabeln reader. Original patch by Ilja. > This patch should make possible to open .voc with longer header. (e.g. > http://www.vokabeln.de/v1/download/GB_Technik.zip). > KEduVocDocument::setDocumentComment changed to allow to insert > multiline file comments from .voc files. CCMAIL: iljuss@gmail.com svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=723538 --- diff --git a/keduvocdocument/keduvocdocument.cpp b/keduvocdocument/keduvocdocument.cpp index 5cedd1e..79133bc 100644 --- a/keduvocdocument/keduvocdocument.cpp +++ b/keduvocdocument/keduvocdocument.cpp @@ -189,7 +189,42 @@ KEduVocDocument::FileType KEduVocDocument::detectFileType( const QString &fileNa line2 = ts.readLine(); } + /* + * Vokabeln.de files: + The header seems to be something like this: + + "Name + Lang1 - Lang2",123,234,456 + 0 + + or something longer: + + "Name + Lang1 - Lang2 + [..] + Blah, blah, blah...",123,234,456 + 0 + */ + + QString tmp; + + if ( line1.startsWith(QChar::fromLatin1('"'))) { + ts.seek(0); + tmp = ts.readLine(); + // There shouldn't be headers longer than 10 lines. + for ( int x=0; x < 10; x++) { + if (tmp.contains( "\"," )) { + tmp = ts.readLine(); + if (tmp.endsWith('0')) { + f->close(); + return Vokabeln; + } + } + tmp = ts.readLine(); + } + } f->close(); + if ( line1.startsWith(QString::fromLatin1(" 0 ) { return Pauker; @@ -205,18 +240,6 @@ KEduVocDocument::FileType KEduVocDocument::detectFileType( const QString &fileNa return Wql; } - /* - 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; } @@ -973,7 +996,7 @@ void KEduVocDocument::setLicense( const QString & s ) void KEduVocDocument::setDocumentComment( const QString & s ) { - d->m_comment = s.simplified(); + d->m_comment = s.trimmed(); } diff --git a/keduvocdocument/keduvocvokabelnreader.cpp b/keduvocdocument/keduvocvokabelnreader.cpp index a12310a..83dde88 100644 --- a/keduvocdocument/keduvocvokabelnreader.cpp +++ b/keduvocdocument/keduvocvokabelnreader.cpp @@ -52,40 +52,53 @@ bool KEduVocVokabelnReader::readDoc( KEduVocDocument *doc ) inputStream.setAutoDetectUnicode( false ); inputStream.seek( 0 ); - QString title, - lang1, - lang2, - expression, - original, - translation, - lessonDescr, - temp; - - int i, - wordCount, - lesson; + QString title; + QString lang1; + QString lang2; + QString expression; + QString original; + QString translation; + QString lessonDescr; + QString temp; + QString comment; + + int i; + int wordCount; + int lesson; int lines = 10000; - QStringList titles, - languages, - words; + QStringList titles; + QStringList languages; + QStringList words; - bool keepGoing = true; + temp = inputStream.readLine(); - while ( keepGoing ) { - temp = inputStream.readLine(); - keepGoing = !( temp.indexOf( "\"," ) > 0 ); - title.append( temp ); - if ( keepGoing ) - title.append( " " ); + if ( temp.startsWith ( QChar::fromLatin1 ( '"' ) ) ) { + while ( temp != QChar::fromLatin1('0') ) { + if ( title.isEmpty() ) { + title = temp; + } else { + comment.append( temp.append('\n') ); + } + temp = inputStream.readLine(); + } + } + + // 1 line header + if (comment.isEmpty()) { + titles = title.split( "\"," ); + m_doc->setTitle(titles[0].mid(1)); + } + // longer header + else { + titles = comment.split( "\"," ); + m_doc->setDocumentComment(titles[0]); + m_doc->setTitle(title.mid(1)); } - titles = title.split( "\"," ); - m_doc->setTitle( titles[0].mid( 1 ) ); wordCount = titles[1].section( ',', 0, 0 ).toInt(); - inputStream.readLine(); inputStream.readLine(); lang1 = inputStream.readLine(); @@ -98,23 +111,25 @@ bool KEduVocVokabelnReader::readDoc( KEduVocDocument *doc ) m_doc->identifier(1).setLocale( languages[1].mid( 1 ) ); m_doc->identifier(1).setName( languages[1].mid( 1 ) ); - keepGoing = true; - while ( keepGoing ) - keepGoing = !( inputStream.readLine().indexOf( "8. Lernhilfe" ) > 0 ); //DO NOT translate + while ( !temp.contains("8. Lernhilfe") ) { + temp = inputStream.readLine(); //DO NOT translate + } - for ( i = 0; i <= 14; i++ ) + for ( i = 0; i <= 14; i++ ) { inputStream.readLine(); + } for ( i = 0; i < wordCount - 1; i++ ) { int c = 0; - expression.resize( 0 ); + expression.clear(); while ( c < 2 ) { temp = inputStream.readLine(); c+= temp.count( "\",", Qt::CaseSensitive ); expression.append( temp ); - if ( c < 2 ) + if ( c < 2 ) { expression.append( " " ); + } } words = expression.split( "\"," ); @@ -127,7 +142,6 @@ bool KEduVocVokabelnReader::readDoc( KEduVocDocument *doc ) kve.setTranslation( 1, translation ); kve.translation( 1 ).gradeFrom( 0 ).setGrade( 0 ); kve.translation( 0 ).gradeFrom( 1 ).setGrade( 0 ); - /// @todo lesson might need a -1. I have no specs or documents to verify. kve.setLesson( lesson ); m_doc->appendEntry( &kve );