Skip to content

Commit

Permalink
fix: refine codes
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Nov 7, 2024
1 parent aa32951 commit a8c0948
Show file tree
Hide file tree
Showing 31 changed files with 261 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
"request": "launch",
"program": "${workspaceFolder}/out/linux/x64/tests/standalone/ten_runtime_smoke_test",
"args": [
"--gtest_filter=CmdConversionTest.CmdConversionConnectCmd"
"--gtest_filter=AudioFrameTest.FromJson"
],
"cwd": "${workspaceFolder}/out/linux/x64/tests/standalone/",
"env": {
Expand Down
25 changes: 23 additions & 2 deletions core/include/ten_utils/lib/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
#include "ten_utils/macro/check.h"

#define TEN_STRING_SIGNATURE 0x178445C0402E320DU
#define TEN_STRING_PRE_BUF_SIZE 512

#if defined(NDEBUG)
#define TEN_STRING_PRE_BUF_SIZE 256
#else
#define TEN_STRING_PRE_BUF_SIZE 8
#endif

typedef struct ten_list_t ten_list_t;

Expand All @@ -36,9 +41,16 @@ inline bool ten_string_check_integrity(const ten_string_t *self) {
if (ten_signature_get(&self->signature) != TEN_STRING_SIGNATURE) {
return false;
}

if (self->buf == NULL) {
return false;
}

return true;
}

TEN_UTILS_API bool ten_string_buf_needs_free(ten_string_t *self);

/**
* @brief Create a string object.
* @return A pointer to the string object.
Expand Down Expand Up @@ -79,6 +91,10 @@ TEN_UTILS_API void ten_string_init(ten_string_t *self);
TEN_UTILS_API void ten_string_init_formatted(ten_string_t *self,
const char *fmt, ...);

TEN_UTILS_API void ten_string_copy_construct(ten_string_t *self,
ten_string_t *other);

// =-=-= 整理 ten_string_copy 的意义
/**
* @brief Initialize a string object from another string object.
* @param self The string object.
Expand All @@ -93,14 +109,19 @@ TEN_UTILS_API void ten_string_copy(ten_string_t *self, ten_string_t *other);
* @param size the max size, copy all if size <= 0
*/
TEN_UTILS_API void ten_string_init_from_c_str(ten_string_t *self,
const char *other, size_t size);
const char *str, size_t size);

TEN_UTILS_API void ten_string_set_from_c_str(ten_string_t *self,
const char *str, size_t size);

/**
* @brief Destroy a string object and release the memory.
* @param self The string object.
*/
TEN_UTILS_API void ten_string_destroy(ten_string_t *self);

TEN_UTILS_API void ten_string_reset(ten_string_t *self);

