fix some bad includes.

start removing the cfopen function.
move a function that was only used in textstore.c.
This commit is contained in:
Enno Rehling 2013-12-29 00:19:22 -08:00
parent 25328d2a62
commit 6521fed160
7 changed files with 145 additions and 148 deletions

View File

@ -8,6 +8,8 @@ INCLUDE (CheckSymbolExists)
CHECK_INCLUDE_FILES (stdbool.h HAVE_STDBOOL_H)
CHECK_SYMBOL_EXISTS (_Bool "stdbool.h" HAVE__BOOL)
CHECK_INCLUDE_FILES (strings.h HAVE_STRINGS_H)
CHECK_SYMBOL_EXISTS (strdup "string.h" HAVE_STRDUP)
CHECK_SYMBOL_EXISTS (_strdup "string.h" HAVE__STRDUP)
CONFIGURE_FILE (
${CMAKE_CURRENT_SOURCE_DIR}/config.h.in

View File

@ -1,3 +1,5 @@
#cmakedefine HAVE_STDBOOL_H 1
#cmakedefine HAVE__BOOL 1
#cmakedefine HAVE_STRINGS_H 1
#cmakedefine HAVE_STRDUP 1
#cmakedefine HAVE__STRDUP 1

View File

@ -34,6 +34,7 @@
#include <util/lists.h>
#include <string.h>
#include <stdio.h>
#undef SUMMARY_BOM /* write a BOM in the summary file */
@ -119,15 +120,19 @@ static void writeturn(void)
FILE *f;
sprintf(zText, "%s/datum", basepath());
f = cfopen(zText, "w");
if (!f)
f = fopen(zText, "w");
if (!f) {
perror(zText);
return;
}
fputs(gamedate2(default_locale), f);
fclose(f);
sprintf(zText, "%s/turn", basepath());
f = cfopen(zText, "w");
if (!f)
f = fopen(zText, "w");
if (!f) {
perror(zText);
return;
}
fprintf(f, "%d\n", turn);
fclose(f);
}

View File

