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
);
91 log_error("open(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
95 if (fstat(fd
, &st
) != 0)
97 log_error("fstat(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
101 buffer_size
= (size_t)st
.st_size
;
102 buffer
= malloc(buffer_size
+ 1);
105 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
);
109 ret
= read(fd
, buffer
, buffer_size
);
112 log_error("read(%s) failed: %d (%s)", store_ptr
->path
, errno
, strerror(errno
));
116 if (ret
< (size_t)buffer_size
)
118 /* this could be handled better but we dont care because it is expected for the file to be small enough */
119 log_error("read(%s) returned less bytes than requested", store_ptr
->path
);
123 buffer
[buffer_size
] = 0;
125 /* read lines and store them in the item array */
126 temp_ptr
= strtok(buffer
, "\n");
128 while (temp_ptr
!= NULL
&& i
< store_ptr
->max_items
)
130 ASSERT(store_ptr
->items
[i
] == NULL
); /* filling an empty array */
132 store_ptr
->items
[i
] = strdup(temp_ptr
);
133 if (store_ptr
->items
[i
] == NULL
)
135 log_error("strdup(\"%s\") failed.", temp_ptr
);
140 temp_ptr
= strtok(NULL
, "\n");
152 ladish_recent_store_create(
153 const char * file_path
,
154 unsigned int max_items
,
155 ladish_recent_store_handle
* store_handle_ptr
)
157 struct ladish_recent_store
* store_ptr
;
160 store_ptr
= malloc(sizeof(struct ladish_recent_store
));
161 if (store_ptr
== NULL
)
163 log_error("malloc() failed to allocate a ladish_recent_store struct");
167 store_ptr
->path
= strdup(file_path
);
168 if (store_ptr
->path
== NULL
)
170 log_error("strdup() failed for recent store path '%s'", file_path
);
174 store_ptr
->items
= malloc(sizeof(char *) * max_items
);
175 if (store_ptr
->items
== NULL
)
177 log_error("malloc() failed to array of %u pointers", max_items
);
181 for (i
= 0; i
< max_items
; i
++)
183 store_ptr
->items
[i
] = NULL
;
186 store_ptr
->max_items
= max_items
;
188 /* try to load items from file */
189 ladish_recent_store_load(store_ptr
);
191 *store_handle_ptr
= (ladish_recent_store_handle
)store_ptr
;
196 free(store_ptr
->path
);
203 #define store_ptr ((struct ladish_recent_store *)store_handle)
206 ladish_recent_store_destroy(
207 ladish_recent_store_handle store_handle
)
209 free(store_ptr
->items
);
210 free(store_ptr
->path
);
215 ladish_recent_store_use_item(
216 ladish_recent_store_handle store_handle
,
217 const char * item_const
)
223 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
225 if (strcmp(store_ptr
->items
[i
], item_const
) == 0)
226 { /* already known item */
228 /* if the item is already the most recent one, there is nothing to do */
231 item
= store_ptr
->items
[i
];
232 store_ptr
->items
[i
] = NULL
; /* mark end for reorder loop */
241 item
= strdup(item_const
);
244 log_error("strdup() failed for recent item '%s'", item_const
);
249 /* we have valid non-NULL item pointer before the iteration begins */
250 /* the item pointer is checked for NULL because that means either
251 end of used items or a mark created when already known item
252 was removed from the array */
253 for (i
= 0; i
< store_ptr
->max_items
&& item
!= NULL
; i
++)
255 item_save
= store_ptr
->items
[i
];
256 store_ptr
->items
[i
] = item
;
261 { /* eventually free the oldest item */
265 ladish_recent_store_save(store_ptr
);
269 ladish_recent_store_iterate_items(
270 ladish_recent_store_handle store_handle
,
271 void * callback_context
,
272 bool (* callback
)(void * callback_context
, const char * item
))
276 for (i
= 0; i
< store_ptr
->max_items
&& store_ptr
->items
[i
] != NULL
; i
++)
278 if (!callback(callback_context
, store_ptr
->items
[i
]))