home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / SORTLEN.CMD < prev    next >
OS/2 REXX Batch file  |  1995-03-28  |  3KB  |  73 lines

  1. @echo off
  2. REM ********************************************************************
  3. REM *** SortLen.cmd - CEnvi2 batch file to sort a file by line length ***
  4. REM *** ver.1         with the shorter lines preceding longer lines. ***
  5. REM ***               This is an example of the C standard library's ***
  6. REM ***               qsort routine.                                 ***
  7. REM ********************************************************************
  8.  
  9. REM ******************************************************************
  10. REM *** Check that only one item was input, and that it was a file ***
  11. REM *** name.  Else display usage screen.                          ***
  12. REM ******************************************************************
  13.    if "%1" == "" GOTO INSTRUCTIONS
  14.    if not "%2" == "" GOTO INSTRUCTIONS
  15.    if not exist %1 GOTO INSTRUCTIONS
  16.  
  17. REM **********************************************************************
  18. REM *** Transfer control to the CEnvi2 code, which will input all lines ***
  19. REM *** into an array, sort the array, and then print the sorted array ***
  20. REM **********************************************************************
  21.    CEnvi2 %0.cmd < %1
  22.    GOTO CENVI_EXIT
  23.  
  24. /*************************************************************************
  25.  *** Build array of all lines in the source file, which was redirected ***
  26.  *** to standard input in the "cenvi2 %0.cmd < %1" line above.         ***
  27.  *************************************************************************/
  28.    LineCount = 0;
  29.    while ( NULL != (text[LineCount] = gets()) ) {
  30.       LineCount++;
  31.    }
  32.  
  33.    if ( 0 == LineCount ) {
  34.       printf("No data to sort.\a\n");
  35.       exit(EXIT_FAILURE);
  36.    }
  37.  
  38. /******************************************
  39.  *** Sort the data based on line length ***
  40.  ******************************************/
  41.    qsort(text,LineCount,"LengthCompare");
  42.  
  43.    LengthCompare(line1,line2) {
  44.       // This routine is called by the qsort() library routine to compare
  45.       // two lines.  Return < 0 if line1 is shorter than line 2, 0 if line1
  46.       // is the same length as line 2, and > 0 if line1 is longer than line2
  47.       return( strlen(line1) - strlen(line2) );
  48.    }
  49.  
  50. /*******************************
  51.  *** Display the sorted text ***
  52.  *******************************/
  53.  
  54.    for ( i = 0; i < LineCount; i++ )
  55.       puts(text[i])
  56.  
  57.  
  58. return(EXIT_SUCCESS);
  59. :CENVI_EXIT
  60. REM *************************************
  61. REM *** CEnvi2 is complete.  Clean up. ***
  62. REM *************************************
  63. GOTO FINISHED
  64.  
  65. :INSTRUCTIONS
  66. ECHO .
  67. ECHO SortLen.cmd - Sort file by line length.
  68. ECHO USAGE: SortLen.cmd FileSpec
  69. GOTO FINISHED
  70.  
  71.  
  72. :FINISHED
  73.