1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
6 * \brief file history implementation
18 #include <sys/types.h>
28 file_history_t file_history_new()
30 file_history_t fh = (file_history_t) xmalloc(sizeof(*fh));
31 fh->wr = wrbuf_alloc();
35 void file_history_destroy(file_history_t *fhp)
39 wrbuf_destroy((*fhp)->wr);
45 void file_history_add_line(file_history_t fh, const char *line)
47 wrbuf_puts(fh->wr, line);
48 wrbuf_puts(fh->wr, "\n");
51 int file_history_load(file_history_t fh)
54 char* homedir = getenv("HOME");
59 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
60 homedir ? "/" : "", ".yazclient.history");
62 f = fopen(fname, "r");
66 while ((c = fgetc(f)) != EOF)
67 wrbuf_putc(fh->wr, c);
73 #define FILE_SAVE_HISTORY_MAX 16384
75 int file_history_save(file_history_t fh)
78 char* homedir = getenv("HOME");
81 size_t sz = wrbuf_len(fh->wr);
85 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
86 homedir ? "/" : "", ".yazclient.history");
88 f = fopen(fname, "w");
96 const char *start = wrbuf_buf(fh->wr);
97 if (sz > FILE_SAVE_HISTORY_MAX)
99 const char *nl = strchr(
100 wrbuf_buf(fh->wr) + sz - FILE_SAVE_HISTORY_MAX,
105 sz = sz - (nl - start);
109 w = fwrite(start, 1, sz, f);
118 int file_history_trav(file_history_t fh, void *client_data,
119 void (*callback)(void *client_data, const char *line))
123 while (off < wrbuf_len(fh->wr))
126 for (i = off; i < wrbuf_len(fh->wr); i++)
128 if (wrbuf_buf(fh->wr)[i] == '\n')
130 wrbuf_buf(fh->wr)[i] = '\0';
131 callback(client_data, wrbuf_cstr(fh->wr) + off);
132 wrbuf_buf(fh->wr)[i] = '\n';
145 * c-file-style: "Stroustrup"
146 * indent-tabs-mode: nil
148 * vim: shiftwidth=4 tabstop=8 expandtab