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

Commit a2273b3

Browse files
committed
[[ Bug 18010 ]] Add optional 'kind' parameter to files/folders
This patch unifies the implementation of files() and folders() and adds an optional second parameter 'kind'. The kind parameter can be empty or "detailed". If it is detailed then the 'detailed' variant of the request is made, otherwise the normal variant is made.
1 parent ad8a9e2 commit a2273b3

File tree

12 files changed

+164
-184
lines changed

12 files changed

+164
-184
lines changed

docs/dictionary/function/files.lcdoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Type: function
44

55
Syntax: the [{ detailed | long }] files
66

7-
Syntax: files([<targetFolder>])
7+
Syntax: files([<targetFolder>[, <outputKind>]])
88

99
Summary:
1010
List the files in a folder.
@@ -38,6 +38,9 @@ filter tDiskContents["the defaultFolder"] without ".."
3838
Parameters:
3939
targetFolder (String):
4040
The folder path to search.
41+
outputKind (enum):
42+
- empty: Use the short form
43+
- "detailed": Use the long/detailed form
4144

4245
Returns (String):
4346
A list of file names or file information, with one

docs/dictionary/function/folders.lcdoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Type: function
44

55
Syntax: the [{ detailed | long }] folders
66

7-
Syntax: folders([<targetFolder>])
7+
Syntax: folders([<targetFolder>[, <outputKind>]])
88

99
Summary:
1010
List the subfolders in a folder.
@@ -32,6 +32,9 @@ filter tDiskContents["the defaultFolder"] without ".."
3232
Parameters:
3333
targetFolder (String):
3434
The folder path to search.
35+
outputKind (enum):
36+
- empty: Use the short form
37+
- "detailed": Use the long/detailed form
3538

3639
Returns (String):
3740
A list of folder names or folder information, with

docs/notes/bugfix-18010.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Add optional 'kind' parameter to files/folders
2+
3+
The files and folders functions can now take an optional
4+
second parameter 'kind'. The kind parameter can be either
5+
empty or "detailed". If empty, the normal (short) form of
6+
the function is returned, otherwise the detailed (long)
7+
form of the function is returned.

engine/src/exec-files.cpp

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,20 @@ MCExecEnumTypeInfo *kMCFilesEofEnumTypeInfo = &_kMCFilesEofEnumTypeInfo;
5353

5454
//////////
5555

56-
void MCFilesEvalDirectories(MCExecContext & ctxt,
57-
MCStringRef & r_string)
58-
{
59-
MCFilesEvalDirectoriesOfDirectory(ctxt, nil, r_string);
60-
}
61-
62-
void MCFilesEvalDirectoriesOfDirectory(MCExecContext& ctxt,
63-
MCStringRef p_directory,
64-
MCStringRef& r_string)
65-
{
66-
if (MCsecuremode & MC_SECUREMODE_DISK)
67-
{
68-
ctxt . LegacyThrow(EE_DISK_NOPERM);
69-
return;
70-
}
71-
MCAutoListRef t_list;
72-
if (MCS_getentries(p_directory, false, false, &t_list))
73-
{
74-
MCListCopyAsString(*t_list, r_string);
75-
}
76-
else
77-
{
78-
MCStringCopy(kMCEmptyString, r_string);
79-
}
80-
}
81-
82-
void MCFilesEvalFiles(MCExecContext & ctxt,
83-
MCStringRef & r_string)
84-
{
85-
MCFilesEvalFilesOfDirectory(ctxt, nil, r_string);
86-
}
87-
88-
void MCFilesEvalFilesOfDirectory(MCExecContext& ctxt,
89-
MCStringRef p_directory,
90-
MCStringRef& r_string)
56+
void
57+
MCFilesEvalFileItemsOfDirectory(MCExecContext& ctxt,
58+
MCStringRef p_directory,
59+
bool p_files,
60+
bool p_detailed,
61+
MCStringRef& r_string)
9162
{
9263
if (MCsecuremode & MC_SECUREMODE_DISK)
9364
{
9465
ctxt . LegacyThrow(EE_DISK_NOPERM);
9566
return;
9667
}
9768
MCAutoListRef t_list;
98-
if (MCS_getentries(p_directory, true, false, &t_list))
69+
if (MCS_getentries(p_directory, p_files, p_detailed, &t_list))
9970
{
10071
MCListCopyAsString(*t_list, r_string);
10172
}

engine/src/exec.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,10 +3148,7 @@ void MCEngineEvalCommandArgumentAtIndex(MCExecContext& ctxt, uinteger_t t_index,
31483148

31493149
///////////
31503150

3151-
void MCFilesEvalDirectories(MCExecContext& ctxt, MCStringRef& r_string);
3152-
void MCFilesEvalDirectoriesOfDirectory(MCExecContext& ctxt, MCStringRef p_directory, MCStringRef& r_string);
3153-
void MCFilesEvalFiles(MCExecContext& ctxt, MCStringRef& r_string);
3154-
void MCFilesEvalFilesOfDirectory(MCExecContext& ctxt, MCStringRef p_directory, MCStringRef& r_string);
3151+
void MCFilesEvalFileItemsOfDirectory(MCExecContext& ctxt, MCStringRef p_directory, bool p_files, bool p_detailed, MCStringRef& r_string);
31553152
void MCFilesEvalDiskSpace(MCExecContext& ctxt, real64_t& r_result);
31563153
void MCFilesEvalDriverNames(MCExecContext& ctxt, MCStringRef& r_string);
31573154
void MCFilesEvalDrives(MCExecContext& ctxt, MCStringRef& r_string);

engine/src/executionerrors.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,12 @@ enum Exec_errors
27612761

27622762
// {EE-0904} stack: password protecting stacks not supported in this edition
27632763
EE_STACK_PASSWORD_NOT_SUPPORTED,
2764+
2765+
// {EE-0905} files: error in kind parameter
2766+
EE_FILES_BADKIND,
2767+
2768+
// {EE-0906} folders: error in kind parameter
2769+
EE_FOLDERS_BADKIND,
27642770
};
27652771

27662772
extern const char *MCexecutionerrors;

engine/src/express.cpp

Lines changed: 83 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -125,34 +125,41 @@ Parse_stat MCExpression::get0params(MCScriptPoint &sp)
125125
return PS_NORMAL;
126126
}
127127

