~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/lib/fileio/rs_fileio.cpp

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
9
** This file may be distributed and/or modified under the terms of the
 
10
** GNU General Public License version 2 as published by the Free Software
 
11
** Foundation and appearing in the file gpl-2.0.txt included in the
 
12
** packaging of this file.
 
13
**
 
14
** This program is distributed in the hope that it will be useful,
 
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
** GNU General Public License for more details.
 
18
**
 
19
** You should have received a copy of the GNU General Public License
 
20
** along with this program; if not, write to the Free Software
 
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
#include <QFileInfo>
 
28
#include <QTextStream>
 
29
#include <memory>
 
30
#include <cstddef>
 
31
#include "rs_fileio.h"
 
32
 
 
33
 
 
34
  RS_FileIO::RS_FileIO()
 
35
  {
 
36
      filters.clear();
 
37
  }
 
38
 
 
39
 
 
40
/**
 
41
 * Calls the import method of the filter responsible for the format
 
42
 * of the given file.
 
43
 *
 
44
 * @param graphic The container to which we will add
 
45
 *        entities. Usually that's an RS_Graphic entity but
 
46
 *        it can also be a polyline, text, ...
 
47
 * @param file Path and name of the file to import.
 
48
 */
 
49
bool RS_FileIO::fileImport(RS_Graphic& graphic, const QString& file,
 
50
        RS2::FormatType type) {
 
51
 
 
52
    RS_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());
 
53
 
 
54
    RS2::FormatType t;
 
55
    if (type == RS2::FormatUnknown) {
 
56
        t = detectFormat(file);
 
57
    }
 
58
    else {
 
59
        t = type;
 
60
    }
 
61
 
 
62
    if (RS2::FormatUnknown != t) {
 
63
        std::unique_ptr<RS_FilterInterface> filter(getImportFilter(file, t));
 
64
        if (filter.get() != NULL ){
 
65
            return filter->fileImport(graphic, file, t);
 
66
        }
 
67
        RS_DEBUG->print(RS_Debug::D_WARNING,
 
68
                        "RS_FileIO::fileImport: failed to import file: %s",
 
69
                        file.toLatin1().data());
 
70
    }
 
71
    else {
 
72
        RS_DEBUG->print(RS_Debug::D_WARNING,
 
73
                        "RS_FileIO::fileImport: failed to detect file format: %s",
 
74
                        file.toLatin1().data());
 
75
    }
 
76
 
 
77
    return false;
 
78
}
 
79
 
 
80
 
 
81
 
 
82
/**
 
83
 * Calls the export method of the object responsible for the format
 
84
 * of the given file.
 
85
 *
 
86
 * @param file Path and name of the file to import.
 
87
 */
 
88
bool RS_FileIO::fileExport(RS_Graphic& graphic, const QString& file,
 
89
        RS2::FormatType type) {
 
90
 
 
91
    RS_DEBUG->print("RS_FileIO::fileExport");
 
92
    //RS_DEBUG->print("Trying to export file '%s'...", file.latin1());
 
93
 
 
94
    if (type==RS2::FormatUnknown) {
 
95
        QString extension;
 
96
        extension = QFileInfo(file).suffix().toLower();
 
97
 
 
98
        if (extension=="dxf") {
 
99
            type = RS2::FormatDXFRW;
 
100
        }
 
101
        else if (extension=="cxf") {
 
102
            type = RS2::FormatCXF;
 
103
        }
 
104
        else if (extension=="lff") {
 
105
            type = RS2::FormatLFF;
 
106
        }
 
107
    }
 
108
 
 
109
    std::unique_ptr<RS_FilterInterface> filter(getExportFilter(file, type));
 
110
    if (filter.get() != NULL ){
 
111
        return filter->fileExport(graphic, file, type);
 
112
    }
 
113
    RS_DEBUG->print("RS_FileIO::fileExport: no filter found");
 
114
 
 
115
    return false;
 
116
}
 
117
 
 
118
 
 
119
/**
 
120
 * Detects and returns the file format of the given file.
 
121
 */
 
122
RS2::FormatType RS_FileIO::detectFormat(const QString& file) {
 
123
    RS2::FormatType type = RS2::FormatUnknown;
 
124
    QFileInfo fi(file);
 
125
 
 
126
    QString ext = fi.suffix().toLower();
 
127
    if (ext=="lff") {
 
128
        type = RS2::FormatLFF;
 
129
    } else if (ext=="cxf") {
 
130
        type = RS2::FormatCXF;
 
131
#ifdef DWGSUPPORT
 
132
    } else if (ext=="dwg") {
 
133
        type = RS2::FormatDWG;
 
134
#endif
 
135
    } else if (ext=="dxf") {
 
136
        type = RS2::FormatDXF1;
 
137
        QFile f(file);
 
138
 
 
139
        if (!f.open(QIODevice::ReadOnly)) {
 
140
            // Error opening file:
 
141
            RS_DEBUG->print(RS_Debug::D_WARNING,
 
142
                                "RS_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
 
143
            type = RS2::FormatUnknown;
 
144
        } else {
 
145
            RS_DEBUG->print("RS_FileIO::detectFormat: "
 
146
                "Successfully opened DXF file: %s",
 
147
                file.toLatin1().data());
 
148
 
 
149
            QTextStream ts(&f);
 
150
            QString line;
 
151
            int c=0;
 
152
            while (!ts.atEnd() && ++c<100) {
 
153
                line = ts.readLine();
 
154
                if (line=="$ACADVER" || line=="ENTITIES") {
 
155
                    type = RS2::FormatDXFRW;
 
156
                    break;
 
157
                }
 
158
                // very simple reduced DXF:
 
159
//                if (line=="ENTITIES" && c<10) {
 
160
//                    type = RS2::FormatDXFRW;
 
161
//                }
 
162
            }
 
163
            f.close();
 
164
        }
 
165
    }
 
166
 
 
167
    return type;
 
168
}
 
169
 
 
170
 
 
171
// EOF