GPAK  1.0.0
A general-purpose archive library
gpak.h
Go to the documentation of this file.
1 /**
2  * @file gpak.h
3  * @author AdamFull
4  * @date 23.04.2023
5  * @brief Gpak (Game Pack) is a file archive library specifically designed for
6  * game archives, providing functionality for creating, modifying and
7  * extracting archive files.
8  *
9  * This library allows users to create, read and manipulate game archive files
10  * using a variety of compression algorithms. It provides a simple API
11  * for managing the contents of an archive and supports various
12  * compression methods such as Deflate, LZ4, and ZSTD.
13  *
14  * The gpak.h header file contains the main API for the Gpak library,
15  * including functions for creating and manipulating archives, reading
16  * and writing files within archives, and managing the archive's
17  * metadata.
18  *
19  * To use the Gpak library, simply include this header file and link
20  * against the Gpak library in your project.
21  */
22 
23 #ifndef GPAK_LIB_H
24 #define GPAK_LIB_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30  #include "gpak_export.h"
31  #include "gpak_data.h"
32 
33  /**
34  * @brief Opens a G-PAK archive.
35  *
36  * This function opens a G-PAK archive at the specified _path and with the given _mode.
37  *
38  * @param _path A string containing the path to the G-PAK archive.
39  * @param _mode The mode in which to open the G-PAK archive.
40  * @return A pointer to the opened gpak_t or NULL if an error occurred.
41  */
42  GPAK_API gpak_t* gpak_open(const char* _path, int _mode);
43 
44  /**
45  * @brief Closes a G-PAK archive.
46  *
47  * This function closes the specified G-PAK archive and deallocates any associated resources.
48  *
49  * @param _pak A pointer to the gpak_t to close.
50  * @return An integer value indicating success (0) or failure (-1).
51  */
52  GPAK_API int gpak_close(gpak_t* _pak);
53 
54  /**
55  * @brief Sets user data for a G-PAK archive.
56  *
57  * This function associates user-defined data with the specified G-PAK archive.
58  *
59  * @param _pak A pointer to the gpak_t.
60  * @param _user_data A pointer to the user-defined data.
61  */
62  GPAK_API void gpak_set_user_data(gpak_t* _pak, void* _user_data);
63 
64  /**
65  * @brief Sets an error handler for a G-PAK archive.
66  *
67  * This function sets a callback function to handle errors that occur during G-PAK operations.
68  *
69  * @param _pak A pointer to the gpak_t.
70  * @param _error_handler A callback function for handling errors.
71  */
72  GPAK_API void gpak_set_error_handler(gpak_t* _pak, gpak_error_handler_t _error_handler);
73 
74  /**
75  * @brief Sets a progress handler for a G-PAK archive.
76  *
77  * This function sets a callback function to handle progress updates during G-PAK operations.
78  *
79  * @param _pak A pointer to the gpak_t.
80  * @param _progress_handler A callback function for handling progress updates.
81  */
82  GPAK_API void gpak_set_process_handler(gpak_t* _pak, gpak_progress_handler_t _progress_handler);
83 
84  /**
85  * @brief Sets the compression algorithm for a G-PAK archive.
86  *
87  * This function sets the compression algorithm to use when adding files to the G-PAK archive.
88  *
89  * @param _pak A pointer to the gpak_t.
90  * @param _algorithm An integer value representing the compression algorithm.
91  */
92  GPAK_API void gpak_set_compression_algorithm(gpak_t* _pak, int _algorithm);
93 
94  /**
95  * @brief Sets the compression level for a G-PAK archive.
96  *
97  * This function sets the compression level to use when adding files to the G-PAK archive.
98  *
99  * @param _pak A pointer to the gpak_t.
100  * @param _level An integer value representing the compression level.
101  */
102  GPAK_API void gpak_set_compression_level(gpak_t* _pak, int _level);
103 
104  /**
105  * @brief Adds a directory to a G-PAK archive.
106  *
107  * This function adds a new directory to the G-PAK archive with the specified _internal_path.
108  *
109  * @param _pak A pointer to the gpak_t
110  * @param _internal_path A string containing the internal path of the directory to add.
111  * @return An integer value indicating success (0) or failure (-1).
112  */
113  GPAK_API int gpak_add_directory(gpak_t* _pak, const char* _internal_path);
114 
115  /**
116  * @brief Adds a file to a G-PAK archive.
117  *
118  * This function adds a new file to the G-PAK archive with the specified _external_path and _internal_path.
119  *
120  * @param _pak A pointer to the gpak_t.
121  * @param _external_path A string containing the external path of the file to add.
122  * @param _internal_path A string containing the internal path of the file within the G-PAK archive.
123  * @return An integer value indicating success (0) or failure (-1).
124  */
125  GPAK_API int gpak_add_file(gpak_t* _pak, const char* _external_path, const char* _internal_path);
126 
127  /**
128  * @brief Retrieves the root directory node of a G-PAK archive.
129  *
130  * This function retrieves the root directory node of the G-PAK archive.
131  *
132  * @param _pak A pointer to the gpak_t.
133  * @return A pointer to the root filesystem_tree_node.
134  */
135  GPAK_API struct filesystem_tree_node* gpak_get_root(gpak_t* _pak);
136 
137  /**
138  * @brief Finds a directory in a G-PAK archive.
139  *
140  * This function searches for a directory with the specified _path in the G-PAK archive.
141  *
142  * @param _pak A pointer to the gpak_t.
143  * @param _path A string containing the path of the directory to find.
144  * @return A pointer to the found directory node or NULL if not found.
145  */
146  GPAK_API struct filesystem_tree_node* gpak_find_directory(gpak_t* _pak, const char* _path);
147 
148  /**
149  * @brief Finds a file in a G-PAK archive.
150  *
151  * This function searches for a file with the specified _path in the G-PAK archive.
152  *
153  * @param _pak A pointer to the gpak_t.
154  * @param _path A string containing the path of the file to find.
155  * @return A pointer to the found file node or NULL if not found.
156  */
157  GPAK_API struct filesystem_tree_file* gpak_find_file(gpak_t* _pak, const char* _path);
158 
159 
160  /**
161  * @brief Opens a file in a G-PAK archive.
162  *
163  * This function opens a file with the specified _path in the G-PAK archive.
164  *
165  * @param _pak A pointer to the gpak_t.
166  * @param _path A string containing the path of the file to open.
167  * @return A pointer to the opened gpak_file_t or NULL if an error occurred.
168  */
169  GPAK_API gpak_file_t* gpak_fopen(gpak_t* _pak, const char* _path);
170 
171  /**
172  * @brief Reads the next character from a G-PAK file.
173  *
174  * This function reads the next character from the specified G-PAK file.
175  *
176  * @param _file A pointer to the gpak_file_t.
177  * @return The next character as an integer or EOF if the end of the file is reached.
178  */
179  GPAK_API int gpak_fgetc(gpak_file_t* _file);
180 
181  /**
182  * @brief Reads a line from a G-PAK file.
183  *
184  * This function reads a line from the specified G-PAK file and stores it in the provided _buffer.
185  *
186  * @param _file A pointer to the gpak_file_t.
187  * @param _buffer A pointer to the buffer where the line will be stored.
188  * @param _max The maximum number of characters to read (including the null-terminator).
189  * @return A pointer to the _buffer or NULL if the end of the file is reached.
190  */
191  GPAK_API char* gpak_fgets(gpak_file_t* _file, char* _buffer, int _max);
192 
193  /**
194  * @brief Unreads a character from a G-PAK file.
195  *
196  * This function pushes back the specified _character into the G-PAK file's read buffer.
197  *
198  * @param _file A pointer to the gpak_file_t.
199  * @param _character The character to unget.
200  * @return The character pushed back or EOF if an error occurred.
201  */
202  GPAK_API int gpak_ungetc(gpak_file_t* _file, int _character);
203 
204  /**
205  * @brief Reads data from a G-PAK file.
206  *
207  * This function reads _elemCount elements of size _elemSize from the specified G-PAK file into the provided _buffer.
208  *
209  * @param _buffer A pointer to the buffer where the data will be stored.
210  * @param _elemSize The size of each element to read.
211  * @param _elemCount The number of elements to read.
212  * @param _file A pointer to the gpak_file_t.
213  * @return The number of elements successfully read.
214  */
215  GPAK_API size_t gpak_fread(void* _buffer, size_t _elemSize, size_t _elemCount, gpak_file_t* _file);
216 
217  /**
218  * @brief Returns the current position in a G-PAK file.
219  *
220  * This function returns the current position in the specified G-PAK file.
221  *
222  * @param _file A pointer to the gpak_file_t.
223  * @return The current position in the file or -1 if an error occurred.
224  */
225  GPAK_API long gpak_ftell(gpak_file_t* _file);
226 
227  /**
228  * @brief Sets the file position in a G-PAK file.
229  *
230  * This function sets the file position in the specified G-PAK file according to the _offset and _origin.
231  *
232  * @param _file A pointer to the gpak_file_t.
233  * @param _offset The offset in bytes from
234  * the _origin to set the new file position.
235  * @param _origin The reference position in the file (SEEK_SET, SEEK_CUR, or SEEK_END).
236  * @return Zero if the operation is successful, or -1 if an error occurred.
237  */
238  GPAK_API long gpak_fseek(gpak_file_t* _file, long _offset, int _origin);
239 
240  /**
241  * @brief Checks if the end of a G-PAK file has been reached.
242  *
243  * This function returns a non-zero value if the end of the specified G-PAK file has been reached.
244  *
245  * @param _file A pointer to the gpak_file_t.
246  * @return A non-zero value if the end of the file is reached, or zero otherwise.
247  */
248  GPAK_API long gpak_feof(gpak_file_t* _file);
249 
250  /**
251  * @brief Closes a G-PAK file.
252  * This function closes the specified G-PAK file and deallocates any associated resources.
253  * @param _file A pointer to the gpak_file_t to close.
254  */
255  GPAK_API void gpak_fclose(gpak_file_t* _file);
256 
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif // GPAK_LIB_H
GPAK_API struct filesystem_tree_file * gpak_find_file(gpak_t *_pak, const char *_path)
Definition: gpak.c:381
GPAK_API void gpak_set_compression_algorithm(gpak_t *_pak, int _algorithm)
Definition: gpak.c:338
GPAK_API long gpak_feof(gpak_file_t *_file)
Definition: gpak.c:465
GPAK_API int gpak_fgetc(gpak_file_t *_file)
Definition: gpak.c:435
GPAK_API int gpak_add_file(gpak_t *_pak, const char *_external_path, const char *_internal_path)
Definition: gpak.c:354
GPAK_API gpak_t * gpak_open(const char *_path, int _mode)
Definition: gpak.c:213
GPAK_API char * gpak_fgets(gpak_file_t *_file, char *_buffer, int _max)
Definition: gpak.c:440
GPAK_API size_t gpak_fread(void *_buffer, size_t _elemSize, size_t _elemCount, gpak_file_t *_file)
Definition: gpak.c:450
GPAK_API void gpak_set_error_handler(gpak_t *_pak, gpak_error_handler_t _error_handler)
Definition: gpak.c:328
GPAK_API int gpak_close(gpak_t *_pak)
Definition: gpak.c:290
GPAK_API int gpak_ungetc(gpak_file_t *_file, int _character)
Definition: gpak.c:445
GPAK_API void gpak_set_user_data(gpak_t *_pak, void *_user_data)
Definition: gpak.c:323
GPAK_API void gpak_set_compression_level(gpak_t *_pak, int _level)
Definition: gpak.c:343
GPAK_API gpak_file_t * gpak_fopen(gpak_t *_pak, const char *_path)
Definition: gpak.c:389
GPAK_API struct filesystem_tree_node * gpak_find_directory(gpak_t *_pak, const char *_path)
Definition: gpak.c:373
GPAK_API long gpak_fseek(gpak_file_t *_file, long _offset, int _origin)
Definition: gpak.c:460
GPAK_API long gpak_ftell(gpak_file_t *_file)
Definition: gpak.c:455
GPAK_API struct filesystem_tree_node * gpak_get_root(gpak_t *_pak)
Definition: gpak.c:368
GPAK_API int gpak_add_directory(gpak_t *_pak, const char *_internal_path)
Definition: gpak.c:348
GPAK_API void gpak_fclose(gpak_file_t *_file)
Definition: gpak.c:470
GPAK_API void gpak_set_process_handler(gpak_t *_pak, gpak_progress_handler_t _progress_handler)
Definition: gpak.c:333
void(* gpak_error_handler_t)(const char *, int32_t, void *)
Definition: gpak_data.h:241
void(* gpak_progress_handler_t)(const char *, size_t, size_t, int32_t, void *)
Definition: gpak_data.h:247