first.";
return;
}
if (uprefs.backup_before_save && backup_existing_file(filename.c_str()))
{
COUT << "NOT DUMPED: COULD NOT CREATE BACKUP FILE "
<< filename << endl;
return;
}
ofstream outf(filename.c_str(), ofstream::out);
if (!outf.is_open()) // open failed
{
CERR << "Unable to )DUMP workspace '" << wsname
<< "': " << strerror(errno) << "." << endl
<< " NOTE: filename: " << filename << endl;
return;
}
HTML_streambuf hout_buf(outf);
HTML_stream hout(&hout_buf);
ostream * sout = &outf;
if (html) sout = &hout;
// print header line, workspace name, time, and date to outf
//
const APL_time_us offset = get_v_Quad_TZ().get_offset();
const APL_time_us gmt = now();
const YMDhmsu time(gmt + 1000000*offset);
{
if (html)
{
outf <<
"" << endl <<
"" << endl <<
" " << endl <<
" " << wsname << ".apl " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
" " << endl <<
"⍝" << endl <<
"⍝ Author: ??????" << endl <<
"⍝ Date: " << time.year << "-"
<< time.month << "-"
<< time.day << endl <<
"⍝ Copyright: Copyright (C) " << time.year << " by ??????" << endl <<
"⍝ License: GPL see http://www.gnu.org/licenses/gpl-3.0.en.html"<< endl <<
"⍝ Support email: ??????@??????" << endl <<
"⍝ Portability: L3 (GNU APL)" << endl <<
"⍝" << endl <<
"⍝ Purpose:" << endl <<
"⍝ ??????" << endl <<
"⍝" << endl <<
"⍝ Description:" << endl <<
"⍝ ??????" << endl <<
"⍝" << endl <<
endl;
}
else
{
outf << "#!" << LibPaths::get_APL_bin_path()
<< "/" << LibPaths::get_APL_bin_name()
<< " --script" << endl;
}
*sout << " ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝" << endl
<< "⍝" << endl
<< "⍝ " << wsname << " ";
get_v_Quad_TZ().print_timestamp(*sout, gmt) << endl
<< " ⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝⍝" << endl
<< endl;
}
int function_count = 0;
int variable_count = 0;
the_workspace.symbol_table.dump(*sout, function_count, variable_count);
the_workspace.dump_commands(*sout);
// system variables
//
#define ro_sv_def(x, _str, _txt)
#define rw_sv_def(x, _str, _txt) if (ID_ ## x != ID_Quad_SYL) \
{ get_v_ ## x().dump(*sout); ++variable_count; }
#include "SystemVariable.def"
if (html) outf << endl << "⍝ EOF
" << endl;
if (silent)
{
get_v_Quad_TZ().print_timestamp(out, gmt) << endl;
}
else
{
out << "DUMPED WORKSPACE '" << wsname << "'" << endl
<< " TO FILE '" << filename << "'" << endl
<< " (" << function_count << " FUNCTIONS, " << variable_count
<< " VARIABLES)" << endl;
}
}
//-----------------------------------------------------------------------------
void
Workspace::dump_commands(ostream & out)
{
vector & cmds = get_user_commands();
loop(c, cmds.size())
out << "]USERCMD " << cmds[c].prefix
<< " " << cmds[c].apl_function
<< " " << cmds[c].mode << endl;
if (cmds.size()) out << endl;
}
//-----------------------------------------------------------------------------
// )LOAD WS, set ⎕LX of loaded WS on success
void
Workspace::load_WS(ostream & out, LibRef libref, const UCS_string & wsname,
UCS_string & quad_lx, bool silent)
{
// )LOAD wsname wsname /absolute or relative
// )LOAD libnum wsname wsname relative
//
if (UTF8_string(wsname).ends_with(".bak"))
{
out << "BAD COMMAND+" << endl;
MORE_ERROR() << wsname <<
" is a backup file! You need to rename it before it can be )LOADed";
return;
}
UTF8_string filename = LibPaths::get_lib_filename(libref, wsname, true,
".xml", ".apl");
int dump_fd = -1;
XML_Loading_Archive in(filename.c_str(), dump_fd);
if (dump_fd != -1) // wsname.apl
{
the_workspace.clear_WS(out, true);
Log(LOG_command_IN) out << "LOADING " << wsname << " from file '"
<< filename << "' ..." << endl;
load_DUMP(out, filename, dump_fd, do_LX, silent, 0); // closes dump_fd
// )DUMP files have no )WSID so create one from the filename
//
const char * wsid_start = strrchr(filename.c_str(), '/');
if (wsid_start == 0) wsid_start = filename.c_str();
else ++wsid_start; // skip /
const char * wsid_end = filename.c_str() + filename.size();
if (wsid_end > (wsid_start - 4) &&
wsid_end[-4] == '.' &&
wsid_end[-3] == 'a' &&
wsid_end[-2] == 'p' &&
wsid_end[-1] == 'l') wsid_end -= 4; // skip .apl extension
const UTF8_string wsid_utf8(utf8P(wsid_start), wsid_end - wsid_start);
const UCS_string wsid_ucs(wsid_utf8);
wsid(out, wsid_ucs, libref, true);
// we cant set ⎕LX because it was not executed yet.
return;
}
else // wsname.xml
{
if (!in.is_open()) // open failed
{
out << ")LOAD " << wsname << " (file " << filename
<< ") failed: " << strerror(errno) << endl;
return;
}
Log(LOG_command_IN) out << "LOADING " << wsname << " from file '"
<< filename << "' ..." << endl;
// got open file. We assume that from here on everything will be fine.
// clear current WS and load it from file
//
the_workspace.clear_WS(out, true);
in.read_Workspace(silent);
}
if (Workspace::get_LX().size()) quad_lx = Workspace::get_LX();
}
//-----------------------------------------------------------------------------
void
Workspace::copy_WS(ostream & out, LibRef libref, const UCS_string & wsname,
UCS_string_vector & lib_ws_objects, bool protection)
{
// )COPY wsname wsname /absolute or relative
// )COPY libnum wsname wsname relative
// )COPY wsname objects... wsname /absolute or relative
// )COPY libnum wsname objects... wsname relative
UTF8_string filename = LibPaths::get_lib_filename(libref, wsname, true,
".xml", ".apl");
int dump_fd = -1;
XML_Loading_Archive in(filename.c_str(), dump_fd);
if (dump_fd != -1)
{
load_DUMP(out, filename, dump_fd, no_LX, false, &lib_ws_objects);
// load_DUMP closes dump_fd
return;
}
if (!in.is_open()) // open failed: try filename.xml unless already .xml
{
CERR << ")COPY " << wsname << " (file " << filename
<< ") failed: " << strerror(errno) << endl;
return;
}
Log(LOG_command_IN) CERR << "LOADING " << wsname << " from file '"
<< filename << "' ..." << endl;
in.set_protection(protection, lib_ws_objects);
in.read_vids();
in.read_Workspace(false);
}
//-----------------------------------------------------------------------------
void
Workspace::wsid(ostream & out, UCS_string arg, LibRef lib, bool silent)
{
arg.remove_leading_and_trailing_whitespaces();
if (arg.size() == 0) // inquire workspace name
{
out << "IS " << the_workspace.WS_name << endl;
return;
}
if (lib == LIB_WSNAME) // i.e. arg may start with a library number
{
if (arg[0] >= '0' && arg[0] <= '9') // it does
{
lib = LibRef(arg[0] - '0');
arg.erase(0); // skip the library number
arg.remove_leading_whitespaces(); // and blanks after it
}
else lib = LIB0;
}
loop(a, arg.size())
{
if (arg[a] <= ' ')
{
out << "Bad WS name '" << arg << "'" << endl;
return;
}
}
if (!silent) out << "WAS " << the_workspace.WS_name << endl;
// prepend the lib number unless it is 0.
//
if (lib != LIB0)
{
arg.prepend(UNI_ASCII_SPACE);
arg.prepend(Unicode('0' + lib));
}
the_workspace.WS_name = arg;
}
//-----------------------------------------------------------------------------
UCS_string &
MORE_ERROR()
{
Workspace::more_error().clear();
return Workspace::more_error();
}
//-----------------------------------------------------------------------------