home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / cenvi / clipsort.cmd < prev    next >
OS/2 REXX Batch file  |  1994-03-15  |  2KB  |  48 lines

  1. EXTPROC CEnvi
  2. //****************************************************************
  3. //*** ClipSort - Perform an alphabetic sort on the data in the ***
  4. //*** ver.1      clipboard.  Sort each line, then replace the  ***
  5. //***            clipboard text with the sorted text.          ***
  6. //***                                                          ***
  7. //*** You might use this tool be selecting text in some        ***
  8. //*** application, copy to clipboard, then execute ClipSort    ***
  9. //*** either explicitly or via a hot-key association.  Then    ***
  10. //*** you can paste immediately back to the selected text to   ***
  11. //*** replace it with a sorted version.                        ***
  12. //****************************************************************
  13.  
  14. #include <ClipBrd.lib>
  15.  
  16. if ( data = GetClipboardData(CF_TEXT) ) {
  17.  
  18.    // determine if the last character is line-feed, so we'll
  19.    // know whether to add line-feed in the sorted list
  20.    AddFinalLineFeed = ( NULL != strchr("\r\n",data[strlen(data)-1]) );
  21.  
  22.    // build array of each line of text in the clipboard
  23.    LineCount = 0;
  24.    Line = strtok(data,"\r\n");
  25.    while ( Line ) {
  26.       strcpy(List[LineCount++],Line);
  27.       Line = strtok(NULL,"\r\n");
  28.    }
  29.  
  30.    // sort this array of text alphabetically, case-insensitive
  31.    qsort(List,LineCount,"stricmp");
  32.  
  33.    // build one long string to put data back into the clipboard
  34.    data[0] = '\0';
  35.    for ( i = 0; i < LineCount; i++ ) {
  36.       strcat(data,List[i]);
  37.       strcat(data,"\r\n");
  38.    }
  39.  
  40.    // if there wasn't supposed to be a line-feed at the end, then remove it
  41.    if ( !AddFinalLineFeed )
  42.       data[strlen(data)-2] = '\0';
  43.  
  44.    // put this new data string in clipboard, replacing old
  45.    PutClipboardData(data,1+strlen(data),CF_TEXT);
  46. }
  47.  
  48.