File: recent_store.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (323 lines) | stat: -rw-r--r-- 7,379 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2010, 2011 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains implementation of the recent items store
 **************************************************************************
 *
 * LADI Session Handler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "recent_store.h"
#include "save.h"

struct ladish_recent_store
{
  char * path;
  unsigned int max_items;
  char ** items;
};

static
void
ladish_recent_store_save(
  struct ladish_recent_store * store_ptr)
{
  unsigned int i;
  int fd;

  fd = open(store_ptr->path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd == -1)
  {
    log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
    return;
  }

  for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
  {
    if (!ladish_write_string(fd, store_ptr->items[i]))
    {
      log_error("write to file '%s' failed", store_ptr->path);
      break;
    }

    if (!ladish_write_string(fd, "\n"))
    {
      log_error("write to file '%s' failed", store_ptr->path);
      break;
    }
  }

  close(fd);
}

static
void
ladish_recent_store_load(
  struct ladish_recent_store * store_ptr)
{
  int fd;
  struct stat st;
  char * buffer;
  size_t buffer_size;
  ssize_t ret;
  char * temp_ptr;
  unsigned int i;

  fd = open(store_ptr->path, O_RDONLY);
  if (fd == -1)
  {
    if (errno != ENOENT)
    {
      log_error("open(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
    }

    goto exit;
  }

  if (fstat(fd, &st) != 0)
  {
    log_error("fstat(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
    goto close;
  }

  buffer_size = (size_t)st.st_size;
  buffer = malloc(buffer_size + 1);
  if (buffer == NULL)
  {
    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);
    goto close;
  }

  ret = read(fd, buffer, buffer_size);
  if (ret == -1)
  {
    log_error("read(%s) failed: %d (%s)", store_ptr->path, errno, strerror(errno));
    goto free;
  }

  if (ret < (size_t)buffer_size)
  {
    /* this could be handled better but we dont care because it is expected for the file to be small enough */
    log_error("read(%s) returned less bytes than requested", store_ptr->path);
    goto free;
  }

  buffer[buffer_size] = 0;

  /* read lines and store them in the item array */
  temp_ptr = strtok(buffer, "\n");
  i = 0;
  while (temp_ptr != NULL && i < store_ptr->max_items)
  {
    ASSERT(store_ptr->items[i] == NULL); /* filling an empty array */

    store_ptr->items[i] = strdup(temp_ptr);
    if (store_ptr->items[i] == NULL)
    {
      log_error("strdup(\"%s\") failed.", temp_ptr);

      /* free the already duplicated items */
      while (i > 0)
      {
        i--;
        free(store_ptr->items[i]);
        store_ptr->items[i] = NULL;
      }

      goto free;
    }

    i++;
    temp_ptr = strtok(NULL, "\n");
  }

free:
  free(buffer);
close:
  close(fd);
exit:
  return;
}

bool
ladish_recent_store_create(
  const char * file_path,
  unsigned int max_items,
  ladish_recent_store_handle * store_handle_ptr)
{
  struct ladish_recent_store * store_ptr;
  unsigned int i;

  store_ptr = malloc(sizeof(struct ladish_recent_store));
  if (store_ptr == NULL)
  {
    log_error("malloc() failed to allocate a ladish_recent_store struct");
    goto fail;
  }

  store_ptr->path = strdup(file_path);
  if (store_ptr->path == NULL)
  {
    log_error("strdup() failed for recent store path '%s'", file_path);
    goto free_store;
  }

  store_ptr->items = malloc(sizeof(char *) * max_items);
  if (store_ptr->items == NULL)
  {
    log_error("malloc() failed to array of %u pointers", max_items);
    goto free_path;
  }

  for (i = 0; i < max_items; i++)
  {
    store_ptr->items[i] = NULL;
  }

  store_ptr->max_items = max_items;

  /* try to load items from file */
  ladish_recent_store_load(store_ptr);

  *store_handle_ptr = (ladish_recent_store_handle)store_ptr;

  return true;

free_path:
  free(store_ptr->path);
free_store:
  free(store_ptr);
fail:
  return false;
}

#define store_ptr ((struct ladish_recent_store *)store_handle)

void
ladish_recent_store_destroy(
  ladish_recent_store_handle store_handle)
{
  unsigned int i;

  for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
  {
    free(store_ptr->items[i]);
  }

  free(store_ptr->items);
  free(store_ptr->path);
  free(store_ptr);
}

void
ladish_recent_store_use_item(
  ladish_recent_store_handle store_handle,
  const char * item_const)
{
  char * item;
  char * item_save;
  unsigned int i;

  for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
  {
    if (strcmp(store_ptr->items[i], item_const) == 0)
    { /* already known item */

      /* if the item is already the most recent one, there is nothing to do */
      if (i != 0)
      {
        item = store_ptr->items[i];
        store_ptr->items[i] = NULL; /* mark end for reorder loop */
        goto reorder;
      }

      return;
    }
  }

  /* new item */
  item = strdup(item_const);
  if (item == NULL)
  {
    log_error("strdup() failed for recent item '%s'", item_const);
    return;
  }

reorder:
  /* we have valid non-NULL item pointer before the iteration begins */
  /* the item pointer is checked for NULL because that means either
     end of used items or a mark created when already known item
     was removed from the array */
  for (i = 0; i < store_ptr->max_items && item != NULL; i++)
  {
    item_save = store_ptr->items[i];
    store_ptr->items[i] = item;
    item = item_save;
  }

  if (item != NULL)
  { /* eventually free the oldest item */
    free(item);
  }

  ladish_recent_store_save(store_ptr);
}

bool
ladish_recent_store_check_known(
  ladish_recent_store_handle store_handle,
  const char * item)
{
  unsigned int i;

  for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
  {
    if (strcmp(store_ptr->items[i], item) == 0)
    {
      return true;
    }
  }

  return false;
}

void
ladish_recent_store_iterate_items(
  ladish_recent_store_handle store_handle,
  void * callback_context,
  bool (* callback)(void * callback_context, const char * item))
{
  unsigned int i;

  for (i = 0; i < store_ptr->max_items && store_ptr->items[i] != NULL; i++)
  {
    if (!callback(callback_context, store_ptr->items[i]))
    {
      break;
    }
  }
}

#undef store_ptr