1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
6 * \brief file history implementation
19 #include <sys/types.h>
29 file_history_t file_history_new()
31 file_history_t fh = (file_history_t) xmalloc(sizeof(*fh));
32 fh->wr = wrbuf_alloc();
36 void file_history_destroy(file_history_t *fhp)
40 wrbuf_destroy((*fhp)->wr);
46 void file_history_add_line(file_history_t fh, const char *line)
48 wrbuf_puts(fh->wr, line);
49 wrbuf_puts(fh->wr, "\n");
52 int file_history_load(file_history_t fh)
55 char* homedir = getenv("HOME");
60 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
61 homedir ? "/" : "", ".yazclient.history");
63 f = fopen(fname, "r");
67 while ((c = fgetc(f)) != EOF)
68 wrbuf_putc(fh->wr, c);
74 #define FILE_SAVE_HISTORY_MAX 16384
76 int file_history_save(file_history_t fh)
79 char* homedir = getenv("HOME");
82 size_t sz = wrbuf_len(fh->wr);
86 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
87 homedir ? "/" : "", ".yazclient.history");
89 f = fopen(fname, "w");
97 const char *start = wrbuf_buf(fh->wr);
98 if (sz > FILE_SAVE_HISTORY_MAX)
100 const char *nl = strchr(
101 wrbuf_buf(fh->wr) + sz - FILE_SAVE_HISTORY_MAX,
106 sz = sz - (nl - start);
110 w = fwrite(start, 1, sz, f);
119 int file_history_trav(file_history_t fh, void *client_data,
120 void (*callback)(void *client_data, const char *line))
124 while (off < wrbuf_len(fh->wr))
127 for (i = off; i < wrbuf_len(fh->wr); i++)
129 if (wrbuf_buf(fh->wr)[i] == '\n')
131 wrbuf_buf(fh->wr)[i] = '\0';
132 callback(client_data, wrbuf_cstr(fh->wr) + off);
133 wrbuf_buf(fh->wr)[i] = '\n';
146 * c-file-style: "Stroustrup"
147 * indent-tabs-mode: nil
149 * vim: shiftwidth=4 tabstop=8 expandtab