OpenASIP 2.2
Loading...
Searching...
No Matches
MDFDocument.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2009 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file MDFDocument.cc
26 *
27 * Definition of MDFDocument class.
28 *
29 * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi)
30 * @note rating: red
31 */
32
33#include <string>
34#include <boost/format.hpp>
35#include <fstream>
36#include "MDFDocument.hh"
37#include "Exception.hh"
38#include "ErrorDialog.hh"
39#include "WxConversion.hh"
40#include "ADFSerializer.hh"
41#include "ProDeTextGenerator.hh"
42#include "ProDeConstants.hh"
43#include "FileSystem.hh"
45
46using std::string;
47
48IMPLEMENT_DYNAMIC_CLASS(MDFDocument, wxDocument)
49
50
51/**
52 * The constructor.
53 */
54MDFDocument::MDFDocument(): model_(NULL) {
55}
56
57
58/**
59 * The destructor.
60 */
62 if (model_ != NULL) {
63 delete model_;
64 model_ = NULL;
65 }
66}
67
68
69/**
70 * Creates a new document.
71 *
72 * @return True, if a new document was succesfully created, false otherwise.
73 */
74bool
76 model_ = new Model();
77 model_->addObserver(this);
78 return wxDocument::OnNewDocument();
79}
80
81
82/**
83 * Opens an .adf or .cfg file. If the opened file extension is .cfg,
84 * the architecture file name is read from the configuration.
85 *
86 * @param fileName Name of the file to read.
87 * @return True if the file was succesfully loaded, false otherwise.
88 */
89bool
90MDFDocument::OnOpenDocument(const wxString& fileName) {
91
93 string fileExtension =
95
96 if (fileExtension == configExtension) {
97 // read .adf filename from the configuration
98 return openCFG(WxConversion::toString(fileName));
99 } else {
100 return openADF(WxConversion::toString(fileName));
101 }
102
103 assert(false);
104 return false;
105}
106
107
108/**
109 * Reads architectrue definition file name from a processor configuration
110 * file, and opens the .adf file.
111 *
112 * @param filename Configuration file to read.
113 * @return True, if the adf defined in the .cfg was succesfully opened,
114 * false otherwise.
115 */
116bool
117MDFDocument::openCFG(const string& filename) {
118
119 std::fstream cfgFile(filename.c_str());
120
121 if (cfgFile.fail()) {
122 return false;
123 }
124
125 ProcessorConfigurationFile cfg(cfgFile);
127
128 if (cfg.errors()) {
129 // Errors in the .cfg file.
130 // Display error strings in an error dialog.
131 wxString message;
132 for (int i = 0;i < cfg.errorCount();i++) {
133 message.Append(WxConversion::toWxString(cfg.errorString(i)));
134 message.Append(_T("\n\n"));
135 }
136 ErrorDialog dialog(GetDocumentWindow(), message);
137 dialog.ShowModal();
138 return false;
139 }
140
141 string adfName;
142
143 try {
144 adfName = cfg.architectureName();
145 } catch (KeyNotFound& e) {
146 wxString message = WxConversion::toWxString(e.errorMessage());
147 ErrorDialog dialog(GetDocumentWindow(), message);
148 dialog.ShowModal();
149 return false;
150 }
151
152 if(openADF(adfName)) {
153 SetFilename(WxConversion::toWxString(adfName), true);
154 return true;
155 } else {
156 return false;
157 }
158}
159
160
161/**
162 * Opens an architecture definition file, and creates model of the
163 * architecture.
164 *
165 * @param filename Architecture file to open.
166 * @return True, if the file was succesfully opened, false otherwise.
167 */
168bool
169MDFDocument::openADF(const string& filename) {
170 try {
171 model_ = new Model(filename);
172 model_->addObserver(this);
173 } catch (Exception const& e) {
174 // Display an error dialog and return false.
176 boost::format fmt =
178 fmt % filename;
179 wxString message = WxConversion::toWxString(fmt.str());
180 message.Append(_T("\n"));
181 message.Append(WxConversion::toWxString(e.errorMessage()));
182 ErrorDialog errorDialog(GetDocumentWindow(), message);
183 errorDialog.ShowModal();
184 return false;
185 }
186
187 // Update document title.
188 string title = FileSystem::fileOfPath(filename);
189 SetTitle(WxConversion::toWxString(title));
190
191 // File opened succesfully.
192 Modify(false);
193 UpdateAllViews();
194 return true;
195}
196
197
198/**
199 * Writes the machine object model to an mdf file using the mdf writer
200 * component and sets the model unmodified.
201 *
202 * @param filename Name of the file into which the Model will be saved.
203 */
204bool
205MDFDocument::OnSaveDocument(const wxString& filename) {
206
207 ADFSerializer writer;
209 try {
210 writer.writeMachine(*model_->getMachine());
211 } catch (Exception& e) {
212 ErrorDialog errorDialog(GetDocumentWindow(),
214 errorDialog.ShowModal();
215 return false;
216 }
217
218 Modify(false);
219 return true;
220}
221
222
223/**
224 * Returns document's model.
225 *
226 * @return Document's model.
227 */
228Model*
230 return model_;
231}
232
233
234/**
235 * Updates the document when the Model changes.
236 */
237void
239 if (model_->isModified()) {
240 Modify(true);
242 }
243 UpdateAllViews();
244}
245
246
#define assert(condition)
void writeMachine(const TTAMachine::Machine &machine)
std::string errorMessage() const
Definition Exception.cc:123
static std::string fileOfPath(const std::string pathName)
static std::string directoryOfPath(const std::string fileName)
Definition FileSystem.cc:79
static std::string fileExtension(const std::string &fileName)
virtual ~MDFDocument()
bool openADF(const std::string &filename)
virtual bool OnOpenDocument(const wxString &filename)
bool openCFG(const std::string &filename)
virtual void update()
virtual bool OnNewDocument()
Model * getModel()
virtual bool OnSaveDocument(const wxString &filename)
Model * model_
Machine Object Model which the document represents.
Definition Model.hh:50
void addObserver(ModelObserver *observer)
Definition Model.cc:143
bool isModified() const
Definition Model.hh:66
void setNotModified()
Definition Model.hh:65
TTAMachine::Machine * getMachine()
Definition Model.cc:88
static const std::string PROCESSOR_CONFIG_FILE_EXTENSION
Processor configuration file extension.
static ProDeTextGenerator * instance()
@ MSG_ERROR_LOADING_FILE
Error: File loading failed.
void setPCFDirectory(const std::string &path)
virtual boost::format text(int textId)
static wxString toWxString(const std::string &source)
static std::string toString(const wxString &source)
void setDestinationFile(const std::string &fileName)