coding style fix
[ladish.git] / daemon / recent_store.c
blob8fc09d7fdb3e69d5bb8f385ea256afb7a632de4d
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
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>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
32 #include "recent_store.h"
33 #include "save.h"
35 struct ladish_recent_store
37 char * path;
38 unsigned int max_items;
39 char ** items;
42 static
43 void
44 ladish_recent_store_save(
45 struct ladish_recent_store * store_ptr)
47 unsigned int i;
48 int fd;
50 fd = open(store_ptr->path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
51 if (fd == -1)
53 log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
54 return;
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);
62 break;
65 if (!ladish_write_string(fd, "\n"))
67 log_error("write to file '%s' failed", store_ptr->path);
68 break;
72 close(fd);
75 static
76 void
77 ladish_recent_store_load(
78 struct ladish_recent_store * store_ptr)
80 int fd;
81 struct stat st;
82 char * buffer;
83 size_t buffer_size;
84 ssize_t ret;
85 char * temp_ptr;
86 unsigned int i;
88 fd = open(store_ptr->path, O_RDONLY);
89 if (fd == -1)
91 if (errno != ENOENT)
93 log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
96 goto exit;
99 if (fstat(fd, &st) != 0)
101 log_error("fstat(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
102 goto close;
105 buffer_size = (size_t)st.st_size;
106 buffer = malloc(buffer_size + 1);
107 if (buffer == NULL)
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);
110 goto close;
113 ret = read(fd, buffer, buffer_size);
114 if (ret == -1)
116 log_error("read(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
117 goto free;
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);
124 goto free;
127 buffer[buffer_size] = 0;
129 /* read lines and store them in the item array */
130 temp_ptr = strtok(buffer, "\n");
131 i = 0;
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);
140 goto free;
143 i++;
144 temp_ptr = strtok(NULL, "\n");
147 free:
148 free(buffer);
149 close:
150 close(fd);
151 exit:
152 return;
155 bool
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;
162 unsigned int i;
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");
168 goto fail;
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);
175 goto free_store;
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);
182 goto free_path;
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;
197 return true;
199 free_path:
200 free(store_ptr->path);
201 free_store:
202 free(store_ptr);
203 fail:
204 return false;
207 #define store_ptr ((struct ladish_recent_store *)store_handle)
209 void
210 ladish_recent_store_destroy(
211 ladish_recent_store_handle store_handle)
213 unsigned int i;
215 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
217 free(store_ptr->items[i]);
220 free(store_ptr->items);
221 free(store_ptr->path);
222 free(store_ptr);
225 void
226 ladish_recent_store_use_item(
227 ladish_recent_store_handle store_handle,
228 const char * item_const)
230 char * item;
231 char * item_save;
232 unsigned int i;
234 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
236 if (strcmp(store_ptr->items[i], item_const) == 0)
237 { /* already known item */
239 /* if the item is already the most recent one, there is nothing to do */
240 if (i != 0)
242 item = store_ptr->items[i];
243 store_ptr->items[i] = NULL; /* mark end for reorder loop */
244 goto reorder;
247 return;
251 /* new item */
252 item = strdup(item_const);
253 if (item == NULL)
255 log_error("strdup() failed for recent item '%s'", item_const);
256 return;
259 reorder:
260 /* we have valid non-NULL item pointer before the iteration begins */
261 /* the item pointer is checked for NULL because that means either
262 end of used items or a mark created when already known item
263 was removed from the array */
264 for (i = 0; i < store_ptr->max_items && item != NULL; i++)
266 item_save = store_ptr->items[i];
267 store_ptr->items[i] = item;
268 item = item_save;
271 if (item != NULL)
272 { /* eventually free the oldest item */
273 free(item);
276 ladish_recent_store_save(store_ptr);
279 bool
280 ladish_recent_store_check_known(
281 ladish_recent_store_handle store_handle,
282 const char * item)
284 unsigned int i;
286 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
288 if (strcmp(store_ptr->items[i], item) == 0)
290 return true;
294 return false;
297 void
298 ladish_recent_store_iterate_items(
299 ladish_recent_store_handle store_handle,
300 void * callback_context,
301 bool (* callback)(void * callback_context, const char * item))
303 unsigned int i;
305 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
307 if (!callback(callback_context, store_ptr->items[i]))
309 break;
314 #undef store_ptr