Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commits (2)
......@@ -178,17 +178,29 @@ static size_t find_section_index(str_list_t list, const char* section)
return(i);
}
static size_t section_start(str_list_t list, size_t index)
{
char* p = list[index];
if(p != NULL) {
SKIP_WHITESPACE(p);
if(*p == INI_OPEN_SECTION_CHAR) // A new section starts immediately?
return strListCount(list);
}
return index;
}
static size_t find_section(str_list_t list, const char* section)
{
size_t i;
char* p;
if(section==ROOT_SECTION)
return(0);
return section_start(list, 0);
i=find_section_index(list,section);
if(list[i]!=NULL)
i++;
return(i);
return section_start(list, i);
}
static char* key_name(char* p, char** vp, BOOL literals_supported)
......@@ -430,6 +442,43 @@ BOOL iniRemoveSections(str_list_t* list, const char* prefix)
return(TRUE);
}
// This sorts comments too, so should not be used on human created/edited files
BOOL iniSortSections(str_list_t* list, BOOL sort_keys)
{
size_t i;
str_list_t root_keys;
str_list_t new_list;
str_list_t keys;
str_list_t section_list = iniGetSectionList(*list, /* prefix */NULL);
root_keys = iniGetSection(*list, ROOT_SECTION);
if(section_list == NULL && root_keys == NULL)
return TRUE;
if(sort_keys)
strListSortAlphaCase(root_keys);
if(section_list != NULL)
strListSortAlphaCase(section_list);
new_list = strListInit();
if(new_list == NULL) {
strListFree(&section_list);
return FALSE;
}
strListAppendList(&new_list, root_keys);
strListFree(&root_keys);
for(i = 0; section_list != NULL && section_list[i] != NULL; i++) {
keys = iniGetSection(*list, section_list[i]);
if(sort_keys)
strListSortAlphaCase(keys);
iniAppendSectionWithKeys(&new_list, section_list[i], keys, /* ini_style_t */NULL);
strListFree(&keys);
}
strListFree(list);
*list = new_list;
return TRUE;
}
BOOL iniRenameSection(str_list_t* list, const char* section, const char* newname)
{
char str[INI_MAX_LINE_LEN];
......@@ -2410,10 +2459,14 @@ void main(int argc, char** argv)
str_list_t list;
for(i=1;i<argc;i++) {
if((fp=iniOpenFile(argv[i],FALSE)) == NULL)
if((fp=iniOpenFile(argv[i],FALSE)) == NULL) {
perror(argv[i]);
continue;
}
if((list=iniReadFile(fp)) != NULL) {
printf("%s\n",iniGetString(list," test | bogus ","key","default",str));
iniSortSections(&list, TRUE);
for(size_t j = 0; list[j] != NULL; j++)
printf("%s\n", list[j]);
strListFree(&list);
}
fclose(fp);
......
......@@ -276,6 +276,7 @@ DLLEXPORT BOOL iniRemoveValue(str_list_t*, const char* section, const char* k
DLLEXPORT BOOL iniRemoveSection(str_list_t*, const char* section);
DLLEXPORT BOOL iniRemoveSections(str_list_t*, const char* prefix);
DLLEXPORT BOOL iniRenameSection(str_list_t*, const char* section, const char* newname);
DLLEXPORT BOOL iniSortSections(str_list_t*, BOOL sort_keys);
DLLEXPORT BOOL iniHasInclude(const str_list_t);
......
......@@ -483,22 +483,26 @@ static int QSORT_CALLBACK_TYPE strListCompareAlphaCaseReverse(const void *arg1,
void strListSortAlpha(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlpha);
if(list != NULL)
qsort(list,strListCount(list),sizeof(char*),strListCompareAlpha);
}
void strListSortAlphaReverse(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaReverse);
if(list != NULL)
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaReverse);
}
void strListSortAlphaCase(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCase);
if(list != NULL)
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCase);
}
void strListSortAlphaCaseReverse(str_list_t list)
{
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCaseReverse);
if(list != NULL)
qsort(list,strListCount(list),sizeof(char*),strListCompareAlphaCaseReverse);
}
str_list_t strListDup(str_list_t list)
......@@ -506,6 +510,8 @@ str_list_t strListDup(str_list_t list)
str_list_t ret;
size_t count=0;
if(list == NULL)
return NULL;
ret = strListInit();
for(; *list; list++)
strListAppend(&ret, *list, count++);
......