You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by pe...@apache.org on 2023/05/06 09:45:37 UTC
[celix] 01/02: Add celix_utils_trimInPlace, and replace all uses of utils_stringTrim.
This is an automated email from the ASF dual-hosted git repository.
pengzheng pushed a commit to branch feature/trim_string_in_place
in repository https://gitbox.apache.org/repos/asf/celix.git
commit 36872594bc93c7d0a9669e4f348740eb4a5e942c
Author: PengZheng <ho...@gmail.com>
AuthorDate: Sat May 6 12:33:21 2023 +0800
Add celix_utils_trimInPlace, and replace all uses of utils_stringTrim.
---
bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_common.c | 3 ++-
.../discovery_common/src/endpoint_discovery_poller.c | 3 ++-
.../discovery_common/src/endpoint_discovery_server.c | 4 ++--
.../remote_service_admin_dfi/src/remote_service_admin_dfi.c | 5 +++--
bundles/shell/remote_shell/src/remote_shell.c | 6 +++---
bundles/shell/shell_tui/src/shell_tui.c | 5 +++--
libs/framework/src/manifest_parser.c | 3 ++-
libs/framework/src/module.c | 2 +-
libs/utils/gtest/src/CelixUtilsTestSuite.cc | 2 +-
libs/utils/include/celix_utils.h | 8 ++++++++
libs/utils/src/celix_convert_utils.c | 4 ++--
libs/utils/src/properties.c | 5 +++--
libs/utils/src/utils.c | 3 +++
13 files changed, 35 insertions(+), 18 deletions(-)
diff --git a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_common.c b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_common.c
index d8f05f79..b46e39c6 100644
--- a/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_common.c
+++ b/bundles/pubsub/pubsub_admin_tcp/src/pubsub_tcp_common.c
@@ -19,6 +19,7 @@
#include <stdio.h>
#include <string.h>
#include "pubsub_tcp_common.h"
+#include "celix_utils.h"
bool psa_tcp_isPassive(const char* buffer) {
@@ -27,7 +28,7 @@ bool psa_tcp_isPassive(const char* buffer) {
if (buffer != NULL) {
char buf[32];
snprintf(buf, 32, "%s", buffer);
- char *trimmed = utils_stringTrim(buf);
+ char *trimmed = celix_utils_trimInPlace(buf);
if (strncasecmp("true", trimmed, strlen("true")) == 0) {
isPassive = true;
} else if (strncasecmp("false", trimmed, strlen("false")) == 0) {
diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
index 355e5438..5589c846 100644
--- a/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
+++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_poller.c
@@ -34,6 +34,7 @@
#include "bundle_context.h"
#include "celix_log_helper.h"
+#include "celix_utils.h"
#include "utils.h"
#include "endpoint_descriptor_reader.h"
@@ -99,7 +100,7 @@ celix_status_t endpointDiscoveryPoller_create(discovery_t *discovery, celix_bund
char *save_ptr = NULL;
char* tok = strtok_r(endpoints, sep, &save_ptr);
while (tok) {
- endpointDiscoveryPoller_addDiscoveryEndpoint(*poller, utils_stringTrim(tok));
+ endpointDiscoveryPoller_addDiscoveryEndpoint(*poller, celix_utils_trimInPlace(tok));
tok = strtok_r(NULL, sep, &save_ptr);
}
// Clean up after ourselves...
diff --git a/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c b/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c
index 46144ee7..a845f826 100644
--- a/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c
+++ b/bundles/remote_services/discovery_common/src/endpoint_discovery_server.c
@@ -26,6 +26,7 @@
#endif
#include "civetweb.h"
#include "celix_errno.h"
+#include "celix_utils.h"
#include "utils.h"
#include "celix_log_helper.h"
#include "discovery.h"
@@ -293,8 +294,7 @@ celix_status_t endpointDiscoveryServer_removeEndpoint(endpoint_discovery_server_
}
static char* format_path(const char* path) {
- char* result = strdup(path);
- result = utils_stringTrim(result);
+ char* result = celix_utils_trim(path);
// check whether the path starts with a leading slash...
if (result[0] != '/') {
size_t len = strlen(result);
diff --git a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
index 9ba47546..db50d005 100644
--- a/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
+++ b/bundles/remote_services/remote_service_admin_dfi/src/remote_service_admin_dfi.c
@@ -33,6 +33,7 @@
#include <jansson.h>
#include "json_serializer.h"
#include "utils.h"
+#include "celix_utils.h"
#include "import_registration_dfi.h"
#include "export_registration_dfi.h"
@@ -597,7 +598,7 @@ celix_status_t remoteServiceAdmin_exportService(remote_service_admin_t *admin, c
token = strtok_r(ecCopy, delimiter, &savePtr);
while (token != NULL) {
- if (strncmp(utils_stringTrim(token), RSA_DFI_CONFIGURATION_TYPE, 1024) == 0) {
+ if (strncmp(celix_utils_trimInPlace(token), RSA_DFI_CONFIGURATION_TYPE, 1024) == 0) {
export = true;
break;
}
@@ -897,7 +898,7 @@ celix_status_t remoteServiceAdmin_importService(remote_service_admin_t *admin, e
token = strtok_r(ecCopy, delimiter, &savePtr);
while (token != NULL) {
- if (strncmp(utils_stringTrim(token), RSA_DFI_CONFIGURATION_TYPE, 1024) == 0) {
+ if (strncmp(celix_utils_trimInPlace(token), RSA_DFI_CONFIGURATION_TYPE, 1024) == 0) {
importService = true;
break;
}
diff --git a/bundles/shell/remote_shell/src/remote_shell.c b/bundles/shell/remote_shell/src/remote_shell.c
index f55d613a..86c281b5 100644
--- a/bundles/shell/remote_shell/src/remote_shell.c
+++ b/bundles/shell/remote_shell/src/remote_shell.c
@@ -32,6 +32,7 @@
#include <sys/socket.h>
#include "celix_log_helper.h"
+#include "celix_utils.h"
#include "remote_shell.h"
@@ -215,8 +216,7 @@ static celix_status_t remoteShell_connection_execute(connection_pt connection, c
celix_status_t status = CELIX_SUCCESS;
if (status == CELIX_SUCCESS) {
- char *dline = strdup(command);
- char *line = utils_stringTrim(dline);
+ char *line = celix_utils_trim(command);
int len = strlen(line);
if (len == 0) {
@@ -228,7 +228,7 @@ static celix_status_t remoteShell_connection_execute(connection_pt connection, c
fflush(connection->socketStream);
}
- free(dline);
+ free(line);
}
return status;
diff --git a/bundles/shell/shell_tui/src/shell_tui.c b/bundles/shell/shell_tui/src/shell_tui.c
index a8f101b7..3f284db7 100644
--- a/bundles/shell/shell_tui/src/shell_tui.c
+++ b/bundles/shell/shell_tui/src/shell_tui.c
@@ -26,6 +26,7 @@
#include "celix_array_list.h"
#include "celix_shell.h"
+#include "celix_utils.h"
#include "shell_tui.h"
#include "utils.h"
#include <signal.h>
@@ -277,7 +278,7 @@ static int shellTui_parseInputPlain(shell_tui_t* shellTui, shell_context_t* ctx)
int nr_chars = read(shellTui->inputFd, buffer, LINE_SIZE-pos-1);
for(int bufpos = 0; bufpos < nr_chars; bufpos++) {
if (buffer[bufpos] == KEY_ENTER) { //end of line -> forward command
- line = utils_stringTrim(in);
+ line = celix_utils_trimInPlace(in);
celixThreadMutex_lock(&shellTui->mutex);
if (shellTui->shell != NULL) {
shellTui->shell->executeCommand(shellTui->shell->handle, line, shellTui->output, shellTui->error);
@@ -392,7 +393,7 @@ static int shellTui_parseInputForControl(shell_tui_t* shellTui, shell_context_t*
pos = 0;
in[pos] = '\0';
- line = utils_stringTrim(dline);
+ line = celix_utils_trimInPlace(dline);
if ((strlen(line) == 0)) {
continue;
}
diff --git a/libs/framework/src/manifest_parser.c b/libs/framework/src/manifest_parser.c
index d53d42c7..074945e3 100644
--- a/libs/framework/src/manifest_parser.c
+++ b/libs/framework/src/manifest_parser.c
@@ -28,6 +28,7 @@
#include <string.h>
#include "utils.h"
+#include "celix_utils.h"
#include "celix_constants.h"
#include "manifest_parser.h"
#include "capability.h"
@@ -155,7 +156,7 @@ static linked_list_pt manifestParser_parseDelimitedString(const char * value, co
}
if (strlen(buffer) > 0) {
- linkedList_addElement(list, strdup(utils_stringTrim(buffer)));
+ linkedList_addElement(list, celix_utils_trim(buffer));
}
}
}
diff --git a/libs/framework/src/module.c b/libs/framework/src/module.c
index 1dec4ffb..24d00731 100644
--- a/libs/framework/src/module.c
+++ b/libs/framework/src/module.c
@@ -439,7 +439,7 @@ static celix_status_t celix_module_loadLibrariesInManifestEntry(celix_module_t*
char lib[128];
lib[127] = '\0';
strncpy(lib, pathToken, 127);
- char *trimmedLib = utils_stringTrim(lib);
+ char *trimmedLib = celix_utils_trimInPlace(lib);
status = celix_module_loadLibraryForManifestEntry(module, trimmedLib, archive, &handle);
if ( (status == CELIX_SUCCESS) && (activator != NULL) && (strcmp(trimmedLib, activator) == 0) ) {
diff --git a/libs/utils/gtest/src/CelixUtilsTestSuite.cc b/libs/utils/gtest/src/CelixUtilsTestSuite.cc
index 5ab42120..6d287550 100644
--- a/libs/utils/gtest/src/CelixUtilsTestSuite.cc
+++ b/libs/utils/gtest/src/CelixUtilsTestSuite.cc
@@ -259,7 +259,7 @@ TEST_F(UtilsTestSuite, StringTrimTest) {
free(trimmed);
// Empty string
- trimmed = utils_stringTrim(celix_utils_strdup(" abc "));
+ trimmed = celix_utils_trim(" abc ");
EXPECT_STREQ("abc", trimmed);
free(trimmed);
}
diff --git a/libs/utils/include/celix_utils.h b/libs/utils/include/celix_utils.h
index b7e8a8c5..53a98d08 100644
--- a/libs/utils/include/celix_utils.h
+++ b/libs/utils/include/celix_utils.h
@@ -105,6 +105,14 @@ CELIX_UTILS_EXPORT bool celix_utils_containsWhitespace(const char* s);
* Caller is owner of the returned string.
*/
CELIX_UTILS_EXPORT char* celix_utils_trim(const char* string);
+/**
+ * @brief Trims the provided string in place.
+ *
+ * The trim will remove eny leading and trailing whitespaces (' ', '\t', etc based on `isspace`)/
+ * @param string the string to be trimmed.
+ * @return string.
+ */
+CELIX_UTILS_EXPORT char* celix_utils_trimInPlace(char* string);
/**
* @brief Check if a string is NULL or empty "".
diff --git a/libs/utils/src/celix_convert_utils.c b/libs/utils/src/celix_convert_utils.c
index e5aa2251..bccc4801 100644
--- a/libs/utils/src/celix_convert_utils.c
+++ b/libs/utils/src/celix_convert_utils.c
@@ -52,7 +52,7 @@ bool celix_utils_convertStringToBool(const char* val, bool defaultValue, bool* c
if (valCopy == NULL) {
return result;
}
- char *trimmed = utils_stringTrim(valCopy);
+ char *trimmed = celix_utils_trimInPlace(valCopy);
if (strcasecmp("true", trimmed) == 0) {
result = true;
if (converted) {
@@ -114,7 +114,7 @@ celix_version_t* celix_utils_convertStringToVersion(const char* val, const celix
if (firstDot != NULL && lastDot != NULL && firstDot != lastDot) {
char buf[64];
char* valCopy = celix_utils_writeOrCreateString(buf, sizeof(buf), "%s", val);
- char *trimmed = utils_stringTrim(valCopy);
+ char *trimmed = celix_utils_trimInPlace(valCopy);
result = celix_version_createVersionFromString(trimmed);
celix_utils_freeStringIfNotEqual(buf, valCopy);
}
diff --git a/libs/utils/src/properties.c b/libs/utils/src/properties.c
index e2792c53..d49870fa 100644
--- a/libs/utils/src/properties.c
+++ b/libs/utils/src/properties.c
@@ -26,6 +26,7 @@
#include "properties.h"
#include "celix_build_assert.h"
#include "celix_properties.h"
+#include "celix_utils.h"
#include "utils.h"
#include "hash_map_private.h"
#include <errno.h>
@@ -191,7 +192,7 @@ static void parseLine(const char* line, celix_properties_t *props) {
if (!isComment) {
//printf("putting 'key'/'value' '%s'/'%s' in properties\n", utils_stringTrim(key), utils_stringTrim(value));
- celix_properties_set(props, utils_stringTrim(key), utils_stringTrim(value));
+ celix_properties_set(props, celix_utils_trimInPlace(key), celix_utils_trimInPlace(value));
}
if(key) {
free(key);
@@ -451,7 +452,7 @@ bool celix_properties_getAsBool(const celix_properties_t *props, const char *key
if (val != NULL) {
char buf[32];
snprintf(buf, 32, "%s", val);
- char *trimmed = utils_stringTrim(buf);
+ char *trimmed = celix_utils_trimInPlace(buf);
if (strncasecmp("true", trimmed, strlen("true")) == 0) {
result = true;
} else if (strncasecmp("false", trimmed, strlen("false")) == 0) {
diff --git a/libs/utils/src/utils.c b/libs/utils/src/utils.c
index da17f1a4..95ab561f 100644
--- a/libs/utils/src/utils.c
+++ b/libs/utils/src/utils.c
@@ -152,6 +152,9 @@ static char* celix_utilsTrimInternal(char *string) {
char* celix_utils_trim(const char* string) {
return celix_utilsTrimInternal(celix_utils_strdup(string));
}
+char* celix_utils_trimInPlace(char* string) {
+ return celix_utilsTrimInternal(string);
+}
char* utils_stringTrim(char* string) {
return celix_utilsTrimInternal(string);