128+
Parse_stat MCExpression::gettheparam(MCScriptPoint& sp, Boolean single, MCExpression** exp)
129+
{
130+
initpoint(sp);
131+
if (sp.skip_token(SP_FACTOR, TT_OF) != PS_NORMAL)
132+
return PS_NORMAL;
133+
if (sp.parseexp(single, False, exp) != PS_NORMAL)
134+
{
135+
MCperror->add
136+
(PE_FACTOR_BADPARAM, sp);
137+
return PS_ERROR;
138+
}
139+
return PS_NORMAL;
140+
}
141+
128142
Parse_stat MCExpression::get0or1param(MCScriptPoint &sp, MCExpression **exp,
129143
Boolean the)
130144
{
131145
if (the)
132146
{
133-
initpoint(sp);
134-
if (sp.skip_token(SP_FACTOR, TT_OF) != PS_NORMAL)
135-
return PS_NORMAL;
136-
if (sp.parseexp(False, False, exp) != PS_NORMAL)
137-
{
138-
MCperror->add
139-
(PE_FACTOR_BADPARAM, sp);
140-
return PS_ERROR;
141-
}
147+
return gettheparam(sp, False, exp);
142148
}
143-
else
144-
{
145-
MCExpression *earray[MAX_EXP];
146-
uint2 ecount = 0;
147-
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount > 1)
148-
{
149-
freeexps(earray, ecount);
150-
return PS_ERROR;
151-
}
152-
else
153-
if (ecount == 1)
154-
*exp = earray[0];
149+
150+
MCExpression *earray[MAX_EXP];
151+
uint2 ecount = 0;
152+
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount > 1)
153+
{
154+
freeexps(earray, ecount);
155+
return PS_ERROR;
156+
}
157+
158+
if (ecount == 1)
159+
{
160+
*exp = earray[0];
155161
}
162+
156163
return PS_NORMAL;
157164
}
158165

