1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
6 * \brief file history implementation
16 #include <sys/types.h>
26 file_history_t file_history_new()
28 file_history_t fh = (file_history_t) xmalloc(sizeof(*fh));
29 fh->wr = wrbuf_alloc();
33 void file_history_destroy(file_history_t *fhp)
37 wrbuf_destroy((*fhp)->wr);
43 void file_history_add_line(file_history_t fh, const char *line)
45 wrbuf_puts(fh->wr, line);
46 wrbuf_puts(fh->wr, "\n");
49 int file_history_load(file_history_t fh)
52 char* homedir = getenv("HOME");
57 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
58 homedir ? "/" : "", ".yazclient.history");
60 f = fopen(fname, "r");
64 while ((c = fgetc(f)) != EOF)
65 wrbuf_putc(fh->wr, c);
71 #define FILE_SAVE_HISTORY_MAX 16384
73 int file_history_save(file_history_t fh)
76 char* homedir = getenv("HOME");
79 size_t sz = wrbuf_len(fh->wr);
83 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
84 homedir ? "/" : "", ".yazclient.history");
86 f = fopen(fname, "w");
94 const char *start = wrbuf_buf(fh->wr);
95 if (sz > FILE_SAVE_HISTORY_MAX)
97 const char *nl = strchr(
98 wrbuf_buf(fh->wr) + sz - FILE_SAVE_HISTORY_MAX,
103 sz = sz - (nl - start);
107 w = fwrite(start, 1, sz, f);
116 int file_history_trav(file_history_t fh, void *client_data,
117 void (*callback)(void *client_data, const char *line))
121 while (off < wrbuf_len(fh->wr))
124 for (i = off; i < wrbuf_len(fh->wr); i++)
126 if (wrbuf_buf(fh->wr)[i] == '\n')
128 wrbuf_buf(fh->wr)[i] = '\0';
129 callback(client_data, wrbuf_cstr(fh->wr) + off);
130 wrbuf_buf(fh->wr)[i] = '\n';
143 * indent-tabs-mode: nil
145 * vim: shiftwidth=4 tabstop=8 expandtab