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.
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
] == '.') {
42 std::string fullfilename
;
44 // TODO: point at the correct journal directories!
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
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
);
64 fs::create_directory(dir
);
65 caos_assert(fs::exists(dir
) && fs::is_directory(dir
));
67 return fullfilename
+ filename
;
71 FILE GLOB (command) directory (integer) filespec (string)
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
);
90 dirportion
= "Journal/" + dirportion
;
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
++) {
101 inputstream
= new std::istringstream(str
);
108 Disconnects everything from the input stream.
110 void caosVM::c_FILE_ICLO() {
118 FILE IOPE (command) directory (integer) filename (string)
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
)
131 std::string fullfilename
= calculateJournalFilename(directory
, filename
, false);
132 inputstream
= new std::ifstream(fullfilename
.c_str());
134 if (inputstream
->fail()) {
141 FILE JDEL (command) directory (integer) filename (string)
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);
160 Disconnects everything from the output stream.
162 void caosVM::c_FILE_OCLO() {
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() {
177 outputstream
->flush();
181 FILE OOPE (command) directory (integer) filename (string) append (integer)
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
)
195 std::string fullfilename
= calculateJournalFilename(directory
, filename
, true);
198 outputstream
= new std::ofstream(fullfilename
.c_str(), std::ios::app
);
200 outputstream
= new std::ofstream(fullfilename
.c_str(), std::ios::trunc
);
202 if (outputstream
->fail()) {
204 throw caosException(boost::str(boost::format("FILE OOPE failed to open %s") % fullfilename
));
209 FVWM (string) name (string)
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
224 Fetches a float from the current input stream, or 0.0 if there is no data.
226 void caosVM::v_INNF() {
228 throw caosException("no input stream in INNF!");
239 Fetches an integer from the current input stream, or 0 if there is no data.
241 void caosVM::v_INNI() {
243 throw caosException("no input stream in INNI!");
254 Fetches a string of text from the input stream.
256 void caosVM::v_INNL() {
258 throw caosException("no input stream in INNL!");
260 std::getline(*inputstream
, str
);
261 result
.setString(str
);
268 Determines whether the current input stream is usable (0 or 1).
270 void caosVM::v_INOK() {
273 else if (inputstream
->fail())
280 WEBB (command) url (string)
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() {
289 std::string to_use
= std::string("http://") + url
;