-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileio.h
148 lines (126 loc) · 5.58 KB
/
fileio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* fileio.h: Simple file/directory access functions with error-handling.
*
* Copyright (C) 2001-2006 by Brian Raiter, under the GNU General Public
* License. No warranty. See COPYING for details.
*/
#ifndef _fileio_h_
#define _fileio_h_
#include <stdint.h>
#include "defs.h"
/* Reset a fileinfo structure to indicate no file.
*/
extern void clearfileinfo(fileinfo *file);
/* Open a file. If the fileinfo structure does not already have a
* filename assigned to it, name will be used as the filename. If msg
* is NULL, no error will be displayed if the file cannot be opened.
* If msg points to a string, an error will be displayed. The text of
* msg will be used only if errno is zero; otherwise a message
* appropriate to the error will be used.
*/
extern int fileopen(fileinfo *file, char const *name, char const *mode,
char const *msg);
/* The following functions correspond directly to C's standard I/O
* functions. The extra msg parameter works as described above for
* fileopen().
*/
extern int filerewind(fileinfo *file, char const *msg);
extern int filegetpos(fileinfo *file, fpos_t *pos, char const *msg);
extern int filesetpos(fileinfo *file, fpos_t *pos, char const *msg);
extern int fileread(fileinfo *file, void *data, unsigned long size,
char const *msg);
extern int filewrite(fileinfo *file, void const *data, unsigned long size,
char const *msg);
extern void fileclose(fileinfo *file, char const *msg);
/* fileskip() works like fseek() with whence set to SEEK_CUR.
*/
extern int fileskip(fileinfo *file, int offset, char const *msg);
/* filetestend() forces a check for EOF by attempting to read a byte
* from the file, and ungetting the byte if one is successfully read.
*/
extern int filetestend(fileinfo *file);
/* The following functions read and write an unsigned integer value
* from the current position in the given file. For the multi-byte
* values, the value is assumed to be stored in little-endian.
*/
extern int filereadint8(fileinfo *file, uint8_t *val8,
char const *msg);
extern int filewriteint8(fileinfo *file, uint8_t val8,
char const *msg);
extern int filereadint16(fileinfo *file, uint16_t *val16,
char const *msg);
extern int filewriteint16(fileinfo *file, uint16_t val16,
char const *msg);
extern int filereadint32(fileinfo *file, uint32_t *val32,
char const *msg);
extern int filewriteint32(fileinfo *file, uint32_t val32,
char const *msg);
/* Read size bytes from the given file and return the bytes in a
* newly allocated buffer.
*/
extern void *filereadbuf(fileinfo *file, unsigned long size, char const *msg);
/* Read one full line from fp and store the first len characters,
* including any trailing newline. len receives the length of the line
* stored in buf, minus any trailing newline, upon return.
*/
extern int filegetline(fileinfo *file, char *buf, int *len, char const *msg);
/* Read a config-style line from a file, looking for the pattern
* "name=value". FALSE is returned if the end of the file is found
* first.
*/
extern int filegetconfigline(fileinfo *file, char **name, char **value,
char const *msg);
/* Return the maximum size of a legal pathname.
*/
extern int getpathbufferlen(void);
/* Return an allocated buffer big enough to hold any legal pathname.
*/
extern char *getpathbuffer(void);
/* Return TRUE if name contains a path but is not a directory itself.
*/
extern int haspathname(char const *name);
/* Return a pointer to the filename, skipping over any directories in
* the front.
*/
extern char *skippathname(char const *name);
/* Append the path and/or file contained in path to dir, storing the
* result in dest. dest and dir can point to the same buffer. dest is
* assumed to be a buffer of size getpathbufferlen(). If the resulting
* path is longer than this, FALSE is returned and errno is set to
* ENAMETOOLONG.
*/
extern int combinepath(char *dest, char const *dir, char const *path);
/* Return the pathname for a directory and/or filename, using the same
* algorithm to construct the path as openfileindir(). The caller must
* free the returned buffer.
*/
extern char *getpathforfileindir(char const *dir, char const *filename);
/* Verify that the given directory exists, or create it if it doesn't.
*/
extern int finddir(char const *dir);
/* Open a file, using dir as the directory if filename is not already
* a complete pathname. FALSE is returned if the directory could not
* be created.
*/
extern int openfileindir(fileinfo *file, char const *dir, char const *filename,
char const *mode, char const *msg);
/* Call filecallback once for every file in dir. The first argument to
* the callback function is an allocated buffer containing the
* filename. data is passed as the second argument to the callback. If
* the callback's return value is zero, the buffer is deallocated
* normally; if the return value is positive, the callback function
* inherits the buffer and the responsibility of freeing it. If the
* return value is negative, findfiles() stops scanning the directory
* and returns. FALSE is returned if the directory could not be
* examined.
*/
extern int findfiles(char const *dir, void *data,
int (*filecallback)(char*, void*));
/* Display a simple error message prefixed by the name of the given
* file. If errno is set, a message appropriate to the value is used;
* otherwise the text pointed to by msg is used. If msg is NULL, the
* function does nothing. The return value is always FALSE.
*/
extern int _fileerr(char const *cfile, unsigned long lineno,
fileinfo *file, char const *msg);
#define fileerr(file, msg) (_fileerr(__FILE__, __LINE__, (file), (msg)))
#endif