The R Project SVN R-packages

Rev

Rev 1254 | Rev 4361 | Go to most recent revision | 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:
1254 iacus 5
 *                     Copyright (C) 2004-5  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
 
36
+ (NSString*) complete: (NSString*) part {
37
    int tl = [part length];
38
    int ls = tl-1, fb;
39
    NSString *dir;
40
    BOOL working=NO;
41
    NSString *fn;
42
 
43
    //NSLog(@"attepted file-completion: \"%@\"", part);
44
    while (ls>0 && [part characterAtIndex:ls]!='/') ls--;
45
    if (ls<1 && (tl==0 || [part characterAtIndex:ls]!='/'))
46
        working=YES;
47
    dir=working?@".":((ls==0)?@"/":[part substringToIndex:ls]);
48
    fb=ls; if (fb<tl && [part characterAtIndex:fb]=='/') fb++;
49
    fn=(fb<tl)?[part substringFromIndex:fb]:@"";
50
    //NSLog(@"directory to look in: \"%@\" for entry beginning with \"%@\"", dir, fn);
51
    {
52
        NSArray *a = [[NSFileManager defaultManager] directoryContentsAtPath:dir];
53
        if (a==nil) return nil;
54
        { 
55
            int i=0, firstMatch=-1, matches=0;
56
            NSString *common=nil;
57
            while (i<[a count]) {
58
                NSString *sx = (NSString*) [a objectAtIndex:i];
59
                if ([sx hasPrefix: fn]) {
60
                    if (matches==0) {
61
                        firstMatch=i;
62
                        common=[[NSString alloc] initWithString: sx];
63
                    } else {
64
                        NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
65
                        [common release];
66
                        common=cpref;
67
                    }
68
                    matches++;
69
                }
70
                i++;
71
            }
72
            if (common!=nil) {
73
                NSString *fnp=[common autorelease];
74
                BOOL isDir=NO;
75
                fnp = ((dir==@".")?fnp:
76
                       ((dir==@"/")?[@"/" stringByAppendingString:fnp]:
77
                        [[dir stringByAppendingString:@"/"] stringByAppendingString:fnp]));
78
                if ([[NSFileManager defaultManager] fileExistsAtPath:fnp isDirectory:&isDir] && isDir)
79
                    fnp = [fnp stringByAppendingString:@"/"];
80
                if ([fnp isEqualToString:@"//"])
81
                    fnp=@"/";
82
                return fnp;
83
            }
84
        }
85
    }
86
    return nil;
87
}
88
 
973 urbaneks 89
+ (NSArray*) completeAll: (NSString*) part cutPrefix: (int) prefix {
90
    int tl = [part length];
91
    int ls = tl-1, fb;
92
    NSString *dir;
93
    BOOL working=NO;
94
    NSString *fn;
95
	NSMutableArray *ca = [[NSMutableArray alloc] initWithCapacity: 8];
96
 
97
    //NSLog(@"attepted file-completion: \"%@\"", part);
98
    while (ls>0 && [part characterAtIndex:ls]!='/') ls--;
99
    if (ls<1 && (tl==0 || [part characterAtIndex:ls]!='/'))
100
        working=YES;
101
    dir=working?@".":((ls==0)?@"/":[part substringToIndex:ls]);
102
    fb=ls; if (fb<tl && [part characterAtIndex:fb]=='/') fb++;
103
    fn=(fb<tl)?[part substringFromIndex:fb]:@"";
104
    //NSLog(@"directory to look in: \"%@\" for entry beginning with \"%@\"", dir, fn);
105
    {
106
        NSArray *a = [[NSFileManager defaultManager] directoryContentsAtPath:dir];
107
        if (a==nil) return nil;
108
        { 
109
            int i=0, firstMatch=-1, matches=0;
110
            NSString *common=nil;
111
            while (i<[a count]) {
112
                NSString *sx = (NSString*) [a objectAtIndex:i];
113
                if ([sx hasPrefix: fn]) {
114
                    if (matches==0) {
115
                        firstMatch=i;
116
                        common=[[NSString alloc] initWithString: sx];
117
                    } else {
118
                        NSString *cpref=[[NSString alloc] initWithString:[common commonPrefixWithString:sx options:0]];
119
                        [common release];
120
                        common=cpref;
121
                    }
122
					[ca addObject: [sx substringFromIndex:prefix]];
123
                    matches++;
124
                }
125
                i++;
126
            }
127
			return ca; 
128
			/*
129
            if (common!=nil) {
130
                NSString *fnp=[common autorelease];
131
                BOOL isDir=NO;
132
                fnp = ((dir==@".")?fnp:
133
                       ((dir==@"/")?[@"/" stringByAppendingString:fnp]:
134
                        [[dir stringByAppendingString:@"/"] stringByAppendingString:fnp]));
135
                if ([[NSFileManager defaultManager] fileExistsAtPath:fnp isDirectory:&isDir] && isDir)
136
                    fnp = [fnp stringByAppendingString:@"/"];
137
                if ([fnp isEqualToString:@"//"])
138
                    fnp=@"/";
139
                return fnp;
140
            } */
141
        }
142
    }
143
    return nil;
144
}
145
 
568 iacus 146
@end