3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/28/2010 Added this text. :-)
15 // JLH 09/07/2010 Fixed file detection algorithm.
20 #include "filtercxf.h"
21 #include "filterdxf.h"
22 #include "filterdxf1.h"
23 #include "filterinterface.h"
25 FileIO * FileIO::uniqueInstance = NULL;
33 * @return Instance to the unique import object.
35 /*static*/ FileIO * FileIO::instance()
38 uniqueInstance = new FileIO();
40 return uniqueInstance;
44 * Registers a new import filter.
46 void FileIO::registerFilter(FilterInterface * f)
52 * @return List of registered filters.
54 QList<FilterInterface *> FileIO::getFilterList()
60 * @return Filter which can import the given file type.
62 FilterInterface * FileIO::getImportFilter(RS2::FormatType t)
64 for(int i=0; i<filterList.size(); i++)
66 FilterInterface * f = filterList[i];
76 * @return Filter which can export the given file type.
78 FilterInterface * FileIO::getExportFilter(RS2::FormatType t)
80 for(int i=0; i<filterList.size(); i++)
82 FilterInterface * f = filterList[i];
92 * Calls the import method of the filter responsible for the format
95 * @param graphic The container to which we will add
96 * entities. Usually that's an Drawing entity but
97 * it can also be a polyline, text, ...
98 * @param file Path and name of the file to import.
100 bool FileIO::fileImport(Drawing & graphic, const QString & file, RS2::FormatType type)
102 DEBUG->print("Trying to import file '%s'...", file.toAscii().data());
104 FilterInterface * filter = NULL;
105 RS2::FormatType t = (type == RS2::FormatUnknown ? detectFormat(file) : type);
106 filter = getImportFilter(t);
109 return filter->fileImport(graphic, file, t);
112 DEBUG->print(Debug::D_WARNING, "FileIO::fileImport: failed to import file: %s",
113 file.toAscii().data());
120 * Calls the export method of the object responsible for the format
123 * @param file Path and name of the file to import.
125 bool FileIO::fileExport(Drawing & graphic, const QString & file, RS2::FormatType type)
127 DEBUG->print("FileIO::fileExport");
129 if (type == RS2::FormatUnknown)
131 QString extension = QFileInfo(file).suffix().toLower();
133 if (extension == "dxf")
134 type = RS2::FormatDXF;
135 else if (extension == "cxf")
136 type = RS2::FormatCXF;
139 FilterInterface * filter = getExportFilter(type);
142 return filter->fileExport(graphic, file, type);
144 DEBUG->print("FileIO::fileExport: no filter found");
150 * Detects and returns the file format of the given file.
152 RS2::FormatType FileIO::detectFormat(const QString & file)
154 RS2::FormatType type = RS2::FormatUnknown;
155 QFileInfo info(file);
156 QString ext = info.suffix().toLower();
160 type = RS2::FormatCXF;
162 else if (ext == "dxf")
164 type = RS2::FormatDXF1;
167 if (!f.open(QIODevice::ReadOnly))
169 // Error opening file:
170 DEBUG->print(Debug::D_WARNING,
171 "FileIO::detectFormat: Cannot open file: %s", file.toAscii().data());
172 type = RS2::FormatUnknown;
176 DEBUG->print("FileIO::detectFormat: Successfully opened DXF file: %s",
177 file.toAscii().data());
182 //I think this is wrong... We're mixing classes here...
183 //AND THAT WAS THE PROBLEM!!!
184 // while (!f.atEnd() && ++c < 100)
185 while (!ts.atEnd() && ++c < 100)
187 QString line = ts.readLine();
189 if (line == "$ACADVER")
190 type = RS2::FormatDXF;
192 // very simple reduced DXF:
193 if (line == "ENTITIES" && c < 10)
194 type = RS2::FormatDXF;