59 unsigned int length) {
60 if (list == NULL || length == 0) {
68 int min_value = list[length - 1]->
index;
75 for (
unsigned int iter = 0; iter < length; iter++) {
76 fprintf(fd,
"%ld %s\n", list[iter]->index - min_value, list[iter]->name);
81 unsigned int *length) {
82 unsigned int real_length = 0;
94 size_t buffer_length = 0;
96 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
98 const char *start = strchr(buffer,
' ');
105 buffer[l - 1] =
'\0';
106 if (real_length < (*length + 2)) {
109 retv = g_realloc(retv, (real_length) *
sizeof(
char *));
112 retv[(*length)] = g_strndup(start, l - 1 - (start - buffer));
114 retv[(*length) + 1] = NULL;
118 if (buffer_length > 0) {
125 unsigned int real_length = 0;
128 if (length == NULL) {
137 size_t buffer_length = 0;
139 while ((l = getline(&buffer, &buffer_length, fd)) > 0) {
146 long int index = strtol(buffer, &start, 10);
147 if (start == buffer || *start ==
'\0') {
151 if ((l - (start - buffer)) < 2) {
154 if (real_length < (*length + 2)) {
157 retv = g_realloc(retv, (real_length) *
sizeof(
_element *));
160 retv[(*length)] = g_malloc(
sizeof(
_element));
163 buffer[l - 1] =
'\0';
165 retv[(*length)]->
index = index;
166 retv[(*length)]->
name = g_strndup(start, l - 1 - (start - buffer));
168 retv[(*length) + 1] = NULL;
172 if (buffer != NULL) {
186 checked_prefix != NULL; checked_prefix = strtok(NULL,
";")) {
189 while (g_unichar_isspace(g_utf8_get_char(checked_prefix))) {
190 checked_prefix = g_utf8_next_char(
195 if (g_str_has_prefix(entry, checked_prefix)) {
201 unsigned int curr = 0;
202 unsigned int length = 0;
205 FILE *fd = g_fopen(filename,
"r");
210 if (fclose(fd) != 0) {
211 g_warning(
"Failed to close history file: %s", g_strerror(errno));
215 for (
unsigned int iter = 0; !found && iter < length; iter++) {
216 if (strcmp(list[iter]->name, entry) == 0) {
228 list = g_realloc(list, (length + 2) *
sizeof(
_element *));
229 list[length] = g_malloc(
sizeof(
_element));
231 if (list[length] != NULL) {
232 list[length]->
name = g_strdup(entry);
234 list[length]->
index = 1;
241 fd = fopen(filename,
"w");
243 g_warning(
"Failed to open file: %s", g_strerror(errno));
248 if (fclose(fd) != 0) {
249 g_warning(
"Failed to close history file: %s", g_strerror(errno));
253 for (
unsigned int iter = 0; iter < length; iter++) {
254 g_free(list[iter]->name);
266 unsigned int curr = 0;
267 unsigned int length = 0;
269 FILE *fd = g_fopen(filename,
"r");
271 g_warning(
"Failed to open file: %s", g_strerror(errno));
278 if (fclose(fd) != 0) {
279 g_warning(
"Failed to close history file: %s", g_strerror(errno));
282 for (
unsigned int iter = 0; !found && iter < length; iter++) {
283 if (strcmp(list[iter]->name, entry) == 0) {
292 g_free(list[curr]->name);
295 list[curr] = list[length - 1];
297 list[length - 1] = NULL;
300 fd = g_fopen(filename,
"w");
306 if (fclose(fd) != 0) {
307 g_warning(
"Failed to close history file: %s", g_strerror(errno));
310 g_warning(
"Failed to open file: %s", g_strerror(errno));
315 for (
unsigned int iter = 0; iter < length; iter++) {
316 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)))