aboutsummaryrefslogtreecommitdiff
path: root/libutil/op_fileio.h
blob: 49b126d6c17ee3e824eda10f0b2d24e0688b0f9a (plain)
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
/**
 * @file op_fileio.h
 * Reading from / writing to files
 *
 * @remark Copyright 2002 OProfile authors
 * @remark Read the file COPYING
 *
 * @author John Levon
 * @author Philippe Elie
 */

#ifndef OP_FILEIO_H
#define OP_FILEIO_H

#ifdef __cplusplus
extern "C" {
#endif

#include "op_types.h"

#include <stdio.h>

/**
 * op_try_open_file - open a file
 * @param name  file name
 * @param mode  mode string
 *
 * Open a file name.
 * Returns file handle or %NULL on failure.
 */
FILE * op_try_open_file(char const * name, char const * mode);

/**
 * op_open_file - open a file
 * @param name  file name
 * @param mode  mode string
 *
 * Open a file name.
 * Failure to open is fatal.
 */
FILE * op_open_file(char const * name, char const * mode);

/**
 * op_read_int_from_file - parse an ASCII value from a file into an integer
 * @param filename  name of file to parse integer value from
 * @param fatal  non-zero if any error must be fatal
 *
 * Reads an ASCII integer from the given file. If an error occur and fatal is
 * zero (u32)-1 is returned else the value read in is returned.
 */
u32 op_read_int_from_file(char const * filename, int fatal);

/**
 * op_close_file - close a file
 * @param fp  file pointer
 *
 * Closes a file pointer. A non-fatal
 * error message is produced if the
 * close fails.
 */
void op_close_file(FILE * fp);

/**
 * op_write_file - write to a file
 * @param fp  file pointer
 * @param buf  buffer
 * @param size  nr. of bytes to write
 *
 * Write size bytes of buffer buf to a file.
 * Failure is fatal.
 */
void op_write_file(FILE * fp, void const * buf, size_t size);

/**
 * op_write_u32 - write four bytes to a file
 * @param fp  file pointer
 * @param val  value to write
 *
 * Write an unsigned four-byte value val to a file.
 * Failure is fatal.
 *
 * No byte-swapping is done.
 */
void op_write_u32(FILE * fp, u32 val);

/**
 * op_write_u64 - write eight bytes to a file
 * @param fp  file pointer
 * @param val  value to write
 *
 * Write an unsigned eight-byte value val to a file.
 * Failure is fatal.
 *
 * No byte-swapping is done.
 */
void op_write_u64(FILE * fp, u64 val);

/**
 * op_write_u8 - write a byte to a file
 * @param fp  file pointer
 * @param val  value to write
 *
 * Write an unsigned byte value val to a file.
 * Failure is fatal.
 */
void op_write_u8(FILE * fp, u8 val);

/**
 * op_get_line - read an ASCII line from a file
 * @param fp  file pointer
 *
 * Get a line of ASCII text from a file. The file is read
 * up to the first '\0' or '\n'. A trailing '\n' is deleted.
 *
 * Returns the dynamically-allocated string containing
 * that line. At the end of a file NULL will be returned.
 * be returned.
 *
 * The string returned must be free()d by the caller.
 *
 * getline() is not a proper solution to replace this function
 */
char * op_get_line(FILE * fp);

/**
 * calc_crc32
 * @param crc current value
 * @param buf pointer to buffer
 * @param len
 *
 * Returns current crc computed from the crc argument and the
 * characters in len characters in buf.
 */
unsigned long calc_crc32(unsigned long crc, unsigned char * buf, size_t len);

#ifdef __cplusplus
}
#endif

#endif /* OP_FILEIO_H */