OpenASIP  2.0
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 
46 using std::string;
47 
48 IMPLEMENT_DYNAMIC_CLASS(MDFDocument, wxDocument)
49 
50 
51 /**
52  * The constructor.
53  */
54 MDFDocument::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  */
74 bool
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  */
89 bool
90 MDFDocument::OnOpenDocument(const wxString& fileName) {
91 
92  string configExtension = ProDeConstants::PROCESSOR_CONFIG_FILE_EXTENSION;
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  */
116 bool
117 MDFDocument::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  */
168 bool
169 MDFDocument::openADF(const string& filename) {
170  try {
171  model_ = new Model(filename);
172  model_->addObserver(this);
173  } catch (Exception 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  */
204 bool
205 MDFDocument::OnSaveDocument(const wxString& filename) {
206 
207  ADFSerializer writer;
208  writer.setDestinationFile(WxConversion::toString(filename));
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  */
228 Model*
230  return model_;
231 }
232 
233 
234 /**
235  * Updates the document when the Model changes.
236  */
237 void
239  if (model_->isModified()) {
240  Modify(true);
242  }
243  UpdateAllViews();
244 }
245 
246 
MDFDocument::getModel
Model * getModel()
Definition: MDFDocument.cc:229
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
MDFDocument.hh
MDFDocument::model_
Model * model_
Machine Object Model which the document represents.
Definition: MDFDocument.hh:67
Exception.hh
ProcessorConfigurationFile
Definition: ProcessorConfigurationFile.hh:46
MDFDocument::OnNewDocument
virtual bool OnNewDocument()
Definition: MDFDocument.cc:75
ProcessorConfigurationFile::architectureName
std::string architectureName()
Definition: ProcessorConfigurationFile.cc:107
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
MDFDocument::~MDFDocument
virtual ~MDFDocument()
Definition: MDFDocument.cc:61
ProDeTextGenerator.hh
FileSystem::fileOfPath
static std::string fileOfPath(const std::string pathName)
Definition: FileSystem.cc:101
ProDeTextGenerator
Definition: ProDeTextGenerator.hh:49
assert
#define assert(condition)
Definition: Application.hh:86
ProcessorConfigurationFile::errors
bool errors()
Definition: ProcessorConfigurationFile.cc:231
Model::setNotModified
void setNotModified()
Definition: Model.hh:65
ProcessorConfigurationFile::setPCFDirectory
void setPCFDirectory(const std::string &path)
Definition: ProcessorConfigurationFile.cc:95
Model::addObserver
void addObserver(ModelObserver *observer)
Definition: Model.cc:143
ErrorDialog
Definition: ErrorDialog.hh:42
ErrorDialog.hh
FileSystem::fileExtension
static std::string fileExtension(const std::string &fileName)
Definition: FileSystem.cc:279
ADFSerializer
Definition: ADFSerializer.hh:49
XMLSerializer::setDestinationFile
void setDestinationFile(const std::string &fileName)
Definition: XMLSerializer.cc:142
FileSystem::directoryOfPath
static std::string directoryOfPath(const std::string fileName)
Definition: FileSystem.cc:79
ProcessorConfigurationFile::errorCount
int errorCount()
Definition: ProcessorConfigurationFile.cc:205
ProcessorConfigurationFile::errorString
std::string errorString(int index)
Definition: ProcessorConfigurationFile.cc:217
MDFDocument::openADF
bool openADF(const std::string &filename)
Definition: MDFDocument.cc:169
Exception
Definition: Exception.hh:54
MDFDocument::openCFG
bool openCFG(const std::string &filename)
Definition: MDFDocument.cc:117
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
MDFDocument
Definition: MDFDocument.hh:51
ProDeConstants.hh
ProDeTextGenerator::instance
static ProDeTextGenerator * instance()
Definition: ProDeTextGenerator.cc:382
Model
Definition: Model.hh:50
ADFSerializer.hh
MDFDocument::update
virtual void update()
Definition: MDFDocument.cc:238
MDFDocument::OnSaveDocument
virtual bool OnSaveDocument(const wxString &filename)
Definition: MDFDocument.cc:205
ADFSerializer::writeMachine
void writeMachine(const TTAMachine::Machine &machine)
Definition: ADFSerializer.cc:259
ProcessorConfigurationFile.hh
WxConversion.hh
KeyNotFound
Definition: Exception.hh:285
ProDeConstants::PROCESSOR_CONFIG_FILE_EXTENSION
static const std::string PROCESSOR_CONFIG_FILE_EXTENSION
Processor configuration file extension.
Definition: ProDeConstants.hh:394
ProDeTextGenerator::MSG_ERROR_LOADING_FILE
@ MSG_ERROR_LOADING_FILE
Error: File loading failed.
Definition: ProDeTextGenerator.hh:244
Model::isModified
bool isModified() const
Definition: Model.hh:66
WxConversion::toString
static std::string toString(const wxString &source)
MDFDocument::OnOpenDocument
virtual bool OnOpenDocument(const wxString &filename)
Definition: MDFDocument.cc:90
Model::getMachine
TTAMachine::Machine * getMachine()
Definition: Model.cc:88