1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
6 * \brief file history implementation
15 #include <sys/types.h>
25 file_history_t file_history_new()
27 file_history_t fh = (file_history_t) xmalloc(sizeof(*fh));
28 fh->wr = wrbuf_alloc();
32 void file_history_destroy(file_history_t *fhp)
36 wrbuf_destroy((*fhp)->wr);
42 void file_history_add_line(file_history_t fh, const char *line)
44 wrbuf_puts(fh->wr, line);
45 wrbuf_puts(fh->wr, "\n");
48 int file_history_load(file_history_t fh)
51 char* homedir = getenv("HOME");
56 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
57 homedir ? "/" : "", ".yazclient.history");
59 f = fopen(fname, "r");
63 while ((c = fgetc(f)) != EOF)
64 wrbuf_putc(fh->wr, c);
70 int file_history_save(file_history_t fh)
73 char* homedir = getenv("HOME");
76 int sz = wrbuf_len(fh->wr);
80 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
81 homedir ? "/" : "", ".yazclient.history");
83 f = fopen(fname, "w");
90 size_t w = fwrite(wrbuf_buf(fh->wr), 1, sz, f);
99 int file_history_trav(file_history_t fh, void *client_data,
100 void (*callback)(void *client_data, const char *line))
104 while (off < wrbuf_len(fh->wr))
107 for (i = off; i < wrbuf_len(fh->wr); i++)
109 if (wrbuf_buf(fh->wr)[i] == '\n')
111 wrbuf_buf(fh->wr)[i] = '\0';
112 callback(client_data, wrbuf_cstr(fh->wr) + off);
113 wrbuf_buf(fh->wr)[i] = '\n';
126 * indent-tabs-mode: nil
128 * vim: shiftwidth=4 tabstop=8 expandtab