[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: NSProcessInfo globallyUniqueString
From: |
Roland Schwingel |
Subject: |
RE: NSProcessInfo globallyUniqueString |
Date: |
Thu, 8 Aug 2002 10:12:50 +0200 |
Hi...
(I couldn't find David Ayers patch in my gnustep mailinglist mails so I decided
to also post my solution - I only had the mail of Richard Frith-Macdonald
forwarded below)
I faced the very same problems a while ago also. I use
this method very frequently for temporary filenames etc.
So to be unique again (and to avoid the : problem - which
awfully breaks on windows) I placed a category to that
method doing this a long time ago:
{
// static time_t startTime = 0;
static NSString *hostName = nil;
static long counter = 1;
static pid_t pid = 0;
if (!hostName)
{
hostName = [[NSProcessInfo processInfo] hostName];
if (![hostName length])
hostName = @"localhost";
else
{
NSArray *components = [hostName componentsSeparatedByString:@"."];
hostName = [[components objectAtIndex:0] retain];
}
#ifdef __MINGW__
pid = GetCurrentProcessId();
#else
pid = getpid();
#endif
// startTime = time(NULL);
}
// return [NSString stringWithFormat:@"tmp-%@-%x-%x-%x",hostName,pid,time(NULL)-startTime,counter++];
return [NSString stringWithFormat:@"tmp-%@-%x-%x",hostName,pid,counter++];
}
I decided that a timestamp is not really necessary when you have a counter which is incremented on every call, so I
omitted that (but still available commented out). OS4.2 is also always adding a counter to the string.
This version now runs for over a year for me perfectly on windows and unix...
Roland
======================================
FROM: Richard Frith-Macdonald
DATE: 08/07/2002 09:16:53
SUBJECT: RE: NSProcessInfo globallyUniqueString
On Monday, July 22, 2002, at 01:41 PM, David Ayers wrote:
> Hallo all,
>
> on my maching the globallyUniqueString produced by NSProcessInfo wasn't
> always globally unique since the precision of the date output only
> evaluated
> to seconds.
Thanks ... your fix didn't absolutely guarantee uniqueness, so I added a
lock
protected counter too.
> Also some of apps I know use this string to create filenames and
> the usage of ':' causes problems. Therefor I propose the following
> patch.
> Yet if some apps already rely on the date format, I've included another
> alternative. But I believe, relying on the format of this string is a
> bug.
I think relying on it to form a legal filename might be considered a bug
as well,
but the current implementation should be ok on unix style machines at
least.