Skip to content

Commit

Permalink
Add ability to loop through sub-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
LB-- committed Jan 28, 2015
1 parent 255e3eb commit 9ddd07a
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 1 deletion.
42 changes: 42 additions & 0 deletions Actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void Extension::LoadJSON(TCHAR const *JSON, int flags)
}
current = root = temp;
bookmarks.clear();
loops.clear();
}

void Extension::EnterObject(TCHAR const *Name)
Expand Down Expand Up @@ -79,6 +80,47 @@ void Extension::GotoBookmark(TCHAR const *Name)
}
}

void Extension::LoopObjects(TCHAR const *LoopName)
{
json_value const *const looper = current;
loops.push_back(Loop(LoopName, looper));
if(IsObject())
{
std::size_t length = looper->u.object.length;
for(std::size_t i = 0; i < length; ++i)
{
if(loops.size() == 0 || loops.back().object != looper)
{
break;
}
loops.back().index = i;
loops.back().sub_name = std::string(looper->u.object.values[i].name, looper->u.object.values[i].name_length);
loops.back().sub = looper->u.object.values[i].value;
Runtime.GenerateEvent(26);
}
}
else if(IsArray())
{
std::size_t length = looper->u.array.length;
for(std::size_t i = 0; i < length; ++i)
{
if(loops.size() == 0 || loops.back().object != looper)
{
break;
}
loops.back().index = i;
loops.back().sub_name.clear();
loops.back().sub = looper->u.array.values[i];
Runtime.GenerateEvent(26);
}
}
if(loops.size() > 0 && loops.back().object == looper)
{
loops.pop_back();
current = looper;
}
}

void Extension::DebugWindow()
{
std::basic_ostringstream<TCHAR> oss;
Expand Down
9 changes: 9 additions & 0 deletions Conditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ bool Extension::OnError()
{
return true;
}
bool Extension::OnLoop(TCHAR const *LoopName)
{
if(loops.size() > 0 && loops.back().name == LoopName)
{
current = loops.back().sub;
return true;
}
return false;
}
bool Extension::IsString()
{
return current->type == json_string;
Expand Down
19 changes: 19 additions & 0 deletions Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ TCHAR const *Extension::GetError()
{
return Runtime.CopyString(error.c_str());
}
TCHAR const *Extension::GetIteratedName()
{
if(loops.size() > 0)
{
TCHAR *t = Edif::ConvertString(loops.back().sub_name.c_str());
TCHAR *c = Runtime.CopyString(t);
Edif::FreeString(t);
return c;
}
return _T("");
}
int Extension::GetIteratedIndex()
{
if(loops.size() > 0)
{
return loops.back().index;
}
return 0;
}

TCHAR const *Extension::GetString()
{
Expand Down
25 changes: 25 additions & 0 deletions Ext.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
[6, "Set Bookmark here"],
[7, "Go to Bookmark"]
],
[8, "Loop through all sub-objects"],
"Separator",
[0, "Modification", true],
"Separator",
Expand All @@ -36,6 +37,8 @@
"Separator",
[0, "On Error"],
"Separator",
[26, "On Loop"],
"Separator",
["This object...",
[1, "Is String?"],
[2, "Is Integer?"],
Expand Down Expand Up @@ -74,6 +77,9 @@
"Separator",
[0, "Get Error"],
"Separator",
[22, "Get Name of Looped Sub-Object"],
[23, "Get Index of Looped Sub-Object"],
"Separator",
["This object...",
[1, "Get String"],
[2, "Get Integer"],
Expand Down Expand Up @@ -142,6 +148,12 @@
[
["String", "Name of Bookmark"]
]
},
{ "Title": "Loop (%0) through all sub-objects",
"Parameters":
[
["String", "Name of Loop"]
]
}
],
"Conditions":
Expand Down Expand Up @@ -291,6 +303,13 @@
[
["Integer", "Index of Value within current Array"]
]
},
{ "Title": "%o: On Sub-Object Loop %0",
"Triggered": true,
"Parameters":
[
["String", "Name of Loop"]
]
}
],
"Expressions":
Expand Down Expand Up @@ -416,6 +435,12 @@
[
["Integer", "Subvalue Index"]
]
},
{ "Title": "LoopedName$(",
"Returns": "String"
},
{ "Title": "LoopedIndex(",
"Returns": "Integer"
}
]
}
4 changes: 4 additions & 0 deletions Extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
LinkAction(5, DebugWindow);
LinkAction(6, SetBookmark);
LinkAction(7, GotoBookmark);
LinkAction(8, LoopObjects);

LinkCondition(0, OnError);
LinkCondition(1, IsString);
Expand Down Expand Up @@ -67,6 +68,7 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
LinkCondition(23,IsArrBoolean);
LinkCondition(24,IsArrNull);
LinkCondition(25,IsArrTrue);
LinkCondition(26,OnLoop);

LinkExpression(0, GetError);
LinkExpression(1, GetString);
Expand All @@ -90,6 +92,8 @@ Extension::Extension(RD *rd, SerializedED *SED, createObjectInfo *COB) : rd(rd),
LinkExpression(19,GetArrDouble);
LinkExpression(20,GetArrNumValues);
LinkExpression(21,GetArrBoolNum);
LinkExpression(22,GetIteratedName);
LinkExpression(23,GetIteratedIndex);

current = root = json_parse("null", 4);
}
Expand Down
40 changes: 40 additions & 0 deletions Extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <sstream>
#include <iomanip>
#include <map>
#include <vector>

class Extension
{
Expand Down Expand Up @@ -77,6 +78,40 @@ class Extension
json_value const *current;
stdtstring error;
std::map<stdtstring, json_value const *> bookmarks;
struct Loop
{
stdtstring name;
json_value const *object;
std::string sub_name;
std::size_t index;
json_value const *sub;

Loop(stdtstring const &id, json_value const *obj)
: name(id)
, object(obj)
, index()
, sub()
{
}
Loop(Loop const &from)
: name(from.name)
, object(from.object)
, sub_name(from.sub_name)
, index(from.index)
, sub(from.sub)
{
}
Loop &operator=(Loop const &from)
{
name = from.name;
object = from.object;
sub_name = from.sub_name;
index = from.index;
sub = from.sub;
return *this;
}
};
std::vector<Loop> loops;

#ifdef UNICODE
std::string UTF8fromUnicode(std::wstring s)
Expand Down Expand Up @@ -108,6 +143,8 @@ class Extension
void SetBookmark(TCHAR const *Name);
void GotoBookmark(TCHAR const *Name);

void LoopObjects(TCHAR const *LoopName);

void DebugWindow();

struct TempDelve
Expand All @@ -131,6 +168,7 @@ class Extension

//Conditions
bool OnError(); //0
bool OnLoop(TCHAR const *LoopName); //26

bool IsString();
bool IsInteger();
Expand Down Expand Up @@ -163,6 +201,8 @@ class Extension

//Expressions
TCHAR const *GetError();
TCHAR const *GetIteratedName();
int GetIteratedIndex();

TCHAR const *GetString();
int GetInteger();
Expand Down
2 changes: 1 addition & 1 deletion Properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void *MMF2Func GetPropValue(mv *mV, SerializedED *SED, UINT PropID)
{
case Prop::Version:
{
return new CPropStringValue(_T("Release v1.0.1"));
return new CPropStringValue(_T("Release v1.1.0"));
}
//case Prop::MyString:
// {
Expand Down

0 comments on commit 9ddd07a

Please sign in to comment.