Some fixes for initialising a data from a string.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@19297 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2004-05-13 09:10:35 +00:00
parent 20c509536e
commit 402ceca220
2 changed files with 755 additions and 693 deletions

View file

@ -1,3 +1,12 @@
2004-05-13 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSCalendarDate.m: ([initWithString:calendarFormat:locale:])
Corrected to return nil when description does not match format.
Also change NSLog to NSDebugMLog for reporting problems, and added
a couple of logs.
Also changed to accept nil/empty string values for format and
description as MacOS-X does.
2004-05-12 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSURL.m: ([-loadResourceDataNotifyingClient:usingCache:])

View file

@ -497,6 +497,7 @@ static inline int getDigits(const char *from, char *to, int limit)
/**
* Initializes an NSCalendarDate using the specified description and format
* string interpreted in the given locale.<br />
* If description does not match fmt exactly, this method returns nil.<br />
* Format specifiers are -
* <list>
* <item>
@ -576,27 +577,20 @@ static inline int getDigits(const char *from, char *to, int limit)
* </item>
* </list>
*/
- (id) initWithString: (NSString *)description
calendarFormat: (NSString *)fmt
locale: (NSDictionary *)locale
- (id) initWithString: (NSString*)description
calendarFormat: (NSString*)fmt
locale: (NSDictionary*)locale
{
// If description does not match this format exactly, this method returns nil
if ([description length] == 0)
{
// Autorelease self because it isn't done by the calling function
// [[NSCalendarDate alloc] initWithString:calendarFormat:locale:];
AUTORELEASE(self);
return nil;
}
else
{
int milliseconds = 0;
/* Default to gregorian year one ... there is no year zero and
* the algorithms we use look odd for earlier dates.
*/
int year = 1;
int month = 1, day = 1;
int hour = 0, min = 0, sec = 0;
int month = 1;
int day = 1;
int hour = 0;
int min = 0;
int sec = 0;
NSTimeZone *tz = nil;
BOOL ampm = NO;
BOOL twelveHrClock = NO;
@ -617,6 +611,7 @@ static inline int getDigits(const char *from, char *to, int limit)
NSString *TForm;
NSMutableData *fd;
BOOL changedFormat = NO;
BOOL error = NO;
if (locale == nil)
{
@ -626,8 +621,14 @@ static inline int getDigits(const char *from, char *to, int limit)
{
fmt = [locale objectForKey: NSTimeDateFormatString];
if (fmt == nil)
{
fmt = @"";
}
}
if (description == nil)
{
description = @"";
}
TForm = [locale objectForKey: NSTimeDateFormatString];
if (TForm == nil)
@ -764,7 +765,9 @@ static inline int getDigits(const char *from, char *to, int limit)
{
if (source[sourceIdx] != format[formatIdx])
{
NSLog(@"Expected literal '%c' but got '%c' parsing"
error = YES;
NSDebugMLog(
@"Expected literal '%c' but got '%c' parsing"
@"'%@' using '%@'", format[formatIdx],
source[sourceIdx], description, fmt);
}
@ -785,7 +788,9 @@ static inline int getDigits(const char *from, char *to, int limit)
{
if (source[sourceIdx] != '%')
{
NSLog(@"Expected literal '%%' but got '%c' parsing"
error = YES;
NSDebugMLog(
@"Expected literal '%%' but got '%c' parsing"
@"'%@' using '%@'", source[sourceIdx],
description, fmt);
}
@ -809,7 +814,7 @@ static inline int getDigits(const char *from, char *to, int limit)
NSString *currDay;
NSArray *dayNames;
currDay = [NSString stringWithCString: tmpStr];
currDay = [[NSString alloc] initWithCString: tmpStr];
dayNames = [locale objectForKey: NSShortWeekDayNameArray];
for (tmpIdx = 0; tmpIdx < 7; tmpIdx++)
{
@ -819,9 +824,19 @@ static inline int getDigits(const char *from, char *to, int limit)
break;
}
}
if (tmpIdx == 7)
{
error = YES;
NSDebugMLog(@"Day of week '%@' not found in locale",
currDay);
}
else
{
dayOfWeek = tmpIdx;
had |= hadw;
}
RELEASE(currDay);
}
break;
case 'A':
@ -842,7 +857,7 @@ static inline int getDigits(const char *from, char *to, int limit)
NSString *currDay;
NSArray *dayNames;
currDay = [NSString stringWithCString: tmpStr];
currDay = [[NSString alloc] initWithCString: tmpStr];
dayNames = [locale objectForKey: NSWeekDayNameArray];
for (tmpIdx = 0; tmpIdx < 7; tmpIdx++)
{
@ -852,9 +867,19 @@ static inline int getDigits(const char *from, char *to, int limit)
break;
}
}
if (tmpIdx == 7)
{
error = YES;
NSDebugMLog(@"Day of week '%@' not found in locale",
currDay);
}
else
{
dayOfWeek = tmpIdx;
had |= hadw;
}
RELEASE(currDay);
}
break;
case 'b':
@ -873,7 +898,7 @@ static inline int getDigits(const char *from, char *to, int limit)
NSString *currMonth;
NSArray *monthNames;
currMonth = [NSString stringWithCString: tmpStr];
currMonth = [[NSString alloc] initWithCString: tmpStr];
monthNames = [locale objectForKey: NSShortMonthNameArray];
for (tmpIdx = 0; tmpIdx < 12; tmpIdx++)
@ -884,9 +909,19 @@ static inline int getDigits(const char *from, char *to, int limit)
break;
}
}
if (tmpIdx == 12)
{
error = YES;
NSDebugMLog(@"Month of year '%@' not found in locale",
currMonth);
}
else
{
month = tmpIdx+1;
had |= hadM;
}
RELEASE(currMonth);
}
break;
case 'B':
@ -907,7 +942,7 @@ static inline int getDigits(const char *from, char *to, int limit)
NSString *currMonth;
NSArray *monthNames;
currMonth = [NSString stringWithCString: tmpStr];
currMonth = [[NSString alloc] initWithCString: tmpStr];
monthNames = [locale objectForKey: NSMonthNameArray];
for (tmpIdx = 0; tmpIdx < 12; tmpIdx++)
@ -918,9 +953,19 @@ static inline int getDigits(const char *from, char *to, int limit)
break;
}
}
if (tmpIdx == 12)
{
error = YES;
NSDebugMLog(@"Month of year '%@' not found in locale",
currMonth);
}
else
{
month = tmpIdx+1;
had |= hadM;
}
RELEASE(currMonth);
}
break;
case 'd': // fall through
@ -1105,6 +1150,8 @@ static inline int getDigits(const char *from, char *to, int limit)
}
RELEASE(fd);
if (error == NO)
{
if (tz == nil)
{
tz = localTZ;
@ -1184,8 +1231,13 @@ static inline int getDigits(const char *from, char *to, int limit)
{
_seconds_since_ref += ((float)milliseconds) / 1000.0;
}
return self;
}
if (error == YES)
{
DESTROY(self);
}
return self;
}
/**
@ -1655,7 +1707,7 @@ static inline int getDigits(const char *from, char *to, int limit)
format = [locale objectForKey: NSTimeDateFormatString];
// If the format is nil then return an empty string
if (!format)
if ([format length] == 0)
return @"";
strcpy (f, [format cString]);
@ -1685,9 +1737,7 @@ static inline int getDigits(const char *from, char *to, int limit)
{
// literal %
case '%':
++i;
buf[j] = f[i];
++j;
buf[j++] = f[i++];
break;
case 'c':
@ -1698,28 +1748,31 @@ static inline int getDigits(const char *from, char *to, int limit)
case 'X':
if (insertionString == NULL)
{
insertionString = [[locale objectForKey: NSTimeFormatString] cString];
insertionString
= [[locale objectForKey: NSTimeFormatString] cString];
}
case 'x':
{
int lengthOfInsertion;
if (insertionString == NULL)
{
insertionString = [[locale objectForKey: NSDateFormatString] cString];
insertionString
= [[locale objectForKey: NSDateFormatString] cString];
}
lengthOfInsertion = strlen (insertionString);
// Insert the insertion string in the format
// + 1 for the nul byte terminating the string
// Note: i is pointing to the x in %x, we remove %x and insert
// the string. The +1 for the length is there to copy the string
// Note: i is pointing to the x in %x,
// we remove %x and insert the string.
// The +1 for the length is there to copy the string
// terminator.
memmove (f + i - 1 + lengthOfInsertion, f + i + 1,
strlen (f + i + 1) + 1);
memcpy (f + i - 1, insertionString, lengthOfInsertion);
// update the lvar containing the length
lf += lengthOfInsertion;
// update the reader position, we removed %x and the i was pointing to
// the x.
// update the reader position, we removed %x
// and the i was pointing to the x.
--i;
break;
}