diff --git a/ChangeLog b/ChangeLog index 79430da98..c0b51468a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2000-12-04 Richard Frith-Macdonald + + * Source/Unicode.m: Tidied to conform to coding standards and changed + to use ISOLatin1 as the default cString encoding. + * Source/GSString.m: Fixed bug getting cString from unicode string - + raise exception when it should. + * Source/NSString.m: ditto. Also use lossyCString in various + places rather than cString. + 2000-12-13 Nicola Pero * Headers/gnustep/base/NSRange.h, Source/NSRange.m: Inlined diff --git a/Source/GSString.m b/Source/GSString.m index 73c88dcda..df39161a5 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -901,7 +901,8 @@ static inline void getCString_u(ivars self, char *buffer, unsigned int maxLength, NSRange aRange, NSRange *leftoverRange) { - int len; + int len; + int result; if (maxLength > self->_count) { @@ -926,8 +927,13 @@ getCString_u(ivars self, char *buffer, unsigned int maxLength, } } - encode_ustrtostr_strict(buffer, &self->_contents.u[aRange.location], - maxLength, defEnc); + result = encode_ustrtostr_strict(buffer, &self->_contents.u[aRange.location], + len, defEnc); + if (result != len) + { + [NSException raise: NSCharacterConversionException + format: @"Can't get cString from Unicode string."]; + } buffer[len] = '\0'; } diff --git a/Source/NSString.m b/Source/NSString.m index 7f4c0b921..8673ec998 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -670,7 +670,7 @@ handle_printf_atsign (FILE *stream, arguments: (va_list)arg_list { #if defined(HAVE_VSPRINTF) || defined(HAVE_VASPRINTF) - const char *format_cp = [format cString]; + const char *format_cp = [format lossyCString]; int format_len = strlen (format_cp); #if HAVE_VASPRINTF char *buf; @@ -783,7 +783,7 @@ handle_printf_atsign (FILE *stream, format_to_go = spec_pos+1; } /* Get a C-string (char*) from the String object, and print it. */ - cstring = [[(id) va_arg (arg_list, id) description] cString]; + cstring = [[(id) va_arg (arg_list, id) description] lossyCString]; if (!cstring) cstring = ""; cstring_len = strlen(cstring); @@ -930,7 +930,7 @@ handle_printf_atsign (FILE *stream, format_to_go = spec_pos+1; } /* Get a C-string (char*) from the String object, and print it. */ - cstring = [[(id) va_arg (arg_list, id) description] cString]; + cstring = [[(id) va_arg (arg_list, id) description] lossyCString]; if (!cstring) cstring = ""; strcat (buf+printed_len, cstring); @@ -1940,7 +1940,14 @@ handle_printf_atsign (FILE *stream, count = 0; while (count < len) { - buffer[count]=unitochar((*caiImp)(self, caiSel, aRange.location + count)); + buffer[count] = encode_unitochar( + (*caiImp)(self, caiSel, aRange.location + count), + _DefaultStringEncoding); + if (buffer[count] == 0) + { + [NSException raise: NSCharacterConversionException + format: @"unable to convert to cString"]; + } count++; } buffer[len] = '\0'; @@ -1960,17 +1967,17 @@ handle_printf_atsign (FILE *stream, - (double) doubleValue { - return atof([self cString]); + return atof([self lossyCString]); } - (float) floatValue { - return (float) atof([self cString]); + return (float) atof([self lossyCString]); } - (int) intValue { - return atoi([self cString]); + return atoi([self lossyCString]); } // Working With Encodings diff --git a/Source/Unicode.m b/Source/Unicode.m index e98fc693f..6ceb36fcb 100644 --- a/Source/Unicode.m +++ b/Source/Unicode.m @@ -168,90 +168,97 @@ NSStringEncoding *GetAvailableEncodings() return _availableEncodings; } -NSStringEncoding GetDefEncoding() +NSStringEncoding +GetDefEncoding() { - char *encoding; - unsigned int count; - NSStringEncoding ret,tmp; - NSStringEncoding *availableEncodings; + char *encoding; + unsigned int count; + NSStringEncoding ret; + NSStringEncoding tmp; + NSStringEncoding *availableEncodings; availableEncodings = GetAvailableEncodings(); encoding = getenv("GNUSTEP_STRING_ENCODING"); - if (encoding) + if (encoding != 0) { count = 0; - while (str_encoding_table[count].enc && - strcmp(str_encoding_table[count].ename,encoding)) + while (str_encoding_table[count].enc + && strcmp(str_encoding_table[count].ename,encoding)) { count++; } if (str_encoding_table[count].enc) { ret = str_encoding_table[count].enc; - if ((ret == NSUnicodeStringEncoding) || - (ret == NSSymbolStringEncoding)) + if ((ret == NSUnicodeStringEncoding) + || (ret == NSSymbolStringEncoding)) { - fprintf(stderr, "WARNING: %s - encoding not supported as default c string encoding.\n", encoding); - fprintf(stderr, "NSASCIIStringEncoding set as default.\n"); - ret = NSASCIIStringEncoding; + fprintf(stderr, "WARNING: %s - encoding not supported as " + "default c string encoding.\n", encoding); + fprintf(stderr, "NSISOLatin1StringEncoding set as default.\n"); + ret = NSISOLatin1StringEncoding; } else /*encoding should be supported but is it implemented?*/ { count = 0; tmp = 0; - while ( !(availableEncodings[count] == 0) ) + while (availableEncodings[count] != 0) { - if ( !(ret == availableEncodings[count]) ) - tmp = 0; + if (ret != availableEncodings[count]) + { + tmp = 0; + } else { tmp = ret; break; } count++; - }; - if (!tmp) + } + if (tmp != 0) { - fprintf(stderr, "WARNING: %s - encoding not yet implemented.\n", encoding); - fprintf(stderr, "NSASCIIStringEncoding set as default.\n"); - ret = NSASCIIStringEncoding; - }; - }; + fprintf(stderr, "WARNING: %s - encoding not yet " + "implemented.\n", encoding); + fprintf(stderr, + "NSISOLatin1StringEncoding set as default.\n"); + ret = NSISOLatin1StringEncoding; + } + } } else /* encoding not found */ { fprintf(stderr, "WARNING: %s - encoding not supported.\n", encoding); - fprintf(stderr, "NSASCIIStringEncoding set as default.\n"); - ret = NSASCIIStringEncoding; + fprintf(stderr, "NSISOLatin1StringEncoding set as default.\n"); + ret = NSISOLatin1StringEncoding; } } else /* environment var not found */ { - /* This shouldn't be required. It really should be in UserDefaults - asf */ - //fprintf(stderr,"WARNING: GNUSTEP_STRING_ENCODING environment variable not found\n"); - //fprintf(stderr, "NSASCIIStringEncoding set as default.\n"); - ret = NSASCIIStringEncoding; + /* shouldn't be required. It really should be in UserDefaults - asf */ + //fprintf(stderr,"WARNING: GNUSTEP_STRING_ENCODING environment found\n"); + //fprintf(stderr, "NSISOLatin1StringEncoding set as default.\n"); + ret = NSISOLatin1StringEncoding; } // Cache the encoding defEnc = ret; return ret; -}; +} NSString* GetEncodingName(NSStringEncoding encoding) { unsigned int count=0; - while (str_encoding_table[count].enc && - !(str_encoding_table[count].enc == encoding)) + while (str_encoding_table[count].enc + && (str_encoding_table[count].enc != encoding)) { count++; } return [NSString stringWithCString: str_encoding_table[count].ename]; -}; +} #ifdef HAVE_ICONV