Added more complete API for dealing with ADC messages.

- Added a construct method which would take a fourcc and a source.
- Added an unescape function that would write into pre-allocated memory.
This commit is contained in:
Jan Vidar Krey 2012-09-28 15:49:46 +02:00
parent fc52f0e030
commit a599b29b9d
2 changed files with 61 additions and 3 deletions

View File

@ -532,6 +532,16 @@ struct adc_message* adc_msg_create(const char* line)
return adc_msg_parse(line, strlen(line));
}
extern struct adc_message* adc_msg_construct_source(fourcc_t fourcc, sid_t source, size_t size)
{
struct adc_message* msg = adc_msg_construct(fourcc, size + 4);
if (!msg)
return NULL;
adc_msg_add_argument(msg, sid_to_string(source));
msg->source = source;
return msg;
}
struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
{
@ -539,9 +549,7 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
if (!msg)
return NULL; /* OOM */
if (size < sizeof(fourcc)) size = sizeof(fourcc);
if (!adc_msg_grow(msg, size+1))
if (!adc_msg_grow(msg, sizeof(fourcc) + size + 1))
{
msg_free(msg);
return NULL; /* OOM */
@ -911,6 +919,42 @@ char* adc_msg_unescape(const char* string)
return new_string;
}
int adc_msg_unescape_to_target(const char* string, char* target, size_t target_size)
{
size_t w = 0;
char* ptr = (char*) target;
char* str = (char*) string;
int escaped = 0;
while (*str && w < (target_size-1))
{
if (escaped) {
if (*str == 's')
*ptr++ = ' ';
else if (*str == '\\')
*ptr++ = '\\';
else if (*str == 'n')
*ptr++ = '\n';
else
*ptr++ = *str;
w++;
escaped = 0;
} else {
if (*str == '\\')
escaped = 1;
else
{
*ptr++ = *str;
w++;
}
}
str++;
}
*ptr = 0;
w++;
return w;
}
char* adc_msg_escape(const char* string)
{
char* str = hub_malloc(adc_msg_escape_length(string)+1);

View File

@ -91,6 +91,12 @@ extern struct adc_message* adc_msg_create(const char* string);
*/
extern struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size);
/**
* Construct a message for the given 'fourcc' and add a source SID to it,
* in addition pre-allocate 'size' bytes at the end of the message.
*/
extern struct adc_message* adc_msg_construct_source(fourcc_t fourcc, sid_t source, size_t size);
/**
* Remove a named argument from the command.
*
@ -195,6 +201,14 @@ extern int adc_msg_add_named_argument_uint64(struct adc_message* cmd, const char
*/
extern char* adc_msg_unescape(const char* string);
/**
* Convert a ADC command escaped string to a regular string.
* @return The number of bytes written to target. If the target is not large enough then
* the -1 is returned, but the string is guaranteed to always be \0 terminated.
*/
extern int adc_msg_unescape_to_target(const char* string, char* target, size_t target_size);
/**
* Convert a string to a ADC command escaped string.
* @return adc command escaped string or NULL if out of memory.