7fb5f3f31bc16fe9d84320c97d0953f0893a965e
[yaz-moved-to-github.git] / client / fhistory.c
1 /* 
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: fhistory.c,v 1.3 2007-03-19 22:17:41 adam Exp $
6  */
7 /** \file fhistory.c
8  *  \brief file history implementation
9  */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <assert.h>
14 #include <time.h>
15 #include <ctype.h>
16 #if HAVE_SYS_TYPES_H
17 #include <sys/types.h>
18 #endif
19
20 #include "fhistory.h"
21
22
23 struct file_history {
24     WRBUF wr;
25 };
26
27 file_history_t file_history_new()
28 {
29     file_history_t fh = xmalloc(sizeof(*fh));
30     fh->wr = wrbuf_alloc();
31     return fh;
32 }
33
34 void file_history_destroy(file_history_t *fhp)
35 {
36     if (*fhp)
37     {
38         wrbuf_destroy((*fhp)->wr);
39         xfree(*fhp);
40         *fhp = 0;
41     }
42 }
43
44 void file_history_add_line(file_history_t fh, const char *line)
45 {
46     wrbuf_puts(fh->wr, line);
47     wrbuf_puts(fh->wr, "\n");
48 }
49
50 int file_history_load(file_history_t fh)
51 {
52     FILE *f;
53     char* homedir = getenv("HOME");
54     char fname[1024];
55     int ret = 0;
56
57     wrbuf_rewind(fh->wr);
58     sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
59             homedir ? "/" : "", ".yazclient.history");
60
61     f = fopen(fname, "r");
62     if (f)
63     {
64         int c;
65         while ((c = fgetc(f)) != EOF)
66             wrbuf_putc(fh->wr, c);
67         fclose(f);
68     }
69     return ret;
70 }
71
72 int file_history_save(file_history_t fh)
73 {
74     FILE *f;
75     char* homedir = getenv("HOME");
76     char fname[1024];
77     int ret = 0;
78     int sz = wrbuf_len(fh->wr);
79
80     if (!sz)
81         return 0;
82     sprintf(fname, "%.500s%s%s", homedir ? homedir : "",
83             homedir ? "/" : "", ".yazclient.history");
84
85     f = fopen(fname, "w");
86     if (!f)
87     {
88         ret = -1;
89     }
90     else
91     {
92         size_t w = fwrite(wrbuf_buf(fh->wr), 1, sz, f);
93         if (w != sz)
94             ret = -1;
95         if (fclose(f))
96             ret = -1;
97     }
98     return ret;
99 }
100
101 int file_history_trav(file_history_t fh, void *client_data,
102                       void (*callback)(void *client_data, const char *line))
103 {
104     int off = 0;
105
106     while (off < wrbuf_len(fh->wr))
107     {
108         int i;
109         for (i = off; i < wrbuf_len(fh->wr); i++)
110         {
111             if (wrbuf_buf(fh->wr)[i] == '\n')
112             {
113                 wrbuf_buf(fh->wr)[i] = '\0';
114                 callback(client_data, wrbuf_cstr(fh->wr) + off);
115                 wrbuf_buf(fh->wr)[i] = '\n';
116                 i++;
117                 break;
118             }
119         }
120         off = i;
121     }
122     return 0;
123 }
124
125 /*
126  * Local variables:
127  * c-basic-offset: 4
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132