mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
New convenience method in additions library
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@27223 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
8d9e1d356d
commit
8958eeea90
3 changed files with 109 additions and 0 deletions
|
@ -1,3 +1,9 @@
|
|||
2008-12-06 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/Additions/GSCategories.m: ([setLaunchPathForTool:])
|
||||
Convenience method to locate a tool in the standard locations or in the
|
||||
PATH and set it as the launch path of a task.
|
||||
|
||||
2008-12-05 Nicola Pero <nicola.pero@meta-innovation.com>
|
||||
|
||||
* configure.ac: On mingw32, set GNUSTEP_BASE_PATH to
|
||||
|
|
|
@ -332,6 +332,14 @@ typedef enum _NSGNUstepStringEncoding
|
|||
- (void) gcFinalize;
|
||||
@end
|
||||
|
||||
@implementation NSTask (GSCategories)
|
||||
/** Sets the launch path given the name of a tool.<br />
|
||||
* Locates the tool by looking in the standard directories,
|
||||
* and failing that in the PATH set in the environment.<br />
|
||||
* Returns the path set, or nil if the tool could not be located.
|
||||
*/
|
||||
- (NSString*) setLaunchPathForTool: (NSString*)name;
|
||||
@end
|
||||
|
||||
/* ------------------------------------------------------------------------
|
||||
* Functions
|
||||
|
|
|
@ -1510,3 +1510,98 @@ newLockAt(Class self, SEL _cmd, id *location)
|
|||
return newLockAt(self, _cmd, location);
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation NSTask (GSCategories)
|
||||
|
||||
static NSString*
|
||||
executablePath(NSFileManager *mgr, NSString *path)
|
||||
{
|
||||
if ([mgr isExecutableFileAtPath: path])
|
||||
{
|
||||
return path;
|
||||
}
|
||||
#if defined(__MINGW32__)
|
||||
if ([path pathExtension] == nil)
|
||||
{
|
||||
NSString *tmp;
|
||||
|
||||
tmp = [path stringByAppendingPathExtension: @"exe"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"com"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
tmp = [path stringByAppendingPathExtension: @"cmd"];
|
||||
if ([mgr isExecutableFileAtPath: tmp])
|
||||
{
|
||||
return tmp;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSString*) setLaunchPathForTool: (NSString*)name
|
||||
{
|
||||
NSEnumerator *enumerator;
|
||||
NSDictionary *env;
|
||||
NSString *pathlist;
|
||||
NSString *path;
|
||||
NSFileManager *mgr;
|
||||
|
||||
mgr = [NSFileManager defaultManager];
|
||||
|
||||
#if defined(GNUSTEP)
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
[self setLaunchPath: path];
|
||||
return path;
|
||||
}
|
||||
}
|
||||
enumerator = [NSSearchPathForDirectoriesInDomains(
|
||||
GSAdminToolsDirectory, NSAllDomainsMask, YES) objectEnumerator];
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
[self setLaunchPath: path];
|
||||
return path;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
env = [[NSProcessInfo processInfo] environment];
|
||||
pathlist = [env objectForKey:@"PATH"];
|
||||
#if defined(__MINGW32__)
|
||||
/* Windows 2000 and perhaps others have "Path" not "PATH" */
|
||||
if (pathlist == nil)
|
||||
{
|
||||
pathlist = [env objectForKey: @"Path"];
|
||||
}
|
||||
enumerator = [[pathlist componentsSeparatedByString: @";"] objectEnumerator];
|
||||
#else
|
||||
enumerator = [[pathlist componentsSeparatedByString: @":"] objectEnumerator];
|
||||
#endif
|
||||
while ((path = [enumerator nextObject]) != nil)
|
||||
{
|
||||
path = [path stringByAppendingPathComponent: name];
|
||||
if ((path = executablePath(mgr, path)) != nil)
|
||||
{
|
||||
[self setLaunchPath: path];
|
||||
return path;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
@end
|
||||
|
||||
|
|
Loading…
Reference in a new issue