int ret;
long message_tag;
- ret = progbuf_get_message_tag (NULL, &message_tag);
+ ret = progbuf_message_tag (NULL, &message_tag);
ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
progbuf_h buf = progbuf_alloc (1);
ck_assert (buf);
- ret = progbuf_get_message_tag (buf, NULL);
+ ret = progbuf_message_tag (buf, NULL);
ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
- ret = progbuf_get_message_tag (buf, &message_tag);
+ ret = progbuf_message_tag (buf, &message_tag);
ck_assert (ret == PROGBUF_SUCCESS);
ck_assert (message_tag == 1);
size_t size;
- ret = progbuf_get_message_buffer (buf, NULL, &size);
+ ret = progbuf_buffer_size (buf, &size);
ck_assert (ret == PROGBUF_SUCCESS);
ck_assert (ret == PROGBUF_SUCCESS);
size_t size;
- ret = progbuf_get_message_buffer (buf, NULL, &size);
+ ret = progbuf_buffer_size (buf, &size);
ck_assert (ret == PROGBUF_SUCCESS);
size_t size;
- ret = progbuf_get_message_buffer (NULL, NULL, NULL);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (NULL, NULL, &size);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (buf, NULL, NULL);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (buf, NULL, &size);
+ ret = progbuf_buffer_size (buf, &size);
ck_assert (ret == PROGBUF_SUCCESS);
ck_assert (size == 1 + 6);
ck_assert (ret == PROGBUF_SUCCESS);
size_t size;
- ret = progbuf_get_message_buffer (buf, NULL, &size);
+ ret = progbuf_buffer_size (buf, &size);
ck_assert (ret == PROGBUF_SUCCESS);
size_t size;
- ret = progbuf_get_message_buffer (NULL, NULL, NULL);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (NULL, NULL, &size);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (buf, NULL, NULL);
- ck_assert (ret == PROGBUF_ERROR_NULL_PARAM);
-
- ret = progbuf_get_message_buffer (buf, NULL, &size);
+ ret = progbuf_buffer_size (buf, &size);
ck_assert (ret == PROGBUF_SUCCESS);
ck_assert (size == 1 + 1 + 10);
if (!buf) \
return PROGBUF_ERROR_NULL_PARAM; \
\
+ if (!buf->buffer) \
+ return PROGBUF_ERROR_NOT_OWNING; \
+ \
val_size = determine_var_##utype##_size (ABS (value)); \
ret = check_buffer_and_expand (buf, val_size + 1); \
if (ret != 0) \
if (!iter || !value) \
return PROGBUF_ERROR_NULL_PARAM; \
\
+ if (!iter->buf->buffer) \
+ return PROGBUF_ERROR_NOT_OWNING; \
+ \
if (iter->read_pos >= iter->buf->size) \
return PROGBUF_ERROR_END_OF_ITER; \
\
if (!buf)
return 0;
+ if (!buf->buffer)
+ return 0;
+
struct progbuf_it_s *iter = malloc (sizeof (struct progbuf_it_s));
if (!iter)
if (!buf)
return PROGBUF_ERROR_NULL_PARAM;
- free (buf->buffer);
+ if (buf->buffer)
+ free (buf->buffer);
+
free (buf);
return PROGBUF_SUCCESS;
}
int
-progbuf_get_message_tag (progbuf_h buf, long *message_tag)
+progbuf_message_tag (progbuf_h buf, long *message_tag)
{
if (!buf || !message_tag)
return PROGBUF_ERROR_NULL_PARAM;
+ if (!buf->buffer)
+ return PROGBUF_ERROR_NOT_OWNING;
+
*message_tag = buf->message_tag;
return PROGBUF_SUCCESS;
}
int
-progbuf_get_message_buffer (progbuf_h buf, char **buffer, size_t *size)
+progbuf_own_buffer (progbuf_h buf, char **buffer, size_t *size)
{
- if (!buf || (!buffer && !size))
+ if (!buf || !buffer || !size)
return PROGBUF_ERROR_NULL_PARAM;
- if (buffer)
- {
- *buffer = buf->buffer;
- }
+ if (!buf->buffer)
+ return PROGBUF_ERROR_NOT_OWNING;
- if (size)
- {
- *size = buf->size;
- }
+ *buffer = buf->buffer;
+ buf->buffer = 0;
+
+ *size = buf->size;
+
+ return 0;
+}
+
+int
+progbuf_copy_buffer (progbuf_h buf, char **buffer, size_t *size)
+{
+ if (!buf || !buffer || !size)
+ return PROGBUF_ERROR_NULL_PARAM;
+
+ if (!buf->buffer)
+ return PROGBUF_ERROR_NOT_OWNING;
+
+ *buffer = malloc (buf->size);
+
+ if (!*buffer)
+ return PROGBUF_ERROR_MEM_ALLOC;
+
+ memcpy (*buffer, buf->buffer, buf->size);
+
+ *size = buf->size;
+
+ return 0;
+}
+
+int
+progbuf_buffer_size (progbuf_h buf, size_t *size)
+{
+ if (!buf || !size)
+ return PROGBUF_ERROR_NULL_PARAM;
+
+ if (!buf->buffer)
+ return PROGBUF_ERROR_NOT_OWNING;
+
+ *size = buf->size;
return 0;
}
if (!iter)
return PROGBUF_ERROR_NULL_PARAM;
+ if (!iter->buf->buffer)
+ return PROGBUF_ERROR_NOT_OWNING;
+
iter->read_pos = iter->buf->header_size;
return PROGBUF_SUCCESS;
#define PROGBUF_ERROR_UNEXPECTED_TYPE -4
#define PROGBUF_ERROR_READ -5
#define PROGBUF_ERROR_INCORRECT_SIGN -6
+#define PROGBUF_ERROR_NOT_OWNING -7
typedef struct progbuf_s *progbuf_h;
typedef struct progbuf_it_s *progbuf_it_h;
progbuf_h progbuf_alloc (long message_tag);
-int progbuf_get_message_tag (progbuf_h buf, long *message_tag);
-int progbuf_get_message_buffer (progbuf_h buf, char **buffer, size_t *size);
+int progbuf_message_tag (progbuf_h buf, long *message_tag);
+int progbuf_own_buffer (progbuf_h buf, char **buffer, size_t *size);
+int progbuf_copy_buffer (progbuf_h buf, char **buffer, size_t *size);
+int progbuf_buffer_size (progbuf_h buf, size_t *size);
int progbuf_free (progbuf_h buf);
int progbuf_set_int (progbuf_h buf, int value);