58 unsigned int length) {
59 if (list == NULL || length == 0) {
67 int min_value = list[length - 1]->
index;
74 for (
unsigned int iter = 0; iter < length; iter++) {
75 fprintf(fd,
"%ld %s\n", list[iter]->index - min_value, list[iter]->name);
80 unsigned int *length) {
81 unsigned int real_length = 0;
93 size_t buffer_length = 0;
95 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
97 const char *start = strchr(buffer,
' ');
104 buffer[l - 1] =
'\0';
105 if (real_length < (*length + 2)) {
108 retv = g_realloc(retv, (real_length) *
sizeof(
char *));
111 retv[(*length)] = g_strndup(start, l - 1 - (start - buffer));
113 retv[(*length) + 1] = NULL;
117 if (buffer_length > 0) {
124 unsigned int real_length = 0;
127 if (length == NULL) {
136 size_t buffer_length = 0;
138 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
145 long int index = strtol(buffer, &start, 10);
146 if (start == buffer || *start ==
'\0') {
150 if ((l - (start - buffer)) < 2) {
153 if (real_length < (*length + 2)) {
156 retv = g_realloc(retv, (real_length) *
sizeof(
_element *));
159 retv[(*length)] = g_malloc(
sizeof(
_element));
162 buffer[l - 1] =
'\0';
164 retv[(*length)]->
index = index;
165 retv[(*length)]->
name = g_strndup(start, l - 1 - (start - buffer));
167 retv[(*length) + 1] = NULL;
171 if (buffer != NULL) {
185 checked_prefix != NULL; checked_prefix = strtok(NULL,
";")) {
188 while (g_unichar_isspace(g_utf8_get_char(checked_prefix))) {
189 checked_prefix = g_utf8_next_char(
194 if (g_str_has_prefix(entry, checked_prefix)) {
200 unsigned int curr = 0;
201 unsigned int length = 0;
204 FILE *fd = g_fopen(filename,
"r");
209 if (fclose(fd) != 0) {
210 g_warning(
"Failed to close history file: %s", g_strerror(errno));
214 for (
unsigned int iter = 0; !found && iter < length; iter++) {
215 if (strcmp(list[iter]->name, entry) == 0) {
227 list = g_realloc(list, (length + 2) *
sizeof(
_element *));
228 list[length] = g_malloc(
sizeof(
_element));
230 if (list[length] != NULL) {
231 list[length]->
name = g_strdup(entry);
233 list[length]->
index = 1;
240 fd = fopen(filename,
"w");
242 g_warning(
"Failed to open file: %s", g_strerror(errno));
247 if (fclose(fd) != 0) {
248 g_warning(
"Failed to close history file: %s", g_strerror(errno));
252 for (
unsigned int iter = 0; iter < length; iter++) {
253 g_free(list[iter]->name);
265 unsigned int curr = 0;
266 unsigned int length = 0;
268 FILE *fd = g_fopen(filename,
"r");
270 g_warning(
"Failed to open file: %s", g_strerror(errno));
277 if (fclose(fd) != 0) {
278 g_warning(
"Failed to close history file: %s", g_strerror(errno));
281 for (
unsigned int iter = 0; !found && iter < length; iter++) {
282 if (strcmp(list[iter]->name, entry) == 0) {
291 g_free(list[curr]->name);
294 list[curr] = list[length - 1];
296 list[length - 1] = NULL;
299 fd = g_fopen(filename,
"w");
305 if (fclose(fd) != 0) {
306 g_warning(
"Failed to close history file: %s", g_strerror(errno));
309 g_warning(
"Failed to open file: %s", g_strerror(errno));
314 for (
unsigned int iter = 0; iter < length; iter++) {
315 g_free(list[iter]->name);
void history_set(const char *filename, const char *entry)
void history_remove(const char *filename, const char *entry)
char ** history_get_list(const char *filename, unsigned int *length)
struct __element _element
static char ** __history_get_element_list_fields(FILE *fd, unsigned int *length)
static void __history_write_element_list(FILE *fd, _element **list, unsigned int length)
static _element ** __history_get_element_list(FILE *fd, unsigned int *length)
static int __element_sort_func(const void *ea, const void *eb, void *data __attribute__((unused)))