~ubuntu-branches/ubuntu/precise/kbibtex/precise

« back to all changes in this revision

Viewing changes to src/fileimporterbibutils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Patrick Davies
  • Date: 2007-10-28 10:06:45 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071028100645-jw9oadntiao5oszf
Tags: 0.2-0ubuntu1
* New upstream release.
* Bumped up compat and debhelper versions to 5.
* Added Original-Maintainer as per Ubuntu spec.
* Moved Homepage as per Debian spec.
* Removed from patches:
  - 10-invalid_pure_specifier.dpatch - applied upstream.
  - 11-accented_characters.dpatch - applied upstream.
  - 12-empty_pubmed_search.dpatch - applied upstream.
  - 13-store_search_urls.dpatch - applied upstream.
* Updated 14-xslt_export and use simple-patchsys instead.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2004-2005 by Thomas Fischer                             *
 
3
 *   fischer@unix-ag.uni-kl.de                                             *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#ifdef HAVE_LIBBIBUTILS
 
25
 
 
26
#include <stdio.h>
 
27
 
 
28
#include <qfile.h>
 
29
#include <qdir.h>
 
30
#include <qstringlist.h>
 
31
 
 
32
#include <fileimporterbibtex.h>
 
33
#include "fileimporterbibutils.h"
 
34
 
 
35
namespace BibTeX
 
36
{
 
37
 
 
38
    FileImporterBibUtils::FileImporterBibUtils( InputFormat inputFormat )
 
39
            : FileImporter(), m_workingDir( createTempDir() ), m_inputFormat( inputFormat ), m_bibTeXImporter( new FileImporterBibTeX() )
 
40
    {
 
41
// nothing
 
42
    }
 
43
 
 
44
    FileImporterBibUtils::~FileImporterBibUtils()
 
45
    {
 
46
        deleteTempDir( m_workingDir );
 
47
        delete m_bibTeXImporter;
 
48
    }
 
49
 
 
50
    File* FileImporterBibUtils::load( QIODevice *iodevice )
 
51
    {
 
52
        File *result = NULL;
 
53
        m_cancelFlag = FALSE;
 
54
        QString inputFileName = QString( m_workingDir ).append( "/input.tmp" );
 
55
        QString bibFileName = QString( m_workingDir ).append( "/input.bib" );
 
56
 
 
57
        /** write temporary data to file to be read by bibutils */
 
58
        QFile inputFile( inputFileName );
 
59
        if ( inputFile.open( IO_WriteOnly ) )
 
60
        {
 
61
            const Q_LONG bufferSize = 16384;
 
62
            char *buffer = new char[bufferSize];
 
63
            Q_LONG readBytes = 0;
 
64
            while (( readBytes = iodevice->readBlock( buffer, bufferSize ) ) > 0 )
 
65
            {
 
66
                if ( inputFile.writeBlock( buffer, readBytes ) != readBytes )
 
67
                {
 
68
                    readBytes = -1;
 
69
                    break;
 
70
                }
 
71
            }
 
72
            inputFile.close();
 
73
            delete buffer;
 
74
            if ( readBytes == -1 )
 
75
                return NULL;
 
76
        }
 
77
        else
 
78
            return NULL;
 
79
 
 
80
        param p;
 
81
        bibl b;
 
82
        bibl_init( &b );
 
83
        bibl_initparams( &p, ( int )m_inputFormat, BIBL_BIBTEXOUT );
 
84
        p.verbose = 3;
 
85
        FILE *fp = fopen( inputFileName.ascii(), "r" );
 
86
        int err = BIBL_ERR_BADINPUT;
 
87
        if ( fp )
 
88
        {
 
89
            err = bibl_read( &b, fp, inputFileName.ascii(), ( int )m_inputFormat, &p );
 
90
            fclose( fp );
 
91
        }
 
92
        if ( err != BIBL_OK )
 
93
            return NULL;
 
94
        qDebug( "Num read references: %ld\n", b.nrefs );
 
95
 
 
96
        fp = fopen( bibFileName.ascii(), "w" );
 
97
        err = -1;
 
98
        if ( fp != NULL )
 
99
        {
 
100
            err = bibl_write( &b, fp, BIBL_BIBTEXOUT, &p );
 
101
            fclose( fp );
 
102
        }
 
103
        bibl_free( &b );
 
104
        if ( err != BIBL_OK )
 
105
            return NULL;
 
106
 
 
107
        QFile bibFile( bibFileName );
 
108
        if ( bibFile.open( IO_ReadOnly ) )
 
109
        {
 
110
            result = m_bibTeXImporter->load( &bibFile );
 
111
            bibFile.close();
 
112
        }
 
113
 
 
114
        return result;
 
115
    }
 
116
 
 
117
    bool FileImporterBibUtils::guessCanDecode( const QString & /*text*/ )
 
118
    {
 
119
        return FALSE; // FIXME
 
120
    }
 
121
 
 
122
    void FileImporterBibUtils::cancel()
 
123
    {
 
124
        m_bibTeXImporter->cancel();
 
125
        m_cancelFlag = TRUE;
 
126
    }
 
127
 
 
128
    QString FileImporterBibUtils::createTempDir()
 
129
    {
 
130
        QString result = QString::null;
 
131
        QFile *devrandom = new QFile( "/dev/random" );
 
132
 
 
133
        if ( devrandom->open( IO_ReadOnly ) )
 
134
        {
 
135
            Q_UINT32 randomNumber;
 
136
            if ( devrandom->readBlock(( char* ) & randomNumber, sizeof( randomNumber ) ) > 0 )
 
137
            {
 
138
                randomNumber |= 0x10000000;
 
139
                result = QString( "/tmp/bibtex-%1" ).arg( randomNumber, sizeof( randomNumber ) * 2, 16 );
 
140
                if ( !QDir().mkdir( result ) )
 
141
                    result = QString::null;
 
142
            }
 
143
            devrandom->close();
 
144
        }
 
145
 
 
146
        delete devrandom;
 
147
 
 
148
        return result;
 
149
    }
 
150
 
 
151
    void FileImporterBibUtils::deleteTempDir( const QString& directory )
 
152
    {
 
153
        QDir dir = QDir( directory );
 
154
        QStringList subDirs = dir.entryList( QDir::Dirs );
 
155
        for ( QStringList::Iterator it = subDirs.begin(); it != subDirs.end(); it++ )
 
156
        {
 
157
            if (( QString::compare( *it, "." ) != 0 ) && ( QString::compare( *it, ".." ) != 0 ) )
 
158
                deleteTempDir( *it );
 
159
        }
 
160
        QStringList allEntries = dir.entryList( QDir::All );
 
161
        for ( QStringList::Iterator it = allEntries.begin(); it != allEntries.end(); it++ )
 
162
            dir.remove( *it );
 
163
 
 
164
        QDir().rmdir( directory );
 
165
    }
 
166
 
 
167
}
 
168
 
 
169
#endif // HAVE_LIBBIBUTILS