The R Project SVN R-packages

Rev

Rev 5095 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
714 iacus 1
/*
2
 *  R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
3
 *  
4
 *  R.app Copyright notes:
5095 urbaneks 5
 *                     Copyright (C) 2004-8  The R Foundation
714 iacus 6
 *                     written by Stefano M. Iacus and Simon Urbanek
7
 *
8
 *                  
9
 *  R Copyright notes:
10
 *                     Copyright (C) 1995-1996   Robert Gentleman and Ross Ihaka
11
 *                     Copyright (C) 1998-2001   The R Development Core Team
12
 *                     Copyright (C) 2002-2004   The R Foundation
13
 *
14
 *  This program is free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 2 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  This program is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  A copy of the GNU General Public License is available via WWW at
25
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
26
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
27
 *  Suite 330, Boston, MA  02111-1307  USA.
28
 */
568 iacus 29
 
714 iacus 30
 
568 iacus 31
#import "FileCompletion.h"
32
 
33
 
34
@implementation FileCompletion
35
 
4361 urbaneks 36
+ (NSArray*) completeAll: (NSString*) part cutPrefix: (int) prefix {
37
	//NSLog(@"FileCompletion completeAll: '%@' curPrefix: %d", part, prefix);
568 iacus 38
 
973 urbaneks 39
    int tl = [part length];
40
    int ls = tl-1, fb;
4361 urbaneks 41
    NSString *dir = nil;
42
    BOOL working=NO, voidFn=NO, homeCompletion=NO;
973 urbaneks 43
    NSString *fn;
44
 
45
    //NSLog(@"attepted file-completion: \"%@\"", part);
4361 urbaneks 46
    while (ls>0 && [part characterAtIndex:ls]!='/') ls--; // ls=last / || 0
47
    if (ls<1 && (tl==0 || [part characterAtIndex:ls]!='/')) {
973 urbaneks 48
        working=YES;
4361 urbaneks 49
		if (tl > 0 && [part characterAtIndex:0]=='~') {
50
			// this is not easy - stricly speaking we should look for all users
51
			// but we use a trick: we find our home and strip the last component
52
			dir = [[@"~" stringByExpandingTildeInPath] stringByDeletingLastPathComponent];
4362 urbaneks 53
			ls = 1; homeCompletion = YES;
4361 urbaneks 54
		}
55
	}
56
    if (!dir) dir = working?@".":((ls==0)?@"/":[part substringToIndex:ls]);
57
	if ([dir characterAtIndex:0] == '~') dir = [dir stringByExpandingTildeInPath];
973 urbaneks 58
    fb=ls; if (fb<tl && [part characterAtIndex:fb]=='/') fb++;
3283 urbaneks 59
    fn=(fb>=0 && fb<tl)?[part substringFromIndex:fb]:@"";
60
	if ([fn length]==0) voidFn=YES;
973 urbaneks 61
    //NSLog(@"directory to look in: \"%@\" for entry beginning with \"%@\"", dir, fn);
62
    {
63
        NSArray *a = [[NSFileManager defaultManager] directoryContentsAtPath:dir];
64
        if (a==nil) return nil;
5095 urbaneks 65
	NSMutableArray *ca = [NSMutableArray arrayWithCapacity: 8];
973 urbaneks 66
        { 
5707 urbaneks 67
            int i=0, matches=0;
973 urbaneks 68
            NSString *common=nil;
69
            while (i<[a count]) {
70
                NSString *sx = (NSString*) [a objectAtIndex:i];
3283 urbaneks 71
                if (voidFn || [sx hasPrefix: fn]) {
4361 urbaneks 72
					BOOL isDir;
73
					if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", dir, sx] isDirectory:&isDir] && isDir)
74
						sx = [sx stringByAppendingString:@"/"];
75
					if (homeCompletion) sx = [@"~" stringByAppendingString:sx];
5707 urbaneks 76
                    if (matches==0)
77
                            common = [[NSString alloc] initWithString: sx];
78
                    else {
973 urbaneks 79
                        NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
80
                        [common release];
81
                        common=cpref;
82
                    }
83
					[ca addObject: [sx substringFromIndex:prefix]];
84
                    matches++;
85
                }
86
                i++;
87
            }
5095 urbaneks 88
	    if (common) [common release];
973 urbaneks 89
			return ca; 
90
        }
91
    }
92
    return nil;
93
}
94
 
568 iacus 95
@end