/**
* @brief Destroy a string object, left the memory.
* @param self The string object.
Expand Down
2 changes: 2 additions & 0 deletions core/include/ten_utils/value/value_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ TEN_UTILS_API bool ten_value_to_string(ten_value_t *self, ten_string_t *str,
TEN_UTILS_API ten_value_t *ten_value_from_type_and_string(TEN_TYPE type,
const char *str,
ten_error_t *err);

TEN_UTILS_API bool ten_value_string_buf_needs_free(ten_value_t *self);
11 changes: 11 additions & 0 deletions core/include_internal/ten_runtime/common/loc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,28 @@ TEN_RUNTIME_PRIVATE_API ten_loc_t *ten_loc_clone(ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_copy(ten_loc_t *self, ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_init(ten_loc_t *self, const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_loc(ten_loc_t *self,
ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API void ten_loc_init_from_value(ten_loc_t *self,
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API void ten_loc_set_from_value(ten_loc_t *self,
ten_value_t *value);

TEN_RUNTIME_PRIVATE_API void ten_loc_set(ten_loc_t *self, const char *app_uri,
const char *graph_id,
const char *extension_group_name,
const char *extension_name);

TEN_RUNTIME_PRIVATE_API void ten_loc_set_from_loc(ten_loc_t *self,
ten_loc_t *src);

TEN_RUNTIME_PRIVATE_API bool ten_loc_is_empty(ten_loc_t *self);

TEN_RUNTIME_PRIVATE_API void ten_loc_clear(ten_loc_t *self);
Expand Down
5 changes: 4 additions & 1 deletion core/include_internal/ten_utils/value/value_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ TEN_UTILS_API bool ten_value_set_float32(ten_value_t *self, float value);

TEN_UTILS_API bool ten_value_set_float64(ten_value_t *self, double value);

TEN_UTILS_API bool ten_value_set_string(ten_value_t *self, const char *value);
TEN_UTILS_API bool ten_value_set_string(ten_value_t *self, const char *str);

TEN_UTILS_API bool ten_value_set_string_with_size(ten_value_t *self,
const char *str, size_t len);

TEN_UTILS_API bool ten_value_set_array_with_move(ten_value_t *self,
ten_list_t *value);
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/addon/addon.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ void ten_addon_find_and_set_base_dir(ten_addon_host_t *self,
// If the addon's base dir cannot be found by searching upward through the
// parent folders, simply trust the passed-in parameter as the addon’s base
// dir.
ten_string_init_from_c_str(&self->base_dir, start_path, strlen(start_path));
ten_string_set_from_c_str(&self->base_dir, start_path, strlen(start_path));
}
}

Expand Down
1 change: 1 addition & 0 deletions core/src/ten_runtime/addon/addon_autoload.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ bool ten_addon_load_all_from_app_base_dir(ten_app_t *app, ten_error_t *err) {

for (int i = 0; i < sizeof(folders) / sizeof(folders[0]); i++) {
ten_string_t module_path;
ten_string_init(&module_path);
ten_string_copy(&module_path, &app->base_dir);

do {
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/addon/ten_env/on_xxx.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ void ten_addon_on_init_done(ten_env_t *self) {
// runtime would use that name instead of the name specified in the codes to
// register it to the extension store.
if (strlen(manifest_name)) {
ten_string_init_from_c_str(&addon_host->name, manifest_name,
strlen(manifest_name));
ten_string_set_from_c_str(&addon_host->name, manifest_name,
strlen(manifest_name));
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/app/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool ten_app_init_uri(ten_app_t *self, ten_value_t *value) {
? ten_value_peek_raw_str(value)
: ten_string_get_raw_str(&default_url);

ten_string_init_from_c_str(&self->uri, url_str, strlen(url_str));
ten_string_set_from_c_str(&self->uri, url_str, strlen(url_str));

ten_string_deinit(&default_url);

Expand Down
75 changes: 53 additions & 22 deletions core/src/ten_runtime/common/loc.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ten_loc_t *ten_loc_create_from_value(ten_value_t *value) {

ten_loc_t *self = ten_loc_create_empty();

ten_loc_init_from_value(self, value);
ten_loc_set_from_value(self, value);
TEN_ASSERT(ten_loc_check_integrity(self), "Should not happen.");

return self;
Expand All @@ -84,7 +84,7 @@ void ten_loc_copy(ten_loc_t *self, ten_loc_t *src) {
TEN_ASSERT(self, "Invalid argument.");
TEN_ASSERT(src && ten_loc_check_integrity(src), "Invalid argument.");

ten_loc_init_from_loc(self, src);
ten_loc_set_from_loc(self, src);
}

void ten_loc_destroy(ten_loc_t *self) {
Expand All @@ -108,13 +108,24 @@ void ten_loc_init_empty(ten_loc_t *self) {
void ten_loc_init_from_loc(ten_loc_t *self, ten_loc_t *src) {
TEN_ASSERT(self && src, "Should not happen.");

ten_loc_init_empty(self);
ten_signature_set(&self->signature, TEN_LOC_SIGNATURE);

ten_loc_init(self, ten_string_get_raw_str(&src->app_uri),
ten_string_get_raw_str(&src->graph_id),
ten_string_get_raw_str(&src->extension_group_name),
ten_string_get_raw_str(&src->extension_name));

TEN_ASSERT(ten_loc_check_integrity(self), "Should not happen.");
}

void ten_loc_set_from_loc(ten_loc_t *self, ten_loc_t *src) {
TEN_ASSERT(self && ten_loc_check_integrity(self) && src,
"Should not happen.");

ten_loc_set(self, ten_string_get_raw_str(&src->app_uri),
ten_string_get_raw_str(&src->graph_id),
ten_string_get_raw_str(&src->extension_group_name),
ten_string_get_raw_str(&src->extension_name));

TEN_ASSERT(ten_loc_check_integrity(self), "Should not happen.");
}

void ten_loc_deinit(ten_loc_t *self) {
Expand All @@ -128,8 +139,9 @@ void ten_loc_deinit(ten_loc_t *self) {
ten_string_deinit(&self->extension_name);
}

void ten_loc_set(ten_loc_t *self, const char *app_uri, const char *graph_id,
const char *extension_group_name, const char *extension_name) {
void ten_loc_init(ten_loc_t *self, const char *app_uri, const char *graph_id,
const char *extension_group_name,
const char *extension_name) {
TEN_ASSERT(self, "Should not happen.");

ten_string_init_formatted(&self->app_uri, "%s", app_uri ? app_uri : "");
Expand All @@ -142,6 +154,20 @@ void ten_loc_set(ten_loc_t *self, const char *app_uri, const char *graph_id,
TEN_ASSERT(ten_loc_check_integrity(self), "Should not happen.");
}

void ten_loc_set(ten_loc_t *self, const char *app_uri, const char *graph_id,
const char *extension_group_name, const char *extension_name) {
TEN_ASSERT(self, "Should not happen.");

ten_string_set_formatted(&self->app_uri, "%s", app_uri ? app_uri : "");
ten_string_set_formatted(&self->graph_id, "%s", graph_id ? graph_id : "");
ten_string_set_formatted(&self->extension_group_name, "%s",
extension_group_name ? extension_group_name : "");
ten_string_set_formatted(&self->extension_name, "%s",
extension_name ? extension_name : "");

TEN_ASSERT(ten_loc_check_integrity(self), "Should not happen.");
}

bool ten_loc_is_empty(ten_loc_t *self) {
TEN_ASSERT(self, "Should not happen.");

Expand Down Expand Up @@ -191,12 +217,12 @@ void ten_loc_to_string(ten_loc_t *self, ten_string_t *result) {
TEN_ASSERT(self && result && ten_loc_check_integrity(self),
"Should not happen.");

ten_string_init_formatted(result,
"app: %s, graph: %s, group: %s, extension: %s",
ten_string_get_raw_str(&self->app_uri),
ten_string_get_raw_str(&self->graph_id),
ten_string_get_raw_str(&self->extension_group_name),
ten_string_get_raw_str(&self->extension_name));
ten_string_set_formatted(result,
"app: %s, graph: %s, group: %s, extension: %s",
ten_string_get_raw_str(&self->app_uri),
ten_string_get_raw_str(&self->graph_id),
ten_string_get_raw_str(&self->extension_group_name),
ten_string_get_raw_str(&self->extension_name));
}

void ten_loc_to_json_string(ten_loc_t *self, ten_string_t *result) {
Expand Down Expand Up @@ -375,11 +401,9 @@ void ten_loc_init_from_json(ten_loc_t *self, ten_json_t *json) {
}
}

void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {
void ten_loc_set_from_value(ten_loc_t *self, ten_value_t *value) {
TEN_ASSERT(self && value, "Should not happen.");

ten_loc_init_empty(self);

ten_value_t *app_value = ten_value_object_peek(value, TEN_STR_APP);
ten_value_t *graph_value = ten_value_object_peek(value, TEN_STR_GRAPH);
ten_value_t *extension_group_value =
Expand All @@ -392,7 +416,7 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *app_str = ten_value_peek_raw_str(app_value);
if (app_str && strlen(app_str) > 0) {
ten_string_init_from_c_str(&self->app_uri, app_str, strlen(app_str));
ten_string_set_from_c_str(&self->app_uri, app_str, strlen(app_str));
}
}

Expand All @@ -401,7 +425,7 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *graph_str = ten_value_peek_raw_str(graph_value);
if (graph_str && strlen(graph_str) > 0) {
ten_string_init_from_c_str(&self->graph_id, graph_str, strlen(graph_str));
ten_string_set_from_c_str(&self->graph_id, graph_str, strlen(graph_str));
}
}

Expand All @@ -411,8 +435,8 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *group_name_str = ten_value_peek_raw_str(extension_group_value);
if (group_name_str && strlen(group_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
ten_string_set_from_c_str(&self->extension_group_name, group_name_str,
strlen(group_name_str));
}
}

Expand All @@ -421,8 +445,15 @@ void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {

const char *extension_name_str = ten_value_peek_raw_str(extension_value);
if (extension_name_str && strlen(extension_name_str) > 0) {
ten_string_init_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
ten_string_set_from_c_str(&self->extension_name, extension_name_str,
strlen(extension_name_str));
}
}
}

void ten_loc_init_from_value(ten_loc_t *self, ten_value_t *value) {
TEN_ASSERT(self && value, "Should not happen.");

ten_loc_init_empty(self);
ten_loc_set_from_value(self, value);
}
4 changes: 2 additions & 2 deletions core/src/ten_runtime/msg/cmd_base/cmd/custom/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "include_internal/ten_runtime/msg/field/field_info.h"
#include "include_internal/ten_runtime/msg/msg.h"
#include "include_internal/ten_utils/value/value_path.h"
#include "include_internal/ten_utils/value/value_set.h"
#include "ten_runtime/common/errno.h"
#include "ten_utils/container/list.h"
#include "ten_utils/container/list_node.h"
Expand All @@ -25,7 +26,6 @@
#include "ten_utils/lib/smart_ptr.h"
#include "ten_utils/lib/string.h"
#include "ten_utils/macro/check.h"
#include "ten_utils/macro/mark.h"
#include "ten_utils/value/value.h"

static void ten_raw_cmd_custom_destroy(ten_cmd_t *self) {
Expand Down Expand Up @@ -233,7 +233,7 @@ bool ten_raw_cmd_custom_set_ten_property(ten_msg_t *self, ten_list_t *paths,
if (!strcmp(TEN_STR_NAME,
ten_string_get_raw_str(&item->obj_item_str))) {
if (ten_value_is_string(value)) {
ten_value_init_string_with_size(
ten_value_set_string_with_size(
&self->name, ten_value_peek_raw_str(value),
strlen(ten_value_peek_raw_str(value)));
success = true;
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_runtime/msg/cmd_base/cmd/start_graph/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ static void ten_raw_cmd_start_graph_add_missing_extension_group_node(
// Create an extension_group item that uses the builtin
// default_extension_group, allowing the extension's extension_group to be
// associated with an extension_group addon.
ten_string_init_formatted(&extension_group_info->extension_group_addon_name,
TEN_STR_DEFAULT_EXTENSION_GROUP);
ten_string_set_formatted(&extension_group_info->extension_group_addon_name,
TEN_STR_DEFAULT_EXTENSION_GROUP);

ten_loc_set(
&extension_group_info->loc,
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_runtime/msg/cmd_base/cmd/timer/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool ten_raw_cmd_timer_set_ten_property(ten_msg_t *self, ten_list_t *paths,
} else if (!strcmp(TEN_STR_NAME,
ten_string_get_raw_str(&item->obj_item_str))) {
if (ten_value_is_string(value)) {
ten_value_init_string_with_size(
ten_value_set_string_with_size(
&self->name, ten_value_peek_raw_str(value),
strlen(ten_value_peek_raw_str(value)));
success = true;
Expand Down
Loading

0 comments on commit a8c0948

Please sign in to comment.