home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cnradv.zip / SORT.C < prev   
C/C++ Source or Header  |  1993-07-04  |  11KB  |  232 lines

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