X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=client%2Ffhistory.c;h=202593958a170d5da6b42f240918e6c9efef18c7;hp=b75c03d8725685fae6e200edac7eeac0174f9f59;hb=bc4b50939a936dbb214c783c18a44039ea048fdd;hpb=d42f045690a68380de575447bf9755f922ec734d diff --git a/client/fhistory.c b/client/fhistory.c index b75c03d..2025939 100644 --- a/client/fhistory.c +++ b/client/fhistory.c @@ -1,14 +1,13 @@ -/* - * Copyright (C) 1995-2007, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2008 Index Data * See the file LICENSE for details. - * - * $Id: fhistory.c,v 1.2 2007-01-24 23:09:48 adam Exp $ */ /** \file fhistory.c * \brief file history implementation */ #include +#include #include #include #include @@ -26,7 +25,7 @@ struct file_history { file_history_t file_history_new() { - file_history_t fh = xmalloc(sizeof(*fh)); + file_history_t fh = (file_history_t) xmalloc(sizeof(*fh)); fh->wr = wrbuf_alloc(); return fh; } @@ -69,6 +68,8 @@ int file_history_load(file_history_t fh) return ret; } +#define FILE_SAVE_HISTORY_MAX 16384 + int file_history_save(file_history_t fh) { FILE *f; @@ -89,7 +90,21 @@ int file_history_save(file_history_t fh) } else { - size_t w = fwrite(wrbuf_buf(fh->wr), 1, sz, f); + size_t w; + const char *start = wrbuf_buf(fh->wr); + if (sz > FILE_SAVE_HISTORY_MAX) + { + const char *nl = strchr( + wrbuf_buf(fh->wr) + sz - FILE_SAVE_HISTORY_MAX, + '\n'); + if (nl) + { + nl++; + sz = sz - (nl - start); + start = nl; + } + } + w = fwrite(start, 1, sz, f); if (w != sz) ret = -1; if (fclose(f)) @@ -111,7 +126,7 @@ int file_history_trav(file_history_t fh, void *client_data, if (wrbuf_buf(fh->wr)[i] == '\n') { wrbuf_buf(fh->wr)[i] = '\0'; - callback(client_data, wrbuf_buf(fh->wr) + off); + callback(client_data, wrbuf_cstr(fh->wr) + off); wrbuf_buf(fh->wr)[i] = '\n'; i++; break;