| 1 | /* |
| 2 | * Crossfire -- cooperative multi-player graphical RPG and adventure game |
| 3 | * |
| 4 | * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team |
| 5 | * Copyright (c) 1992 Frank Tore Johansen |
| 6 | * |
| 7 | * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are |
| 8 | * welcome to redistribute it under certain conditions. For details, please |
| 9 | * see COPYING and LICENSE. |
| 10 | * |
| 11 | * The authors can be reached via e-mail at <[email protected]>. |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * @file |
| 16 | * Functions for creating text output files. During (re-)writing a new file or |
| 17 | * if a write error occurs the old file contents are still available. |
| 18 | * |
| 19 | * Example usage: |
| 20 | * <code> |
| 21 | * <pre> |
| 22 | * OutputFile of; |
| 23 | * FILE *f; |
| 24 | * f = of_open(&of, "some_file.txt", llevError); |
| 25 | * if (f == NULL) |
| 26 | * return; |
| 27 | * fprintf(f, "..."); |
| 28 | * fprintf(f, "..."); |
| 29 | * if (!of_close(&of)) |
| 30 | * return; |
| 31 | * </pre> |
| 32 | * </code> |
| 33 | */ |
| 34 | |
| 35 | #ifndef OUTPUT_FILE_H |
| 36 | #define OUTPUT_FILE_H |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | |
| 40 | |
| 41 | typedef struct |
| 42 | { |
| 43 | /** |
| 44 | * The original output file that is written. |
| 45 | */ |
| 46 | char *fname; |
| 47 | |
| 48 | /** |
| 49 | * The temporary output file that is written to until #of_close() is |
| 50 | * called. |
| 51 | */ |
| 52 | char *fname_tmp; |
| 53 | |
| 54 | /** |
| 55 | * The file pointer for fname_tmp. |
| 56 | */ |
| 57 | FILE *file; |
| 58 | } OutputFile; |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Opens an output file. It is equivalent to <code>fopen(fname, "w");</code> |
| 63 | * but uses a temporary file instead of <code>fname</code> until #of_close() is |
| 64 | * called. |
| 65 | * |
| 66 | * @param of an #OutputFile instance allocated by the caller; it must not be |
| 67 | * freed or reused until #of_close() has been called |
| 68 | * |
| 69 | * @param fname the filename to open; the string may be freed or reused |
| 70 | * afterwards |
| 71 | * |
| 72 | * @return the file pointer or <code>NULL</code> if the file could not be |
| 73 | * opened; in this case an error message has been printed and the original file |
| 74 | * content are unchanged |
| 75 | */ |
| 76 | FILE *of_open(OutputFile *of, const char *fname); |
| 77 | |
| 78 | /** |
| 79 | * Closes an output file. This function (or #of_cancel()} must be called |
| 80 | * exactly once for each successful call to #of_open(). |
| 81 | * |
| 82 | * @param of the output file instance to operate on |
| 83 | * |
| 84 | * @return whether writing was successful; if unsuccessful an error message has |
| 85 | * been printed and the original file content are unchanged |
| 86 | */ |
| 87 | int of_close(OutputFile *of); |
| 88 | |
| 89 | /** |
| 90 | * Cancels a save process. This closes and deletes the temporary file, leaving |
| 91 | * the original file contents unmodified. This function (or on_close()} must |
| 92 | * be called exactly once for each successful call to op_open(). |
| 93 | * |
| 94 | * @param of the output file instance to operate on |
| 95 | */ |
| 96 | void of_cancel(OutputFile *of); |
| 97 | |
| 98 | #endif |