eb69df50273ca17ddf416ca9f634e57178e1ac6a
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.
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
38 //#include <winspool.h>
50 // directories first...
51 static int compareType(const WIN32_FIND_DATA
* fd1
, const WIN32_FIND_DATA
* fd2
)
53 int dir1
= fd1
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
54 int dir2
= fd2
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
56 return dir2
==dir1
? 0: dir2
<dir1
? -1: 1;
60 static int compareName(const void* arg1
, const void* arg2
)
62 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
63 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
65 int cmp
= compareType(fd1
, fd2
);
69 return lstrcmpi(fd1
->cFileName
, fd2
->cFileName
);
72 static int compareExt(const void* arg1
, const void* arg2
)
74 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
75 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
76 const TCHAR
*name1
, *name2
, *ext1
, *ext2
;
78 int cmp
= compareType(fd1
, fd2
);
82 name1
= fd1
->cFileName
;
83 name2
= fd2
->cFileName
;
85 ext1
= _tcsrchr(name1
, _T('.'));
86 ext2
= _tcsrchr(name2
, _T('.'));
98 cmp
= lstrcmpi(ext1
, ext2
);
102 return lstrcmpi(name1
, name2
);
105 static int compareSize(const void* arg1
, const void* arg2
)
107 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
108 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
110 int cmp
= compareType(fd1
, fd2
);
114 cmp
= fd2
->nFileSizeHigh
- fd1
->nFileSizeHigh
;
121 cmp
= fd2
->nFileSizeLow
- fd1
->nFileSizeLow
;
123 return cmp
<0? -1: cmp
>0? 1: 0;
126 static int compareDate(const void* arg1
, const void* arg2
)
128 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->data
;
129 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->data
;
131 int cmp
= compareType(fd1
, fd2
);
135 return CompareFileTime(&fd2
->ftLastWriteTime
, &fd1
->ftLastWriteTime
);
139 static int (*sortFunctions
[])(const void* arg1
, const void* arg2
) = {
140 compareName
, // SORT_NAME
141 compareExt
, // SORT_EXT
142 compareSize
, // SORT_SIZE
143 compareDate
// SORT_DATE
147 void SortDirectory(Entry
* parent
, SORT_ORDER sortOrder
)
149 Entry
* entry
= parent
->down
;
154 for(entry
=parent
->down
; entry
; entry
=entry
->next
)
159 array
= (Entry
**) alloca(len
*sizeof(Entry
*));
161 array
= (Entry
**) malloc(len
*sizeof(Entry
*));
163 memset(array
, 0, len
*sizeof(Entry
*));
165 for(entry
=parent
->down
; entry
; entry
=entry
->next
)
168 // call qsort with the appropriate compare function
169 qsort(array
, len
, sizeof(array
[0]), sortFunctions
[sortOrder
]);
171 parent
->down
= array
[0];
173 for(p
=array
; --len
; p
++)