mosquitto/src/topic_tok.c

111 lines
2.2 KiB
C
Raw Normal View History

2019-05-14 12:24:46 +00:00
/*
Copyright (c) 2010-2019 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
http://www.eclipse.org/legal/epl-v10.html
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
#include "util_mosq.h"
#include "utlist.h"
static char *strtok_hier(char *str, char **saveptr)
2019-05-14 12:24:46 +00:00
{
char *c;
2019-05-14 12:24:46 +00:00
if(str != NULL){
*saveptr = str;
2019-05-14 12:24:46 +00:00
}
if(*saveptr == NULL){
2019-05-14 12:24:46 +00:00
return NULL;
}
c = strchr(*saveptr, '/');
if(c){
str = *saveptr;
*saveptr = c+1;
c[0] = '\0';
}else if(*saveptr){
/* No match, but surplus string */
str = *saveptr;
*saveptr = NULL;
2019-05-14 12:24:46 +00:00
}
return str;
2019-05-14 12:24:46 +00:00
}
int sub__topic_tokenise(const char *subtopic, char **local_sub, char ***topics, const char **sharename)
2019-05-14 12:24:46 +00:00
{
char *saveptr = NULL;
char *token;
int count;
int topic_index = 0;
2019-05-14 12:24:46 +00:00
int i;
*local_sub = mosquitto__strdup(subtopic);
if((*local_sub) == NULL) return MOSQ_ERR_NOMEM;
2019-05-14 12:24:46 +00:00
count = 0;
saveptr = *local_sub;
while(saveptr){
saveptr = strchr(&saveptr[1], '/');
count++;
2019-05-14 12:24:46 +00:00
}
*topics = mosquitto__calloc(count+3 /* 3=$,shared,sharename */, sizeof(char *));
if((*topics) == NULL){
mosquitto__free(*local_sub);
return MOSQ_ERR_NOMEM;
2019-05-14 12:24:46 +00:00
}
if((*local_sub)[0] != '$'){
(*topics)[topic_index] = "";
topic_index++;
}
token = strtok_hier((*local_sub), &saveptr);
while(token){
(*topics)[topic_index] = token;
topic_index++;
token = strtok_hier(NULL, &saveptr);
2019-05-14 12:24:46 +00:00
}
if(!strcmp((*topics)[0], "$share")){
if(count < 2){
mosquitto__free(*local_sub);
mosquitto__free(*topics);
return MOSQ_ERR_PROTOCOL;
}
2019-05-14 12:24:46 +00:00
if(sharename){
(*sharename) = (*topics)[1];
}
2019-05-14 12:24:46 +00:00
for(i=1; i<count-1; i++){
(*topics)[i] = (*topics)[i+1];
}
(*topics)[0] = "";
(*topics)[count-1] = NULL;
2019-05-14 12:24:46 +00:00
}
return MOSQ_ERR_SUCCESS;
2019-05-14 12:24:46 +00:00
}