home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / NEXTARCH / PROSPERO.M < prev    next >
Encoding:
Text File  |  1992-01-25  |  4.0 KB  |  198 lines

  1. #import "ProsperoVLINK.h"
  2.  
  3. #import <objc/List.h>
  4. #import <libc.h>
  5. #import <strings.h>
  6.  
  7. @implementation ProsperoVLINK
  8.  
  9. /* Initialize an instance from a VLINK structure.  Parsing is based on
  10.     the display_link() proceedure of archie.c in the Prospero distribution. */
  11. - initVLINK : (VLINK) v
  12. {
  13. PATTRIB link;
  14. int y,m,d,hr,min,sec;
  15. int n;
  16.  
  17.     [super init];
  18.  
  19.     /* File path, component name and file level */
  20.     filePath = (char *) malloc(strlen(v->filename)+1);
  21.     subPath = (char *) malloc(strlen(v->filename)+1);
  22.     if(filePath == NULL || subPath == NULL)
  23.         return nil;
  24.     strcpy(filePath,v->filename);
  25.     componentIndex = strlen(v->filename) - strlen(v->name);
  26.     for(n = 0, fileDepth = 0; n < strlen(filePath); n ++)
  27.         if(filePath[n] == '/')
  28.             fileDepth ++;
  29.  
  30.     /* Create a unique hostname NXAtom */
  31.     hostname = NXUniqueString(v->host);
  32.  
  33.     /* Parse v->lattrib */
  34.     link = v->lattrib;
  35.     while(link)
  36.     {
  37.         if( strcmp(SIZE,link->aname) == 0)
  38.             fileSize = atoi(link->value.ascii);
  39.         else if(strcmp(MODES,link->aname) == 0)
  40.             strncpy(unixMode,link->value.ascii,10);
  41.         else if(strcmp(DATE,link->aname) == 0)
  42.         {
  43.             sscanf(link->value.ascii,"%4d%2d%2d%2d%2d",
  44.                 &y,&m,&d,&hr,&min);
  45.             [super initFromDate : d : m : y];
  46.             sec = 0;
  47.             [super setTime : hr : min : sec];
  48.         }
  49.         link = link->next;
  50.     }
  51.     listing = nil;
  52.  
  53.     return self;
  54. }
  55.  
  56. - initVLINK : (DirEntryPtr) dirList host: (const char *) host
  57.     parent: (const char *) parentPath
  58. {
  59. int y,m,d,hr,min,sec;
  60. int n,pathLength;
  61.  
  62.     [super init];
  63.  
  64.     /* File path, component name and file level */
  65.     pathLength = strlen(parentPath) + strlen(dirList->name) + 2;
  66.     filePath = (char *) malloc(pathLength);
  67.     subPath = (char *) malloc(pathLength);
  68.     if(filePath == NULL || subPath == NULL)
  69.         return nil;
  70.     sprintf(filePath,"%s/%s",parentPath,dirList->name);
  71.     componentIndex = strlen(parentPath) + 1;
  72.     for(n = 0, fileDepth = 0; n < strlen(filePath); n ++)
  73.         if(filePath[n] == '/')
  74.             fileDepth ++;
  75.     hostname = host;
  76.  
  77.     /* File attributes */
  78.     fileSize = dirList->bytes;
  79.     strncpy(unixMode,dirList->mode,10);
  80.     sscanf(dirList->date,"%4d%2d%2d%2d%2d",
  81.                 &y,&m,&d,&hr,&min);
  82.     [super initFromDate : d : m : y];
  83.     sec = 0;
  84.     [super setTime : hr : min : sec];
  85.  
  86.     return self;
  87. }
  88.  
  89.  
  90. /* Go through the DirEntry list and generate a ProsperoVLINK with
  91.     the correct path and hostname */
  92. - setListing: (DirEntryPtr) list
  93. {
  94. DirEntryPtr next;
  95. int parentPathLength;
  96. id vlink;
  97.  
  98.     parentPathLength = strlen(filePath);
  99.     listing = [[List alloc] initCount: 0];
  100.     if(listing == nil)
  101.         return nil;
  102.  
  103.     while(list != NULL)
  104.     {
  105.         vlink = [[ProsperoVLINK alloc]  initVLINK: list  host: hostname
  106.                 parent: filePath];
  107.         if(vlink == nil)
  108.         {
  109.             [listing free];
  110.             return nil;
  111.         }
  112.         [listing addObject: vlink];
  113.         next = list->next;
  114.         free(list);
  115.         list = next;
  116.     }
  117.     return self;
  118. }
  119.  
  120. - free
  121. {
  122.     free(filePath);
  123.     free(subPath);
  124.     [listing freeObjects];
  125.     [listing free];
  126.  
  127.     return [super free];
  128. }
  129.  
  130. /* Instance variable access methods */
  131. - (int) fileSize
  132. {
  133.     return fileSize;
  134. }
  135. - (int) mode
  136. {
  137. int mode,i,j;
  138.     mode = 0;
  139.     for(i = 1,j = 8; i < 10; i ++, j --)
  140.         if(unixMode[i] != '-')
  141.             mode |= (1 << j);
  142.     return mode;
  143. }
  144. - (const char *) unixMode
  145. {
  146.     return (const char *) unixMode;
  147. }
  148. - (BOOL) isDirectory
  149. {
  150.     if(unixMode[0] == 'd')
  151.         return YES;
  152.     return NO;
  153. }
  154. - (int) fileDepth;
  155. {
  156.     return fileDepth;
  157. }
  158. - (const char *) filePath
  159. {
  160.     return (const char *) filePath;
  161. }
  162. - (const char *) fileName
  163. {
  164.     return (const char *) &filePath[componentIndex];
  165. }
  166. - (const char *) hostname
  167. {
  168.     return hostname;
  169. }
  170.  
  171. - (const char *) nameAtLevel: (int) level
  172. {
  173. int n;
  174. char *thisLevel,*nextLevel;
  175.     if(level > fileDepth)
  176.         return NULL;
  177.     strcpy(subPath,filePath);
  178.     thisLevel = subPath;
  179.     nextLevel = index(++thisLevel,'/');
  180.     n = 1;
  181.     while( n < level)
  182.     {
  183.         thisLevel = nextLevel;
  184.         nextLevel = index(++thisLevel,'/');
  185.         n ++;
  186.     }
  187.     if(nextLevel != NULL)
  188.         *nextLevel = '\0';
  189.     return (const char *) thisLevel;
  190. }
  191.  
  192. - listing
  193. {
  194.     return listing;
  195. }
  196.  
  197. @end
  198.