2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: fhistory.c,v 1.2 2007-01-24 23:09:48 adam Exp $
8 * \brief file history implementation
17 #include <sys/types.h>
27 file_history_t file_history_new()
29 file_history_t fh = xmalloc(sizeof(*fh));
30 fh->wr = wrbuf_alloc();
34 void file_history_destroy(file_history_t *fhp)
38 wrbuf_destroy((*fhp)->wr);
44 void file_history_add_line(file_history_t fh, const char *line)
46 wrbuf_puts(fh->wr, line);
47 wrbuf_puts(fh->wr, "\n");
50 int file_history_load(file_history_t fh)
53 char* homedir = getenv("HOME");
58 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
59 homedir ? "/" : "", ".yazclient.history");
61 f = fopen(fname, "r");
65 while ((c = fgetc(f)) != EOF)
66 wrbuf_putc(fh->wr, c);
72 int file_history_save(file_history_t fh)
75 char* homedir = getenv("HOME");
78 int sz = wrbuf_len(fh->wr);
82 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
83 homedir ? "/" : "", ".yazclient.history");
85 f = fopen(fname, "w");
92 size_t w = fwrite(wrbuf_buf(fh->wr), 1, sz, f);
101 int file_history_trav(file_history_t fh, void *client_data,
102 void (*callback)(void *client_data, const char *line))
106 while (off < wrbuf_len(fh->wr))
109 for (i = off; i < wrbuf_len(fh->wr); i++)
111 if (wrbuf_buf(fh->wr)[i] == '\n')
113 wrbuf_buf(fh->wr)[i] = '\0';
114 callback(client_data, wrbuf_buf(fh->wr) + off);
115 wrbuf_buf(fh->wr)[i] = '\n';
128 * indent-tabs-mode: nil
130 * vim: shiftwidth=4 tabstop=8 expandtab