Crossfire Server, Trunk  R20513
output_file.c
Go to the documentation of this file.
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 <crossfire@metalforge.org>.
12  */
13 
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 #include "global.h"
19 #include "logger.h"
20 #include "output_file.h"
21 
22 
27 #define TMP_EXT ".tmp"
28 
29 
30 FILE *of_open(OutputFile *of, const char *fname) {
31  char *fname_tmp;
32  FILE *f;
33 
34  fname_tmp = malloc(strlen(fname)+sizeof(TMP_EXT));
35  if (fname_tmp == NULL) {
36  LOG(llevError, "%s: out of memory\n", fname);
37  return NULL;
38  }
39 
40  sprintf(fname_tmp, "%s%s", fname, TMP_EXT);
41  remove(fname_tmp);
42  f = fopen(fname_tmp, "w");
43  if (f == NULL) {
44  LOG(llevError, "%s: %s\n", fname_tmp, strerror(errno));
45  free(fname_tmp);
46  return NULL;
47  }
48 
49  of->fname = strdup_local(fname);
50  if (of->fname == NULL) {
51  LOG(llevError, "%s: out of memory\n", fname);
52  free(fname_tmp);
53  fclose(f);
54  return NULL;
55  }
56  of->fname_tmp = fname_tmp;
57  of->file = f;
58  return f;
59 }
60 
61 int of_close(OutputFile *of) {
62  if (ferror(of->file)) {
63  LOG(llevError, "%s: write error\n", of->fname);
64  fclose(of->file);
65  remove(of->fname_tmp);
66  free(of->fname_tmp);
67  free(of->fname);
68  return 0;
69  }
70  if (fclose(of->file) != 0) {
71  LOG(llevError, "%s: %s\n", of->fname, strerror(errno));
72  remove(of->fname_tmp);
73  free(of->fname_tmp);
74  free(of->fname);
75  return 0;
76  }
77  if (rename(of->fname_tmp, of->fname) != 0) {
78  LOG(llevError, "%s: cannot rename from %s: %s\n", of->fname, of->fname_tmp, strerror(errno));
79  remove(of->fname_tmp);
80  free(of->fname_tmp);
81  free(of->fname);
82  return 0;
83  }
84  free(of->fname_tmp);
85  free(of->fname);
86  return 1;
87 }
88 
90 {
91  fclose(of->file);
92  remove(of->fname_tmp);
93  free(of->fname_tmp);
94  free(of->fname);
95 }
Error, serious thing.
Definition: logger.h:11
void of_cancel(OutputFile *of)
Cancels a save process.
Definition: output_file.c:89
#define strdup_local
Definition: compat.h:25
FILE * file
The file pointer for fname_tmp.
Definition: output_file.h:57
Global type definitions and header inclusions.
char * fname_tmp
The temporary output file that is written to until of_close() is called.
Definition: output_file.h:52
int of_close(OutputFile *of)
Closes an output file.
Definition: output_file.c:61
#define TMP_EXT
The extension for temporary files that is appended to the original output filename during write opera...
Definition: output_file.c:27
Log levels.
FILE * of_open(OutputFile *of, const char *fname)
Opens an output file.
Definition: output_file.c:30
Functions for creating text output files.
void LOG(LogLevel logLevel, const char *format,...)
Logs a message to stderr, or to file.
Definition: logger.c:51
char * fname
The original output file that is written.
Definition: output_file.h:46