Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
common.h File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <zstd.h>

Go to the source code of this file.

Macros

#define UNUSED_ATTR
 
#define HEADER_FUNCTION   static UNUSED_ATTR
 
#define CHECK(cond, ...)
 
#define CHECK_ZSTD(fn)
 

Enumerations

enum  COMMON_ErrorCode {
  ERROR_fsize = 1 , ERROR_fopen = 2 , ERROR_fclose = 3 , ERROR_fread = 4 ,
  ERROR_fwrite = 5 , ERROR_loadFile = 6 , ERROR_saveFile = 7 , ERROR_malloc = 8 ,
  ERROR_largeFile = 9
}
 

Functions

HEADER_FUNCTION size_t fsize_orDie (const char *filename)
 
HEADER_FUNCTION FILE * fopen_orDie (const char *filename, const char *instruction)
 
HEADER_FUNCTION void fclose_orDie (FILE *file)
 
HEADER_FUNCTION size_t fread_orDie (void *buffer, size_t sizeToRead, FILE *file)
 
HEADER_FUNCTION size_t fwrite_orDie (const void *buffer, size_t sizeToWrite, FILE *file)
 
HEADER_FUNCTION void * malloc_orDie (size_t size)
 
HEADER_FUNCTION size_t loadFile_orDie (const char *fileName, void *buffer, size_t bufferSize)
 
HEADER_FUNCTION void * mallocAndLoadFile_orDie (const char *fileName, size_t *bufferSize)
 
HEADER_FUNCTION void saveFile_orDie (const char *fileName, const void *buff, size_t buffSize)
 

Macro Definition Documentation

◆ CHECK

#define CHECK ( cond,
... )
Value:
do { \
if (!(cond)) { \
fprintf(stderr, \
"%s:%d CHECK(%s) failed: ", \
__FILE__, \
__LINE__, \
#cond); \
fprintf(stderr, "" __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(1); \
} \
} while (0)

CHECK Check that the condition holds. If it doesn't print a message and die.

◆ CHECK_ZSTD

#define CHECK_ZSTD ( fn)
Value:
do { \
size_t const err = (fn); \
CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
} while (0)
const char * ZSTD_getErrorName(size_t code)
Definition zstd_common.c:41
#define ZSTD_isError
Definition zstd_internal.h:49

CHECK_ZSTD Check the zstd error code and die if an error occurred after printing a message.

◆ HEADER_FUNCTION

#define HEADER_FUNCTION   static UNUSED_ATTR

◆ UNUSED_ATTR

#define UNUSED_ATTR

Enumeration Type Documentation

◆ COMMON_ErrorCode

Enumerator
ERROR_fsize 
ERROR_fopen 
ERROR_fclose 
ERROR_fread 
ERROR_fwrite 
ERROR_loadFile 
ERROR_saveFile 
ERROR_malloc 
ERROR_largeFile 

Function Documentation

◆ fclose_orDie()

HEADER_FUNCTION void fclose_orDie ( FILE * file)

fclose_orDie() : Close an opened file using given FILE pointer.

◆ fopen_orDie()

HEADER_FUNCTION FILE * fopen_orDie ( const char * filename,
const char * instruction )

fopen_orDie() : Open a file using given file path and open option.

Returns
If successful this function will return a FILE pointer to an opened file otherwise it sends an error to stderr and exits.

◆ fread_orDie()

HEADER_FUNCTION size_t fread_orDie ( void * buffer,
size_t sizeToRead,
FILE * file )

fread_orDie() :

Read sizeToRead bytes from a given file, storing them at the location given by buffer.

Returns
The number of bytes read.

◆ fsize_orDie()

HEADER_FUNCTION size_t fsize_orDie ( const char * filename)

fsize_orDie() : Get the size of a given file path.

Returns
The size of a given file path.

◆ fwrite_orDie()

HEADER_FUNCTION size_t fwrite_orDie ( const void * buffer,
size_t sizeToWrite,
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.

Returns
The number of bytes written.

◆ loadFile_orDie()

HEADER_FUNCTION size_t loadFile_orDie ( const char * fileName,
void * buffer,
size_t bufferSize )

loadFile_orDie() : load file into buffer (memory).

Note: This function will send an error to stderr and exit if it cannot read data from the given file path.

Returns
If successful this function will load file into buffer and return file size, otherwise it will printout an error to stderr and exit.

◆ malloc_orDie()

HEADER_FUNCTION void * malloc_orDie ( size_t size)

malloc_orDie() : Allocate memory.

Returns
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.

◆ mallocAndLoadFile_orDie()

HEADER_FUNCTION void * mallocAndLoadFile_orDie ( const char * fileName,
size_t * bufferSize )

mallocAndLoadFile_orDie() : allocate memory buffer and then load file into it.

Note: This function will send an error to stderr and exit if memory allocation fails or it cannot read data from the given file path.

Returns
If successful this function will return buffer and bufferSize(=fileSize), otherwise it will printout an error to stderr and exit.

◆ saveFile_orDie()

HEADER_FUNCTION void saveFile_orDie ( const char * fileName,
const void * buff,
size_t buffSize )

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.