Skip to content

Commit 9ddd07a

Browse files
committed
Add ability to loop through sub-objects
1 parent 255e3eb commit 9ddd07a

7 files changed

+140
-1
lines changed

Diff for: Actions.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void Extension::LoadJSON(TCHAR const *JSON, int flags)
3535
}
3636
current = root = temp;
3737
bookmarks.clear();
38+
loops.clear();
3839
}
3940

4041
void Extension::EnterObject(TCHAR const *Name)
@@ -79,6 +80,47 @@ void Extension::GotoBookmark(TCHAR const *Name)
7980
}
8081
}
8182

83+
void Extension::LoopObjects(TCHAR const *LoopName)
84+
{
85+
json_value const *const looper = current;
86+
loops.push_back(Loop(LoopName, looper));
87+
if(IsObject())
88+
{
89+
std::size_t length = looper->u.object.length;
90+
for(std::size_t i = 0; i < length; ++i)
91+
{
92+
if(loops.size() == 0 || loops.back().object != looper)
93+
{
94+
break;
95+
}
96+
loops.back().index = i;
97+
loops.back().sub_name = std::string(looper->u.object.values[i].name, looper->u.object.values[i].name_length);
98+
loops.back().sub = looper->u.object.values[i].value;
99+
Runtime.GenerateEvent(26);
100+
}
101+
}
102+
else if(IsArray())
103+
{
104+
std::size_t length = looper->u.array.length;
105+
for(std::size_t i = 0; i < length; ++i)
106+
{
107+
if(loops.size() == 0 || loops.back().object != looper)
108+
{
109+
break;
110+
}
111+
loops.back().index = i;
112+
loops.back().sub_name.clear();
113+
loops.back().sub = looper->u.array.values[i];
114+
Runtime.GenerateEvent(26);
115+
}
116+
}
117+
if(loops.size() > 0 && loops.back().object == looper)
118+
{
119+
loops.pop_back();
120+
current = looper;
121+
}
122+
}
123+
82124
void Extension::DebugWindow()
83125
{
84126
std::basic_ostringstream<TCHAR> oss;

Diff for: Conditions.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ bool Extension::OnError()
55
{
66
return true;
77
}
8+
bool Extension::OnLoop(TCHAR const *LoopName)
9+
{
10+
if(loops.size() > 0 && loops.back().name == LoopName)
11+
{
12+
current = loops.back().sub;
13+
return true;
14+
}
15+
return false;
16+
}
817
bool Extension::IsString()
918
{
1019
return current->type == json_string;

Diff for: Expressions.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@ TCHAR const *Extension::GetError()
44
{
55
return Runtime.CopyString(error.c_str());
66
}
7+
TCHAR const *Extension::GetIteratedName()
8+
{
9+
if(loops.size() > 0)
10+
{
11+
TCHAR *t = Edif::ConvertString(loops.back().sub_name.c_str());
12+
TCHAR *c = Runtime.CopyString(t);
13+
Edif::FreeString(t);
14+
return c;
15+
}
16+
return _T("");
17+
}
18+
int Extension::GetIteratedIndex()
19+
{
20+
if(loops.size() > 0)
21+
{
22+
return loops.back().index;
23+
}
24+
return 0;
25+
}
726

827
TCHAR const *Extension::GetString()
928
{

Diff for: Ext.json

+25
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
[6, "Set Bookmark here"],
2626
[7, "Go to Bookmark"]
2727
],
28+
[8, "Loop through all sub-objects"],
2829
"Separator",
2930
[0, "Modification", true],
3031
"Separator",
@@ -36,6 +37,8 @@
3637
"Separator",
3738
[0, "On Error"],
3839
"Separator",
40+
[26, "On Loop"],
41+
"Separator",
3942
["This object...",
4043
[1, "Is String?"],
4144
[2, "Is Integer?"],
@@ -74,6 +77,9 @@
7477
"Separator",
7578
[0, "Get Error"],
7679
"Separator",
80+
[22, "Get Name of Looped Sub-Object"],
81+
[23, "Get Index of Looped Sub-Object"],
82+
"Separator",
7783
["This object...",
7884
[1, "Get String"],
7985
[2, "Get Integer"],
@@ -142,6 +148,12 @@
142148
[
143149
["String", "Name of Bookmark"]
144150
]
151+
},
152+
{ "Title": "Loop (%0) through all sub-objects",
153+
"Parameters":
154+
[
155+
["String", "Name of Loop"]
156+
]
145157
}
146158
],
147159
"Conditions":
@@ -291,6 +303,13 @@
291303
[
292304
["Integer", "Index of Value within current Array"]
293305
]
306+
},
307+
{ "Title": "%o: On Sub-Object Loop %0",
308+
"Triggered": true,
309+
"Parameters":
310+
[
311+
["String", "Name of Loop"]
312+
]
294313
}
295314
],
296315
"Expressions":
@@ -416,6 +435,12 @@
416435
[
417436
["Integer", "Subvalue Index"]
418437
]
438+
},
439+
{ "Title": "LoopedName$(",
440+
"Returns": "String"
441+
},
442+
{ "Title": "LoopedIndex(",
443+
"Returns": "Integer"
419444
}
420445
]
421446
}

