home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / mkfsort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  154 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*
  19.  *
  20.  * routines to sort an array of file objects
  21.  * uses qsort alg
  22.  *
  23.  * Designed and implemented by Lou Montulli '94
  24.  */
  25.  
  26. #include "mkutils.h"
  27. #include "mkfsort.h"
  28. #include "mkgeturl.h"
  29.  
  30. #ifdef PROFILE
  31. #pragma profile on
  32. #endif
  33.  
  34. PRIVATE int net_file_sort_method = SORT_BY_NAME;
  35.  
  36. /* print a text string in place of the NET_FileEntryInfo special_type int */
  37. PUBLIC char *
  38. NET_PrintFileType(int special_type)
  39. {
  40.     switch(special_type)
  41.       {
  42.         case NET_FILE_TYPE:
  43.             return("FILE");
  44.         case NET_DIRECTORY:
  45.             return("DIRECTORY");
  46.         case NET_SYM_LINK:
  47.             return("SYMBOLIC-LINK");
  48.         case NET_SYM_LINK_TO_DIR:
  49.             return("SYM-DIRECTORY");
  50.         case NET_SYM_LINK_TO_FILE:
  51.             return("SYM-FILE");
  52.         default:
  53.             XP_ASSERT(0);
  54.             return("FILE");
  55.       }
  56. }
  57.  
  58. PRIVATE void 
  59. NET_SetFileSortMethod(int method)
  60. {
  61.     net_file_sort_method = method;
  62. }
  63.  
  64. MODULE_PRIVATE void NET_FreeEntryInfoStruct(NET_FileEntryInfo *entry_info)
  65. {
  66.     if(entry_info) 
  67.       {
  68.         FREEIF(entry_info->filename);
  69.         /* free the struct */
  70.         XP_FREE(entry_info);
  71.       }
  72. }
  73.  
  74. MODULE_PRIVATE NET_FileEntryInfo * NET_CreateFileEntryInfoStruct (void)
  75. {
  76.     NET_FileEntryInfo * new_entry = XP_NEW(NET_FileEntryInfo);
  77.  
  78.     if(!new_entry) 
  79.        return(NULL);
  80.  
  81.     XP_MEMSET(new_entry, 0, sizeof(NET_FileEntryInfo));
  82.  
  83.     new_entry->permissions = -1;
  84.  
  85.     return(new_entry);
  86.  
  87. }
  88.  
  89. /* This function is used as a comparer function for the Qsort routine.
  90.  * It uses a function FE_FileSortMethod() to determine the
  91.  * field to sort on.
  92.  *
  93.  */
  94. PRIVATE int
  95. NET_CompareFileEntryInfoStructs (const void *ent2, const void *ent1)
  96. {
  97.     int status;
  98.     const NET_FileEntryInfo *entry1 = *(NET_FileEntryInfo **) ent1;
  99.     const NET_FileEntryInfo *entry2 = *(NET_FileEntryInfo **) ent2;
  100.  
  101.     if(!entry1 || !entry2)
  102.         return(-1);
  103.  
  104.     switch(net_file_sort_method)
  105.       {
  106.         case SORT_BY_SIZE:
  107.                         /* both equal or both 0 */
  108.                         if(entry1->size == entry2->size)
  109.                             return(XP_STRCMP(entry2->filename, entry1->filename));
  110.                         else
  111.                             if(entry1->size > entry2->size)
  112.                                 return(-1);
  113.                             else
  114.                                 return(1);
  115.                         /* break; NOT NEEDED */
  116.         case SORT_BY_TYPE:
  117.                         if(entry1->cinfo && entry1->cinfo->desc && 
  118.                                         entry2->cinfo && entry2->cinfo->desc) 
  119.                           {
  120.                             status = XP_STRCMP(entry1->cinfo->desc, entry2->cinfo->desc);
  121.                             if(status)
  122.                                 return(status);
  123.                             /* else fall to filename comparison */
  124.                           }
  125.                         return (XP_STRCMP(entry2->filename, entry1->filename));
  126.                         /* break; NOT NEEDED */
  127.         case SORT_BY_DATE:
  128.                         if(entry1->date == entry2->date) 
  129.                             return(XP_STRCMP(entry2->filename, entry1->filename));
  130.                         else
  131.                             if(entry1->size > entry2->size)
  132.                                 return(-1);
  133.                             else
  134.                                 return(1);
  135.                         /* break; NOT NEEDED */
  136.         case SORT_BY_NAME:
  137.         default:
  138.                         return (XP_STRCMP(entry2->filename, entry1->filename));
  139.       }
  140. }
  141.  
  142. /* sort the files
  143.  */
  144. MODULE_PRIVATE void
  145. NET_DoFileSort(SortStruct * sort_list)
  146. {
  147.     NET_DoSort(sort_list, NET_CompareFileEntryInfoStructs);
  148. }
  149.  
  150. #ifdef PROFILE
  151. #pragma profile off
  152. #endif
  153.  
  154.