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) Meta Platforms, Inc. and affiliates.
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/* UNUSED_ATTR tells the compiler it is okay if the function is unused. */
26#if defined(__GNUC__)
27# define UNUSED_ATTR __attribute__((unused))
28#else
29# define UNUSED_ATTR
30#endif
31
32#define HEADER_FUNCTION static UNUSED_ATTR
33
34
35/*
36 * Define the returned error code from utility functions.
37 */
49
53#define CHECK(cond, ...) \
54 do { \
55 if (!(cond)) { \
56 fprintf(stderr, \
57 "%s:%d CHECK(%s) failed: ", \
58 __FILE__, \
59 __LINE__, \
60 #cond); \
61 fprintf(stderr, "" __VA_ARGS__); \
62 fprintf(stderr, "\n"); \
63 exit(1); \
64 } \
65 } while (0)
66
71#define CHECK_ZSTD(fn) \
72 do { \
73 size_t const err = (fn); \
74 CHECK(!ZSTD_isError(err), "%s", ZSTD_getErrorName(err)); \
75 } while (0)
76
82HEADER_FUNCTION size_t fsize_orDie(const char *filename)
83{
84 struct stat st;
85 if (stat(filename, &st) != 0) {
86 /* error */
87 perror(filename);
88 exit(ERROR_fsize);
89 }
90
91 off_t const fileSize = st.st_size;
92 size_t const size = (size_t)fileSize;
93 /* 1. fileSize should be non-negative,
94 * 2. if off_t -> size_t type conversion results in discrepancy,
95 * the file size is too large for type size_t.
96 */
97 if ((fileSize < 0) || (fileSize != (off_t)size)) {
98 fprintf(stderr, "%s : filesize too large \n", filename);
99 exit(ERROR_largeFile);
100 }
101 return size;
102}
103
110HEADER_FUNCTION FILE* fopen_orDie(const char *filename, const char *instruction)
111{
112 FILE* const inFile = fopen(filename, instruction);
113 if (inFile) return inFile;
114 /* error */
115 perror(filename);
116 exit(ERROR_fopen);
117}
118
123{
124 if (!fclose(file)) { return; };
125 /* error */
126 perror("fclose");
127 exit(ERROR_fclose);
128}
129
137HEADER_FUNCTION size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
138{
139 size_t const readSize = fread(buffer, 1, sizeToRead, file);
140 if (readSize == sizeToRead) return readSize; /* good */
141 if (feof(file)) return readSize; /* good, reached end of file */
142 /* error */
143 perror("fread");
144 exit(ERROR_fread);
145}
146
157HEADER_FUNCTION size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
158{
159 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
160 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
161 /* error */
162 perror("fwrite");
163 exit(ERROR_fwrite);
164}
165
174{
175 void* const buff = malloc(size);
176 if (buff) return buff;
177 /* error */
178 perror("malloc");
179 exit(ERROR_malloc);
180}
181
191HEADER_FUNCTION size_t loadFile_orDie(const char* fileName, void* buffer, size_t bufferSize)
192{
193 size_t const fileSize = fsize_orDie(fileName);
194 CHECK(fileSize <= bufferSize, "File too large!");
195
196 FILE* const inFile = fopen_orDie(fileName, "rb");
197 size_t const readSize = fread(buffer, 1, fileSize, inFile);
198 if (readSize != (size_t)fileSize) {
199 fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
200 exit(ERROR_fread);
201 }
202 fclose(inFile); /* can't fail, read only */
203 return fileSize;
204}
205
215HEADER_FUNCTION void* mallocAndLoadFile_orDie(const char* fileName, size_t* bufferSize)
216{
217 size_t const fileSize = fsize_orDie(fileName);
218 *bufferSize = fileSize;
219 void* const buffer = malloc_orDie(*bufferSize);
220 loadFile_orDie(fileName, buffer, *bufferSize);
221 return buffer;
222}
223
232HEADER_FUNCTION void saveFile_orDie(const char* fileName, const void* buff, size_t buffSize)
233{
234 FILE* const oFile = fopen_orDie(fileName, "wb");
235 size_t const wSize = fwrite(buff, 1, buffSize, oFile);
236 if (wSize != (size_t)buffSize) {
237 fprintf(stderr, "fwrite: %s : %s \n", fileName, strerror(errno));
238 exit(ERROR_fwrite);
239 }
240 if (fclose(oFile)) {
241 perror(fileName);
242 exit(ERROR_fclose);
243 }
244}
245
246#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
HEADER_FUNCTION void * mallocAndLoadFile_orDie(const char *fileName, size_t *bufferSize)
Definition common.h:215
HEADER_FUNCTION void saveFile_orDie(const char *fileName, const void *buff, size_t buffSize)
Definition common.h:232
HEADER_FUNCTION size_t fwrite_orDie(const void *buffer, size_t sizeToWrite, FILE *file)
Definition common.h:157
HEADER_FUNCTION size_t loadFile_orDie(const char *fileName, void *buffer, size_t bufferSize)
Definition common.h:191
#define HEADER_FUNCTION
Definition common.h:32
HEADER_FUNCTION size_t fsize_orDie(const char *filename)
Definition common.h:82
HEADER_FUNCTION size_t fread_orDie(void *buffer, size_t sizeToRead, FILE *file)
Definition common.h:137
HEADER_FUNCTION void fclose_orDie(FILE *file)
Definition common.h:122
HEADER_FUNCTION void * malloc_orDie(size_t size)
Definition common.h:173
HEADER_FUNCTION FILE * fopen_orDie(const char *filename, const char *instruction)
Definition common.h:110
size_t size
Definition platform.h:559