Navigation Menu

Skip to content

Commit

Permalink
Add grn_text_printf() and grn_text_vprintf()
Browse files Browse the repository at this point in the history
TODO: Document it.
  • Loading branch information
kou committed Jun 16, 2014
1 parent 13dfae8 commit ee8bfcf
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/groonga.h
Expand Up @@ -22,6 +22,8 @@
extern "C" {
#endif

#include <stdarg.h>

#ifndef GRN_API
#if defined(_WIN32) || defined(_WIN64)
#define GRN_API __declspec(dllimport)
Expand Down Expand Up @@ -1204,6 +1206,10 @@ GRN_API const char *grn_text_urldec(grn_ctx *ctx, grn_obj *buf,
GRN_API grn_rc grn_text_escape_xml(grn_ctx *ctx, grn_obj *buf,
const char *s, unsigned int len);
GRN_API grn_rc grn_text_time2rfc1123(grn_ctx *ctx, grn_obj *bulk, int sec);
GRN_API grn_rc grn_text_printf(grn_ctx *ctx, grn_obj *bulk,
const char *format, ...) GRN_ATTRIBUTE_PRINTF(3);
GRN_API grn_rc grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk,
const char *format, va_list args);

typedef struct _grn_obj_format grn_obj_format;

Expand Down
47 changes: 47 additions & 0 deletions lib/str.c
Expand Up @@ -17,6 +17,7 @@
#include "groonga_in.h"
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "db.h"
#include "str.h"
Expand Down Expand Up @@ -2463,6 +2464,52 @@ grn_text_time2rfc1123(grn_ctx *ctx, grn_obj *bulk, int sec)
return GRN_SUCCESS;
}

grn_rc
grn_text_printf(grn_ctx *ctx, grn_obj *bulk, const char *format, ...)
{
va_list args;

va_start(args, format);
grn_text_vprintf(ctx, bulk, format, args);
va_end(args);

return GRN_SUCCESS;
}

grn_rc
grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk, const char *format, va_list args)
{
int rest_size, written_size;

{
va_list copied_args;

rest_size = GRN_BULK_REST(bulk);
va_copy(copied_args, args);
written_size = vsnprintf(GRN_BULK_CURR(bulk), rest_size,
format, copied_args);
va_end(copied_args);
}

if (written_size >= rest_size) {
grn_rc rc;
int required_size = written_size + 1; /* "+ 1" for terminate '\0'. */

rc = grn_bulk_reserve(ctx, bulk, GRN_BULK_VSIZE(bulk) + required_size);
if (rc) {
return rc;
}
written_size = vsnprintf(GRN_BULK_CURR(bulk), required_size,
format, args);
}

if (written_size < 0) {
return GRN_INVALID_ARGUMENT;
}

GRN_BULK_INCR_LEN(bulk, written_size);
return GRN_SUCCESS;
}

grn_rc
grn_bulk_fin(grn_ctx *ctx, grn_obj *buf)
Expand Down
23 changes: 23 additions & 0 deletions test/unit/core/test-text.c
Expand Up @@ -103,3 +103,26 @@ test_urldec(void)
GRN_TEXT_VALUE(&buffer),
GRN_TEXT_LEN(&buffer));
}

void
test_printf_inplace_size(void)
{
const char *inplace_size_string = "inplace size is <= 24 ";
grn_text_printf(&context, &buffer, "%s", inplace_size_string);
cut_assert_equal_memory(inplace_size_string,
strlen(inplace_size_string),
GRN_TEXT_VALUE(&buffer),
GRN_BULK_VSIZE(&buffer));
}

void
test_printf_outplace_size(void)
{
const char *outplace_size_string = "outplace size is > 24 ";
grn_text_printf(&context, &buffer, "%s", outplace_size_string);
cut_assert_equal_memory(outplace_size_string,
strlen(outplace_size_string),
GRN_TEXT_VALUE(&buffer),
GRN_BULK_VSIZE(&buffer));
}

0 comments on commit ee8bfcf

Please sign in to comment.