]> www.git.momoyon.org Git - commonlib.git/commitdiff
Change slurp_file() to return file_size instead of success.
authormomoyon <ahmedsamyh10@gmail.com>
Sun, 6 Apr 2025 15:30:13 +0000 (20:30 +0500)
committermomoyon <ahmedsamyh10@gmail.com>
Sun, 6 Apr 2025 15:30:13 +0000 (20:30 +0500)
commonlib.h
tests/.slurp_file.out.expected
tests/slurp_file.c

index 3eb80c8c3a53ea4ee41c8b896234ddb2bebe9f41..ebef2cad06ebd7b0af99fed38430a7afc683c827 100644 (file)
@@ -208,8 +208,8 @@ bool c_os_file_exists(cstr filename);
 // File\r
 //\r
 \r
-// reads entire file and gives back the string holding the contents. (caller must be responsible for freeing the string!)\r
-const char* c_slurp_file(const char* filename, bool* success);\r
+// reads entire file and gives back the file content and filesize in bytes. (caller must be responsible for freeing the string!)\r
+const char* c_slurp_file(const char* filename, int *file_size);\r
 void c_touch_file_if_doesnt_exist(cstr file);\r
 \r
 //\r
@@ -334,7 +334,7 @@ bool c_os_file_exists(cstr filename) {
     result = ret_val;\\r
     goto defer\r
 \r
-const char *c_slurp_file(const char* filename, bool* success) {\r
+const char *c_slurp_file(const char* filename, int *file_size) {\r
     FILE* f = fopen(filename, "rb");\r
     char* result = NULL;\r
 \r
@@ -355,6 +355,8 @@ const char *c_slurp_file(const char* filename, bool* success) {
         defer(NULL);\r
     }\r
 \r
+    *file_size = (int)fsize;\r
+\r
     result = C_MALLOC(sizeof(char)*(fsize+1));\r
 \r
     if (result == NULL){\r
@@ -377,7 +379,7 @@ const char *c_slurp_file(const char* filename, bool* success) {
 \r
  defer:\r
     if (f) fclose(f);\r
-    *success = result != NULL;\r
+    if (result == NULL) *file_size = -1;\r
     return result;\r
 }\r
 \r
index 148364823c270d0a076ef4abb80bc8909e92a4e4..f2d6e9799c8ff313926b65f9fac06be08b695644 100644 (file)
@@ -1,15 +1,29 @@
 [INFO] buff: '#define COMMONLIB_IMPLEMENTATION
+
 #include "../commonlib.h"
 
+
+
 int main(void) {
-    bool success = false;
-    const char *buff = c_slurp_file(__FILE__, &success);
+
+    int file_size = 0;
+
+    const char *buff = c_slurp_file(__FILE__, &file_size);
+
     
-    if (!success) { return 1; }
 
-    c_log_info("buff: '%s'", buff);
+    if (file_size < 0) { return 1; }
+
+
+
+    c_log_info("buff: '%s' (%d bytes)", buff, file_size);
+
     
+
     // We don't care about leaking memory since we are just exiting right away!
+
     return 0;
+
 }
-'
+
+' (375 bytes)
index 50d4ae42da7d8990d7ff59332584ba595641b06e..f5413ea355e5e6b15408748d3aa720a74964a845 100644 (file)
@@ -2,12 +2,12 @@
 #include "../commonlib.h"
 
 int main(void) {
-    bool success = false;
-    const char *buff = c_slurp_file(__FILE__, &success);
+    int file_size = 0;
+    const char *buff = c_slurp_file(__FILE__, &file_size);
     
-    if (!success) { return 1; }
+    if (file_size < 0) { return 1; }
 
-    c_log_info("buff: '%s'", buff);
+    c_log_info("buff: '%s' (%d bytes)", buff, file_size);
     
     // We don't care about leaking memory since we are just exiting right away!
     return 0;