1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the recent items store
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
32 #include "recent_store.h"
35 struct ladish_recent_store
38 unsigned int max_items
;
44 ladish_recent_store_save(
45 struct ladish_recent_store
* store_ptr
)
50 fd
= open(store_ptr
->path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
53 log_error("open(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
57 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
59 if (!ladish_write_string(fd
, store_ptr
->items
[i
]))
61 log_error("write to file '%s' failed", store_ptr
->path
);
65 if (!ladish_write_string(fd
, "\n"))
67 log_error("write to file '%s' failed", store_ptr
->path
);
77 ladish_recent_store_load(
78 struct ladish_recent_store
* store_ptr
)
88 fd
= open(store_ptr
->path
, O_RDONLY
);
93 log_error("open(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
99 if (fstat(fd
, &st
) != 0)
101 log_error("fstat(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
105 buffer_size
= (size_t)st
.st_size
;
106 buffer
= malloc(buffer_size
+ 1);
109 log_error("malloc() failed to allocate %zy byte buffer for reading the contents of the recent items file '%s'", buffer_size
+ 1, store_ptr
->path
);
113 ret
= read(fd
, buffer
, buffer_size
);
116 log_error("read(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
120 if (ret
< (size_t)buffer_size
)
122 /* this could be handled better but we dont care because it is expected for the file to be small enough */
123 log_error("read(%s) returned less bytes than requested", store_ptr
->path
);
127 buffer
[buffer_size
] = 0;
129 /* read lines and store them in the item array */
130 temp_ptr
= strtok(buffer
, "\n");
132 while (temp_ptr
!= NULL
&& i
< store_ptr
->max_items
)
134 ASSERT(store_ptr
->items
[i
] == NULL
); /* filling an empty array */
136 store_ptr
->items
[i
] = strdup(temp_ptr
);
137 if (store_ptr
->items
[i
] == NULL
)
139 log_error("strdup(\"%s\") failed.", temp_ptr
);
144 temp_ptr
= strtok(NULL
, "\n");
156 ladish_recent_store_create(
157 const char * file_path
,
158 unsigned int max_items
,
159 ladish_recent_store_handle
* store_handle_ptr
)
161 struct ladish_recent_store
* store_ptr
;
164 store_ptr
= malloc(sizeof(struct ladish_recent_store
));
165 if (store_ptr
== NULL
)
167 log_error("malloc() failed to allocate a ladish_recent_store struct");
171 store_ptr
->path
= strdup(file_path
);
172 if (store_ptr
->path
== NULL
)
174 log_error("strdup() failed for recent store path '%s'", file_path
);
178 store_ptr
->items
= malloc(sizeof(char *) * max_items
);
179 if (store_ptr
->items
== NULL
)
181 log_error("malloc() failed to array of %u pointers", max_items
);
185 for (i
= 0; i
< max_items
; i
++)
187 store_ptr
->items
[i
] = NULL
;
190 store_ptr
->max_items
= max_items
;
192 /* try to load items from file */
193 ladish_recent_store_load(store_ptr
);
195 *store_handle_ptr
= (ladish_recent_store_handle
)store_ptr
;
200 free(store_ptr
->path
);
207 #define store_ptr ((struct ladish_recent_store *)store_handle)
210 ladish_recent_store_destroy(
211 ladish_recent_store_handle store_handle
)
213 free(store_ptr
->items
);
214 free(store_ptr
->path
);
219 ladish_recent_store_use_item(
220 ladish_recent_store_handle store_handle
,
221 const char * item_const
)
227 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
229 if (strcmp(store_ptr
->items
[i
], item_const
) == 0)
230 { /* already known item */
232 /* if the item is already the most recent one, there is nothing to do */
235 item
= store_ptr
->items
[i
];
236 store_ptr
->items
[i
] = NULL
; /* mark end for reorder loop */
245 item
= strdup(item_const
);
248 log_error("strdup() failed for recent item '%s'", item_const
);
253 /* we have valid non-NULL item pointer before the iteration begins */
254 /* the item pointer is checked for NULL because that means either
255 end of used items or a mark created when already known item
256 was removed from the array */
257 for (i
= 0; i
< store_ptr
->max_items
&& item
!= NULL
; i
++)
259 item_save
= store_ptr
->items
[i
];
260 store_ptr
->items
[i
] = item
;
265 { /* eventually free the oldest item */
269 ladish_recent_store_save(store_ptr
);
273 ladish_recent_store_check_known(
274 ladish_recent_store_handle store_handle
,
279 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
281 if (strcmp(store_ptr
->items
[i
], item
) == 0)
291 ladish_recent_store_iterate_items(
292 ladish_recent_store_handle store_handle
,
293 void * callback_context
,
294 bool (* callback
)(void * callback_context
, const char * item
))
298 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
300 if (!callback(callback_context
, store_ptr
->items
[i
]))