X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=client%2Ffhistory.c;h=03081e5cf209e8a0a834b59867ee636d4cb079b6;hp=0020f76fc483ea5acb3bff672418a621b1669d5f;hb=01e36a11adc97a240cc595f89280c8a619770251;hpb=07dca8aeae6bc30c59e71b30a9ad83da57c0440d diff --git a/client/fhistory.c b/client/fhistory.c index 0020f76..03081e5 100644 --- a/client/fhistory.c +++ b/client/fhistory.c @@ -1,18 +1,19 @@ -/* - * Copyright (C) 1995-2007, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data * See the file LICENSE for details. - * - * $Id: fhistory.c,v 1.1 2007-01-24 11:50:18 adam Exp $ */ /** \file fhistory.c * \brief file history implementation */ +#if HAVE_CONFIG_H +#include +#endif #include +#include #include #include #include -#include #if HAVE_SYS_TYPES_H #include #endif @@ -26,7 +27,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,13 +70,18 @@ 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; char* homedir = getenv("HOME"); char fname[1024]; int ret = 0; + size_t sz = wrbuf_len(fh->wr); + if (!sz) + return 0; sprintf(fname, "%.500s%s%s", homedir ? homedir : "", homedir ? "/" : "", ".yazclient.history"); @@ -86,8 +92,22 @@ int file_history_save(file_history_t fh) } else { - size_t w = fwrite(wrbuf_buf(fh->wr), 1, wrbuf_len(fh->wr), f); - if (w != wrbuf_len(fh->wr)) + 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)) ret = -1; @@ -98,17 +118,17 @@ int file_history_save(file_history_t fh) int file_history_trav(file_history_t fh, void *client_data, void (*callback)(void *client_data, const char *line)) { - int off = 0; + size_t off = 0; while (off < wrbuf_len(fh->wr)) { - int i; + size_t i; for (i = off; i < wrbuf_len(fh->wr); i++) { 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; @@ -122,6 +142,7 @@ int file_history_trav(file_history_t fh, void *client_data, /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab