Hi,
I have a piece of code that I want to convert into a function
void getConfig() {
File myFile = FileSystem.open("/mnt/sda1/config.json", FILE_READ);
int jlen = myFile.size();
int i = 0;
char json[400];
if (myFile) {
while (myFile.available()) {
char c = myFile.read();
json[i] = c;
i++;
}
}
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("Parsing fail");
return;
}
JsonObject& url = root["url"];
const char *_host = url["host"];
const char *_apiVersion = url["apiVersion"];
const char *_resources = url["resources"];
JsonObject& destinations = root["destinations"];
const char *_stationFrom = destinations["from"];
const char *_stationTo = destinations["to"];
JsonObject& configs = root["configs"];
const char *_limit = configs["limit"];
const char *_fields = configs["fields"];
const char *cca = _host;
char *var = "?from=";
int ccalen = strlen(cca);
int varlen = strlen(var);
int totallen = ccalen + varlen;
char newText[totallen];
strncpy(newText, _host, totallen);
strcat(newText, var);
Serial.println(newText);
}
output from newText is:
http://transport.opendata.ch?from=
thats ok..
and now with function:
char * catcat(const char *cca, char *var) {
int ccalen = strlen(cca);
int varlen = strlen(var);
int totallen = ccalen + varlen;
char newText[totallen];
strncpy(newText, cca, totallen);
strcat(newText, var);
return newText;
}
void getConfig() {
File myFile = FileSystem.open("/mnt/sda1/config.json", FILE_READ);
int jlen = myFile.size();
int i = 0;
char json[400];
if (myFile) {
while (myFile.available()) {
char c = myFile.read();
json[i] = c;
i++;
}
}
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("Parsing fail");
return;
}
JsonObject& url = root["url"];
const char *_host = url["host"];
const char *_apiVersion = url["apiVersion"];
const char *_resources = url["resources"];
JsonObject& destinations = root["destinations"];
const char *_stationFrom = destinations["from"];
const char *_stationTo = destinations["to"];
JsonObject& configs = root["configs"];
const char *_limit = configs["limit"];
const char *_fields = configs["fields"];
char *newUrl;
newUrl = catcat(_host, "?from=");
Serial.println(newUrl);
...
}
then the output is:
http://tra��a��Da� �,a�a�9
I think I did something wrong on the return part.
some one has a idea pls.
THX