File: fileElement.m

package info (click to toggle)
ftp.app 0.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 420 kB
  • ctags: 30
  • sloc: objc: 2,105; makefile: 55; sh: 17
file content (394 lines) | stat: -rw-r--r-- 14,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 Project: FTP

 Copyright (C) 2005 Riccardo Mottola

 Author: Riccardo Mottola

 Created: 2005-04-18

 Single element of a file listing

 This application is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This application is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Library General Public License for more details.

 You should have received a copy of the GNU General Public
 License along with this library; if not, write to the Free
 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#import "fileElement.h"


@implementation fileElement

- (void)dealloc
{
    [filename release];
    [super dealloc];
}

/*
 initialize a file element using attrbutes of NSFileManager
 */
- (id)initWithFileAttributes :(NSString *)fname :(NSDictionary *)attribs
{
    [super init];

    size = [attribs fileSize];
    modifDate = [[attribs objectForKey:NSFileModificationDate] retain];
    if ([attribs fileType] == NSFileTypeDirectory)
        isDir = YES;
    else
        isDir = NO;
    filename = [fname retain];
    
    return self;
}

/* as a parser aid, check if a string is a month */
- (int)checkMonth: (NSString *)token
{
    if ([token compare:@"Jan" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 1;
    
    if ([token compare:@"Feb" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 2;
    
    if ([token compare:@"Mar" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 3;
    
    if ([token compare:@"Apr" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 4;
    
    if ([token compare:@"May" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 5;
    
    if ([token compare:@"Jun" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 6;
    
    if ([token compare:@"Jul" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 7;
    
    if ([token compare:@"Aug" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 8;
    
    if ([token compare:@"Sep" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 9;
    
    if ([token compare:@"Oct" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 10;
    
    if ([token compare:@"Nov" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 11;
    
    if ([token compare:@"Dec" options:NSCaseInsensitiveSearch ] == NSOrderedSame)
        return 12;
    
    return 0;
}

/*
 tries to parse a single line of LIST
 currently unix-style results will work, like

 drwx------  22 multix  staff   704 Apr  2 14:46 Documents

 or these where one of user/group are omitted
 drwxr-xr-x   8 users     4096 Apr 15 19:58 Documents
 
 inital blocks can be omitted, like:
 drwx------  multix  staff   704 Apr  2 14:46 Documents
 
 user/group could be numerical
 drwx------  22 4567  120   704 Apr  2 14:46 Documents
 
 the hour time could be the year
 drwx------  22 multix  staff   704 Apr  2 2005 Documents
 
 the filename could contain one or more spaces
 -rw-r--r--   1 multix  staff     0 May 25 10:08 test file
 
 the filesize can be zero..
 
 it will skip a line that is not considered meaningful, like:
 total 74184 (typical from Unix ls -l)
 and return nil.
 
 some attempt at scanning OS400 / i5 ftp listings is attempted too,
 those are in the style of:
 SVIFELMA        32768 02/05/07 15:32:42 *FILE      INSOLUTO
 SVIFELMA                                *MEM       INSOLUTO.INSOLUTO

 the feof line is ignored
 
 */
- (id)initWithLsLine :(char *)line
{
    NSString       *fullLine;
    NSMutableArray *splitLine;
    unichar        ch;
    unsigned       elementsFound;
    NSCharacterSet *whiteSet;
    unsigned       lineLen;
    NSRange        searchRange;
    NSRange        tokenEnd;
    NSRange        tokenRange;
    BOOL           foundStandardMonth;
    BOOL           foundOneBeforeMonth;
    

    [super init];

    // typical Unix end of listing
    if (strstr(line, "total") == line)
        return nil;
    
    // typical IBM OS400 end of listing
    if (strstr(line, "feof") == line)
        return nil;

    
    whiteSet = [NSCharacterSet whitespaceCharacterSet];
    splitLine = [NSMutableArray arrayWithCapacity:5];
    fullLine = [NSString stringWithCString:line];
    lineLen = [fullLine length];
    ch = [fullLine characterAtIndex:0];
    if (ch == '-' || ch == 'd' || ch == 'l')
    {
        // this is a unix-like listing
        unsigned cursor;

        // file permissions block
        cursor = 0;
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically links - user - group - size
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"second token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
            
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"third token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"fourth token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"fifth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        foundOneBeforeMonth = [self checkMonth:[splitLine objectAtIndex:4]];
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically month
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"sixth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        foundStandardMonth = [self checkMonth:[splitLine objectAtIndex:5]];
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically day of the month
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//        NSLog(@"seventh token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);
        
        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;
        
        // typically year or hour
        // but it could be fileName already
        if (foundStandardMonth)
        {
            searchRange = NSMakeRange(cursor, lineLen-cursor);
            tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
            tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
//            NSLog(@"eighth token: %@ %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange), [fullLine substringWithRange:tokenRange]);
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
            cursor = NSMaxRange(tokenEnd);
        
            while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
                cursor++;
        }
        
        // typically the filename
        if (cursor < lineLen)
        {
            tokenRange = NSMakeRange(cursor, lineLen-cursor);
//            NSLog(@"last token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        }
        
        elementsFound = [splitLine count];
        
        // copy back the found data
        if (foundStandardMonth && elementsFound == 9)
        {
            // everything is fine and ok, we have a full-blown listing and parsed it well;            
            isDir = NO;
            if ([[splitLine objectAtIndex:0] characterAtIndex:0] == 'd')
                isDir = YES;
            [[NSScanner scannerWithString: [splitLine objectAtIndex:4]] scanLongLong:(long long*)&size];
            filename = [[splitLine objectAtIndex:8] retain];
        } else if (foundOneBeforeMonth && elementsFound == 8)
        {
            // we miss the link count or user probably
            isDir = NO;
            if ([[splitLine objectAtIndex:0] characterAtIndex:0] == 'd')
                isDir = YES;
            [[NSScanner scannerWithString: [splitLine objectAtIndex:3]] scanLongLong:(long long*)&size];
            filename = [[splitLine objectAtIndex:7] retain];
        } else
            return nil;
        
    } else if ((fullLine != NULL) && ([fullLine rangeOfString: @"*FILE"].location != NSNotFound))
    {
        // maybe it is an IBM AS400 / i5 style line
	// this is an IBM listing, having the *FILE element
        unsigned cursor;

        // username block
        cursor = 0;
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
        tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// file size        
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// date     
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// time       
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// record type       
        searchRange = NSMakeRange(cursor, lineLen-cursor);
        tokenEnd = [fullLine rangeOfCharacterFromSet:whiteSet options:0 range:searchRange];
    	tokenRange = NSMakeRange(cursor, tokenEnd.location-cursor);
        [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        cursor = NSMaxRange(tokenEnd);

        while ([fullLine characterAtIndex:cursor] == ' ' && cursor <= lineLen)
            cursor++;

    	// file name    
                // typically the filename
        if (cursor < lineLen)
        {
            tokenRange = NSMakeRange(cursor, lineLen-cursor);
//            NSLog(@"last token: %@ %@", NSStringFromRange(tokenEnd), NSStringFromRange(tokenRange));
            [splitLine addObject:[fullLine substringWithRange:tokenRange]];
        }

    	// everything is fine and ok, we have a full-blown listing and parsed it well;            
        isDir = NO; /* OS400 is not hierarchical */
        [[NSScanner scannerWithString: [splitLine objectAtIndex:1]] scanLongLong:(long long*)&size];
        filename = [[splitLine objectAtIndex:5] retain];

    } else
    {
	/* we don't know better */
	return nil;
    }
    
    /* let's ignore the current and the parent directory some servers send over... */
    if([filename isEqualToString:@"."])
        return nil;
    else if([filename isEqualToString:@".."])
        return nil;

    return self;
}

/* accessors */
- (NSString *)filename
{
    return self->filename;
}

- (BOOL)isDir
{
    return isDir;
}

- (unsigned long long)size
{
    return size;
}


@end