Add french translation to up-to-dating script
[ladish.git] / daemon / recent_store.c
blob64b9cb326e4b73a6b3c4f91b6041648cc4ff8220
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 free(store_ptr->items);
214 free(store_ptr->path);
215 free(store_ptr);
218 void
219 ladish_recent_store_use_item(
220 ladish_recent_store_handle store_handle,
221 const char * item_const)
223 char * item;
224 char * item_save;
225 unsigned int i;
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 */
233 if (i != 0)
235 item = store_ptr->items[i];
236 store_ptr->items[i] = NULL; /* mark end for reorder loop */
237 goto reorder;
240 return;
244 /* new item */
245 item = strdup(item_const);
246 if (item == NULL)
248 log_error("strdup() failed for recent item '%s'", item_const);
249 return;
252 reorder:
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;
261 item = item_save;
264 if (item != NULL)
265 { /* eventually free the oldest item */
266 free(item);
269 ladish_recent_store_save(store_ptr);
272 bool
273 ladish_recent_store_check_known(
274 ladish_recent_store_handle store_handle,
275 const char * item)
277 unsigned int i;
279 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
281 if (strcmp(store_ptr->items[i], item) == 0)
283 return true;
287 return false;
290 void
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))
296 unsigned int i;
298 for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
300 if (!callback(callback_context, store_ptr->items[i]))
302 break;
307 #undef store_ptr