home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio-DSP / NU / Source / DictManager.m < prev    next >
Encoding:
Text File  |  1992-04-05  |  6.2 KB  |  246 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "DictManager.h"
  5. #import <strings.h>
  6. #import <stdlib.h>
  7. #import <appkit/Panel.h>
  8. #import <appkit/NXBrowser.h>
  9. #import <appkit/NXBrowserCell.h>
  10. #import <appkit/Text.h>
  11. #import <appkit/ScrollView.h>
  12. #import <objc/HashTable.h>
  13.  
  14. #define DICTKNT 3  /* number of dictionaries implemented */
  15. const char *dictType[DICTKNT] = {"@","i","*"} ;
  16. id dict[DICTKNT] ;
  17.  
  18. #define CHUNK 127
  19. char **addString(const char *string, int length, char **list,
  20.         int count)
  21. // Adds the specified string to a list of strings.  It allocates 
  22. // more memory in chunks as needed. Robbed and hacked from
  23. // the BusyBox app.
  24. { if(!list)
  25.     list = (char **)malloc(CHUNK*sizeof(char *));
  26.   list[count] = (char *)malloc((length+1)*sizeof(char));
  27.   strcpy(list[count], string);
  28.   count++;
  29.   if(!(count% CHUNK))
  30.     list = (char **)realloc(list,(((count/CHUNK)+1)*CHUNK)*sizeof(char *));
  31.   list[count] = NULL;
  32.   return list;
  33. }
  34.  
  35. void freeList(char **list)
  36. // Frees the array of strings
  37. { char **strings;
  38.   if(list)
  39.   { strings = list;
  40.     while (*strings)
  41.       free(*strings++);
  42.     free(list);
  43.   }
  44. }
  45.  
  46. char *
  47. copy(aStr)
  48. char *aStr ;
  49. { // mallocs memory for a copy of aStr, returns pointer
  50.   // to copy
  51.   char *newStr ;
  52.   newStr = (char *) malloc(strlen(aStr) + 1) ;
  53.   strcpy(newStr, aStr) ;
  54.   return newStr ;
  55. }
  56.  
  57.  
  58. @implementation DictManager
  59.  
  60. + initialize ;
  61. { // set up the global dictionaries
  62.   int i ;
  63.   for(i = 0 ; i < DICTKNT ; i++)
  64.   { dict[i] = [HashTable new] ;
  65.     [dict[i] initKeyDesc: "*" valueDesc: dictType[i]] ;
  66.   }
  67.   return self ;
  68. }
  69.  
  70.  
  71. + insertKey: (char *) aKey value: (void *) data type: (char *) aType ;
  72. { // insert aKey<->data pair into the dictionary indicated by aType;
  73.   int i ;
  74.   if(!strcmp(aType,"*")) // handle strings as special case
  75.   { if([dict[2] isKey: aKey]) // free up old data
  76.        free([dict[2] insertKey:(const void *) copy(aKey) value:(void *) copy(data)]) ;
  77.     else
  78.       [dict[2] insertKey: (const void *) copy(aKey) value:(void *) copy(data)] ;
  79.   }
  80.   else
  81.   { for(i = 0 ; i < DICTKNT && strcmp(aType,dictType[i]) ; i++) ;
  82.     if([dict[i] isKey: aKey]) // already exists...
  83.       [dict[i] insertKey: (const void *) copy(aKey)  value: (void *) data] ;
  84.     else 
  85.       [dict[i] insertKey: (const void *) copy(aKey)  value: (void *) data] ;
  86.   }
  87.   return self ;
  88. }
  89.       
  90.  
  91. + removeKey: (char *) aKey type: (char *) aType ;
  92. { // insert aKey<->data pair into the dictionary indicated by aType;
  93.   int i ;
  94.   if(!strcmp(aType,"*")) // handle strings as special case
  95.   { if([dict[2] isKey: aKey]) // free up old data
  96.     { free([dict[2] valueForKey: aKey]) ;
  97.       [dict[2] removeKey: aKey] ;
  98.     }
  99.   }
  100.   else
  101.   { for(i = 0 ; i < DICTKNT && strcmp(aType,dictType[i]) ; i++) ;
  102.       [dict[i] removeKey: (const void *) aKey] ;
  103.   }
  104.   return self ;
  105. }
  106.       
  107.  
  108. + (void *) valueForKey: (char *) aKey type: (char *) aType ;
  109. { int i ;
  110.   for(i = 0 ; i < DICTKNT && strcmp(aType,dictType[i]) ; i++) ;
  111.   if(i == DICTKNT)
  112.   { NXRunAlertPanel("Nu","Unknown data type: \"%s\"",NULL,NULL,NULL,aType) ;
  113.     return (void *) 0 ;
  114.   }
  115.   else
  116.     return [dict[i] valueForKey: aKey] ;
  117. }
  118.  
  119. - (int)browser:sender fillMatrix:matrix inColumn:(int)column ;
  120. { if(column == 0)
  121.     return DICTKNT ;
  122.   else
  123.   { char typeBuf[24] ;
  124.     int i,j ;
  125.     const void  *key; 
  126.     void  *value; 
  127.     NXHashState  state ;
  128.     id table ;
  129.     char **aList = NULL ;  
  130.     strcpy(typeBuf, 
  131.       [[[browserView matrixInColumn: 0] selectedCell] stringValue]) ;
  132.     for(i = 0 ; i < DICTKNT && strcmp(typeBuf,dictType[i]) ; i++) ;
  133.     table = dict[i] ; 
  134.     state =  [table initState]; 
  135.     j = 0 ;
  136.     while ([table nextState: &state key: &key value: &value]) 
  137.       aList = addString((const char *) key, strlen(key), aList, j++) ;
  138.     freeList(browserList) ;
  139.     browserList = aList ;
  140.     return j ;
  141.   }
  142. }
  143.  
  144.  
  145. - browser:sender loadCell:cell atRow:(int)row inColumn:(int)column ;
  146. { if(column == 0)
  147.   { [cell setStringValue: dictType[row]] ;
  148.     [cell setLeaf: NO] ;
  149.   }
  150.   else
  151.   { [cell setStringValue: browserList[row]] ;
  152.     [cell setLeaf:YES] ;
  153.   }
  154.   return self ;
  155. }
  156.  
  157. - browserHit:sender ;
  158. { char path[128] ;
  159.   char type[2], *key[128] ;
  160.   if([browserView selectedColumn] != 1)
  161.     return self ;
  162.   [browserView setPathSeparator: ' '] ;
  163.   [browserView getPath: path toColumn: 2] ;
  164.   sscanf(path,"%s %s",&type,&key) ;
  165.   if(!strcmp(type,"@"))
  166.   { id anId = (id) [dict[0] valueForKey:key] ; 
  167.     [textView setText: [anId name]] ;
  168.     return self ;
  169.   }
  170.   if(!strcmp(type,"i"))
  171.   { char buf[128] ;
  172.     int i = (int) [dict[1] valueForKey:key] ;
  173.     sprintf(buf,"%d", i) ;
  174.     [textView setText: buf] ;
  175.     return self ;
  176.   }
  177.   if(!strcmp(type,"*"))
  178.   { [textView setText: [dict[2] valueForKey:key]] ;
  179.     return self ;
  180.   }  
  181.   return self ;
  182. }
  183.  
  184. - delete: sender ;
  185. { char path[128] ;
  186.   char type[2], *key[128] ;
  187.   int i = 0;
  188.   // delete the selected dictionary entry
  189.   if([browserView selectedColumn] != 1)
  190.     return self ;
  191.   [browserView setPathSeparator: ' '] ;
  192.   [browserView getPath: path toColumn: 2] ;
  193.   sscanf(path,"%s %s",&type,&key) ;
  194.   if(!strcmp(type,"@"))
  195.      i = 0 ;
  196.   else if(!strcmp(type,"i"))
  197.     i = 1 ;
  198.   else if(!strcmp(type,"*"))
  199.     i = 2 ;
  200.   [dict[i] removeKey:key] ;
  201.   sprintf(path," %s",&type) ;
  202.   [browserView loadColumnZero] ;
  203.   [browserView setPath: path] ;
  204.   [textView setText: ""] ;
  205.   return self ;
  206. }
  207.  
  208. - makeKeyAndOrderFront: sender ;
  209. { [browserView loadColumnZero] ;
  210.   return [super makeKeyAndOrderFront: sender] ;
  211. }
  212.  
  213. - newValue: sender ;
  214. { char path[128] ;
  215.   char type[2], key[128], *theText ;
  216.   int textLen ;
  217.   if([browserView selectedColumn] != 1)
  218.     return self ;
  219.   [browserView setPathSeparator: ' '] ;
  220.   [browserView getPath: path toColumn: 2] ;
  221.   sscanf(path,"%s %s",&type,&key) ;
  222.   if(!strcmp(type,"@"))
  223.   { [textView setText: "Sorry: cannot change value of an id"] ;
  224.     return self ;
  225.   }
  226.   textLen = [textView textLength] ;
  227.   theText = (char *) malloc(textLen + 1) ;
  228.   [textView getSubstring: theText start:0 length: textLen] ; 
  229.   if(!strcmp(type,"i"))
  230.   { int i ;
  231.     sscanf(theText,"%d",&i) ;
  232.     [DictManager insertKey: key value: (void *) i type: "i"] ;
  233.   }
  234.   else if(!strcmp(type,"*"))
  235.     [DictManager insertKey: key value: (void *) theText type: "*"] ;
  236.   free(theText) ;
  237.   return self ;
  238. }  
  239.  
  240. - setTextView: anObject ;
  241. { textView = [anObject docView] ;
  242.   return self ;
  243. }
  244.  
  245. @end
  246.