try to make build portable: remove SDL_mixer dependency, remove -f from cp command...
[openc2e.git] / caosVM_files.cpp
blob8875f53facd8d5504a29255c830537e77bfcdcd6
1 /*
2 * caosVM_files.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Sat Aug 13 2005.
6 * Copyright (c) 2005 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
21 #include "caosVM.h"
22 #include "World.h"
23 #include <fstream>
24 #include <iostream>
25 #include <boost/format.hpp>
26 #include <boost/filesystem/convenience.hpp>
27 namespace fs = boost::filesystem;
29 std::string calculateJournalFilename(int directory, std::string filename, bool writable) {
30 // sanitise string: remove leading dots, replace slashes with underscores
31 // todo: check DS behaviour for backslashes (a problem on Windows)
32 std::string::size_type r;
33 while ((r = filename.find("/", 0)) != std::string::npos)
34 filename.replace(r, 1, "_");
35 for (unsigned int i = 0; i < filename.size(); i++) {
36 if (filename[i] == '.') {
37 filename.erase(i, 1);
38 i--;
39 } else break;
42 std::string fullfilename;
44 // TODO: point at the correct journal directories!
46 if (!writable) {
47 // search all directories for a readable file
48 fullfilename = world.findFile("/Journal/" + filename);
50 // if we found one, return that
51 if (fullfilename.size()) return fullfilename;
54 // otherwise, we should always write to the journal directory
55 switch (directory) {
56 case 0: fullfilename = world.getUserDataDir() + "/Journal/"; break;
57 case 1: fullfilename = world.getUserDataDir() + "/Journal/"; break;
58 case 2: fullfilename = world.getUserDataDir() + "/Journal/"; break;
59 default: throw caosException("unknown Journal directory");
62 fs::path dir = fs::path(fullfilename, fs::native);
63 if (!fs::exists(dir))
64 fs::create_directory(dir);
65 caos_assert(fs::exists(dir) && fs::is_directory(dir));
67 return fullfilename + filename;
70 /**
71 FILE GLOB (command) directory (integer) filespec (string)
72 %status maybe
74 Globs the given journal directory (pass 0 for the world directory, or 1 for the main directory) for the
75 given filespec (you can use wildcards, and descend into subdirectories using '..').
77 The result is placed on the input stream for reading with standard input commands (eg, INNI and INNL);
78 this consists of the number of results on the first line, and then the full filename paths to the matched
79 files on the remaining lines.
81 void caosVM::c_FILE_GLOB() {
82 VM_PARAM_STRING(filespec)
83 VM_PARAM_INTEGER(directory)
85 std::string::size_type n = filespec.find_last_of("/\\") + 1;
86 std::string dirportion; dirportion.assign(filespec, 0, n);
87 std::string specportion; specportion.assign(filespec, n, filespec.size() - n);
89 if (directory == 1)
90 dirportion = "Journal/" + dirportion;
91 else
92 throw creaturesException("whoops, openc2e doesn't support FILE GLOB in world journal directory yet, bug fuzzie");
94 std::vector<std::string> possiblefiles = world.findFiles(dirportion, specportion);
96 std::string str = boost::str(boost::format("%d\n") % possiblefiles.size());
97 for (std::vector<std::string>::iterator i = possiblefiles.begin(); i != possiblefiles.end(); i++) {
98 str += *i + "\n";
101 inputstream = new std::istringstream(str);
105 FILE ICLO (command)
106 %status maybe
108 Disconnects everything from the input stream.
110 void caosVM::c_FILE_ICLO() {
111 if (inputstream) {
112 delete inputstream;
113 inputstream = 0;
118 FILE IOPE (command) directory (integer) filename (string)
119 %status maybe
121 Puts the given filename in the given directory (pass 1 for the world directory, or 0 for the main
122 directory) on the current VM's input stream, for use by INOK, INNL, INNI and INNF.
123 If a file is already open, it will be closed first.
125 void caosVM::c_FILE_IOPE() {
126 VM_PARAM_STRING(filename)
127 VM_PARAM_INTEGER(directory)
129 c_FILE_ICLO();
131 std::string fullfilename = calculateJournalFilename(directory, filename, false);
132 inputstream = new std::ifstream(fullfilename.c_str());
134 if (inputstream->fail()) {
135 delete inputstream;
136 inputstream = 0;
141 FILE JDEL (command) directory (integer) filename (string)
142 %status stub
144 Removes the given file in the given directory (pass 1 for the world directory, or 0 for the main
145 directory) immediately.
147 void caosVM::c_FILE_JDEL() {
148 VM_PARAM_STRING(filename)
149 VM_PARAM_INTEGER(directory)
151 std::string fullfilename = calculateJournalFilename(directory, filename, true);
153 // TODO
157 FILE OCLO (command)
158 %status maybe
160 Disconnects everything from the output stream.
162 void caosVM::c_FILE_OCLO() {
163 if (outputstream) {
164 delete outputstream;
165 outputstream = 0;
170 FILE OFLU (command)
171 %status maybe
173 Flushes the current output stream; if this is a file, all data in the buffer will be written to it.
175 void caosVM::c_FILE_OFLU() {
176 if (outputstream)
177 outputstream->flush();
181 FILE OOPE (command) directory (integer) filename (string) append (integer)
182 %status maybe
184 Puts the given filename in the given directory (pass 1 for the world directory, or 0 for the main
185 directory) on the current VM's output stream.
186 If a file is already open, it will be closed first.
188 void caosVM::c_FILE_OOPE() {
189 VM_PARAM_INTEGER(append)
190 VM_PARAM_STRING(filename)
191 VM_PARAM_INTEGER(directory)
193 c_FILE_OCLO();
195 std::string fullfilename = calculateJournalFilename(directory, filename, true);
197 if (append)
198 outputstream = new std::ofstream(fullfilename.c_str(), std::ios::app);
199 else
200 outputstream = new std::ofstream(fullfilename.c_str(), std::ios::trunc);
202 if (outputstream->fail()) {
203 outputstream = 0;
204 throw caosException(boost::str(boost::format("FILE OOPE failed to open %s") % fullfilename));
209 FVWM (string) name (string)
210 %status stub
212 Returns an safe (not-in-use) filename for naming worlds and other saved files.
214 void caosVM::v_FVWM() {
215 VM_PARAM_STRING(name)
217 result.setString(name + "_something"); // TODO
221 INNF (float)
222 %status maybe
224 Fetches a float from the current input stream, or 0.0 if there is no data.
226 void caosVM::v_INNF() {
227 if (!inputstream)
228 throw caosException("no input stream in INNF!");
230 float f = 0.0f;
231 *inputstream >> f;
232 result.setFloat(f);
236 INNI (integer)
237 %status maybe
239 Fetches an integer from the current input stream, or 0 if there is no data.
241 void caosVM::v_INNI() {
242 if (!inputstream)
243 throw caosException("no input stream in INNI!");
245 int i = 0;
246 *inputstream >> i;
247 result.setInt(i);
251 INNL (string)
252 %status maybe
254 Fetches a string of text from the input stream.
256 void caosVM::v_INNL() {
257 if (!inputstream)
258 throw caosException("no input stream in INNL!");
259 std::string str;
260 std::getline(*inputstream, str);
261 result.setString(str);
265 INOK (integer)
266 %status maybe
268 Determines whether the current input stream is usable (0 or 1).
270 void caosVM::v_INOK() {
271 if (!inputstream)
272 result.setInt(0);
273 else if (inputstream->fail())
274 result.setInt(0);
275 else
276 result.setInt(1);
280 WEBB (command) url (string)
281 %status stub
283 Launches the specified URL, prepended with 'http://' (so you'd only specify, for example, 'example.com/foo.html'), in the user's browser.
285 void caosVM::c_WEBB() {
286 VM_PARAM_STRING(url)
288 // TODO
289 std::string to_use = std::string("http://") + url;
292 /* vim: set noet: */