File: output_file.h

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (98 lines) | stat: -rw-r--r-- 2,581 bytes parent folder | download | duplicates (4)
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
/*
 * Crossfire -- cooperative multi-player graphical RPG and adventure game
 *
 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
 * Copyright (c) 1992 Frank Tore Johansen
 *
 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
 * welcome to redistribute it under certain conditions. For details, please
 * see COPYING and LICENSE.
 *
 * The authors can be reached via e-mail at <crossfire@metalforge.org>.
 */

/**
 * @file
 * Functions for creating text output files. During (re-)writing a new file or
 * if a write error occurs the old file contents are still available.
 *
 * Example usage:
 * <code>
 * <pre>
 * OutputFile of;
 * FILE *f;
 * f = of_open(&of, "some_file.txt", llevError);
 * if (f == NULL)
 *     return;
 * fprintf(f, "...");
 * fprintf(f, "...");
 * if (!of_close(&of))
 *     return;
 * </pre>
 * </code>
 */

#ifndef OUTPUT_FILE_H
#define OUTPUT_FILE_H

#include <stdio.h>


typedef struct
{
    /**
     * The original output file that is written.
     */
    char *fname;

    /**
     * The temporary output file that is written to until #of_close() is
     * called.
     */
    char *fname_tmp;

    /**
     * The file pointer for fname_tmp.
     */
    FILE *file;
} OutputFile;


/**
 * Opens an output file. It is equivalent to <code>fopen(fname, "w");</code>
 * but uses a temporary file instead of <code>fname</code> until #of_close() is
 * called.
 *
 * @param of an #OutputFile instance allocated by the caller; it must not be
 * freed or reused until #of_close() has been called
 *
 * @param fname the filename to open; the string may be freed or reused
 * afterwards
 *
 * @return the file pointer or <code>NULL</code> if the file could not be
 * opened; in this case an error message has been printed and the original file
 * content are unchanged
 */
FILE *of_open(OutputFile *of, const char *fname);

/**
 * Closes an output file. This function (or #of_cancel()} must be called
 * exactly once for each successful call to #of_open().
 *
 * @param of the output file instance to operate on
 *
 * @return whether writing was successful; if unsuccessful an error message has
 * been printed and the original file content are unchanged
 */
int of_close(OutputFile *of);

/**
 * Cancels a save process. This closes and deletes the temporary file, leaving
 * the original file contents unmodified. This function (or on_close()} must
 * be called exactly once for each successful call to op_open().
 *
 * @param of the output file instance to operate on
 */
void of_cancel(OutputFile *of);

#endif