Browse Source

Directory listing: Sort by name as secondary criterion

Even if the list is sorted by size or date, use the name as additional sort criterion.
E.g., if sorted by size, files with the same size will be sorted by name.
bel2125 3 years ago
parent
commit
836b2f52e1
1 changed files with 16 additions and 7 deletions
  1. 16 7
      src/civetweb.c

+ 16 - 7
src/civetweb.c

@@ -9617,10 +9617,8 @@ print_dir_entry(struct mg_connection *conn, struct de *de)
 
 
 
 
 /* This function is called from send_directory() and used for
 /* This function is called from send_directory() and used for
- * sorting directory entries by size, or name, or modification time.
- * On windows, __cdecl specification is needed in case if project is built
- * with __stdcall convention. qsort always requires __cdels callback. */
-static int WINCDECL
+ * sorting directory entries by size, name, or modification time. */
+static int
 compare_dir_entries(const void *p1, const void *p2, void *arg)
 compare_dir_entries(const void *p1, const void *p2, void *arg)
 {
 {
 	const char *query_string = (const char *)(arg != NULL ? arg : "");
 	const char *query_string = (const char *)(arg != NULL ? arg : "");
@@ -9632,13 +9630,15 @@ compare_dir_entries(const void *p1, const void *p2, void *arg)
 			query_string = "n";
 			query_string = "n";
 		}
 		}
 
 
+		/* Sort Directories vs Files */
 		if (a->file.is_directory && !b->file.is_directory) {
 		if (a->file.is_directory && !b->file.is_directory) {
 			return -1; /* Always put directories on top */
 			return -1; /* Always put directories on top */
 		} else if (!a->file.is_directory && b->file.is_directory) {
 		} else if (!a->file.is_directory && b->file.is_directory) {
 			return 1; /* Always put directories on top */
 			return 1; /* Always put directories on top */
-		} else if (*query_string == 'n') {
-			cmp_result = strcmp(a->file_name, b->file_name);
-		} else if (*query_string == 's') {
+		}
+
+		/* Sort by size or date */
+		if (*query_string == 's') {
 			cmp_result = (a->file.size == b->file.size)
 			cmp_result = (a->file.size == b->file.size)
 			                 ? 0
 			                 ? 0
 			                 : ((a->file.size > b->file.size) ? 1 : -1);
 			                 : ((a->file.size > b->file.size) ? 1 : -1);
@@ -9650,6 +9650,15 @@ compare_dir_entries(const void *p1, const void *p2, void *arg)
 			                                                           : -1);
 			                                                           : -1);
 		}
 		}
 
 
+		/* Sort by name:
+		 * if (*query_string == 'n')  ...
+		 * but also sort files of same size/date by name as secondary criterion.
+		 */
+		if (cmp_result == 0) {
+			cmp_result = strcmp(a->file_name, b->file_name);
+		}
+
+		/* For descending order, invert result */
 		return (query_string[1] == 'd') ? -cmp_result : cmp_result;
 		return (query_string[1] == 'd') ? -cmp_result : cmp_result;
 	}
 	}
 	return 0;
 	return 0;