samedi 25 juin 2016

sort files in descending alphabetical order

I am trying to modify this file https://github.com/HaarigerHarald/omxiv/blob/master/omxiv.c, I think it is function getImageFilesInDir. I need to change it so it will return back not files in directory in alphabetical ascending order but in alphabetical descending order (img05.png, img04.png, img03.png, img02.png, img01.png), so image viewer will show me the first image that file with the greatest number (in my case img05.png)

I tried something like for(i=0; i-1; i--) but it did not help. Any idea please how to do it?

static int getImageFilesInDir(char ***list, const char* path){
    struct dirent **namelist;
    int imageNum;
    imageNum = scandir(path, &namelist, imageFilter, alphasort);
    if (imageNum < 0)
        return imageNum;
    else {
        *list=malloc(sizeof(char*) *imageNum);
        int i;
        for(i=0; i<imageNum; i++) {
            if(strcmp(path, ".") == 0 || strcmp(path, "./") == 0){
                (*list)[i]= malloc(strlen(namelist[i]->d_name)+1);
                strcpy((*list)[i], namelist[i]->d_name);
            }else{
                if(strrchr(path, '/')- path != strlen(path)-1){
                    (*list)[i]= malloc(strlen(path)+strlen(namelist[i]->d_name)+2);
                    strcpy((*list)[i],path);
                    (*list)[i][strlen(path)]='/';
                    strcpy((*list)[i]+strlen(path)+1,namelist[i]->d_name);
                }else{
                    (*list)[i]= malloc(strlen(path)+strlen(namelist[i]->d_name)+1);
                    strcpy((*list)[i],path);
                    strcpy((*list)[i]+strlen(path),namelist[i]->d_name);
                }
            }
            free(namelist[i]);
        }
        free(namelist);
    }
    return imageNum;
}

(This is my first touch with C)

Aucun commentaire:

Enregistrer un commentaire