Skip to content

Commit

Permalink
Merge pull request #7 from mgdm/tokenizeTopic
Browse files Browse the repository at this point in the history
Tokenize topic
  • Loading branch information
mgdm committed Dec 5, 2013
2 parents 7d0eac7 + 7112304 commit bbab605
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ properties.
| qos | int | The QoS value applied to this message. |
| retain | bool | Whether this is a retained message or not. |

This class has one static method.
This class has two static methods.

#### topicMatchesSub

Expand All @@ -347,6 +347,14 @@ otherwise false.
| topic | string | The topic to match |
| subscription | string | The subscription to match |

#### tokeniseTopic

Tokenise a topic or subscription string into an array of strings representing the topic hierarchy.

| Parameter | Type | Description |
| --- | --- | ---- |
| topic | string | The topic to tokenise |

### Class Mosquitto\Exception

This is an exception that may be thrown by many of the operations in the Client
Expand Down
37 changes: 37 additions & 0 deletions mosquitto_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ ZEND_BEGIN_ARG_INFO(Mosquitto_Message_topicMatchesSub_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, subscription)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(Mosquitto_Message_tokeniseTopic_args, ZEND_SEND_BY_VAL)
ZEND_ARG_INFO(0, topic)
ZEND_END_ARG_INFO()

/* }}} */

PHP_METHOD(Mosquitto_Message, __construct)
Expand Down Expand Up @@ -57,6 +61,38 @@ PHP_METHOD(Mosquitto_Message, topicMatchesSub)
}
/* }}} */

/* {{{ Mosquitto\Message::tokeniseTopic() */
PHP_METHOD(Mosquitto_Message, tokeniseTopic)
{
char *topic = NULL, **topics = NULL;
int topic_len = 0, retval = 0, count = 0, i = 0;

PHP_MOSQUITTO_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &topic, &topic_len) == FAILURE) {
PHP_MOSQUITTO_RESTORE_ERRORS();
return;
}

retval = mosquitto_sub_topic_tokenise(topic, &topics, &count);

if (retval == MOSQ_ERR_NOMEM) {
zend_throw_exception_ex(mosquitto_ce_exception, 0 TSRMLS_CC, "Failed to tokenise topic");
return;
}

array_init(return_value);
for (i = 0; i < count; i++) {
if (topics[i] == NULL) {
add_next_index_null(return_value);
} else {
add_next_index_string(return_value, topics[i], 1);
}
}

mosquitto_sub_topic_tokens_free(&topics, count);
}
/* }}} */

PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(mid);
PHP_MOSQUITTO_MESSAGE_LONG_PROPERTY_READER_FUNCTION(qos);

Expand Down Expand Up @@ -357,6 +393,7 @@ static zend_object_value mosquitto_message_object_new(zend_class_entry *ce TSRML
const zend_function_entry mosquitto_message_methods[] = {
PHP_ME(Mosquitto_Message, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(Mosquitto_Message, topicMatchesSub, Mosquitto_Message_topicMatchesSub_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Mosquitto_Message, tokeniseTopic, Mosquitto_Message_tokeniseTopic_args, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_FE_END
};

Expand Down

0 comments on commit bbab605

Please sign in to comment.