-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
139 lines (113 loc) · 5.52 KB
/
README
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
CFile
=====
CFile is a library to allow you to read and write files independent of
what their compression method is. It determines the correct set of
routines based on the name of the file, and it can use libMagic to
work the type out if that's available.
How to use it
-------------
I've tried to make CFile a drop-in replacement for the standard stdio
file handling routines. For example:
cfile *fp = cfile_open("hello.txt", "w");
cfprint(fp, "Hello, %s\n", "world");
cfclose(fp);
What it provides
----------------
CFile provides the following functions:
Opening a file:
* cfile *cfile_open(char name[], char mode[])
- open a given file for reading or writing.
* cfile *cfile_dopen(int fd char mode[])
- open a file from a file descriptor. This does not try to detect the
type of file being read - all files are treated as uncompressed.
Reading from a file:
* bool cfeof(cfile *fp)
- return whether we have read past the end of the underlying file.
* off_t cfsize(cfile *fp)
- return the size of the uncompressed content of the file in bytes. This
is useful for knowing how far you have currently read through the file.
* char *cfgets(cfile *fp, char *str, size_t len)
- read at most len-1 bytes from the file, or until a newline - whichever
happens first. Terminates the string with \0. Similar to fgets.
* bool cfgetline(cfile *fp, char **line)
- read an entire line of text from the file, until a newline or end of
file. Uses talloc_realloc to resize the line pointer behind the scenes,
so you always get a complete line of text no matter how long it was.
If you allocated the string, it will still be owned by your talloc
context. You can supply *line = NULL here, in which case you will get
a string allocated under the cfile's context.
* int cfread(cfile *fp, void *ptr, size_t size, size_t num)
- Read at most num elements of size bytes into ptr. Returns the number
of elements (not bytes) read, which may be less than requested if we
ran out of file to read. Similar to fread.
Writing to a file:
* int cfprintf(cfile *fp, const char *fmt, ...)
* int cvfprintf(cfile *fp, const char *fmt, va_list ap)
- print a string, formatted using the standard printf formatting syntax,
to the file. Similar to fprintf and vfprintf.
* int cfwrite(cfile *fp, const void *ptr, size_t size, size_t num)
- Write at most num elements of size bytes from ptr into the file.
Returns the number of elements (not bytes) written, which may be less
than the number requested if we ran out of space or a similar file
error occurred. Similar to fwrite.
* int cfflush(cfile *fp)
- Attempt to flush any current buffered data to disk. Some compression
libraries may clear internal state at this point, resulting in reduced
compression. Similar to fflush.
Closing the file:
* int cfclose(cfile *fp)
- close the given cfile.
Other utilities:
* void cfile_set_context(void *context)
- Set the talloc context for all Cfile allocations. If you don't set one,
cfile will automatically create its own. If you supply a context and
have already opened files, talloc_steal will be used to give your
context the ownership of all the current cfile pointers.
Building CFile
==============
Required libraries:
-------------------
To compile CFile, you'll need the following:
on a Fedora system, install the packages:
libtalloc-devel
libattr-devel
libmagic-devel
zlib-devel
bzip2-devel
xz-devel
How to compile it
-----------------
CFile has fairly minimal build requirements:
$ make clean
rm -f *.o *.a test-cat
$ make
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o cfile.o cfile.c
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o cfile_bzip2.o cfile_bzip2.c
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o cfile_gzip.o cfile_gzip.c
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o cfile_normal.o cfile_normal.c
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o cfile_null.o cfile_null.c
test -f libcfile.a && rm libcfile.a || true
ar cq libcfile.a cfile.o cfile_bzip2.o cfile_gzip.o cfile_normal.o cfile_null.o
cc -Wall -Wshadow -Werror-implicit-function-declaration -Wmissing-prototypes -W
strict-prototypes -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-format
-attribute -Wformat=2 -Wno-format-y2k -Wdeclaration-after-statement -Wextra -We
rror -I. -c -o test-cat.o test-cat.c
gcc -g test-cat.o -o test-cat -L. -lcfile -lz -lbz2 -ltalloc
$
This will compile the library (libcfile.a) and the test programs 'test-cat',
'test-xz' and 'get_raw_size'.