Browse Source

util: bfg_slurp_file function to quickly read a file

Luke Dashjr 12 years ago
parent
commit
0f73832f89
2 changed files with 35 additions and 0 deletions
  1. 33 0
      util.c
  2. 2 0
      util.h

+ 33 - 0
util.c

@@ -2859,6 +2859,39 @@ void notifier_init(notifier_t pipefd)
 #endif
 }
 
+
+void *bfg_slurp_file(void * const bufp, size_t bufsz, const char * const filename)
+{
+	char *buf = bufp;
+	FILE * const F = fopen(filename, "r");
+	if (!F)
+		goto err;
+	
+	if (!buf)
+	{
+		fseek(F, 0, SEEK_END);
+		const long filesz = ftell(F);
+		if (unlikely(filesz < 0))
+		{
+			fclose(F);
+			goto err;
+		}
+		rewind(F);
+		bufsz = filesz + 1;
+		buf = malloc(bufsz);
+	}
+	const size_t rsz = fread(buf, 1, bufsz - 1, F);
+	fclose(F);
+	buf[rsz] = '\0';
+	return buf;
+
+err:
+	if (buf)
+		buf[0] = '\0';
+	return NULL;
+}
+
+
 void notifier_wake(notifier_t fd)
 {
 	if (fd[1] == INVSOCK)

+ 2 - 0
util.h

@@ -181,6 +181,8 @@ enum bfg_strerror_type {
 };
 extern const char *bfg_strerror(int, enum bfg_strerror_type);
 
+extern void *bfg_slurp_file(void *buf, size_t bufsz, const char *filename);
+
 typedef SOCKETTYPE notifier_t[2];
 extern void notifier_init(notifier_t);
 extern void notifier_wake(notifier_t);