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