Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit c0c1705

Browse files
committed
[Bug 15970] Add optional with format _ clause to save
Make it possible to directly specify the version of the stack file format to use when issuing a `save` command.
1 parent 0d66c07 commit c0c1705

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

engine/src/cmds.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,12 +1919,9 @@ class MCSave : public MCStatement
19191919
{
19201920
MCChunk *target;
19211921
MCExpression *filename;
1922+
MCExpression *format;
19221923
public:
1923-
MCSave()
1924-
{
1925-
target = NULL;
1926-
filename = NULL;
1927-
}
1924+
MCSave() : target(NULL), filename(NULL), format(NULL) {}
19281925
virtual ~MCSave();
19291926
virtual Parse_stat parse(MCScriptPoint &);
19301927
virtual void exec_ctxt(MCExecContext &ctxt);

engine/src/cmdss.cpp

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,19 +2135,32 @@ Parse_stat MCSave::parse(MCScriptPoint &sp)
21352135
(PE_SAVE_BADEXP, sp);
21362136
return PS_ERROR;
21372137
}
2138-
if (sp.next(type) != PS_NORMAL)
2139-
return PS_NORMAL;
2140-
if (sp.lookup(SP_FACTOR, te) != PS_NORMAL || te->which != PT_AS)
2138+
2139+
/* Parse optional "as _" clause */
2140+
if (sp.skip_token(SP_FACTOR, TT_PREP, PT_AS) == PS_NORMAL)
21412141
{
2142-
sp.backup();
2143-
return PS_NORMAL;
2142+
if (sp.parseexp(False, True, &filename) != PS_NORMAL)
2143+
{
2144+
MCperror->add(PE_SAVE_BADFILEEXP, sp);
2145+
return PS_ERROR;
2146+
}
21442147
}
2145-
if (sp.parseexp(False, True, &filename) != PS_NORMAL)
2148+
2149+
/* Parse optional "with format _" clause */
2150+
if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_WITH) == PS_NORMAL)
21462151
{
2147-
MCperror->add
2148-
(PE_SAVE_BADFILEEXP, sp);
2149-
return PS_ERROR;
2152+
if (sp.skip_token(SP_FACTOR, TT_FUNCTION, F_FORMAT) != PS_NORMAL)
2153+
{
2154+
MCperror->add(PE_SAVE_BADFORMATEXP, sp);
2155+
return PS_ERROR;
2156+
}
2157+
if (sp.parseexp(False, True, &format) != PS_NORMAL)
2158+
{
2159+
MCperror->add(PE_SAVE_BADFORMATEXP, sp);
2160+
return PS_ERROR;
2161+
}
21502162
}
2163+
21512164
return PS_NORMAL;
21522165
}
21532166

@@ -2205,16 +2218,45 @@ void MCSave::exec_ctxt(MCExecContext &ctxt)
22052218
ctxt . LegacyThrow(EE_SAVE_NOTASTACK);
22062219
return;
22072220
}
2221+
2222+
MCStack *t_stack = static_cast<MCStack *>(optr);
2223+
2224+
MCAutoStringRef t_filename;
22082225
if (filename != NULL)
22092226
{
2210-
MCAutoStringRef t_filename;
22112227
if (!ctxt . EvalExprAsStringRef(filename, EE_SAVE_BADNOFILEEXP, &t_filename))
22122228
return;
2229+
}
22132230

2214-
MCInterfaceExecSaveStackAs(ctxt, (MCStack *)optr, *t_filename);
2231+
MCAutoStringRef t_format;
2232+
if (format != NULL)
2233+
{
2234+
if (!ctxt.EvalExprAsStringRef(format, EE_SAVE_BADNOFORMATEXP, &t_format))
2235+
return;
2236+
}
2237+
2238+
if (NULL != filename)
2239+
{
2240+
if (NULL != format)
2241+
{
2242+
MCInterfaceExecSaveStackAsWithVersion(ctxt, t_stack, *t_filename, *t_format);
2243+
}
2244+
else
2245+
{
2246+
MCInterfaceExecSaveStackAs(ctxt, t_stack, *t_filename);
2247+
}
22152248
}
22162249
else
2217-
MCInterfaceExecSaveStack(ctxt, (MCStack*) optr);
2250+
{
2251+
if (NULL != format)
2252+
{
2253+
MCInterfaceExecSaveStackWithVersion(ctxt, t_stack, *t_format);
2254+
}
2255+
else
2256+
{
2257+
MCInterfaceExecSaveStack(ctxt, t_stack);
2258+
}
2259+
}
22182260
}
22192261

