Skip to content

Commit

Permalink
Merge pull request #42 from nmcdonnell-kx/master
Browse files Browse the repository at this point in the history
Reapply Rian's change without strndup
  • Loading branch information
nmcdonnell-kx authored Mar 22, 2021
2 parents b27c5f9 + 48b6206 commit 7985c95
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ static char* getStringFromList(K propValues,int row, const char** value, char* e
return errStr;
}

static char* getCharArrayAsStringFromList(K propValues,int row, const char** value, char* errStr)
{
if ((int)(kK(propValues)[row]->t) == KC)
{
// Don't use strndup because it's not supported on early OS X builds
char* str = malloc(kK(propValues)[row]->n + 1);
memcpy(str, kC(kK(propValues)[row]), kK(propValues)[row]->n);
str[kK(propValues)[row]->n] = '\0';
*value = str;
return 0;
}
return errStr;
}

static char* getIntFromList(K propValues,int row, int* value, char* errStr)
{
if ((int)(kK(propValues)[row]->t) == -KI)
Expand All @@ -77,6 +91,7 @@ EXP K connX(K tcpconn,K pname, K opt){
return krr("options");
client = 0;

MQTTClient_willOptions will_opts = MQTTClient_willOptions_initializer;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;

K propNames = (kK(opt)[0]);
Expand Down Expand Up @@ -115,6 +130,15 @@ EXP K connX(K tcpconn,K pname, K opt){
errStr = getIntFromList(propValues,row,&conn_opts.maxInflightMessages,"maxInflightMessages type incorrect");
else if (strcmp(kS(propNames)[row],"cleanstart")==0)
errStr = getIntFromList(propValues,row,&conn_opts.cleanstart,"cleanstart type incorrect");
else if (strcmp(kS(propNames)[row],"lastWillTopic")==0){
conn_opts.will = &will_opts;
errStr = getStringFromList(propValues,row,&will_opts.topicName,"lastWillTopic type incorrect");}
else if (strcmp(kS(propNames)[row],"lastWillQos")==0)
errStr = getIntFromList(propValues,row,&will_opts.qos,"lastWillQos type incorrect");
else if (strcmp(kS(propNames)[row],"lastWillMessage")==0)
errStr = getCharArrayAsStringFromList(propValues,row,&will_opts.message,"lastWillMessage type incorrect");
else if (strcmp(kS(propNames)[row],"lastWillRetain")==0)
errStr = getIntFromList(propValues,row,&will_opts.retained,"lastWillRetain type incorrect");
else
errStr = "Unsupported conn opt name in dictionary";
}
Expand Down

0 comments on commit 7985c95

Please sign in to comment.