OPNGLIB
PNG optimization library
 All Data Structures Files Functions Typedefs Pages
io.h
1 /*
2  * optk/io.h
3  * I/O utilities.
4  *
5  * Copyright (C) 2003-2012 Cosmin Truta.
6  *
7  * This software is distributed under the zlib license.
8  * Please see the accompanying LICENSE file.
9  */
10 
11 #ifndef OPTK_IO_H_
12 #define OPTK_IO_H_
13 
14 #include <stdio.h>
15 #include "integer.h"
16 
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 
23 /*
24  * File translation modes.
25  * These symbols are used by optk_fsetmode().
26  */
27 #define OPTK_FMODE_BINARY 0x0000
28 #define OPTK_FMODE_TEXT 0x0001
29 #define OPTK_FMODE_WTEXT 0x0002
30 #define OPTK_FMODE_U8TEXT 0x0008
31 #define OPTK_FMODE_U16TEXT 0x0010
32 #define OPTK_FMODE_U32TEXT 0x0020
33 
34 /*
35  * The file offset type.
36  * This is a signed integer type that is at least 64-bits wide.
37  */
38 typedef optk_int64_t optk_foffset_t;
39 #define OPTK_FOFFSET_MIN OPTK_INT64_MIN
40 #define OPTK_FOFFSET_MAX OPTK_INT64_MAX
41 #define OPTK_FOFFSET_SCNd OPTK_SCNd64
42 #define OPTK_FOFFSET_SCNi OPTK_SCNi64
43 #define OPTK_FOFFSET_SCNo OPTK_SCNo64
44 #define OPTK_FOFFSET_SCNx OPTK_SCNx64
45 #define OPTK_FOFFSET_PRId OPTK_PRId64
46 #define OPTK_FOFFSET_PRIi OPTK_PRIi64
47 #define OPTK_FOFFSET_PRIo OPTK_PRIo64
48 #define OPTK_FOFFSET_PRIx OPTK_PRIx64
49 #define OPTK_FOFFSET_PRIX OPTK_PRIX64
50 
51 /*
52  * The file size type.
53  * This is an unsigned integer type that is at least 64-bits wide.
54  */
55 typedef optk_uint64_t optk_fsize_t;
56 #define OPTK_FSIZE_MAX OPTK_UINT64_MAX
57 #define OPTK_FSIZE_SCNu OPTK_SCNu64
58 #define OPTK_FSIZE_SCNo OPTK_SCNo64
59 #define OPTK_FSIZE_SCNx OPTK_SCNx64
60 #define OPTK_FSIZE_PRIu OPTK_PRIu64
61 #define OPTK_FSIZE_PRIo OPTK_PRIo64
62 #define OPTK_FSIZE_PRIx OPTK_PRIx64
63 #define OPTK_FSIZE_PRIX OPTK_PRIX64
64 
65 /*
66  * Returns the current value of the file position indicator.
67  * On error, the function returns (osys_foffset_t)(-1).
68  */
69 optk_foffset_t
70 optk_ftello(FILE *stream);
71 
72 /*
73  * Sets the file position indicator at the specified file offset.
74  * On success, the function returns 0. On error, it returns -1.
75  */
76 int
77 optk_fseeko(FILE *stream, optk_foffset_t offset, int whence);
78 
79 /*
80  * Gets the size of the specified file stream.
81  * This function may change the file position indicator.
82  * On success, the function returns 0. On error, it returns -1.
83  */
84 int
85 optk_fgetsize(FILE *stream, optk_fsize_t *size);
86 
87 /*
88  * Reads a block of data from the specified file offset.
89  * The file-position indicator is saved and restored after reading.
90  * The file buffer is flushed before and after reading.
91  * On success, the function returns the number of bytes read.
92  * On error, it returns 0.
93  */
94 size_t
95 optk_fread_at(FILE *stream, long offset, int whence,
96  void *block, size_t blocksize);
97 
98 /*
99  * Writes a block of data at the specified file offset.
100  * The file-position indicator is saved and restored after writing.
101  * The file buffer is flushed before and after writing.
102  * On success, the function returns the number of bytes written.
103  * On error, it returns 0.
104  */
105 size_t
106 optk_fwrite_at(FILE *stream, long offset, int whence,
107  const void *block, size_t blocksize);
108 
109 /*
110  * Sets the translation mode of the specified file stream to the
111  * specified mode, which can be one of the OPTK_FMODE_ constants.
112  * On success, the function returns 0.
113  * On error, it returns -1.
114  */
115 int
116 optk_fsetmode(FILE *stream, int fmode);
117 
118 /*
119  * Checks if the specified file stream is associated with a terminal device.
120  * The function returns one of the following values:
121  * 1: the specified file is associated with a terminal device.
122  * 0: the specified file is not associated with a terminal device.
123  * -1: the implementation is unable to perform this check.
124  */
125 int
126 optk_ftty(FILE *stream);
127 
128 /*
129  * Extracts the base component of a path name.
130  * The function returns a pointer to the base component within
131  * the given path name. The length of the base component string
132  * is stored into basename_len, if basename_len is not NULL.
133  */
134 char *
135 optk_get_basename(const char *path, size_t *basename_len);
136 
137 /*
138  * Extracts the directory component of a path name.
139  * The function returns a pointer to the directory component within
140  * the given path name. The length of the directory component string
141  * is stored into dirname_len, if dirname_len is not NULL.
142  * If with_dir_separator is non-zero, the directory separator is
143  * included in the result (e.g. "examples/").
144  */
145 char *
146 optk_get_dirname(const char *path, int with_dir_separator,
147  size_t *dirname_len);
148 
149 /*
150  * Creates a path name by changing the directory component of a given
151  * path name.
152  * The new directory name may optionally contain the directory separator.
153  * If the given buffer is non-NULL and sufficiently large to hold
154  * the resulting path, the resulting path is stored into the buffer.
155  * The function returns the length of the resulting path (excluding the
156  * terminating '\0'), regardless whether the buffer is written or not.
157  */
158 size_t
159 optk_set_dirname(char *buffer, size_t bufsize, const char *path,
160  const char *new_dirname);
161 
162 /*
163  * Extracts the extension component of a path name.
164  * The function returns a pointer to the extension component within
165  * the given path name. The length of the extension component string
166  * is stored into extname_len, if extname_len is not NULL.
167  * If with_ext_separator is non-zero, the extension separator is
168  * included in the result (e.g. ".gz").
169  * The number num_ext indicates the maximum number of extensions to
170  * be returned; -1 indicates all extensions.
171  */
172 char *
173 optk_get_extname(const char *path, int with_ext_separator, int num_ext,
174  size_t *extname_len);
175 
176 /*
177  * Creates a path name by changing the extension component of a given
178  * path name.
179  * The number num_ext indicates the maximum number of extensions in the
180  * original path (or -1 for all extensions) to be replaced.
181  * The new extension name may optionally contain the extension separator.
182  * The new extension can be the NULL, indicating that the resulting
183  * path has neither an extension nor an extension separator.
184  * Otherwise, the new path will always contain the extension separator
185  * (even when the new extension name is the empty string).
186  * If the given buffer is non-NULL and sufficiently large to hold
187  * the resulting path, the resulting path is stored into the buffer.
188  * The function returns the length of the resulting path (excluding the
189  * terminating '\0'), regardless whether the buffer is written or not.
190  */
191 size_t
192 optk_set_extname(char *buffer, size_t bufsize, const char *path, int num_ext,
193  const char *new_extname);
194 
195 /*
196  * Copies the source file onto the destination file path.
197  * On success, the function returns 0.
198  * On error, it returns -1.
199  */
200 int
201 optk_copy(const char *src_path, const char *dest_path, int clobber);
202 
203 /*
204  * Moves the source file onto the destination file path.
205  * On success, the function returns 0.
206  * On error, it returns -1.
207  */
208 int
209 optk_move(const char *src_path, const char *dest_path, int clobber);
210 
211 /*
212  * Changes the name of a file path.
213  * On success, the function returns 0.
214  * On error, it returns -1.
215  */
216 int
217 optk_rename(const char *src_path, const char *dest_path, int clobber);
218 
219 /*
220  * Copies the attributes (access mode, time stamp, etc.) of the source
221  * file path onto the destination file path.
222  * On success, the function returns 0.
223  * On error, it returns -1.
224  */
225 int
226 optk_copy_attr(const char *src_path, const char *dest_path);
227 
228 /*
229  * Creates a new directory.
230  * If the directory is successfully created, the function returns 1.
231  * If a directory with the same name already exists, the function returns 0.
232  * On error, the function returns -1.
233  */
234 int
235 optk_create_dir(const char *dirname);
236 
237 /*
238  * Determines if the accessibility of the specified path satisfies
239  * the specified access mode. The access mode consists of one or more
240  * characters that indicate the checks to be performed, as follows:
241  * 'e': the path exists (default).
242  * 'f': the path exists and is a regular file.
243  * 'd': the path exists and is a directory.
244  * 'r': the path exists and read permission is granted.
245  * 'w': the path exists and write permission is granted.
246  * 'x': the path exists and execute permission is granted.
247  * For example, to determine if a file can be opened for reading using
248  * fopen(), use "fr" in the access mode.
249  * If all checks succeed, the function returns 1.
250  * If at least one check fails, the function returns 0.
251  * If at least one check cannot be performed, the function returns -1.
252  */
253 int
254 optk_test(const char *path, const char *mode);
255 
256 /*
257  * Determines if a path is an accessible directory.
258  * This function has a behavior similar to optk_test(path, "d"), but
259  * it might do a better handling of special paths like current dir,
260  * drive name, path ending with dir separator, etc.
261  * If the path is an accessible directory, the function returns 1.
262  * If the path is not an accessible directory, the function returns 0.
263  * If the check cannot be performed, the function returns -1.
264  */
265 int
266 optk_test_dir(const char *path);
267 
268 /*
269  * Determines if two accessible paths are equivalent.
270  * Two paths are said to be equivalent if they point to the same
271  * physical location (e.g. on the disk).
272  * If the two paths are equivalent, the function returns 1.
273  * If the two paths are not equivalent, the function returns 0.
274  * If at least one path is not accessible or does not exist, or
275  * if the check cannot be performed, the function returns -1.
276  */
277 int
278 optk_test_eq(const char *path1, const char *path2);
279 
280 /*
281  * Removes a directory entry.
282  * On success, the function returns 0.
283  * On error, it returns -1.
284  */
285 int
286 optk_unlink(const char *path);
287 
288 
289 #ifdef __cplusplus
290 } /* extern "C" */
291 #endif
292 
293 
294 #endif /* OPTK_IO_H_ */