[x265] [PATCH 1 of 5] common: introduce x265_slurp_file, reads data from file to string buffer

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Fri Jul 11 13:12:56 CEST 2014


# HG changeset patch
# User Aarthi Thirumalai
# Date 1405074962 -19800
#      Fri Jul 11 16:06:02 2014 +0530
# Node ID 86b4c19eb7061753fa98332a1b59024c387845d0
# Parent  29ae3f84c3eaa36ecb2669b380e7f31824e802fe
common: introduce x265_slurp_file, reads data from file to string buffer.

diff -r 29ae3f84c3ea -r 86b4c19eb706 source/common/common.cpp
--- a/source/common/common.cpp	Fri Jul 11 15:25:10 2014 +0530
+++ b/source/common/common.cpp	Fri Jul 11 16:06:02 2014 +0530
@@ -156,6 +156,53 @@
 uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane)
 {
     uint32_t size = (uint32_t)(width >> x265_cli_csps[csp].width[plane]) * (height >> x265_cli_csps[csp].height[plane]);
-
     return size;
 }
+
+char* x265_slurp_file(const char *filename)
+{
+    if (!filename)
+        return NULL;
+
+    int bError = 0;
+    size_t fSize;
+    char *buf = NULL;
+
+    FILE *fh = fopen(filename, "rb");
+    if (!fh)
+    {
+        x265_log(NULL, X265_LOG_ERROR, "unable to open file %s \n", filename);
+        return NULL;
+    }
+
+    bError |= fseek(fh, 0, SEEK_END) < 0;
+    bError |= (fSize = ftell(fh)) <= 0;
+    bError |= fseek(fh, 0, SEEK_SET) < 0;
+    if (bError)
+        goto error;
+
+    buf = X265_MALLOC(char, fSize + 2);
+    if (!buf)
+    {
+        x265_log(NULL, X265_LOG_ERROR, "unable to allocate memory \n");
+        goto error;
+    }
+
+    bError |= fread(buf, 1, fSize, fh) != fSize;
+    if (buf[fSize - 1] != '\n')
+        buf[fSize++] = '\n';
+    buf[fSize] = 0;
+    fclose(fh);
+
+    if (bError)
+    {
+        x265_log(NULL, X265_LOG_ERROR, "unable to read the file\n");
+        free(buf);
+        buf = NULL;
+    }
+    return buf;
+
+error:
+    fclose(fh);
+    return NULL;
+}
diff -r 29ae3f84c3ea -r 86b4c19eb706 source/common/common.h
--- a/source/common/common.h	Fri Jul 11 15:25:10 2014 +0530
+++ b/source/common/common.h	Fri Jul 11 16:06:02 2014 +0530
@@ -208,7 +208,6 @@
 double x265_ssim2dB(double ssim);
 double x265_qScale2qp(double qScale);
 double x265_qp2qScale(double qp);
-
 uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane);
-
+char* x265_slurp_file(const char *filename);
 #endif // ifndef X265_COMMON_H


More information about the x265-devel mailing list