Add bt_common_append_file_content_to_g_string()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 9 Jul 2019 23:40:59 +0000 (19:40 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2019 15:38:52 +0000 (11:38 -0400)
This new function appends the content of a file (`FILE *`) to a
`GString *` object.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I8552873d0c26baafe994e0294c3609f2a221c6e0
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1841
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/common/common.c
src/common/common.h

index 8961475fc5a91dea011205624035db56d5866d3c..c8890cd2cab5748690d8951fb4fea9aab3ce795d 100644 (file)
@@ -1862,3 +1862,44 @@ int bt_common_g_string_append_printf(GString *str, const char *fmt, ...)
        }
        return print_len;
 }
        }
        return print_len;
 }
+
+BT_HIDDEN
+int bt_common_append_file_content_to_g_string(GString *str, FILE *fp)
+{
+       const size_t chunk_size = 4096;
+       int ret = 0;
+       char *buf;
+       size_t read_len;
+       gsize orig_len = str->len;
+
+       BT_ASSERT(str);
+       BT_ASSERT(fp);
+       buf = g_malloc(chunk_size);
+       if (!buf) {
+               ret = -1;
+               goto end;
+       }
+
+       while (true) {
+               if (ferror(fp)) {
+                       ret = -1;
+                       goto end;
+               }
+
+               if (feof(fp)) {
+                       break;
+               }
+
+               read_len = fread(buf, 1, chunk_size, fp);
+               g_string_append_len(str, buf, read_len);
+       }
+
+end:
+       if (ret) {
+               /* Remove what was appended */
+               g_string_truncate(str, orig_len);
+       }
+
+       g_free(buf);
+       return ret;
+}
index 4a2362e34e14be0477d07f47c64c535100ce1e38..1751f20bd74ad65c47fcb07b1362906f6ac7b7d2 100644 (file)
@@ -31,6 +31,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 
 #include <unistd.h>
 #include <string.h>
 
@@ -351,6 +352,15 @@ GString *bt_common_fold(const char *str, unsigned int total_length,
 BT_HIDDEN
 int bt_common_get_term_size(unsigned int *width, unsigned int *height);
 
 BT_HIDDEN
 int bt_common_get_term_size(unsigned int *width, unsigned int *height);
 
+/*
+ * Appends the textual content of `fp` to `str`, starting from its
+ * current position to the end of the file.
+ *
+ * This function does NOT rewind `fp` once it's done or on error.
+ */
+BT_HIDDEN
+int bt_common_append_file_content_to_g_string(GString *str, FILE *fp);
+
 /*
  * Wraps read() function to handle EINTR and partial reads.
  * On success, it returns `count` received as parameter. On error, it returns a
 /*
  * Wraps read() function to handle EINTR and partial reads.
  * On success, it returns `count` received as parameter. On error, it returns a
This page took 0.026151 seconds and 4 git commands to generate.