2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: fhistory.c,v 1.1 2007-01-24 11:50:18 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");
79 sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
80 homedir ? "/" : "", ".yazclient.history");
82 f = fopen(fname, "w");
89 size_t w = fwrite(wrbuf_buf(fh->wr), 1, wrbuf_len(fh->wr), f);
90 if (w != wrbuf_len(fh->wr))
98 int file_history_trav(file_history_t fh, void *client_data,
99 void (*callback)(void *client_data, const char *line))
103 while (off < wrbuf_len(fh->wr))
106 for (i = off; i < wrbuf_len(fh->wr); i++)
108 if (wrbuf_buf(fh->wr)[i] == '\n')
110 wrbuf_buf(fh->wr)[i] = '\0';
111 callback(client_data, wrbuf_buf(fh->wr) + off);
112 wrbuf_buf(fh->wr)[i] = '\n';
125 * indent-tabs-mode: nil
127 * vim: shiftwidth=4 tabstop=8 expandtab