String Array with variables, call in function one by one.

Hi there,

The initial load of variables are done as showed below.
I would like to have an array to use it for load/save after boot, and make the code more clean.

Basically the intention is to have an array with variable names, which are called in the function one by one.

Create an array with those variables, which needs to be saved, and later on use the same for load the previously saved batch.

String Variables[] = 
{
wireless_ssid, 
wireless_pass, 
local_ip, 
local_gateway,
local_dns
};

This is used now.
Can we improve the code to use the array?
It would be good to have the array, insted of the full repeated list of variables in the middle of the code.

void ConfigFile_Save_Defaults() {
  debug1("CreateDefault_ConfigFile: ");
  File configFile = SPIFFS.open("/config.json", "w");
  if (!configFile) {
    debug1("- failed to open config file for writing");
    return;
  }
  DynamicJsonDocument doc(2048);
  JsonObject json = doc.to<JsonObject>();
  //
  json["wireless_ssid"]           = wireless_ssid;
  json["wireless_pass"]           = wireless_pass;
  //insted of list of variables, use the array
  //
  serializeJson(json, Serial);         //debug to Terminal
  serializeJson(json, configFile);    //write to confFile
  configFile.close();
  debug1("- saved OK.");
}

I would be grateful for any help or hints.
Thenks

If you have access to stl libraries. Look into std::array<> and std::pair<>. They are seemed to be what you need.

what about a statically defined list of variable references and associate strings

typedef struct {
    const char*  sym;
    int*         var;
} Var_t;

Var_t vars [] = {
 { "wireless_ssid", & wireless_ssid },
 { "wireless_pass", & wireless_pass },
 { "local_ip",      & local_ip },
 { "local_gateway", & local_gateway },
 { "local_dns",     & local_dns },
};

#define VARS_SIZE (sizeof(vars)/sizeof(Var_t))

void
load (void)
{
    Var_t* p = vars;

    for (int n = 0; n < VARS_SIZE; n++)  {
        json [p->sym] = p->var;
    }
}

gcjr:
what about a statically defined list of variable references and associate strings

typedef struct {

const char*  sym;
   int*         var;
} Var_t;

Var_t vars [] = {
{ "wireless_ssid", & wireless_ssid },
{ "wireless_pass", & wireless_pass },
{ "local_ip",      & local_ip },
{ "local_gateway", & local_gateway },
{ "local_dns",     & local_dns },
};

#define VARS_SIZE (sizeof(vars)/sizeof(Var_t))

void
load (void)
{
   Var_t* p = vars;

for (int n = 0; n < VARS_SIZE; n++)  {
       json [p->sym] = p->var;
   }
}

This is more or less looks good.Bit more code, but I can live with it.
modified to fit the function, but got compile error:

could not convert '& wireless_ssid' from 'String*' to 'String'

typedef struct {
  String   sym;
  String   var;
} Var_t;

Var_t vars [] = {
  { "wireless_ssid", & wireless_ssid },
  { "wireless_pass", & wireless_pass },
  { "local_ip",      & local_ip },
  { "gateway_ip",    & gateway_ip },
  { "subnet_ip",     & subnet_ip }
};

#define VARS_SIZE (sizeof(vars)/sizeof(Var_t))

void variables (void)
{
  Var_t* p = vars;

  for (int n = 0; n < VARS_SIZE; n++)  {
    json [p->sym] = p->var;
  }
}
typedef struct {
  String   sym;
  String   var;
} Var_t;

you defined both elements at "String" but "var" is a pointer to the variable

gcjr:

typedef struct {

String  sym;
  String  var;
} Var_t;




you defined both elements at "String" but "var" is a pointer to the variable

actually yes, since my variables are storing values in Strings.
most of the variables are strings, due to the web design, which saves the files to string values.

So you are saying I cannot use them as String?

each element in vars statically defines a string name and the location of the variable which I assumed was an integer. I'm not that familiar with String. the table needs to know how to obtain the value of var. I don't know if you can initialize the vars[] entry with a pointer to a String which can be dereferenced (i.e. *pString) when reading the value.

ok, i see,
in this case it seems I need to find another solution.

Any other addvices?

i think this is what you want, a way a list of variables (pointers) and corresponding string description so that you can manage their transfer in JSON format.

i think you want a pointer to the data so that you know where to get it or put the values in memory.

it's easy enough to create a String for a corresponding value (e.g. int, float) as well as translate a value represented by a String to an int or float. but i think all your data are actually manages as Strings

there's no reason the variable reference can't be to a char (e.g. "192.68.1.1"), but you may be able to reference a String.

typedef struct {
  String   sym;
  char    *var;
} Var_t;

Var_t vars [] = {
  { "wireless_ssid", & wireless_ssid },
...

or (i'm not that familiar with String)
maybe both sym and var need to be references (e.g. *sym)

typedef struct {
  String   sym;
  String  *var;
} Var_t;