22202262
void MCSave::compile(MCSyntaxFactoryRef ctxt)

engine/src/exec-interface.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,14 @@ void MCInterfaceExecSaveStack(MCExecContext& ctxt, MCStack *p_target)
25012501
MCInterfaceExecSaveStackAs(ctxt, p_target, kMCEmptyString);
25022502
}
25032503

2504+
void
2505+
MCInterfaceExecSaveStackWithVersion(MCExecContext & ctxt,
2506+
MCStack *p_target,
2507+
MCStringRef p_version)
2508+
{
2509+
MCInterfaceExecSaveStackAsWithVersion(ctxt, p_target, kMCEmptyString, p_version);
2510+
}
2511+
25042512
void MCInterfaceExecSaveStackAs(MCExecContext& ctxt, MCStack *p_target, MCStringRef p_new_filename)
25052513
{
25062514
ctxt . SetTheResultToEmpty();
@@ -2510,6 +2518,24 @@ void MCInterfaceExecSaveStackAs(MCExecContext& ctxt, MCStack *p_target, MCString
25102518
p_target -> saveas(p_new_filename, MCstackfileversion);
25112519
}
25122520

2521+
void
2522+
MCInterfaceExecSaveStackAsWithVersion(MCExecContext & ctxt,
2523+
MCStack *p_target,
2524+
MCStringRef p_new_filename,
2525+
MCStringRef p_version)
2526+
{
2527+
ctxt.SetTheResultToEmpty();
2528+
if (!ctxt.EnsureDiskAccessIsAllowed())
2529+
return;
2530+
2531+
MCInterfaceStackFileVersion t_version;
2532+
MCInterfaceStackFileVersionParse(ctxt, p_version, t_version);
2533+
if (ctxt.HasError())
2534+
return;
2535+
2536+
p_target->saveas(p_new_filename, t_version.version);
2537+
}
2538+
25132539
////////////////////////////////////////////////////////////////////////////////
25142540
void MCInterfaceExecMoveObject(MCExecContext& ctxt, MCObject *p_target, MCPoint *p_motion, uindex_t p_motion_count, double p_duration, int p_units, bool p_wait, bool p_dispatch)
25152541
{

engine/src/exec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,7 +3049,9 @@ void MCInterfaceExecUnhiliteObject(MCExecContext& ctxt, MCObjectPtr p_targets);
30493049
void MCInterfaceExecHiliteObject(MCExecContext& ctxt, MCObjectPtr p_targets);
30503050

30513051
void MCInterfaceExecSaveStack(MCExecContext& ctxt, MCStack *p_target);
3052+
void MCInterfaceExecSaveStackWithVersion(MCExecContext & ctxt, MCStack *p_target, MCStringRef p_version);
30523053
void MCInterfaceExecSaveStackAs(MCExecContext& ctxt, MCStack *p_target, MCStringRef p_new_filename);
3054+
void MCInterfaceExecSaveStackAsWithVersion(MCExecContext& ctxt, MCStack *p_target, MCStringRef p_new_filename, MCStringRef p_version);
30533055

30543056
void MCInterfaceExecMoveObjectBetween(MCExecContext& ctxt, MCObject *p_target, MCPoint p_from, MCPoint p_to, double p_duration, int p_units, bool p_wait, bool p_dispatch);
30553057
void MCInterfaceExecMoveObjectAlong(MCExecContext& ctxt, MCObject *p_target, MCPoint *p_motion, uindex_t p_motion_count, bool p_is_relative, double p_duration, int p_units, bool p_wait, bool p_dispatch);

engine/src/executionerrors.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2691,7 +2691,10 @@ enum Exec_errors
26912691
EE_DO_BADWIDGETEXP,
26922692

26932693
// {EE-0881} documentFilename: bad filename
2694-
EE_DOCUMENTFILENAME_BADFILENAME,
2694+
EE_DOCUMENTFILENAME_BADFILENAME,
2695+
2696+
// {EE-0882} save: error in file format expression
2697+
EE_SAVE_BADNOFORMATEXP,
26952698
};
26962699

26972700
extern const char *MCexecutionerrors;

engine/src/parseerrors.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,10 @@ enum Parse_errors
17501750

17511751
// {PE-0568} launch: error in widget expression
17521752
PE_LAUNCH_BADWIDGETEXP,
1753+
1754+
// {PE-0568} save: error in format expression
1755+
PE_SAVE_BADFORMATEXP,
1756+
17531757
};
17541758

17551759
extern const char *MCparsingerrors;

0 commit comments

Comments
 (0)