Home | History | Annotate | Download | only in libutil
      1 /**
      2  * @file op_fileio.h
      3  * Reading from / writing to files
      4  *
      5  * @remark Copyright 2002 OProfile authors
      6  * @remark Read the file COPYING
      7  *
      8  * @author John Levon
      9  * @author Philippe Elie
     10  */
     11 
     12 #ifndef OP_FILEIO_H
     13 #define OP_FILEIO_H
     14 
     15 #ifdef __cplusplus
     16 extern "C" {
     17 #endif
     18 
     19 #include "op_types.h"
     20 
     21 #include <stdio.h>
     22 
     23 /**
     24  * op_try_open_file - open a file
     25  * @param name  file name
     26  * @param mode  mode string
     27  *
     28  * Open a file name.
     29  * Returns file handle or %NULL on failure.
     30  */
     31 FILE * op_try_open_file(char const * name, char const * mode);
     32 
     33 /**
     34  * op_open_file - open a file
     35  * @param name  file name
     36  * @param mode  mode string
     37  *
     38  * Open a file name.
     39  * Failure to open is fatal.
     40  */
     41 FILE * op_open_file(char const * name, char const * mode);
     42 
     43 /**
     44  * op_read_int_from_file - parse an ASCII value from a file into an integer
     45  * @param filename  name of file to parse integer value from
     46  * @param fatal  non-zero if any error must be fatal
     47  *
     48  * Reads an ASCII integer from the given file. If an error occur and fatal is
     49  * zero (u32)-1 is returned else the value read in is returned.
     50  */
     51 u32 op_read_int_from_file(char const * filename, int fatal);
     52 
     53 /**
     54  * op_close_file - close a file
     55  * @param fp  file pointer
     56  *
     57  * Closes a file pointer. A non-fatal
     58  * error message is produced if the
     59  * close fails.
     60  */
     61 void op_close_file(FILE * fp);
     62 
     63 /**
     64  * op_write_file - write to a file
     65  * @param fp  file pointer
     66  * @param buf  buffer
     67  * @param size  nr. of bytes to write
     68  *
     69  * Write size bytes of buffer buf to a file.
     70  * Failure is fatal.
     71  */
     72 void op_write_file(FILE * fp, void const * buf, size_t size);
     73 
     74 /**
     75  * op_write_u32 - write four bytes to a file
     76  * @param fp  file pointer
     77  * @param val  value to write
     78  *
     79  * Write an unsigned four-byte value val to a file.
     80  * Failure is fatal.
     81  *
     82  * No byte-swapping is done.
     83  */
     84 void op_write_u32(FILE * fp, u32 val);
     85 
     86 /**
     87  * op_write_u64 - write eight bytes to a file
     88  * @param fp  file pointer
     89  * @param val  value to write
     90  *
     91  * Write an unsigned eight-byte value val to a file.
     92  * Failure is fatal.
     93  *
     94  * No byte-swapping is done.
     95  */
     96 void op_write_u64(FILE * fp, u64 val);
     97 
     98 /**
     99  * op_write_u8 - write a byte to a file
    100  * @param fp  file pointer
    101  * @param val  value to write
    102  *
    103  * Write an unsigned byte value val to a file.
    104  * Failure is fatal.
    105  */
    106 void op_write_u8(FILE * fp, u8 val);
    107 
    108 /**
    109  * op_get_line - read an ASCII line from a file
    110  * @param fp  file pointer
    111  *
    112  * Get a line of ASCII text from a file. The file is read
    113  * up to the first '\0' or '\n'. A trailing '\n' is deleted.
    114  *
    115  * Returns the dynamically-allocated string containing
    116  * that line. At the end of a file NULL will be returned.
    117  * be returned.
    118  *
    119  * The string returned must be free()d by the caller.
    120  *
    121  * getline() is not a proper solution to replace this function
    122  */
    123 char * op_get_line(FILE * fp);
    124 
    125 /**
    126  * calc_crc32
    127  * @param crc current value
    128  * @param buf pointer to buffer
    129  * @param len
    130  *
    131  * Returns current crc computed from the crc argument and the
    132  * characters in len characters in buf.
    133  */
    134 unsigned long calc_crc32(unsigned long crc, unsigned char * buf, size_t len);
    135 
    136 #ifdef __cplusplus
    137 }
    138 #endif
    139 
    140 #endif /* OP_FILEIO_H */
    141