@ -118,132 +118,6 @@ char *rns(FILE * f, char *c, size_t size)
extern unsigned int __at_hashkey(const char *s);
FILE *cfopen(const char *filename, const char *mode)
{
FILE *F = fopen(filename, mode);
if (F == 0) {
perror(filename);
return NULL;
}
setvbuf(F, 0, _IOFBF, 32 * 1024); /* 32 kb buffer size */
return F;
}
int freadstr(FILE * F, int encoding, char *start, size_t size)
{
char *str = start;
bool quote = false;
for (;;) {
int c = fgetc(F);
if (isxspace(c)) {
if (str == start) {
continue;
}
if (!quote) {
*str = 0;
return (int)(str - start);
}
}
switch (c) {
case EOF:
return EOF;
case '"':
if (!quote && str != start) {
log_error(
("datafile contains a \" that isn't at the start of a string.\n"));
assert
(!"datafile contains a \" that isn't at the start of a string.\n");
}
if (quote) {
*str = 0;
return (int)(str - start);
}
quote = true;
break;
case '\\':
c = fgetc(F);
switch (c) {
case EOF:
return EOF;
case 'n':
if ((size_t) (str - start + 1) < size) {
*str++ = '\n';
}
break;
default:
if ((size_t) (str - start + 1) < size) {
if (encoding == XML_CHAR_ENCODING_8859_1 && c & 0x80) {
char inbuf = (char)c;
size_t inbytes = 1;
size_t outbytes = size - (str - start);
int ret = unicode_latin1_to_utf8(str, &outbytes, &inbuf, &inbytes);
if (ret > 0)
str += ret;
else {
log_error("input data was not iso-8859-1! assuming utf-8\n");
encoding = XML_CHAR_ENCODING_ERROR;
*str++ = (char)c;
}
} else {
*str++ = (char)c;
}
}
}
break;
default:
if ((size_t) (str - start + 1) < size) {
if (encoding == XML_CHAR_ENCODING_8859_1 && c & 0x80) {
char inbuf = (char)c;
size_t inbytes = 1;
size_t outbytes = size - (str - start);
int ret = unicode_latin1_to_utf8(str, &outbytes, &inbuf, &inbytes);
if (ret > 0)
str += ret;
else {
log_error("input data was not iso-8859-1! assuming utf-8\n");
encoding = XML_CHAR_ENCODING_ERROR;
*str++ = (char)c;
}
} else {
*str++ = (char)c;
}
}
}
}
}
/** writes a quoted string to the file
* no trailing space, since this is used to make the creport.
*/
int fwritestr(FILE * F, const char *str)
{
int nwrite = 0;
fputc('\"', F);
if (str)
while (*str) {
int c = (int)(unsigned char)*str++;
switch (c) {
case '"':
case '\\':
fputc('\\', F);
fputc(c, F);
nwrite += 2;
break;
case '\n':
fputc('\\', F);
fputc('n', F);
nwrite += 2;
break;
default:
fputc(c, F);
++nwrite;
}
}
fputc('\"', F);
return nwrite + 2;
}
static unit *unitorders(FILE * F, int enc, struct faction *f)
{
@ -389,13 +263,11 @@ int readorders(const char *filename)
int nfactions = 0;
struct faction *f = NULL;
if (filename) {
F = cfopen(filename, "rb");
}
F = fopen(filename, "rb");
if (!F) {
perror(filename);
return -1;
}
if (verbosity >= 1)
puts(" - lese Befehlsdatei...\n");
@ -679,13 +551,16 @@ int current_turn(void)
{
char zText[MAX_PATH];
int cturn = 0;
FILE *f;
FILE *F;
sprintf(zText, "%s/turn", basepath());
f = cfopen(zText, "r");
if (f) {
fscanf(f, "%d\n", &cturn);
fclose(f);
F = fopen(zText, "r");
if (!F) {
perror(zText);
}
else {
fscanf(F, "%d\n", &cturn);
fclose(F);
}
return cturn;
}

View File

@ -28,14 +28,11 @@ extern "C" {
/* Nach MAX_INPUT_SIZE brechen wir das Einlesen der Zeile ab und nehmen an,
* dass hier ein Fehler (fehlende ") vorliegt */
FILE *cfopen(const char *filename, const char *mode);
int readorders(const char *filename);
int creategame(void);
extern int readgame(const char *filename, int mode, int backup);
int writegame(const char *filename, int mode);
extern void rsf(FILE * F, char *s, size_t len);
/* Versionsänderungen: */
extern int data_version;
extern const char *game_name;
@ -70,9 +67,6 @@ extern "C" {
struct storage *store);
extern void a_finalizestring(struct attrib *a);
extern int freadstr(FILE * F, int encoding, char *str, size_t size);
extern int fwritestr(FILE * F, const char *str);
extern void create_backup(char *file);
#ifdef __cplusplus

View File

@ -12,16 +12,135 @@ without prior permission by the authors of Eressea.
#include "save.h"
#include "version.h"
#include <util/unicode.h>
#include <util/base36.h>
#include <util/log.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/encoding.h>
#define NULL_TOKEN '@'
/** writes a quoted string to the file
* no trailing space, since this is used to make the creport.
*/
static int fwritestr(FILE * F, const char *str)
{
int nwrite = 0;
fputc('\"', F);
if (str)
while (*str) {
int c = (int)(unsigned char)*str++;
switch (c) {
case '"':
case '\\':
fputc('\\', F);
fputc(c, F);
nwrite += 2;
break;
case '\n':
fputc('\\', F);
fputc('n', F);
nwrite += 2;
break;
default:
fputc(c, F);
++nwrite;
}
}
fputc('\"', F);
return nwrite + 2;
}
static int freadstr(FILE * F, int encoding, char *start, size_t size)
{
char *str = start;
bool quote = false;
for (;;) {
int c = fgetc(F);
if (isxspace(c)) {
if (str == start) {
continue;
}
if (!quote) {
*str = 0;
return (int)(str - start);
}
}
switch (c) {
case EOF:
return EOF;
case '"':
if (!quote && str != start) {
log_error(
("datafile contains a \" that isn't at the start of a string.\n"));
assert
(!"datafile contains a \" that isn't at the start of a string.\n");
}
if (quote) {
*str = 0;
return (int)(str - start);
}
quote = true;
break;
case '\\':
c = fgetc(F);
switch (c) {
case EOF:
return EOF;
case 'n':
if ((size_t)(str - start + 1) < size) {
*str++ = '\n';
}
break;
default:
if ((size_t)(str - start + 1) < size) {
if (encoding == XML_CHAR_ENCODING_8859_1 && c & 0x80) {
char inbuf = (char)c;
size_t inbytes = 1;
size_t outbytes = size - (str - start);
int ret = unicode_latin1_to_utf8(str, &outbytes, &inbuf, &inbytes);
if (ret > 0)
str += ret;
else {
log_error("input data was not iso-8859-1! assuming utf-8\n");
encoding = XML_CHAR_ENCODING_ERROR;
*str++ = (char)c;
}
}
else {
*str++ = (char)c;
}
}
}
break;
default:
if ((size_t)(str - start + 1) < size) {
if (encoding == XML_CHAR_ENCODING_8859_1 && c & 0x80) {
char inbuf = (char)c;
size_t inbytes = 1;
size_t outbytes = size - (str - start);
int ret = unicode_latin1_to_utf8(str, &outbytes, &inbuf, &inbytes);
if (ret > 0)
str += ret;
else {
log_error("input data was not iso-8859-1! assuming utf-8\n");
encoding = XML_CHAR_ENCODING_ERROR;
*str++ = (char)c;
}
}
else {
*str++ = (char)c;
}
}
}
}
}
static int txt_w_brk(struct storage *store)
{
putc('\n', (FILE *) store->userdata);
@ -90,7 +209,7 @@ static char *txt_r_tok(struct storage *store)
if (result[0] == NULL_TOKEN || result[0] == 0) {
return NULL;
}
return strdup(result);
return _strdup(result);
}
static void txt_r_tok_buf(struct storage *store, char *result, size_t size)
@ -121,7 +240,7 @@ static char *txt_r_str(struct storage *store)
char buffer[DISPLAYSIZE];
/* you should not use this */
freadstr((FILE *) store->userdata, store->encoding, buffer, sizeof(buffer));
return strdup(buffer);
return _strdup(buffer);
}
static void txt_r_str_buf(struct storage *store, char *result, size_t size)

View File

@ -14,7 +14,7 @@ without prior permission by the authors of Eressea.
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
extern void log_open(const char *filename);
extern void log_close(void);
extern void log_flush(void);