6 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
44 // directories first...
45 static int compareType(const WIN32_FIND_DATA
* fd1
, const WIN32_FIND_DATA
* fd2
)
47 int dir1
= fd1
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
48 int dir2
= fd2
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
50 return dir2
==dir1
? 0: dir2
<dir1
? -1: 1;
54 static int compareName(const void* arg1
, const void* arg2
)
56 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
57 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
59 int cmp
= compareType(fd1
, fd2
);
63 return lstrcmpi(fd1
->cFileName
, fd2
->cFileName
);
66 static int compareExt(const void* arg1
, const void* arg2
)
68 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
69 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
70 const TCHAR
*name1
, *name2
, *ext1
, *ext2
;
72 int cmp
= compareType(fd1
, fd2
);
76 name1
= fd1
->cFileName
;
77 name2
= fd2
->cFileName
;
79 ext1
= _tcsrchr(name1
, _T('.'));
80 ext2
= _tcsrchr(name2
, _T('.'));
92 cmp
= lstrcmpi(ext1
, ext2
);
96 return lstrcmpi(name1
, name2
);
99 static int compareSize(const void* arg1
, const void* arg2
)
101 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
102 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
104 int cmp
= compareType(fd1
, fd2
);
108 cmp
= fd2
->nFileSizeHigh
- fd1
->nFileSizeHigh
;
115 cmp
= fd2
->nFileSizeLow
- fd1
->nFileSizeLow
;
117 return cmp
<0? -1: cmp
>0? 1: 0;
120 static int compareDate(const void* arg1
, const void* arg2
)
122 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
123 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
125 int cmp
= compareType(fd1
, fd2
);
129 return CompareFileTime(&fd2
->ftLastWriteTime
, &fd1
->ftLastWriteTime
);
133 static int (*sortFunctions
[])(const void* arg1
, const void* arg2
) = {
134 compareName
, // SORT_NAME
135 compareExt
, // SORT_EXT
136 compareSize
, // SORT_SIZE
137 compareDate
// SORT_DATE
141 void SortDirectory(Entry
* parent
, SORT_ORDER sortOrder
)
143 Entry
* entry
= parent
->down
;
148 for(entry
=parent
->down
; entry
; entry
=entry
->next
)
153 array
= (Entry
**) alloca(len
*sizeof(Entry
*));
155 array
= (Entry
**) malloc(len
*sizeof(Entry
*));
157 memset(array
, 0, len
*sizeof(Entry
*));
159 for(entry
=parent
->down
; entry
; entry
=entry
->next
)
162 // call qsort with the appropriate compare function
163 qsort(array
, len
, sizeof(array
[0]), sortFunctions
[sortOrder
]);
165 parent
->down
= array
[0];
167 for(p
=array
; --len
; p
++)