home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / COWS / Code / COWSArrayLibrary.m < prev    next >
Encoding:
Text File  |  1994-03-25  |  5.3 KB  |  274 lines

  1. /*
  2.     Copyright (C) 1994 Sean Luke
  3.  
  4.     COWSArrayLibrary.m
  5.     Version 1.0
  6.     Sean Luke
  7. */
  8.  
  9.  
  10. #import "COWSArrayLibrary.h"
  11. #import "COWSArrayNode.h"
  12. #import <stdio.h>
  13.  
  14. @implementation COWSArrayLibrary
  15.  
  16. - init
  17.     {
  18.     id returnval=[super init];
  19.     hashtable=[[HashTable alloc] initKeyDesc:"*"valueDesc:"@"];
  20.     counter=0;
  21.     return returnval;
  22.     }
  23.  
  24.  
  25. - free
  26.     {
  27.     [hashtable freeObjects];
  28.     [hashtable empty];
  29.     [hashtable free];
  30.     return [super free];    
  31.     }
  32.     
  33.  
  34. - loadLibrary:sender
  35.     {
  36.     [sender addLibraryFunction:"make-array"
  37.             selector:@selector(array_makearray:)
  38.             target:self];
  39.     [sender addLibraryFunction:"free-array"
  40.             selector:@selector(array_freearray:)
  41.             target:self];
  42.     [sender addLibraryFunction:"set-array"
  43.             selector:@selector(array_setarray:)
  44.             target:self];
  45.     [sender addLibraryFunction:"array"
  46.             selector:@selector(array_getarray:)
  47.             target:self];
  48.     [sender addLibraryFunction:"array?"
  49.             selector:@selector(array_getarray:)
  50.             target:self];
  51.     
  52.     // clear out objects
  53.     [hashtable freeObjects];
  54.     [hashtable empty];
  55.     return self;
  56.     }
  57.  
  58.     
  59.  
  60.  
  61. - array_makearray:arg_list
  62.     {
  63.     id return_val=[[COWSStringNode alloc] init];
  64.     int limits[COWSMAXARRAYLIMITS];
  65.     int x=0;
  66.     char key[255]; /*some big number */
  67.     id current;
  68.     
  69.     while ([arg_list top]!=NULL)
  70.         {
  71.         if (x==COWSMAXARRAYLIMITS)        // too many limits already
  72.             {
  73.             [return_val setString:"make-array error:  too many array limits"];
  74.             [return_val setError:YES];
  75.             return return_val;
  76.             }
  77.         current=[arg_list pop];
  78.         limits[x]=(int) atof([current string]);
  79.         [current free];
  80.         x++;
  81.         }
  82.     
  83.     current=[[COWSArrayNode alloc] initTable:(const int*) limits:x];
  84.     if (current==NULL)
  85.         {
  86.         [return_val setString:"make-array error:  weird array limits"];
  87.         [return_val setError:YES];
  88.         return return_val;
  89.         }
  90.     
  91.     sprintf(key,"array %d",counter);
  92.     [hashtable insertKey:newstr(key) value:current];
  93.     counter++;
  94.  
  95.     [return_val setString:key];
  96.     return return_val;
  97.     }
  98.  
  99.  
  100. - array_freearray:arg_list        // removes by first item no matter what
  101.     {
  102.     id return_val=[[COWSStringNode alloc] init];
  103.     if ([arg_list top]!=NULL)
  104.         {
  105.         id current=[arg_list pop];
  106.         [hashtable removeKey: (const void*) [current string]];
  107.         [current free];
  108.         [return_val setString:"t"];
  109.         return return_val;
  110.         }
  111.     [return_val setString:"free-array error:  nothing to free"];
  112.     [return_val setError:YES];
  113.     return return_val;
  114.     }
  115.  
  116.  
  117. - array_setarray:arg_list
  118.     {
  119.     id return_val=[[COWSStringNode alloc] init];
  120.     id array;
  121.     id value;
  122.     id answer;
  123.     int coords[COWSMAXARRAYLIMITS];
  124.     int x=0;
  125.  
  126.     if ([arg_list top]!=NULL)        // no array name
  127.         {
  128.         id current=[arg_list pop];
  129.         array=[hashtable valueForKey:(const void*)[current string]];
  130.         [current free];
  131.         if (array==nil)
  132.             {
  133.             [return_val setString:"set-array error:  no such array"];
  134.             [return_val setError:YES];
  135.             return return_val;
  136.             }
  137.         }
  138.     else
  139.         {
  140.         [return_val setString:"set-array error:  no array to set"];
  141.         [return_val setError:YES];
  142.         return return_val;
  143.         }
  144.         
  145.     if ([arg_list top]!=NULL)        // no value to set
  146.         {
  147.         value=[arg_list pop];
  148.         }
  149.     else
  150.         {
  151.         [return_val setString:"set-array error:  no value to set"];
  152.         [return_val setError:YES];
  153.         return return_val;
  154.         }
  155.         
  156.     while ([arg_list top]!=NULL)
  157.         {
  158.         id current;
  159.         if (x==COWSMAXARRAYLIMITS)        // too many limits already
  160.             {
  161.             [return_val setString:
  162.                 "set-array error:  too many coordinates"];
  163.             [return_val setError:YES];
  164.             return return_val;
  165.             }
  166.         current=[arg_list pop];
  167.         coords[x]=(int) atof([current string]);
  168.         [current free];
  169.         x++;
  170.         }
  171.  
  172.     answer=[array setValue:(const int*) coords:[value string]];
  173.  
  174.     [return_val setString:[value string]];
  175.     [value free];
  176.     
  177.     if (answer==NULL)
  178.         {
  179.         [return_val setString:"set-array error:  weird coordinates"];
  180.         [return_val setError:YES];
  181.         return return_val;
  182.         }
  183.         
  184.     return return_val;
  185.     }
  186.  
  187.  
  188. - array_getarray:arg_list
  189.     {
  190.     id return_val=[[COWSStringNode alloc] init];
  191.     id array;
  192.     const char* answer;
  193.     int coords[COWSMAXARRAYLIMITS];
  194.     int x=0;
  195.  
  196.     if ([arg_list top]!=NULL)        // no array name
  197.         {
  198.         id current=[arg_list pop];
  199.         array=[hashtable valueForKey:(const void*)[current string]];
  200.         [current free];
  201.         if (array==nil)
  202.             {
  203.             [return_val setString:"array error:  no such array"];
  204.             [return_val setError:YES];
  205.             return return_val;
  206.             }
  207.         }
  208.     else
  209.         {
  210.         [return_val setString:"array error:  no array to set"];
  211.         [return_val setError:YES];
  212.         return return_val;
  213.         }
  214.         
  215.     while ([arg_list top]!=NULL)
  216.         {
  217.         id current;    
  218.         if (x==COWSMAXARRAYLIMITS)        // too many limits already
  219.             {
  220.             [return_val setString:"array error:  too many coordinates"];
  221.             [return_val setError:YES];
  222.             return return_val;
  223.             }
  224.         current=[arg_list pop];
  225.         coords[x]=(int) atof([current string]);
  226.         [current free];
  227.         x++;
  228.         }
  229.  
  230.     answer=[array value:(const int*) coords];
  231.  
  232.     if (answer==NULL)
  233.         {
  234.         [return_val setString:"array error:  weird coordinates"];
  235.         [return_val setError:YES];
  236.         return return_val;
  237.         }
  238.         
  239.     [return_val setString:answer];
  240.     return return_val;
  241.     }
  242.  
  243.  
  244. - array_isarray:arg_list
  245.     {
  246.     id return_val=[[COWSStringNode alloc] init];
  247.     id array;
  248.  
  249.     if ([arg_list top]!=NULL)        // no array name
  250.         {
  251.         id current=[arg_list pop];
  252.         array=[hashtable valueForKey:(const void*)[current string]];
  253.         [current free];
  254.         if (array==nil)
  255.             {
  256.             [return_val setString:"f"];
  257.             }
  258.         else [return_val setString:"t"];
  259.         }
  260.     else
  261.         {
  262.         [return_val setString:"array error:  no array to set"];
  263.         [return_val setError:YES];
  264.         return return_val;
  265.         }
  266.     return return_val;
  267.     }
  268.  
  269.  
  270. - pauseCancelled:sender
  271.     {
  272.     return self;
  273.     }
  274. @end