1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-06 15:45:37 +02:00

refactor utils.h and implement simple_compressionCCtx.c

This commit is contained in:
Yi Jin
2018-12-14 18:12:05 -08:00
parent 193fbd30f2
commit 04d06ad885
8 changed files with 106 additions and 68 deletions

View File

@ -9,7 +9,7 @@
*/
/*
* This header file has common utility functions used in examples.
* This header file has common utility functions used in examples.
*/
#ifndef UTILS_H
#define UTILS_H
@ -21,7 +21,7 @@
#include <sys/stat.h> // stat
/*
* Define the returned error code from utility functions.
* Define the returned error code from utility functions.
*/
typedef enum {
ERROR_fsize = 1,
@ -35,21 +35,33 @@ typedef enum {
ERROR_largeFile = 9,
} UTILS_ErrorCode;
/*! fsize_orDie() :
/*! fsize_orDie() :
* Get the size of a given file path.
*
*
* @return The size of a given file path.
*/
static off_t fsize_orDie(const char *filename)
static size_t fsize_orDie(const char *filename)
{
struct stat st;
if (stat(filename, &st) == 0) return st.st_size;
/* error */
perror(filename);
exit(ERROR_fsize);
if (stat(filename, &st) != 0) {
/* error */
perror(filename);
exit(ERROR_fsize);
}
off_t const fileSize = st.st_size;
size_t const size = (size_t)fileSize;
/* if off_t -> size_t conversion causes discrepancy, the file size is
* too big for at least 1 type to handle
*/
if (size != fileSize) { /* narrowcast overflow */
fprintf(stderr, "%s : filesize too large \n", filename);
exit(ERROR_largeFile);
}
return size;
}
/*! fopen_orDie() :
/*! fopen_orDie() :
* Open a file using given file path and open option.
*
* @return If successful this function will return a FILE pointer to an
@ -64,7 +76,7 @@ static FILE* fopen_orDie(const char *filename, const char *instruction)
exit(ERROR_fopen);
}
/*! fclose_orDie() :
/*! fclose_orDie() :
* Close an opened file using given FILE pointer.
*/
static void fclose_orDie(FILE* file)
@ -75,11 +87,11 @@ static void fclose_orDie(FILE* file)
exit(ERROR_fclose);
}
/*! fread_orDie() :
*
/*! fread_orDie() :
*
* Read sizeToRead bytes from a given file, storing them at the
* location given by buffer.
*
*
* @return The number of bytes read.
*/
static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
@ -93,12 +105,12 @@ static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
}
/*! fwrite_orDie() :
*
*
* Write sizeToWrite bytes to a file pointed to by file, obtaining
* them from a location given by buffer.
*
* Note: This function will send an error to stderr and exit if it
* cannot write data to the given file pointer.
* cannot write data to the given file pointer.
*
* @return The number of bytes written.
*/
@ -113,7 +125,7 @@ static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
/*! malloc_orDie() :
* Allocate memory.
*
*
* @return If successful this function returns a pointer to allo-
* cated memory. If there is an error, this function will send that
* error to stderr and exit.
@ -128,39 +140,41 @@ static void* malloc_orDie(size_t size)
}
/*! loadFile_orDie() :
* Read size bytes from a file.
*
* Read size bytes from a file. If buffer is not provided (i.e., buffer == null),
* malloc will be called to allocate one.
*
* Note: This function will send an error to stderr and exit if it
* cannot read data from the given file path.
*
*
* @return If successful this function will return a pointer to read
* data otherwise it will printout an error to stderr and exit.
*/
static void* loadFile_orDie(const char* fileName, size_t* size)
static void* loadFile_orDie(const char* fileName, size_t* size, void* buffer, int bufferSize)
{
off_t const fileSize = fsize_orDie(fileName);
size_t const buffSize = (size_t)fileSize;
if ((off_t)buffSize < fileSize) { /* narrowcast overflow */
fprintf(stderr, "%s : filesize too large \n", fileName);
exit(ERROR_largeFile);
}
size_t const fileSize = fsize_orDie(fileName);
FILE* const inFile = fopen_orDie(fileName, "rb");
void* const buffer = malloc_orDie(buffSize);
size_t const readSize = fread(buffer, 1, buffSize, inFile);
if (readSize != (size_t)buffSize) {
if (!buffer) {
buffer = malloc_orDie(fileSize);
}
else if (bufferSize < fileSize) {
fprintf(stderr, "%s : filesize bigger than provided buffer.\n", fileName);
}
size_t const readSize = fread(buffer, 1, fileSize, inFile);
if (readSize != (size_t)fileSize) {
fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
exit(ERROR_fread);
}
fclose(inFile); /* can't fail, read only */
*size = buffSize;
*size = fileSize;
return buffer;
}
/*! saveFile_orDie() :
*
*
* Save buffSize bytes to a given file path, obtaining them from a location pointed
* to by buff.
*
*
* Note: This function will send an error to stderr and exit if it
* cannot write to a given file.
*/