home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / SpaceSaver / Source / ModuleList.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.9 KB  |  156 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. #import "ModuleList.h"
  13. #import <stdlib.h> // for free() etc
  14. #import <strings.h>     // for strcasecmp()
  15. #import <objc/hashtable.h> // for NXCopyStringBuffer()
  16.  
  17. #define str_copy(str) ((str == NULL) ? NULL : NXCopyStringBuffer(str))
  18. #define str_free(str) { if (str != NULL) free(str); }
  19.  
  20. @implementation ModuleInfo
  21.  
  22. - init { return [self initWithView:nil name:NULL path:NULL]; }
  23.  
  24. - initWithView:aView name:(const char *) aName path:(const char *) aPath
  25. {
  26.     [super init];
  27.  
  28.     view = aView;
  29.     viewName = str_copy(aName);
  30.     path = str_copy(aPath);
  31.  
  32.     return self;
  33. }
  34.  
  35. - setView:newView
  36. {
  37.     id oldView = view;
  38.  
  39.     view = newView;
  40.  
  41.     return oldView;
  42. }
  43.  
  44. - view { return view; }
  45.  
  46. - setHeader:(struct mach_header *) h { header = h; return self; }
  47.  
  48. - (struct mach_header *) header { return header; }
  49.  
  50. - (const char *) viewName { return viewName; }
  51.  
  52. - (const char *) path { return path; }
  53.  
  54. - setPath:(const char *) p
  55. {
  56.     str_free(path);
  57.     path = str_copy(p);
  58.  
  59.     return self;
  60. }
  61.  
  62. - appendPath:(const char *) p
  63. {
  64.     if (altPaths != NULL) {
  65.         altPaths = realloc(altPaths, strlen(altPaths) + strlen(p) + 2);
  66.         (void) strcat(strcat(altPaths,"\t"), p);
  67.         }
  68.     else altPaths = str_copy(p);
  69.  
  70.     return self;
  71. }
  72.  
  73. // if the path is bogus, this will set the path to the next one
  74. // returns self if successful, nil if there is no additional path
  75.  
  76. - useNextPath
  77. {
  78.     char *p1 = altPaths, *p2 = altPaths;
  79.  
  80.     if (altPaths == NULL) return nil;
  81.  
  82.     while (*p1 != '\0' && *p1 != '\t') p1++;
  83.  
  84.     if (*p1 == '\t') {
  85.         *p1 = '\0';
  86.         path = realloc(path, strlen(p1) + 1);
  87.         (void) strcpy(path, p1);
  88.         while (*p2++ = *p1++);
  89.         altPaths = realloc(altPaths, strlen(altPaths) + 1);
  90.         }
  91.     else { // last one
  92.         str_free(path);
  93.         path = altPaths;
  94.         altPaths = NULL;
  95.         }
  96.  
  97.     return self;
  98. }
  99.  
  100. - discardAltPaths
  101. {
  102.     str_free(altPaths);
  103.     altPaths = NULL;
  104.  
  105.     return self;
  106. }
  107.  
  108. - free
  109. {
  110.     [view free];
  111.     str_free(viewName);
  112.     str_free(path);
  113.     str_free(altPaths);
  114.  
  115.     return [super free];
  116. }
  117.  
  118. @end
  119.  
  120. @implementation ModuleList
  121.  
  122. - (const char *) nameAt:(int) i
  123. {
  124.     return [[self objectAt:i] viewName];
  125. }
  126.  
  127. - (int) indexOfName:(const char *) name;
  128. {
  129.     int i, n = [self count];
  130.  
  131.     for (i = 0; i < n; i++) {
  132.         if (strcmp(name, [[self objectAt:i] viewName]) == 0) return i;
  133.         }
  134.  
  135.     return -1;
  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.  
  152.     return self;
  153. }
  154.  
  155. @end
  156.