2016-06-22 4 views
-2

이 파일 https://github.com/HaarigerHarald/omxiv/blob/master/omxiv.c을 수정하려고합니다.이 파일은 getImageFilesInDir 함수라고 생각합니다. 알파벳순으로 오름차순으로 디렉토리에있는 파일이 아닌 알파벳순으로 내림차순으로 (img05.png, img04.png, img03.png, img02.png, img01.png) 다시 표시하도록 이미지 뷰어를 표시하도록 변경해야합니다. 가장 큰 번호로 파일 한 첫 번째 이미지 (내 경우 img05.png)알파벳순으로 내림차순으로 파일 정렬

나는 for(i=0; i-1; i--)과 같은 것을 시도했지만 도움이되지 않았습니다. 어떤 아이디어로 만드시겠습니까?

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; 
} 

당신은 단지 scandir()alphasort()가하는 일의 부정적인를 수행 콜백을 비교 줄 필요가

+2

[mcve] 또는 [SSCCE (Short, Self Contained, Correct Example)]로 질문을 ** 편집하십시오. (http://sscce.org) – NathanOliver

+0

비교 콜백 함수를 구현하고 싶습니다. 'alphasort'를 대체합니다. 'scandir()'에 대한 RTFM. – alk

+0

또는 반환 된 네임리스트 배열의 항목 순서 만 바꿀 수 있습니다. – FredK

답변

3

(이것은 C 내 첫 번째 터치입니다).

int descalphasort(const struct dirent **a, const struct dirent **b) 
{ 
    return - alphasort(a, b); 
} 

또는 @chux 지적으로

에서, alphasort() 매개 변수를 반전 : 당신은 말 그대로 단지 alphasort() 출력 부정 할 수

int descalphasort(const struct dirent **a, const struct dirent **b) 
{ 
    return alphasort(b, a); 
} 

을 그리고 alphasort 대신 scandir() 통화 descalphasort를 사용합니다.

+1

'alphasort (b, a);'는 어때? – chux

+0

@chux 좋은 지적. 그것도 작동합니다. – Havenard

+0

친구 감사합니다, 매력처럼 작동합니다! :) – peter