arg_append any content is ok

This commit is contained in:
lyon1998 2022-03-12 20:52:25 +08:00
parent b3d6206c45
commit 2b02818c9e
3 changed files with 27 additions and 1 deletions

View File

@ -67,3 +67,12 @@ TEST(arg_test, null) {
arg_deinit(arg); arg_deinit(arg);
EXPECT_EQ(pikaMemNow(), 0); EXPECT_EQ(pikaMemNow(), 0);
} }
TEST(arg_test, append) {
Arg* arg = arg_setStr(NULL, (char*)"", (char*)"test");
arg = arg_append(arg, (void*)"hello", sizeof("hello"));
EXPECT_STREQ(arg_getStr(arg), "test");
EXPECT_STREQ(arg_getStr(arg) + sizeof("test"), (char*)"hello");
arg_deinit(arg);
EXPECT_EQ(pikaMemNow(), 0);
}

View File

@ -174,7 +174,8 @@ Arg* arg_setNull(Arg* self) {
} }
Arg* arg_setFloat(Arg* self, char* name, float val) { Arg* arg_setFloat(Arg* self, char* name, float val) {
return content_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val), NULL); return content_init(name, ARG_TYPE_FLOAT, (uint8_t*)&val, sizeof(val),
NULL);
} }
float arg_getFloat(Arg* self) { float arg_getFloat(Arg* self) {
@ -246,3 +247,17 @@ Arg* arg_copy(Arg* argToBeCopy) {
argCopied = arg_setType(argCopied, arg_getType(argToBeCopy)); argCopied = arg_setType(argCopied, arg_getType(argToBeCopy));
return argCopied; return argCopied;
} }
Arg* arg_append(Arg* arg_in, void* new_content, size_t new_size) {
uint8_t* old_content = arg_getContent(arg_in);
size_t old_size = arg_getContentSize(arg_in);
/* create arg_out */
Arg* arg_out = arg_setContent(NULL, NULL, old_size + new_size);
/* copy old content */
__platform_memcpy(arg_getContent(arg_out), old_content, old_size);
/* copy new content */
__platform_memcpy(arg_getContent(arg_out) + old_size, new_content,
new_size);
arg_deinit(arg_in);
return arg_out;
}

View File

@ -111,4 +111,6 @@ Arg* arg_init(Arg* self, void* voidPointer);
void arg_deinit(Arg* self); void arg_deinit(Arg* self);
Arg* New_arg(void* voidPointer); Arg* New_arg(void* voidPointer);
Arg* arg_append(Arg* arg_in, void* new_content, size_t new_size);
#endif #endif