Skip to content

Commit

Permalink
sds: add printf style function
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Sep 19, 2022
1 parent ebf50a8 commit 4a0c13c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/cfl/cfl_sds.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ cfl_sds_t cfl_sds_cat(cfl_sds_t s, const char *str, int len);
cfl_sds_t cfl_sds_create_size(size_t size);
void cfl_sds_set_len(cfl_sds_t s, size_t len);
void cfl_sds_cat_safe(cfl_sds_t *buf, const char *str, int len);
cfl_sds_t cfl_sds_printf(cfl_sds_t *sds, const char *fmt, ...);

#endif
53 changes: 53 additions & 0 deletions src/cfl_sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
* string storage
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>

#include <cfl/cfl_sds.h>
Expand Down Expand Up @@ -181,3 +183,54 @@ void cfl_sds_cat_safe(cfl_sds_t *buf, const char *str, int len)
*buf = tmp;
}

cfl_sds_t cfl_sds_printf(cfl_sds_t *sds, const char *fmt, ...)
{
va_list ap;
int len = strlen(fmt)*2;
int size;
cfl_sds_t tmp = NULL;
cfl_sds_t s;
struct cfl_sds *head;

if (len < 64) len = 64;

s = *sds;
if (cfl_sds_avail(s)< len) {
tmp = cfl_sds_increase(s, len);
if (!tmp) {
return NULL;
}
*sds = s = tmp;
}

va_start(ap, fmt);
size = vsnprintf((char *) (s + cfl_sds_len(s)), cfl_sds_avail(s), fmt, ap);
if (size < 0) {
va_end(ap);
return NULL;
}
va_end(ap);

if (size > cfl_sds_avail(s)) {
tmp = cfl_sds_increase(s, size);
if (!tmp) {
return NULL;
}
*sds = s = tmp;

va_start(ap, fmt);
size = vsnprintf((char *) (s + cfl_sds_len(s)), cfl_sds_avail(s), fmt, ap);
if (size > cfl_sds_avail(s)) {
va_end(ap);
return NULL;
}
va_end(ap);
}

head = CFL_SDS_HEADER(s);
head->len += size;
s[head->len] = '\0';

return s;
}

1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include_directories(lib/acutest)

set(UNIT_TESTS_FILES
kv.c
sds.c
hash.c
list.c
)
Expand Down
59 changes: 59 additions & 0 deletions tests/sds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* CFL
* ===
* Copyright (C) 2022 The CFL Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cfl/cfl.h>
#include "cfl_tests_internal.h"

static void test_sds_usage()
{
cfl_sds_t s;

s = cfl_sds_create("test");
TEST_CHECK(s != NULL);
TEST_CHECK(cfl_sds_len(s) == 4);
TEST_CHECK(cfl_sds_alloc(s) == 4);
TEST_CHECK(strcmp("test", s) == 0);

s = cfl_sds_cat(s, ",cat message", 12);
TEST_CHECK(strcmp("test,cat message", s) == 0);

cfl_sds_destroy(s);
}

static void test_sds_printf()
{
int len;
cfl_sds_t s;
cfl_sds_t tmp;
char *str = "0123456789ABCDEFGHIJQLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvewxyz";

s = cfl_sds_create_size(10);
tmp = cfl_sds_printf(&s, "%s=%s", str, str);

len = (strlen(str) * 2) + 1;
TEST_CHECK(tmp == s);
TEST_CHECK(cfl_sds_len(s) == len);
cfl_sds_destroy(s);
}

TEST_LIST = {
{ "sds_usage" , test_sds_usage},
{ "sds_printf", test_sds_printf},
{ 0 }
};

0 comments on commit 4a0c13c

Please sign in to comment.