Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
Fix json save (#1235)
Browse files Browse the repository at this point in the history
* Add global variable initialization support

* Fix json globals to include initial value
  • Loading branch information
dchassin authored Jan 4, 2023
1 parent 7fb2e96 commit 10d5da7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
8 changes: 5 additions & 3 deletions runtime/gridlabd.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,14 @@ typedef enum e_passconfig

typedef enum {FAILED=FALSE, SUCCESS=TRUE} STATUS;

typedef struct s_globalvar {
typedef struct s_globalvar
{
PROPERTY *prop;
struct s_globalvar *next;
uint32 flags;
void (*callback)(char*);
void (*callback)(const char *);
LOCKVAR lock;
const char *initial;
struct s_globalvar *next;
} GLOBALVAR;

typedef enum {
Expand Down
15 changes: 15 additions & 0 deletions source/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ STATUS GldGlobals::init(void)
that message and try again.
*/
} else {
char buffer[1024];
getvar(p->name,buffer,sizeof(buffer)-1);
var->initial = strdup(buffer);
var->prop->keywords = p->keys;
var->callback = p->callback;
}
Expand Down Expand Up @@ -651,6 +654,10 @@ GLOBALVAR *GldGlobals::create_v(const char *name, va_list arg)
*/
}
}
else if ( proptype == PT_DEFAULT )
{
var->initial = strdup(va_arg(arg,char*));
}
else if ( proptype == PT_DESCRIPTION )
{
prop->description = va_arg(arg,char*);
Expand Down Expand Up @@ -708,6 +715,14 @@ GLOBALVAR *GldGlobals::create_v(const char *name, va_list arg)
}
}

if ( var->initial != NULL )
{
if ( class_string_to_property(var->prop,(void*)var->prop->addr,var->initial) <= 0 )
{
throw_exception("global_create(char *name='%s',...): cannot set initial value '%s'", name, var->initial);
}
}

if ( lastvar == NULL )
{
/* first variable */
Expand Down
1 change: 1 addition & 0 deletions source/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct s_globalvar
uint32 flags;
void (*callback)(const char *);
LOCKVAR lock;
const char *initial;
struct s_globalvar *next;
} GLOBALVAR;

Expand Down
14 changes: 11 additions & 3 deletions source/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ int GldJsonWriter::write_globals(FILE *fp)
if ( prop->flags&PF_OUTPUT ) strcat(flags,flags[0]?"|":""),strcat(flags,"OUTPUT");
if ( prop->flags&PF_DYNAMIC ) strcat(flags,flags[0]?"|":""),strcat(flags,"DYNAMIC");
if ( prop->flags ) len += write("\t\t\t\t\"flags\" : \"%s\",",flags);
len += write("\n\t\t\t\"access\" : \"%s\",",access);
if ( var->initial != NULL )
{
len += write("\n\t\t\t\"initial\" : \"%s\",", escape(var->initial));
}
len += write("\n\t\t\t\"access\" : \"%s\",",access);
if ( buffer[0] == '\"' )
len += write("\n\t\t\t\"value\" : \"%s\"", escape(buffer+1,strlen(buffer)-2));
else
Expand Down Expand Up @@ -657,7 +661,9 @@ int GldJsonWriter::write_output(FILE *fp)
int len = 0;
json = fp;
len += write("{\t\"application\": \"gridlabd\",\n");
len += write("\t\"version\" : \"%u.%u.%u\"",global_version_major,global_version_minor,global_version_patch);
len += write("\t\"version\" : \"%u.%u.%u\",\n",global_version_major,global_version_minor,global_version_patch);
len += write("\t\"branch\": \"%s\",\n",BRANCH);
len += write("\t\"build\": \"%06d\"",BUILDNUM);
if ( (global_filesave_options&FSO_MODULES) == FSO_MODULES )
{
len += write_modules(fp);
Expand Down Expand Up @@ -741,7 +747,9 @@ int GldJsonWriter::dump_modules()
int len = 0;
json = stdout;
len += write("{\t\"application\": \"gridlabd\",\n");
len += write("\t\"version\" : \"%u.%u.%u\"",global_version_major,global_version_minor,global_version_patch);
len += write("\t\"version\" : \"%u.%u.%u\",\n",global_version_major,global_version_minor,global_version_patch);
len += write("\t\"branch\": \"%s\",\n",BRANCH);
len += write("\t\"build\": \"%06d\"",BUILDNUM);
len += write_modules(json);
len += write_classes(json,true);
len += write("\n}\n");
Expand Down

0 comments on commit 10d5da7

Please sign in to comment.