void config_file_dump(config_file_t *conf, FILE *file, bool sort) { struct config_entry_list *list = NULL; struct config_include_list *includes = conf->includes; while (includes) { fprintf(file, "#include \"%s\"\n", includes->path); includes = includes->next; } if (sort) list = merge_sort_linked_list((struct config_entry_list*) conf->entries, config_sort_compare_func); else list = (struct config_entry_list*)conf->entries; conf->entries = list; while (list) { if (!list->readonly && list->key) fprintf(file, "%s = \"%s\"\n", list->key, list->value); list = list->next; } }
void config_file_dump_orbis(config_file_t *conf, int fd) { struct config_entry_list *list = NULL; struct config_include_list *includes = conf->includes; while (includes) { char cad[256]; sprintf(cad,"#include %s\n", includes->path); orbisWrite(fd, cad, strlen(cad)); includes = includes->next; } list = merge_sort_linked_list((struct config_entry_list*)conf->entries, config_sort_compare_func); conf->entries = list; while (list) { if (!list->readonly && list->key) { char newlist[256]; sprintf(newlist,"%s = %s\n", list->key, list->value); orbisWrite(fd, newlist, strlen(newlist)); } list = list->next; } }
/* https://stackoverflow.com/questions/7685/merge-sort-a-linked-list */ static struct config_entry_list* merge_sort_linked_list(struct config_entry_list *list, int (*compare)(struct config_entry_list *one,struct config_entry_list *two)) { struct config_entry_list *right = list, *temp = list, *last = list, *result = 0, *next = 0, *tail = 0; /* Trivial case. */ if (!list || !list->next) return list; /* Find halfway through the list (by running two pointers, one at twice the speed of the other). */ while (temp && temp->next) { last = right; right = right->next; temp = temp->next->next; } /* Break the list in two. (prev pointers are broken here, but we fix later) */ last->next = 0; /* Recurse on the two smaller lists: */ list = merge_sort_linked_list(list, compare); right = merge_sort_linked_list(right, compare); /* Merge: */ while (list || right) { /* Take from empty lists, or compare: */ if (!right) { next = list; list = list->next; } else if (!list) { next = right; right = right->next; } else if (compare(list, right) < 0) { next = list; list = list->next; } else { next = right; right = right->next; } if (!result) result = next; else tail->next = next; tail = next; } return result; }