Commit 6de208fe authored by Per Cederqvist's avatar Per Cederqvist

Implement dbfile wrappers for putc, fputs, feof and ferror, and

use them.
* src/server/ram-io.c, src/server/ram-io.h
(dbfile_putc): New function.
(dbfile_fputs): New function.
(dbfile_feof): New function.
(dbfile_ferror): New function.
* src/server/simple-cache.c (save_one_text): Use dbfile_ferror.
(sync_part): Ditto.
* src/server/ram-output.c (foutput_string): Use dbfile_putc.
(foutput_ulong): Ditto.
(foutput_atsign): Ditto.
(foutput_newline): Ditto.
(foutput_space): Ditto.
(foutput_bool): Ditto.
(foutput_array_start): Use dbfile_fputs.
(foutput_array_end): Ditto.
(foutput_array_nodata): Ditto.
* src/server/ram-io.c (dbfile_change_magic): Use dbfile_ferror.
* src/server/local-to-global.c (l2g_write): Use dbfile_putc.
* src/server/dbck-cache.c (init_cache): Use dbfile_feof().
parent 39b34b6a
2007-07-03 Per Cederqvist <ceder@lysator.liu.se>
Implement dbfile wrappers for putc, fputs, feof and ferror, and
use them.
* src/server/ram-io.c, src/server/ram-io.h
(dbfile_putc): New function.
(dbfile_fputs): New function.
(dbfile_feof): New function.
(dbfile_ferror): New function.
* src/server/simple-cache.c (save_one_text): Use dbfile_ferror.
(sync_part): Ditto.
* src/server/ram-output.c (foutput_string): Use dbfile_putc.
(foutput_ulong): Ditto.
(foutput_atsign): Ditto.
(foutput_newline): Ditto.
(foutput_space): Ditto.
(foutput_bool): Ditto.
(foutput_array_start): Use dbfile_fputs.
(foutput_array_end): Ditto.
(foutput_array_nodata): Ditto.
* src/server/ram-io.c (dbfile_change_magic): Use dbfile_ferror.
* src/server/local-to-global.c (l2g_write): Use dbfile_putc.
* src/server/dbck-cache.c (init_cache): Use dbfile_feof().
Let dbfile_open_write() write the file header. Extract the code
that changes the state to dbfile_change_magic(). Reduce code
duplication by teaching foutput_header() to handle old file
......
......@@ -908,7 +908,7 @@ init_cache(void)
*/
for ( i = 1;
(fp->format == 0 && i < next_free_num && !feof(fp->fp)) ||
(fp->format == 0 && i < next_free_num && !dbfile_feof(fp)) ||
(fp->format == 1 && !done) ||
(fp->format == 2 && !done) ||
(fp->format == 3 && !done);
......
......@@ -1275,7 +1275,7 @@ l2g_write(struct dbfile *fp, const Local_to_global *l2g)
Text_no val;
Local_text_no current = 0;
putc('[', fp->fp);
dbfile_putc('[', fp);
put_ulong(l2g->first_unused, fp->fp);
for (binfo = l2g->blocks; binfo < l2g->blocks + l2g->num_blocks; ++binfo)
......@@ -1285,9 +1285,9 @@ l2g_write(struct dbfile *fp, const Local_to_global *l2g)
key = key_value(binfo, ix);
if (key > current + 3 || current == 0)
{
putc(' ', fp->fp);
dbfile_putc(' ', fp);
put_ulong(key, fp->fp);
putc(':', fp->fp);
dbfile_putc(':', fp);
put_ulong(val, fp->fp);
current = key;
}
......@@ -1295,15 +1295,15 @@ l2g_write(struct dbfile *fp, const Local_to_global *l2g)
{
while (++current < key)
{
putc(',', fp->fp);
putc('0', fp->fp);
dbfile_putc(',', fp);
dbfile_putc('0', fp);
}
putc(',', fp->fp);
dbfile_putc(',', fp);
put_ulong(val, fp->fp);
}
}
putc(']', fp->fp);
dbfile_putc(']', fp);
}
......
......@@ -163,7 +163,7 @@ dbfile_change_magic(struct dbfile *fp,
const char *magic)
{
rewind(fp->fp);
if (ferror(fp->fp) != 0)
if (dbfile_ferror(fp) != 0)
{
kom_log("dbfile_change_magic: rewind failed.\n");
return FAILURE;
......@@ -171,7 +171,7 @@ dbfile_change_magic(struct dbfile *fp,
foutput_header(fp, magic, 0);
if (ferror(fp->fp) != 0)
if (dbfile_ferror(fp) != 0)
{
kom_log("dbfile_change_magic: writing new header %s failed.\n",
magic);
......@@ -184,7 +184,7 @@ dbfile_change_magic(struct dbfile *fp,
return FAILURE;
}
if (ferror(fp->fp) != 0)
if (dbfile_ferror(fp) != 0)
{
kom_log("dbfile_change_magic(): ferror after fflush failed.\n");
return FAILURE;
......@@ -214,9 +214,35 @@ dbfile_getc(struct dbfile *fp)
return getc(fp->fp);
}
int
dbfile_putc(int c,
struct dbfile *fp)
{
return putc(c, fp->fp);
}
int
dbfile_fputs(const char *s,
struct dbfile *fp)
{
return fputs(s, fp->fp);
}
int
dbfile_ungetc(struct dbfile *fp,
int c)
{
return ungetc(c, fp->fp);
}
int
dbfile_feof(struct dbfile *fp)
{
return feof(fp->fp);
}
int
dbfile_ferror(struct dbfile *fp)
{
return ferror(fp->fp);
}
......@@ -72,6 +72,23 @@ dbfile_ftell(struct dbfile *fp);
int
dbfile_getc(struct dbfile *fp);
/* Call fputs() on the file. */
int
dbfile_fputs(const char *s,
struct dbfile *fp);
/* Call putc() on the file. */
int
dbfile_putc(int c, struct dbfile *fp);
/* Call ungetc() on the file. */
int
dbfile_ungetc(struct dbfile *fp, int c);
/* Call feof() on the file. */
int
dbfile_feof(struct dbfile *fp);
/* Call ferror() on the file. */
int
dbfile_ferror(struct dbfile *fp);
......@@ -617,7 +617,7 @@ foutput_string(struct dbfile *fp,
String str)
{
foutput_ulong((unsigned long)str.len, fp);
putc('H', fp->fp);
dbfile_putc('H', fp);
if (str.len)
fwrite(str.string, str.len, 1, fp->fp);
......@@ -948,7 +948,7 @@ foutput_ulong (unsigned long l,
foutput_space(fp);
if (l < 10)
putc("0123456789"[l], fp->fp);
dbfile_putc("0123456789"[l], fp);
else
{
cp = buf + sizeof(buf);
......@@ -964,41 +964,41 @@ foutput_ulong (unsigned long l,
void
foutput_atsign(struct dbfile *fp)
{
putc('@', fp->fp);
dbfile_putc('@', fp);
}
void
foutput_newline(struct dbfile *fp)
{
putc('\n', fp->fp);
dbfile_putc('\n', fp);
}
void
foutput_space(struct dbfile *fp)
{
putc(' ', fp->fp);
dbfile_putc(' ', fp);
}
void
foutput_array_start(struct dbfile *fp)
{
fputs(" {", fp->fp);
dbfile_fputs(" {", fp);
}
void
foutput_array_end(struct dbfile *fp)
{
fputs(" }", fp->fp);
dbfile_fputs(" }", fp);
}
void
foutput_array_nodata(struct dbfile *fp)
{
fputs(" *", fp->fp);
dbfile_fputs(" *", fp);
}
void
foutput_bool(struct dbfile *fp, int val)
{
putc(val + '0', fp->fp);
dbfile_putc(val + '0', fp);
}
......@@ -2383,7 +2383,7 @@ save_one_text(void)
if (sync_next == highest_text_no)
#endif
{
if ( ferror(file_b->fp) != 0 )
if ( dbfile_ferror(file_b) != 0 )
{
kom_log ("save_one_text(): ferror() detected.\n");
sync_state = sync_error;
......@@ -2569,7 +2569,7 @@ sync_part(void)
restart_kom("sync(): sync_state==%d", sync_state);
}
if ( file_b != NULL && ferror(file_b->fp) != 0)
if ( file_b != NULL && dbfile_ferror(file_b) != 0)
sync_state = sync_error;
#ifdef LOGACCESSES
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment