mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 00:11:26 +00:00
More code tidyups and NSError updates.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23921 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
6ba4584136
commit
9e7b96ab0e
87 changed files with 746 additions and 539 deletions
|
@ -40,7 +40,6 @@ Additions_OBJC_FILES =\
|
|||
GSMime.m \
|
||||
GSXML.m \
|
||||
GSFunctions.m \
|
||||
GSPrivate.m \
|
||||
behavior.m
|
||||
|
||||
ifneq ($(OBJC_RUNTIME_LIB), gnu)
|
||||
|
|
|
@ -900,6 +900,80 @@ static void MD5Transform (uint32_t buf[4], uint32_t const in[16])
|
|||
}
|
||||
@end
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSError class.
|
||||
* Possibly to be made public
|
||||
*/
|
||||
@implementation NSError(GSCategories)
|
||||
|
||||
#ifndef HAVE_STRERROR
|
||||
static const char *
|
||||
strerror(int eno)
|
||||
{
|
||||
extern char *sys_errlist[];
|
||||
extern int sys_nerr;
|
||||
|
||||
if (eno < 0 || eno >= sys_nerr)
|
||||
{
|
||||
return("unknown error number");
|
||||
}
|
||||
return(sys_errlist[eno]);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Returns an NSError instance encapsulating the last system error.
|
||||
* The user info dictionary of this object will be mutable, so that
|
||||
* additional information can be placed in it by higher level code.
|
||||
*/
|
||||
+ (NSError*) _last
|
||||
{
|
||||
NSError *error;
|
||||
NSString *domain;
|
||||
NSDictionary *info;
|
||||
long code;
|
||||
#if defined(__MINGW32__)
|
||||
LPVOID lpMsgBuf;
|
||||
NSString *message;
|
||||
|
||||
code = GetLastError();
|
||||
domain = NSOSStatusErrorDomain;
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR) &lpMsgBuf, 0, NULL );
|
||||
message = [NSString stringWithCharacters: lpMsgBuf length: wcslen(lpMsgBuf)];
|
||||
LocalFree(lpMsgBuf);
|
||||
info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
||||
message, NSLocalizedDescriptionKey,
|
||||
nil];
|
||||
#else
|
||||
extern int errno;
|
||||
NSString *message;
|
||||
|
||||
code = errno;
|
||||
/* FIXME ... not all are POSIX, should we use NSMachErrorDomain for some? */
|
||||
domain = NSPOSIXErrorDomain;
|
||||
message = [NSString stringWithCString: strerror(code)
|
||||
encoding: [NSString defaultCStringEncoding]];
|
||||
/* FIXME ... can we do better localisation? */
|
||||
info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
|
||||
message, NSLocalizedDescriptionKey,
|
||||
nil];
|
||||
#endif
|
||||
|
||||
/* NB we use a mutable dictionary so that calling code can add extra
|
||||
* information to the dictionary before passing it up to higher level
|
||||
* code.
|
||||
*/
|
||||
error = [self errorWithDomain: domain code: code userInfo: info];
|
||||
return error;
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* GNUstep specific (non-standard) additions to the NSNumber class.
|
||||
*/
|
||||
|
|
|
@ -250,7 +250,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
|
||||
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0)
|
||||
{
|
||||
NSLog(@"unable to create socket - %@", [_GSPrivate error]);
|
||||
NSLog(@"unable to create socket - %@", [NSError _last]);
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
}
|
||||
|
@ -268,7 +268,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
if (bind(net, (struct sockaddr *)&sin, sizeof(sin)) < 0)
|
||||
{
|
||||
NSLog(@"unable to bind to port %s:%d - %@", inet_ntoa(sin.sin_addr),
|
||||
NSSwapBigShortToHost(sin.sin_port), [_GSPrivate error]);
|
||||
NSSwapBigShortToHost(sin.sin_port), [NSError _last]);
|
||||
(void) close(net);
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
|
@ -276,7 +276,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
|
||||
if (listen(net, 5) < 0)
|
||||
{
|
||||
NSLog(@"unable to listen on port - %@", [_GSPrivate error]);
|
||||
NSLog(@"unable to listen on port - %@", [NSError _last]);
|
||||
(void) close(net);
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
|
@ -284,7 +284,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
|
||||
if (getsockname(net, (struct sockaddr*)&sin, &size) < 0)
|
||||
{
|
||||
NSLog(@"unable to get socket name - %@", [_GSPrivate error]);
|
||||
NSLog(@"unable to get socket name - %@", [NSError _last]);
|
||||
(void) close(net);
|
||||
RELEASE(self);
|
||||
return nil;
|
||||
|
@ -313,7 +313,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|||
|
||||
if (getsockname([self fileDescriptor], (struct sockaddr*)&sin, &size) < 0)
|
||||
{
|
||||
NSLog(@"unable to get socket name - %@", [_GSPrivate error]);
|
||||
NSLog(@"unable to get socket name - %@", [NSError _last]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
/* Private internal methods for use within the base library
|
||||
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
This file is part of the GNUstep Base Library.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
#include "config.h"
|
||||
#include <string.h>
|
||||
#include <Foundation/Foundation.h>
|
||||
#include "GNUstepBase/GSCategories.h"
|
||||
#include "GNUstepBase/GSLock.h"
|
||||
#include "GSPrivate.h"
|
||||
|
||||
/* Test for ASCII whitespace which is safe for unicode characters */
|
||||
#define space(C) ((C) > 127 ? NO : isspace(C))
|
||||
|
||||
#ifndef HAVE_STRERROR
|
||||
const char *
|
||||
strerror(int eno)
|
||||
{
|
||||
extern char *sys_errlist[];
|
||||
extern int sys_nerr;
|
||||
|
||||
if (eno < 0 || eno >= sys_nerr)
|
||||
{
|
||||
return("unknown error number");
|
||||
}
|
||||
return(sys_errlist[eno]);
|
||||
}
|
||||
#endif
|
||||
|
||||
@implementation GSPrivate
|
||||
|
||||
- (NSString*) error
|
||||
{
|
||||
#if defined(__MINGW32__)
|
||||
return [self error: GetLastError()];
|
||||
#else
|
||||
extern int errno;
|
||||
return [self error: errno];
|
||||
#endif
|
||||
}
|
||||
|
||||
- (NSString*) error: (long)number
|
||||
{
|
||||
NSString *text;
|
||||
#if defined(__MINGW32__)
|
||||
LPVOID lpMsgBuf;
|
||||
|
||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
NULL, number, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
(LPWSTR) &lpMsgBuf, 0, NULL );
|
||||
text = [NSString stringWithCharacters: lpMsgBuf length: wcslen(lpMsgBuf)];
|
||||
LocalFree(lpMsgBuf);
|
||||
#else
|
||||
text = [NSString stringWithCString: strerror(number)
|
||||
encoding: [NSString defaultCStringEncoding]];
|
||||
#endif
|
||||
return text;
|
||||
}
|
||||
@end
|
||||
|
||||
|
|
@ -33,6 +33,7 @@
|
|||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSBundle.h>
|
||||
#include <Foundation/NSDictionary.h>
|
||||
#include <Foundation/NSError.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSLock.h>
|
||||
|
@ -331,7 +332,8 @@ static void GSSetupEncodingTable(void)
|
|||
}
|
||||
}
|
||||
|
||||
static BOOL isEncodingSupported(NSStringEncoding enc)
|
||||
BOOL
|
||||
GSPrivateIsEncodingSupported(NSStringEncoding enc)
|
||||
{
|
||||
GSSetupEncodingTable();
|
||||
|
||||
|
@ -1160,7 +1162,7 @@ tables:
|
|||
const char *estr = 0;
|
||||
BOOL done = NO;
|
||||
|
||||
if (isEncodingSupported(enc) == YES)
|
||||
if (GSPrivateIsEncodingSupported(enc) == YES)
|
||||
{
|
||||
estr = encodingTable[enc]->iconv;
|
||||
}
|
||||
|
@ -1181,7 +1183,7 @@ tables:
|
|||
if (cd == (iconv_t)-1)
|
||||
{
|
||||
NSLog(@"No iconv for encoding %@ tried to use %s",
|
||||
[_GSPrivate encodingName: enc], estr);
|
||||
GSPrivateEncodingName(enc), estr);
|
||||
result = NO;
|
||||
goto done;
|
||||
}
|
||||
|
@ -1853,7 +1855,7 @@ iconv_start:
|
|||
const char *estr = 0;
|
||||
BOOL done = NO;
|
||||
|
||||
if (isEncodingSupported(enc) == YES)
|
||||
if (GSPrivateIsEncodingSupported(enc) == YES)
|
||||
{
|
||||
if (strict == NO)
|
||||
{
|
||||
|
@ -1886,7 +1888,7 @@ iconv_start:
|
|||
if (cd == (iconv_t)-1)
|
||||
{
|
||||
NSLog(@"No iconv for encoding %@ tried to use %s",
|
||||
[_GSPrivate encodingName: enc], estr);
|
||||
GSPrivateEncodingName(enc), estr);
|
||||
result = NO;
|
||||
goto done;
|
||||
}
|
||||
|
@ -2043,9 +2045,10 @@ iconv_start:
|
|||
|
||||
#undef GROW
|
||||
|
||||
@implementation GSPrivate (Unicode)
|
||||
|
||||
- (NSStringEncoding*) availableEncodings
|
||||
|
||||
NSStringEncoding*
|
||||
GSPrivateAvailableEncodings()
|
||||
{
|
||||
if (_availableEncodings == 0)
|
||||
{
|
||||
|
@ -2068,7 +2071,7 @@ iconv_start:
|
|||
pos = 0;
|
||||
for (i = 0; i < encTableSize+1; i++)
|
||||
{
|
||||
if (isEncodingSupported(i) == YES)
|
||||
if (GSPrivateIsEncodingSupported(i) == YES)
|
||||
{
|
||||
encodings[pos++] = i;
|
||||
}
|
||||
|
@ -2081,7 +2084,8 @@ iconv_start:
|
|||
return _availableEncodings;
|
||||
}
|
||||
|
||||
- (NSStringEncoding) defaultCStringEncoding
|
||||
NSStringEncoding
|
||||
GSPrivateDefaultCStringEncoding()
|
||||
{
|
||||
if (defEnc == GSUndefinedEncoding)
|
||||
{
|
||||
|
@ -2233,7 +2237,7 @@ iconv_start:
|
|||
#endif
|
||||
defEnc = NSISOLatin1StringEncoding;
|
||||
}
|
||||
else if (isEncodingSupported(defEnc) == NO)
|
||||
else if (GSPrivateIsEncodingSupported(defEnc) == NO)
|
||||
{
|
||||
fprintf(stderr, "WARNING: %s - encoding not implemented as "
|
||||
"default c string encoding.\n", encoding);
|
||||
|
@ -2246,28 +2250,24 @@ iconv_start:
|
|||
return defEnc;
|
||||
}
|
||||
|
||||
- (NSString*) encodingName: (NSStringEncoding)encoding
|
||||
NSString*
|
||||
GSPrivateEncodingName(NSStringEncoding encoding)
|
||||
{
|
||||
if (isEncodingSupported(encoding) == NO)
|
||||
if (GSPrivateIsEncodingSupported(encoding) == NO)
|
||||
{
|
||||
return @"Unknown encoding";
|
||||
}
|
||||
return [NSString stringWithUTF8String: encodingTable[encoding]->ename];
|
||||
}
|
||||
|
||||
- (BOOL) isByteEncoding: (NSStringEncoding)encoding
|
||||
BOOL
|
||||
GSPrivateIsByteEncoding(NSStringEncoding encoding)
|
||||
{
|
||||
if (isEncodingSupported(encoding) == NO)
|
||||
if (GSPrivateIsEncodingSupported(encoding) == NO)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
return encodingTable[encoding]->eightBit;
|
||||
}
|
||||
|
||||
- (BOOL) isEncodingSupported: (NSStringEncoding)encoding
|
||||
{
|
||||
return isEncodingSupported(encoding);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue