discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fix for stringByExpandingTildeInPath


From: David Wetzel
Subject: fix for stringByExpandingTildeInPath
Date: Fri, 20 Apr 2001 23:44:53 +0200

Hi,

This did not work:

    theValue = [[NSString stringWithFormat:@"~%@", _loginName] 
stringByExpandingTildeInPath];

but with this fixed code it does.
Someone with CVS write access should check it in.

Dave

---
   _  _
 _(_)(_)_  David Wetzel, Turbocat's Development,
(_) __ (_) Buchhorster Strasse 23, D-16567 Muehlenbeck/Berlin, FRG,
  _/  \_   Fax +49 33056 82835 Phone +49 33056 82834
 (______)  http://www.turbocat.de/  dave@turbocat.de
           DEVELOPMENT * CONSULTING * ADMINISTRATION


- (NSString*) stringByExpandingTildeInPath
{
  NSString      *homedir;
  NSRange       first_slash_range;
  
  if ([self length] == 0)
    return AUTORELEASE([self copy]);
  if ([self characterAtIndex: 0] != 0x007E)
    return AUTORELEASE([self copy]);

  first_slash_range = [self rangeOfString: pathSepString];

  if (first_slash_range.location != 1)
    {
      /* It is of the form `~username/blah/...' */
      int uname_len;
      NSString *uname;

      if (first_slash_range.length != 0)
        uname_len = first_slash_range.length - 1;
      else {
        /* It is actually of the form `~username' */
        uname_len = [self length] - 1;
        first_slash_range.location = [self length];
                }
      uname = [self substringWithRange: ((NSRange){1, uname_len})];
      homedir = NSHomeDirectoryForUser (uname);
    }
  else
    {
      /* It is of the form `~/blah/...' */
      homedir = NSHomeDirectory ();
    }
  
  return [NSStringClass stringWithFormat: @"%@%@", homedir, 
    [self substringFromIndex: first_slash_range.location]];
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]