2 * This file is part of ShapeFusion (Copyright 2000 Tito Dal Canton)
4 * ShapeFusion is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * ShapeFusion is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with ShapeFusion; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "ShapeFusionDocManager.h"
21 #include "BigEndianBuffer.h"
23 #include <wx/filename.h>
24 #include <wx/wfstream.h>
26 static wxDocTemplate* FindTemplate(wxList& templates, const wxString& description)
28 for (unsigned int i = 0; i < templates.GetCount(); ++i) {
29 wxDocTemplate* temp = reinterpret_cast<wxDocTemplate*>(templates.Item(i)->GetData());
30 if (temp->GetDescription() == description) {
38 wxDocTemplate* ShapeFusionDocManager::FindTemplateForPath(const wxString& path)
40 // if we recognize the extension, assume it's correct
41 wxFileName filename(path);
42 wxString ext = filename.GetExt();
44 if (ext == wxT("sndA") || ext == wxT("snd2")) {
45 return ::FindTemplate(GetTemplates(), wxT("Sounds"));
46 } else if (ext == wxT("shpA") || ext == wxT("shp2")) {
47 return ::FindTemplate(GetTemplates(), wxT("Shapes"));
50 wxFileInputStream stream(path);
56 // check for sounds file
58 stream.SeekI(0, wxFromStart);
60 unsigned char header[8];
61 stream.Read(header, 8);
63 BigEndianBuffer buffer(header, 4);
65 unsigned long version = buffer.ReadULong();
66 if ((version == 0 || version == 1)
67 && strncmp(reinterpret_cast<const char*>(&header[4]), "snd2", 4) == 0) {
68 return ::FindTemplate(GetTemplates(), wxT("Sounds"));
72 stream.SeekI(0, wxFromEnd);
73 long filesize = stream.TellI();
74 stream.SeekI(0, wxFromStart);
76 // check for shapes file
78 bool is_shapes = true;
79 for (int i = 0; i < 32; ++i) {
80 unsigned char header[32];
81 stream.Read(header, 32);
83 BigEndianBuffer buffer(header, 20);
84 unsigned long status_flags = buffer.ReadULong();
85 long offset = buffer.ReadLong();
86 long length = buffer.ReadLong();
87 long offset16 = buffer.ReadLong();
88 long length16 = buffer.ReadLong();
91 || (offset != -1 && (offset >= filesize || offset + length > filesize))
92 || (offset16 != -1 && (offset16 >= filesize || offset16 + length16 > filesize))) {
99 return ::FindTemplate(GetTemplates(), wxT("Shapes"));
105 stream.SeekI(0, wxFromStart);
106 unsigned char header[128];
107 stream.Read(header, 128);
109 BigEndianBuffer header_buffer(header, 128);
110 int version = header_buffer.ReadShort();
111 int data_version = header_buffer.ReadShort();
112 if ((version == 0 || version == 1 || version == 2 || version == 4) && (data_version == 0 || data_version == 1 || data_version == 2)) {
113 header_buffer.Position(72);
114 long directory_offset = header_buffer.ReadLong();
115 if (directory_offset >= filesize)
118 unsigned char tag[4];
120 if (strncmp(reinterpret_cast<const char*>(tag), "MNpx", 4) == 0) {
121 return ::FindTemplate(GetTemplates(), wxT("Physics"));
129 wxDocTemplate* ShapeFusionDocManager::SelectDocumentPath(wxDocTemplate** templates, int noTemplates, wxString& path, long, bool)
131 wxString pathTmp = wxFileSelector(_("Select a file"),
134 wxDocTemplate* theTemplate = 0;
136 if (!pathTmp.empty()) {
137 if (!wxFileExists(pathTmp))
140 if (!wxTheApp->GetAppName().empty()) {
141 msgTitle = wxTheApp->GetAppName();
143 msgTitle = wxString(_("File error"));
146 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION);
148 path = wxEmptyString;
151 m_lastDirectory = wxPathOnly(pathTmp);
155 theTemplate = FindTemplateForPath(path);
159 (void) wxMessageBox(_("Sorry, the format for this file is unknown."), _("Open File"), wxOK | wxICON_EXCLAMATION);
162 return FindTemplateForPath(path);
164 path = wxEmptyString;