@@ -161,32 +168,47 @@ Parse_stat MCExpression::get1param(MCScriptPoint &sp, MCExpression **exp,
161168
{
162169
if (the)
163170
{
164-
initpoint(sp);
165-
if (sp.skip_token(SP_FACTOR, TT_OF) != PS_NORMAL)
166-
{
167-
MCperror->add
168-
(PE_FACTOR_NOOF, sp);
169-
return PS_ERROR;
170-
}
171-
if (sp.parseexp(True, False, exp) != PS_NORMAL)
172-
{
173-
MCperror->add
174-
(PE_FACTOR_BADPARAM, sp);
175-
return PS_ERROR;
176-
}
171+
return gettheparam(sp, True, exp);
177172
}
178-
else
173+
174+
MCExpression *earray[MAX_EXP];
175+
uint2 ecount = 0;
176+
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount != 1)
177+
{
178+
freeexps(earray, ecount);
179+
return PS_ERROR;
180+
}
181+
182+
*exp = earray[0];
183+
184+
return PS_NORMAL;
185+
}
186+
187+
Parse_stat MCExpression::get0or1or2params(MCScriptPoint &sp, MCExpression **exp1,
188+
MCExpression **exp2, Boolean the)
189+
{
190+
if (the)
179191
{
180-
MCExpression *earray[MAX_EXP];
181-
uint2 ecount = 0;
182-
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount != 1)
183-
{
184-
freeexps(earray, ecount);
185-
return PS_ERROR;
186-
}
187-
else
188-
*exp = earray[0];
192+
return gettheparam(sp, False, exp1);
189193
}
194+
195+
MCExpression *earray[MAX_EXP];
196+
uint2 ecount = 0;
197+
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount > 2)
198+
{
199+
freeexps(earray, ecount);
200+
return PS_ERROR;
201+
}
202+
203+
if (ecount > 0)
204+
{
205+
*exp1 = earray[0];
206+
if (ecount > 1)
207+
{
208+
*exp2 = earray[1];
209+
}
210+
}
211+
190212
return PS_NORMAL;
191213
}
192214

@@ -195,33 +217,24 @@ Parse_stat MCExpression::get1or2params(MCScriptPoint &sp, MCExpression **exp1,
195217
{
196218
if (the)
197219
{
198-
initpoint(sp);
199-
if (sp.skip_token(SP_FACTOR, TT_OF) != PS_NORMAL)
200-
{
201-
MCperror->add
202-
(PE_FACTOR_NOOF, sp);
203-
return PS_ERROR;
204-
}
205-
if (sp.parseexp(True, False, exp1) != PS_NORMAL)
206-
{
207-
MCperror->add
208-
(PE_FACTOR_BADPARAM, sp);
209-
return PS_ERROR;
210-
}
211-
}
212-
else
213-
{
214-
MCExpression *earray[MAX_EXP];
215-
uint2 ecount = 0;
216-
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount < 1 || ecount > 2)
217-
{
218-
freeexps(earray, ecount);
219-
return PS_ERROR;
220-
}
221-
*exp1 = earray[0];
222-
if (ecount == 2)
223-
*exp2 = earray[1];
220+
return gettheparam(sp, True, exp1);
224221
}
222+
223+
MCExpression *earray[MAX_EXP];
224+
uint2 ecount = 0;
225+
if (getexps(sp, earray, ecount) != PS_NORMAL || ecount < 1 || ecount > 2)
226+
{
227+
freeexps(earray, ecount);
228+
return PS_ERROR;
229+
}
230+
231+
*exp1 = earray[0];
232+
233+
if (ecount == 2)
234+
{
235+
*exp2 = earray[1];
236+
}
237+
225238
return PS_NORMAL;
226239
}
227240

engine/src/express.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class MCExpression
115115
Parse_stat get0params(MCScriptPoint &);
116116
Parse_stat get0or1param(MCScriptPoint &sp, MCExpression **exp, Boolean the);
117117
Parse_stat get1param(MCScriptPoint &, MCExpression **exp, Boolean the);
118+
Parse_stat get0or1or2params(MCScriptPoint &, MCExpression **e1,
119+
MCExpression **e2, Boolean the);
118120
Parse_stat get1or2params(MCScriptPoint &, MCExpression **e1,
119121
MCExpression **e2, Boolean the);
120122
Parse_stat get2params(MCScriptPoint &, MCExpression **e1, MCExpression **e2);
@@ -133,6 +135,11 @@ class MCExpression
133135
Parse_stat getparams(MCScriptPoint &spt, MCParameter **params);
134136
void initpoint(MCScriptPoint &);
135137
static bool compare_array_element(void *context, MCArrayRef array, MCNameRef key, MCValueRef value);
138+
139+
private:
140+
/* The single parameter is parsed to the 'single' argument of parseexp -
141+
* for 0 param fetches this is False, for others this is True. */
142+
Parse_stat gettheparam(MCScriptPoint& sp, Boolean single, MCExpression** exp);
136143
};
137144

138145
class MCFuncref : public MCExpression

0 commit comments

Comments
 (0)