]> icculus.org git repositories - icculus/xz.git/blob - src/lzma/io.h
Oh well, big messy commit again. Some highlights:
[icculus/xz.git] / src / lzma / io.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       io.h
4 /// \brief      I/O types and functions
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This program is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This program is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #ifndef IO_H
21 #define IO_H
22
23 #include "private.h"
24
25
26 // Some systems have suboptimal BUFSIZ. Use a bit bigger value on them.
27 #if BUFSIZ <= 1024
28 #       define IO_BUFFER_SIZE 8192
29 #else
30 #       define IO_BUFFER_SIZE BUFSIZ
31 #endif
32
33
34 typedef struct {
35         /// Name of the source filename (as given on the command line) or
36         /// pointer to static "(stdin)" when reading from standard input.
37         const char *src_name;
38
39         /// Destination filename converted from src_name or pointer to static
40         /// "(stdout)" when writing to standard output.
41         char *dest_name;
42
43         /// File descriptor of the source file
44         int src_fd;
45
46         /// File descriptor of the target file
47         int dest_fd;
48
49         /// Stat of the source file.
50         struct stat src_st;
51
52         /// Stat of the destination file.
53         struct stat dest_st;
54
55         /// True once end of the source file has been detected.
56         bool src_eof;
57
58 } file_pair;
59
60
61 /// \brief      Opens a file pair
62 extern file_pair *io_open(const char *src_name);
63
64
65 /// \brief      Closes the file descriptors and frees possible allocated memory
66 ///
67 /// The success argument determines if source or destination file gets
68 /// unlinked:
69 ///  - false: The destination file is unlinked.
70 ///  - true: The source file is unlinked unless writing to stdout or --keep
71 ///    was used.
72 extern void io_close(file_pair *pair, bool success);
73
74
75 /// \brief      Reads from the source file to a buffer
76 ///
77 /// \param      pair    File pair having the source file open for reading
78 /// \param      buf     Destination buffer to hold the read data
79 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
80 ///
81 /// \return     On success, number of bytes read is returned. On end of
82 ///             file zero is returned and pair->src_eof set to true.
83 ///             On error, SIZE_MAX is returned and error message printed.
84 extern size_t io_read(file_pair *pair, uint8_t *buf, size_t size);
85
86
87 /// \brief      Writes a buffer to the destination file
88 ///
89 /// \param      pair    File pair having the destination file open for writing
90 /// \param      buf     Buffer containing the data to be written
91 /// \param      size    Size of the buffer; assumed be smaller than SSIZE_MAX
92 ///
93 /// \return     On success, zero is returned. On error, -1 is returned
94 ///             and error message printed.
95 extern bool io_write(const file_pair *pair, const uint8_t *buf, size_t size);
96
97 #endif