SaveCommand.cc Source File

Back to the index.

SaveCommand.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "commands/SaveCommand.h"
29 #include "GXemul.h"
30 
31 #include <fstream>
32 
33 
35  : Command("save", "[filename [component-path]]")
36 {
37 }
38 
39 
41 {
42 }
43 
44 
45 static void ShowMsg(GXemul& gxemul, const string& msg)
46 {
47  gxemul.GetUI()->ShowDebugMessage(msg);
48 }
49 
50 
51 bool SaveCommand::Execute(GXemul& gxemul, const vector<string>& arguments)
52 {
53  string filename = gxemul.GetEmulationFilename();
54  string path = "root";
55 
56  if (arguments.size() > 2) {
57  ShowMsg(gxemul, "Too many arguments.\n");
58  return false;
59  }
60 
61  if (arguments.size() > 0)
62  filename = arguments[0];
63 
64  if (filename == "") {
65  ShowMsg(gxemul, "No filename given.\n");
66  return false;
67  }
68 
69  if (arguments.size() > 1)
70  path = arguments[1];
71 
72  vector<string> matches = gxemul.GetRootComponent()->
73  FindPathByPartialMatch(path);
74  if (matches.size() == 0) {
75  ShowMsg(gxemul, path+" is not a path to a known component.\n");
76  return false;
77  }
78  if (matches.size() > 1) {
79  ShowMsg(gxemul, path+" matches multiple components:\n");
80  for (size_t i=0; i<matches.size(); i++)
81  ShowMsg(gxemul, " " + matches[i] + "\n");
82  return false;
83  }
84 
85  refcount_ptr<Component> component =
86  gxemul.GetRootComponent()->LookupPath(matches[0]);
87  if (component.IsNULL()) {
88  ShowMsg(gxemul, "Lookup of " + path + " failed.\n");
89  return false;
90  }
91 
92  const string extension = ".gxemul";
93  if (filename.length() < extension.length() || filename.substr(
94  filename.length() - extension.length()) != extension)
95  ShowMsg(gxemul, "Warning: the name "+filename+" does not have"
96  " a .gxemul extension. Continuing anyway.\n");
97 
98  // Write to the file:
99  {
100  std::fstream outputstream(filename.c_str(),
101  std::ios::out | std::ios::trunc);
102  if (outputstream.fail()) {
103  ShowMsg(gxemul, "Error: Could not open " + filename +
104  " for writing.\n");
105  return false;
106  }
107 
108  SerializationContext context;
109  component->Serialize(outputstream, context);
110  }
111 
112  // Check that the file exists:
113  {
114  std::fstream inputstream(filename.c_str(), std::ios::in);
115  if (inputstream.fail()) {
116  ShowMsg(gxemul, "Error: Could not open " + filename +
117  " for reading after writing to it; saving "
118  " the emulation setup failed!\n");
119  return false;
120  }
121  }
122 
123  ShowMsg(gxemul, "Emulation setup saved to " + filename + "\n");
124  gxemul.SetEmulationFilename(filename);
125 
126  return true;
127 }
128 
129 
131 {
132  return "Saves the emulation to a file.";
133 }
134 
135 
137 {
138  return
139  "Saves the entire emulation setup, or a part of it, to a file in the filesystem.\n"
140  "The filename may be omitted, if it is known from an earlier save or load\n"
141  "command. If the component path is omitted, the entire emulation setup, starting\n"
142  "from the 'root' component, is saved.\n"
143  "\n"
144  "The filename extension should usually be .gxemul.\n"
145  "\n"
146  "See also: load (to load an emulation setup)\n";
147 }
148 
void SetEmulationFilename(const string &filename)
Sets the current emulation setup&#39;s filename.
Definition: GXemul.cc:623
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
virtual string GetShortDescription() const
Returns a short (one-line) description of the command.
Definition: SaveCommand.cc:130
The main emulator class.
Definition: GXemul.h:54
const string & GetEmulationFilename() const
Gets the current emulation setup&#39;s filename.
Definition: GXemul.cc:617
A context used during serialization of objects.
virtual string GetLongDescription() const
Returns a long description/help message for the command.
Definition: SaveCommand.cc:136
virtual bool Execute(GXemul &gxemul, const vector< string > &arguments)
Executes the command on a given GXemul instance.
Definition: SaveCommand.cc:51
void Serialize(ostream &ss, SerializationContext &context) const
Serializes the Component into a string stream.
Definition: Component.cc:1070
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
SaveCommand()
Constructs an SaveCommand.
Definition: SaveCommand.cc:34
virtual ~SaveCommand()
Definition: SaveCommand.cc:40
refcount_ptr< Component > GetRootComponent()
Gets a pointer to the root configuration component.
Definition: GXemul.cc:667
UI * GetUI()
Gets a pointer to the GXemul instance&#39; active UI.
Definition: GXemul.cc:661
const refcount_ptr< Component > LookupPath(string path) const
Looks up a path from this Component, and returns a pointer to the found Component, if any.
Definition: Component.cc:778
bool IsNULL() const
Checks whether or not an object is referenced by the reference counted pointer.
Definition: refcount_ptr.h:216

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13