home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / CNRMNU.ZIP / SORT.C < prev   
Text File  |  1993-01-01  |  11KB  |  218 lines

  1. /*********************************************************************
  2.  *                                                                   *
  3.  * MODULE NAME :  sort.c                 AUTHOR:  Rick Fishman       *
  4.  * DATE WRITTEN:  11-01-92                                           *
  5.  *                                                                   *
  6.  * DESCRIPTION:                                                      *
  7.  *                                                                   *
  8.  *  This module is part of CNRMENU.EXE. It contains the functions    *
  9.  *  necessary to implement container sorting.                        *
  10.  *                                                                   *
  11.  * CALLABLE FUNCTIONS:                                               *
  12.  *                                                                   *
  13.  *  VOID SortContainer( HWND hwndClient, ULONG ulSortType );         *
  14.  *                                                                   *
  15.  * HISTORY:                                                          *
  16.  *                                                                   *
  17.  *  11-01-92 - Program coded.                                        *
  18.  *                                                                   *
  19.  *  Rick Fishman                                                     *
  20.  *  Code Blazers, Inc.                                               *
  21.  *  4113 Apricot                                                     *
  22.  *  Irvine, CA. 92720                                                *
  23.  *  CIS ID: 72251,750                                                *
  24.  *                                                                   *
  25.  *********************************************************************/
  26.  
  27. #pragma strings(readonly)   // used for debug version of memory mgmt routines
  28.  
  29. /*********************************************************************/
  30. /*------- Include relevant sections of the OS/2 header files --------*/
  31. /*********************************************************************/
  32.  
  33. #define INCL_WINERRORS
  34. #define INCL_WINFRAMEMGR
  35. #define INCL_WINSTDCNR
  36. #define INCL_WINWINDOWMGR
  37.  
  38. /**********************************************************************/
  39. /*----------------------------- INCLUDES -----------------------------*/
  40. /**********************************************************************/
  41.  
  42. #include <os2.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "cnrmenu.h"
  47.  
  48. /*********************************************************************/
  49. /*------------------- APPLICATION DEFINITIONS -----------------------*/
  50. /*********************************************************************/
  51.  
  52. /**********************************************************************/
  53. /*---------------------------- STRUCTURES ----------------------------*/
  54. /**********************************************************************/
  55.  
  56. /**********************************************************************/
  57. /*----------------------- FUNCTION PROTOTYPES ------------------------*/
  58. /**********************************************************************/
  59.  
  60. static SHORT APIENTRY NameCompare( PRECORDCORE prc1,PRECORDCORE prc2,PVOID pv );
  61. static SHORT APIENTRY DirCompare ( PRECORDCORE prc1,PRECORDCORE prc2,PVOID pv );
  62. static SHORT APIENTRY DateCompare( PRECORDCORE prc1,PRECORDCORE prc2,PVOID pv );
  63.  
  64. /**********************************************************************/
  65. /*------------------------ GLOBAL VARIABLES --------------------------*/
  66. /**********************************************************************/
  67.  
  68. /**********************************************************************/
  69. /*-------------------------- SortContainer ---------------------------*/
  70. /*                                                                    */
  71. /*  SORT THE CONTAINER BY A KEY.                                      */
  72. /*                                                                    */
  73. /*  INPUT: client window handle,                                      */
  74. /*         type of sort                                               */
  75. /*                                                                    */
  76. /*  1.                                                                */
  77. /*                                                                    */
  78. /*  OUTPUT: nothing                                                   */
  79. /*                                                                    */
  80. /*--------------------------------------------------------------------*/
  81. /**********************************************************************/
  82. VOID SortContainer( HWND hwndClient, ULONG ulSortType )
  83. {
  84.     HWND hwndCnr = WinWindowFromID( hwndClient, CNR_DIRECTORY );
  85.  
  86.     switch( ulSortType )
  87.     {
  88.         case IDM_SORT_NAME:
  89.  
  90.             WinSendMsg( hwndCnr, CM_SORTRECORD, MPFROMP( NameCompare ), NULL );
  91.  
  92.             break;
  93.  
  94.         case IDM_SORT_DIRORDER:
  95.  
  96.             WinSendMsg( hwndCnr, CM_SORTRECORD, MPFROMP( DirCompare ), NULL );
  97.  
  98.             break;
  99.  
  100.         case IDM_SORT_DATETIME:
  101.  
  102.             WinSendMsg( hwndCnr, CM_SORTRECORD, MPFROMP( DateCompare ), NULL );
  103.  
  104.             break;
  105.     }
  106.  
  107.     return;
  108. }
  109.  
  110. /**********************************************************************/
  111. /*--------------------------- NameCompare ----------------------------*/
  112. /*                                                                    */
  113. /*  COMPARISON FUNCTION FOR FILENAME SORT.                            */
  114. /*                                                                    */
  115. /*  INPUT: first record in the compare,                               */
  116. /*         second record in the sort,                                 */
  117. /*         dummy parm to satisfy function prototype                   */
  118. /*                                                                    */
  119. /*  1.                                                                */
  120. /*                                                                    */
  121. /*  OUTPUT: 0 = both are equal                                        */
  122. /*         -1 = first is less than second                             */
  123. /*         +1 = first is greater than second                          */
  124. /*                                                                    */
  125. /*--------------------------------------------------------------------*/
  126. /**********************************************************************/
  127. static SHORT APIENTRY NameCompare( PRECORDCORE prc1, PRECORDCORE prc2, PVOID pv)
  128. {
  129.     pv = pv;    // to keep the compiler happy
  130.  
  131.     return strcmp( ((PCNRITEM)prc1)->szFileName, ((PCNRITEM)prc2)->szFileName );
  132. }
  133.  
  134. /**********************************************************************/
  135. /*---------------------------- DirCompare ----------------------------*/
  136. /*                                                                    */
  137. /*  COMPARISON FUNCTION FOR DIRECTORY ORDER SORT.                     */
  138. /*                                                                    */
  139. /*  INPUT: first record in the compare,                               */
  140. /*         second record in the sort,                                 */
  141. /*         dummy parm to satisfy function prototype                   */
  142. /*                                                                    */
  143. /*  1.                                                                */
  144. /*                                                                    */
  145. /*  OUTPUT: 0 = both are equal                                        */
  146. /*         -1 = first is less than second                             */
  147. /*         +1 = first is greater than second                          */
  148. /*                                                                    */
  149. /*--------------------------------------------------------------------*/
  150. /**********************************************************************/
  151. static SHORT APIENTRY DirCompare( PRECORDCORE prc1, PRECORDCORE prc2, PVOID pv )
  152. {
  153.     INT iDirPosition1 = ((PCNRITEM) prc1)->iDirPosition;
  154.     INT iDirPosition2 = ((PCNRITEM) prc2)->iDirPosition;
  155.  
  156.     pv = pv;    // to keep the compiler happy
  157.  
  158.     if( iDirPosition1 == iDirPosition2 )
  159.         return 0;
  160.     else if( iDirPosition1 < iDirPosition2 )
  161.         return -1;
  162.     else
  163.         return +1;
  164. }
  165.  
  166. /**********************************************************************/
  167. /*--------------------------- DateCompare ----------------------------*/
  168. /*                                                                    */
  169. /*  COMPARISON FUNCTION FOR FILE DATE/TIME SORT.                      */
  170. /*                                                                    */
  171. /*  INPUT: first record in the compare,                               */
  172. /*         second record in the sort,                                 */
  173. /*         dummy parm to satisfy function prototype                   */
  174. /*                                                                    */
  175. /*  1.                                                                */
  176. /*                                                                    */
  177. /*  OUTPUT: 0 = both are equal                                        */
  178. /*         -1 = first is less than second                             */
  179. /*         +1 = first is greater than second                          */
  180. /*                                                                    */
  181. /*--------------------------------------------------------------------*/
  182. /**********************************************************************/
  183. static SHORT APIENTRY DateCompare( PRECORDCORE prc1, PRECORDCORE prc2, PVOID pv)
  184. {
  185.     CDATE date1 = ((PCNRITEM) prc1)->date;
  186.     CDATE date2 = ((PCNRITEM) prc2)->date;
  187.     CHAR  szDate1[ 9 ], szDate2[ 9 ];
  188.     INT   iResult;
  189.  
  190.     pv = pv;    // to keep the compiler happy
  191.  
  192.     (void) sprintf( szDate1,"%04u%02u%02u",date1.year, date1.month, date1.day );
  193.     (void) sprintf( szDate2,"%04u%02u%02u",date2.year, date2.month, date2.day );
  194.  
  195.     iResult = strcmp( szDate1, szDate2 );
  196.  
  197.     if( !iResult )
  198.     {
  199.         CTIME time1 = ((PCNRITEM) prc1)->time;
  200.         CTIME time2 = ((PCNRITEM) prc2)->time;
  201.         INT iSecs1 = (INT)((time1.hours*3600)+(time1.minutes*60)+time1.seconds);
  202.         INT iSecs2 = (INT)((time2.hours*3600)+(time2.minutes*60)+time2.seconds);
  203.  
  204.         if( iSecs1 == iSecs2 )
  205.             iResult = 0;
  206.         else if( iSecs1 < iSecs2 )
  207.             iResult = -1;
  208.         else
  209.             iResult = +1;
  210.     }
  211.  
  212.     return iResult;
  213. }
  214.  
  215. /*************************************************************************
  216.  *                     E N D     O F     S O U R C E                     *
  217.  *************************************************************************/
  218.