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