home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Examples / AppKit / Backspace / ModuleList.m < prev    next >
Encoding:
Text File  |  1993-07-15  |  2.6 KB  |  155 lines

  1. //
  2. //  ModuleList.m
  3. //
  4. //    a simple storage class that holds all the information BackSpace needs
  5. //  about each module.  This file contains 2 classes; one for the information
  6. //  on a single module and one for a list to store those ModuleInfo's
  7. //
  8. //  You may freely copy, distribute, and reuse the code in this example.
  9. //  NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  10. //  fitness for any particular use.
  11.  
  12.  
  13.  
  14. #import "ModuleList.h"
  15. #import <stdlib.h>            // for free() etc
  16. #import <strings.h>            // for strcasecmp()
  17. #import <objc/hashtable.h>        // for NXCopyStringBuffer()
  18.  
  19. #define str_copy(str)    ((str == NULL) ? NULL : NXCopyStringBuffer(str))
  20. #define str_free(str)    {if (str) free(str);}
  21.  
  22.  
  23. @implementation ModuleInfo
  24. - init
  25. {    return [self initWithView:nil name:NULL path:NULL];
  26. }
  27.  
  28. - initWithView:aView name:(const char *)aName path:(const char *)aPath
  29. {
  30.     [super init];
  31.     view = aView;
  32.     viewName = str_copy(aName);
  33.     path = str_copy(aPath);
  34.     return self;
  35. }
  36.  
  37. - setView:newView
  38. {
  39.     id oldView = view;
  40.     view = newView;
  41.     return oldView;
  42. }
  43.  
  44. - view
  45. {    return view;
  46. }
  47.  
  48. - setHeader:(struct mach_header *)h
  49. {    header = h;
  50.     return self;
  51. }
  52.  
  53. - (struct mach_header *) header
  54. {    return header;
  55. }
  56.  
  57. - (const char *) viewName
  58. {    return viewName;
  59. }
  60.  
  61. - (const char *) path
  62. {    return path;
  63. }
  64.  
  65. - setPath: (const char *)p
  66. {
  67.     str_free(path);
  68.     path = str_copy(p);
  69.     return self;
  70. }
  71.  
  72. - appendPath: (const char *)p
  73. {
  74.     if (altPaths)
  75.     {
  76.         altPaths = realloc(altPaths,strlen(altPaths)+strlen(p)+2);
  77.         strcat(altPaths,"\t");
  78.         strcat(altPaths,p);
  79.     }
  80.     else altPaths = str_copy(p);
  81.     return self;
  82. }
  83.  
  84. // if the path is bogus, this will set the path to the next one
  85. // returns self if successful, nil if there is no additional path
  86. - useNextPath
  87. {
  88.     char *p1, *p2;
  89.  
  90.     if (altPaths)
  91.     {
  92.         p1 = p2 = altPaths;
  93.         while (*p1 && *p1 != '\t') p1++;
  94.         if (*p1 == '\t')
  95.         {
  96.             *p1=0;
  97.             path = realloc(path,strlen(p1)+1);
  98.             strcpy(path,p1);
  99.             while (*p2++ = *p1++);
  100.             altPaths = realloc(altPaths,strlen(altPaths)+1);
  101.         }
  102.         else        // last one
  103.         {
  104.             str_free(path);
  105.             path = altPaths;
  106.             altPaths = NULL;
  107.         }
  108.         return self;
  109.     }
  110.     return nil;
  111. }
  112.  
  113. - discardAltPaths
  114. {
  115.     str_free(altPaths);
  116.     altPaths = NULL;
  117.     return self;
  118. }
  119.  
  120. - free
  121. {
  122.     [view free];
  123.     str_free(viewName);
  124.     str_free(path);
  125.     str_free(altPaths);
  126.     return [super free];
  127. }
  128.  
  129. @end
  130.  
  131. @implementation ModuleList
  132.  
  133. - (const char *) nameAt: (int) i
  134. {
  135.     return [[self objectAt: i] viewName];
  136. }
  137.  
  138. - viewAt: (int) i
  139. {
  140.     return [[self objectAt: i] view];
  141. }
  142.  
  143. static int docompare(const void *x, const void *y)
  144. {
  145.     return strcasecmp([(id)(*(ModuleInfo **)x) viewName], [(id)(*(ModuleInfo **)y) viewName]);
  146. }
  147.  
  148. - sort
  149. {
  150.     qsort((ModuleInfo **)dataPtr, numElements, sizeof(id), docompare);
  151.     return self;
  152. }
  153.  
  154. @end
  155.