]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
strings: Create a function to copy a fixed-length string
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Jan 2025 21:30:07 +0000 (21:30 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 6 Jan 2025 21:30:07 +0000 (21:30 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/string.c
src/pakfire/string.h
tests/libpakfire/string.c

index 9b75effbd4f4b4213e2247310d53f4f95dc05289..00e8233c78b8e73006cff86338c3e05c81b2429c 100644 (file)
@@ -70,6 +70,20 @@ int __pakfire_string_set(char* s, const size_t length, const char* value) {
        return __pakfire_string_format(s, length, "%s", value);
 }
 
+int __pakfire_string_setn(char* s, const size_t length, const char* value, const size_t l) {
+       // Fail if we don't have enough space
+       if (l >= length)
+               return -ENOBUFS;
+
+       // Copy the data
+       memcpy(s, value, l);
+
+       // Terminate the result buffer
+       s[l] = '\0';
+
+       return l;
+}
+
 int pakfire_string_startswith(const char* s, const char* prefix) {
        // Validate input
        if (!s || !prefix)
index 16e3d57fbe181093a295c026fd5077abfc5ef05c..2fbccd14594e23daa48bc475b955daf3649905bb 100644 (file)
@@ -48,6 +48,10 @@ int __pakfire_string_vformat(char* s, const size_t length,
        __pakfire_string_set(s, sizeof(s), value)
 int __pakfire_string_set(char* s, const size_t length, const char* value);
 
+#define pakfire_string_setn(s, value, l) \
+       __pakfire_string_setn(s, sizeof(s), value, l)
+int __pakfire_string_setn(char* s, const size_t length, const char* value, const size_t l);
+
 int pakfire_string_startswith(const char* s, const char* prefix);
 int pakfire_string_endswith(const char* s, const char* suffix);
 int pakfire_string_matches(const char* s, const char* pattern);
index bfe4f6acf73d94a7603c53d700fe6ce9da215b26..56a093cf994e7238e8dc979beaf2e263e82768db 100644 (file)
@@ -44,6 +44,26 @@ FAIL:
        return EXIT_FAILURE;
 }
 
+static int test_string_setn(const struct test* t) {
+       char buffer[8];
+
+       // Some test data to copy
+       const char* s1 = "ABCDEFG";
+       const char* s2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+       // Write a string into a buffer which just has just enough space
+       ASSERT(pakfire_string_setn(buffer, s1, strlen(s1)) == strlen(s1));
+       ASSERT_STRING_EQUALS(buffer, s1);
+
+       // Write a string which would not fit
+       ASSERT_ERROR(pakfire_string_setn(buffer, s2, strlen(s2)), ENOBUFS);
+
+       return EXIT_SUCCESS;
+
+FAIL:
+       return EXIT_FAILURE;
+}
+
 static int test_string_startswith(const struct test* t) {
        ASSERT_TRUE(pakfire_string_startswith("ABC", "A"));
        ASSERT_FALSE(pakfire_string_startswith("ABC", "B"));
@@ -370,6 +390,7 @@ FAIL:
 
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_string_set, 0);
+       testsuite_add_test(test_string_setn, 0);
        testsuite_add_test(test_string_startswith, 0);
        testsuite_add_test(test_string_endswith, 0);
        testsuite_add_test(test_string_matches, 0);