home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / internet / yamtools20 / yamtools20english / arexxqsort.lha / arexxqsort / quicksort.c < prev    next >
C/C++ Source or Header  |  1990-09-30  |  5KB  |  203 lines

  1. /* A test program to demonstrate the direct variable interface to ARexx.
  2.  * Opens a public port called "VarTest" and then waits for REXX messages.
  3.  * The port stays open until a "CLOSE" command is received.
  4.  * Usage:  run vartest
  5.  * Then send commands from within ARexx by "address 'VarTest' command"
  6.  *
  7.  *    This version for Manx. WGL
  8.  */
  9.  
  10. #include "exec/exec.h"
  11.  
  12. #include "rexx/storage.h"
  13. #include "rexx/rxslib.h"
  14.  
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include <functions.h>
  18.  
  19.  
  20. struct RexxLib *RexxSysBase;
  21.  
  22. extern LONG  CheckRexxMsg();
  23. extern LONG  GetRexxMsg();
  24. extern LONG  SetRexxMsg();
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char **argv;
  29. {
  30.    struct MsgPort MyPort;
  31.    struct RexxMsg *rmptr;
  32.    LONG           test,quitflag,error;
  33.    char           buffer[256];
  34.    char           **strings = 0;
  35.    char           *malloc();
  36.    int            i,left,right,string_number;
  37.    int            atoi(),cmp();
  38.    STRPTR         value;
  39.    
  40.    RexxSysBase = (struct RexxLib *) 
  41.                  OpenLibrary("rexxsyslib.library",(LONG) RXSVERS);
  42.    if (RexxSysBase == 0L) {
  43.       printf("Bad News -- no REXX library\n");
  44.       return(20L);
  45.       }
  46.  
  47.    /* Initialize our message port   */
  48.    InitPort(&MyPort,"QuickSortPort");
  49.  
  50.    /* Make the port public          */
  51.    AddPort(&MyPort);
  52.  
  53.    for (;;) {                          /* wait for messages             */
  54.       Wait(1L<<MyPort.mp_SigBit);
  55.       rmptr = (struct RexxMsg *) GetMsg(&MyPort);
  56.  
  57.  
  58.  
  59.       /* Make sure it's a valid context */
  60.       if (CheckRexxMsg(rmptr)) {
  61.          
  62.          if((test = strcmp(rmptr->rm_Args[0],"QSORT")) == 0) {
  63.  
  64.          /* Get left and right bounds of string array to be sorted       */
  65.          /* when called from and exec program having added QuickSortPort */
  66.          /* as a function host Arg[1] is QuickSortPort                   */
  67.         
  68.             left = atoi(rmptr->rm_Args[1]);
  69.             right = atoi(rmptr->rm_Args[2]);
  70.             string_number = right - left + 1;        
  71.             strings = (char **)AllocMem((long)string_number*4,MEMF_PUBLIC|MEMF_CLEAR);
  72.             if(strings==NULL) goto REPLY ;
  73.             
  74.             /* copy RexxVariable array into strings */
  75.           
  76.             for(i = left; i <= right;i++){
  77.  
  78.                /* put name of stem in buffer  */
  79.  
  80.                sprintf(buffer,"%s.%d",rmptr->rm_Args[3],i);
  81.  
  82.                /* Now get the string from the program */
  83.  
  84.                if(error = GetRexxVar(rmptr,buffer,&value)) goto REPLY;
  85.  
  86.                strings[i-left] = malloc(strlen((char*)value)+1);
  87.                strcpy(strings[i-left],(char *)value);
  88.             }
  89.             
  90.             /* Now sort the strings array */
  91.            
  92.             qs_string(strings,left-1,right-1) ;
  93.  
  94.             
  95.                
  96.  
  97.          /* Put sorted array back in old stem variable */
  98.           
  99.             for(i = left; i <= right;i++){
  100.  
  101.                /* put name of stem in buffer  */
  102.  
  103.                sprintf(buffer,"%s.%d",rmptr->rm_Args[3],i);
  104.  
  105.                /* Now get the string from the program */
  106.  
  107.                error = SetRexxVar(rmptr,buffer,strings[i-left],(long)strlen(strings[i-left])); 
  108.                if(error) goto REPLY;
  109.                free(strings[i-left]);
  110.                strings[i-left]=NULL;
  111.             }
  112.  
  113.  
  114.  
  115.          }
  116.  
  117.       /* See whether it's the close command                             */
  118.          else quitflag = test = strcmp(rmptr->rm_Args[0],"CLOSE");
  119.       
  120.       }
  121.  
  122. REPLY:
  123.  
  124.      /* send it back                  */
  125.       if(test) {
  126.          rmptr->rm_Result1 = 5L;
  127.          rmptr->rm_Result2 = 1L;
  128.       }
  129.       else {
  130.         rmptr->rm_Result1 = 0L;           /* return code                   */
  131.         rmptr->rm_Result2 = 0L;           /* secondary result              */
  132.       }
  133.       ReplyMsg(rmptr); 
  134.  
  135.       
  136.       if(strings) {
  137.          for(i = 0 ; i< string_number; i++){
  138.             if( strings[i] ) free(strings[i]);
  139.          } 
  140.          FreeMem(strings,(long)string_number*4L);
  141.          strings = NULL;
  142.       }
  143.  
  144.      if (quitflag == 0L) break;            /* all done?                     */
  145.    }
  146.  
  147.    RemPort(&MyPort);                   /* unlink it                     */
  148.    FreePort(&MyPort); 
  149.                    /* release the port resources    */
  150.  
  151.    return(0L);
  152. }
  153.  
  154. qs_string(item,left,right) 
  155. char *item[];
  156. int left,right;
  157. {
  158.  
  159.    int i,j;
  160.    char *x,*y;
  161.    i = left; j=right;
  162.    x = item[(left+right)/2];
  163.  
  164.    do {
  165.      while(strcmpu(item[i],x) < 0 && i <right) i++;
  166.      while(strcmpu(item[j],x) > 0 && j >left)  j--;
  167.      
  168.      if(i<=j) {
  169.         y = item[i];
  170.         item[i] = item[j];
  171.         item[j] = y;
  172.         i++;j--;
  173.      }
  174.    } while(i<=j);
  175.  
  176.    if(left<j) qs_string(item,left,j);
  177.    if(i<right) qs_string(item,i,right);
  178. }
  179.  
  180. strcmpu(s1,s2)
  181. char *s1,*s2;
  182. {  
  183.    int swap = 1;
  184.    char *temp;
  185.   
  186.    if (strlen(s1) < strlen(s2)){
  187.       temp = s1;
  188.       s1 = s2;
  189.       s2 = temp;
  190.       swap = -1;
  191.    }
  192.  
  193.    while(*s1) {
  194.       if(toupper(*s1) - toupper(*s2)) 
  195.          return swap*(toupper(*s1) - toupper(*s2));
  196.       else {
  197.         s1++;
  198.         s2++;
  199.       }
  200.    }
  201.    return 0;
  202. }
  203.