version 1.1.1.1.2.5, 2011/11/18 17:23:59
|
version 1.1.1.1.2.6, 2011/11/21 16:28:05
|
Line 202 mqtt_sizeLen(u_int len)
|
Line 202 mqtt_sizeLen(u_int len)
|
} |
} |
|
|
/* |
/* |
* mqtt_str2var Create MQTT variable from string | * mqtt_str2sub Create MQTT subscribe variable from string(s) |
* @csStr = string | * @csStr = strings |
* @strLen = string length | * @strnum = number of strings elements |
* return: NULL error or != ok variable, must be free after use! | * @qoses = QoS elements applied to subscribe variable, |
| * count of elements must be equal with csStr elements |
| * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub() |
*/ |
*/ |
inline mqtthdr_var_t * | inline mqtt_subscr_t * |
mqtt_str2var(const u_char *csStr, u_short strLen) | mqtt_str2sub(const char **csStr, u_short strnum, u_char *qoses) |
{ |
{ |
mqtthdr_var_t *v; | mqtt_subscr_t *v; |
| register int i, items; |
| const char **strs; |
|
|
if (!csStr) |
if (!csStr) |
return NULL; |
return NULL; |
if (!(v = malloc(strLen + sizeof(mqtthdr_var_t)))) { | for (items = 0, strs = csStr; *strs; items++, strs++) |
| if (strnum && items >= strnum) { |
| items = strnum; |
| break; |
| } |
| |
| if (!(v = malloc((items + 1) * sizeof(mqtt_subscr_t)))) { |
LOGERR; |
LOGERR; |
return NULL; |
return NULL; |
} else |
} else |
memset(v, 0, strLen + sizeof(mqtthdr_var_t)); | memset(v, 0, (items + 1) * sizeof(mqtt_subscr_t)); |
|
|
memcpy(v->var_data, csStr, strLen); | for (i = 0; i < items; i++) { |
v->var_sb.val = htons(strLen); | v[i].sub_sb.val = htons(strlen(csStr[i])); |
| v[i].sub_data = strdup(csStr[i]); |
| if (qoses && qoses[i] < MQTT_QOS_RESERVED) |
| v[i].sub_qos = qoses[i]; |
| } |
|
|
return v; |
return v; |
|
} |
|
|
|
/* |
|
* mqtt_freeSub() Free array from subscribe variables |
|
* |
|
* @subs = Subscribe variables |
|
* return: none |
|
*/ |
|
inline void |
|
mqtt_freeSub(mqtt_subscr_t ** __restrict subs) |
|
{ |
|
mqtt_subscr_t *v; |
|
|
|
if (!subs) |
|
return; |
|
|
|
for (v = *subs; v; v++) |
|
free(v->sub_data); |
|
free(*subs); |
|
*subs = NULL; |
} |
} |