Diff for: Extension.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
4040
LinkAction(5, DebugWindow);
4141
LinkAction(6, SetBookmark);
4242
LinkAction(7, GotoBookmark);
43+
LinkAction(8, LoopObjects);
4344

4445
LinkCondition(0, OnError);
4546
LinkCondition(1, IsString);
@@ -67,6 +68,7 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
6768
LinkCondition(23,IsArrBoolean);
6869
LinkCondition(24,IsArrNull);
6970
LinkCondition(25,IsArrTrue);
71+
LinkCondition(26,OnLoop);
7072

7173
LinkExpression(0, GetError);
7274
LinkExpression(1, GetString);
@@ -90,6 +92,8 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
9092
LinkExpression(19,GetArrDouble);
9193
LinkExpression(20,GetArrNumValues);
9294
LinkExpression(21,GetArrBoolNum);
95+
LinkExpression(22,GetIteratedName);
96+
LinkExpression(23,GetIteratedIndex);
9397

9498
current = root = json_parse("null", 4);
9599
}

Diff for: Extension.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <sstream>
33
#include <iomanip>
44
#include <map>
5+
#include <vector>
56

67
class Extension
78
{
@@ -77,6 +78,40 @@ class Extension
7778
json_value const *current;
7879
stdtstring error;
7980
std::map<stdtstring, json_value const *> bookmarks;
81+
struct Loop
82+
{
83+
stdtstring name;
84+
json_value const *object;
85+
std::string sub_name;
86+
std::size_t index;
87+
json_value const *sub;
88+
89+
Loop(stdtstring const &id, json_value const *obj)
90+
: name(id)
91+
, object(obj)
92+
, index()
93+
, sub()
94+
{
95+
}
96+
Loop(Loop const &from)
97+
: name(from.name)
98+
, object(from.object)
99+
, sub_name(from.sub_name)
100+
, index(from.index)
101+
, sub(from.sub)
102+
{
103+
}
104+
Loop &operator=(Loop const &from)
105+
{
106+
name = from.name;
107+
object = from.object;
108+
sub_name = from.sub_name;
109+
index = from.index;
110+
sub = from.sub;
111+
return *this;
112+
}
113+
};
114+
std::vector<Loop> loops;
80115

81116
#ifdef UNICODE
82117
std::string UTF8fromUnicode(std::wstring s)
@@ -108,6 +143,8 @@ class Extension
108143
void SetBookmark(TCHAR const *Name);
109144
void GotoBookmark(TCHAR const *Name);
110145

146+
void LoopObjects(TCHAR const *LoopName);
147+
111148
void DebugWindow();
112149

113150
struct TempDelve
@@ -131,6 +168,7 @@ class Extension
131168

132169
//Conditions
133170
bool OnError(); //0
171+
bool OnLoop(TCHAR const *LoopName); //26
134172

135173
bool IsString();
136174
bool IsInteger();
@@ -163,6 +201,8 @@ class Extension
163201

164202
//Expressions
165203
TCHAR const *GetError();
204+
TCHAR const *GetIteratedName();
205+
int GetIteratedIndex();
166206

167207
TCHAR const *GetString();
168208
int GetInteger();

Diff for: Properties.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void *MMF2Func GetPropValue(mv *mV, SerializedED *SED, UINT PropID)
139139
{
140140
case Prop::Version:
141141
{
142-
return new CPropStringValue(_T("Release v1.0.1"));
142+
return new CPropStringValue(_T("Release v1.1.0"));
143143
}
144144
//case Prop::MyString:
145145
// {

0 commit comments

Comments
 (0)