Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11/*
12 * This header file has common utility functions used in examples.
13 */
14#ifndef COMMON_H
15#define COMMON_H
16
17#include <stdlib.h> // malloc, free, exit
18#include <stdio.h> // fprintf, perror, fopen, etc.
19#include <string.h> // strerror
20#include <errno.h> // errno
21#include <sys/stat.h> // stat
22#include <zstd.h>
23
24/*
25 * Define the returned error code from utility functions.
26 */
38
42#define CHECK(cond, ...) \
43 do { \
44 if (!(cond)) { \
45 fprintf(stderr, \
46 "%s:%d CHECK(%s) failed: ", \
47 __FILE__, \
48 __LINE__, \
49 #cond); \
50 fprintf(stderr, "" __VA_ARGS__); \
51 fprintf(stderr, "\n"); \
52 exit(1); \
53 } \
54 } while (0)
55
60#define CHECK_ZSTD(fn) \
61 do { \
62 size_t const err = (fn); \
63 CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
64 } while (0)
65
71static size_t fsize_orDie(const char *filename)
72{
73 struct stat st;
74 if (stat(filename, &st) != 0) {
75 /* error */
76 perror(filename);
77 exit(ERROR_fsize);
78 }
79
80 off_t const fileSize = st.st_size;
81 size_t const size = (size_t)fileSize;
82 /* 1. fileSize should be non-negative,
83 * 2. if off_t -> size_t type conversion results in discrepancy,
84 * the file size is too large for type size_t.
85 */
86 if ((fileSize < 0) || (fileSize != (off_t)size)) {
87 fprintf(stderr, "%s : filesize too large \n", filename);
88 exit(ERROR_largeFile);
89 }
90 return size;
91}
92
99static FILE* fopen_orDie(const char *filename, const char *instruction)
100{
101 FILE* const inFile = fopen(filename, instruction);
102 if (inFile) return inFile;
103 /* error */
104 perror(filename);
105 exit(ERROR_fopen);
106}
107
111static void fclose_orDie(FILE* file)
112{
113 if (!fclose(file)) { return; };
114 /* error */
115 perror("fclose");
116 exit(ERROR_fclose);
117}
118
126static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
127{
128 size_t const readSize = fread(buffer, 1, sizeToRead, file);
129 if (readSize == sizeToRead) return readSize; /* good */
130 if (feof(file)) return readSize; /* good, reached end of file */
131 /* error */
132 perror("fread");
133 exit(ERROR_fread);
134}
135
146static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
147{
148 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
149 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
150 /* error */
151 perror("fwrite");
152 exit(ERROR_fwrite);
153}
154
162static void* malloc_orDie(size_t size)
163{
164 void* const buff = malloc(size);
165 if (buff) return buff;
166 /* error */
167 perror("malloc");
168 exit(ERROR_malloc);
169}
170
180static size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize)
181{
182 size_t const fileSize = fsize_orDie(fileName);
183 CHECK(fileSize <= bufferSize, "File too large!");
184
185 FILE* const inFile = fopen_orDie(fileName, "rb");
186 size_t const readSize = fread(buffer, 1, fileSize, inFile);
187 if (readSize != (size_t)fileSize) {
188 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
189 exit(ERROR_fread);
190 }
191 fclose(inFile); /* can't fail, read only */
192 return fileSize;
193}
194
204static void* mallocAndLoadFile_orDie(const char* fileName, size_t* bufferSize) {
205 size_t const fileSize = fsize_orDie(fileName);
206 *bufferSize = fileSize;
207 void* const buffer = malloc_orDie(*bufferSize);
208 loadFile_orDie(fileName, buffer, *bufferSize);
209 return buffer;
210}
211
220static void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
221{
222 FILE* const oFile = fopen_orDie(fileName, "wb");
223 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
224 if (wSize != (size_t)buffSize) {
225 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
226 exit(ERROR_fwrite);
227 }
228 if (fclose(oFile)) {
229 perror(fileName);
230 exit(ERROR_fclose);
231 }
232}
233
234#endif
#define CHECK(cond,...)
Definition common.h:42
COMMON_ErrorCode
Definition common.h:27
@ ERROR_largeFile
Definition common.h:36
@ ERROR_malloc
Definition common.h:35
@ ERROR_loadFile
Definition common.h:33
@ ERROR_saveFile
Definition common.h:34
@ ERROR_fopen
Definition common.h:29
@ ERROR_fclose
Definition common.h:30
@ ERROR_fread
Definition common.h:31
@ ERROR_fsize
Definition common.h:28
@ ERROR_fwrite
Definition common.h:32
size_t size
Definition platform.h:559