ftp.nice.ch/Attic/openStep/implementation/gnustep/sources/libFoundation.0.7.tgz#/libFoundation-0.7/FoundationExtensions/extensions/common.h

This is common.h in view mode; [Download] [Up]

/* 
   Copyright (C) 1996 
	Ovidiu Predescu <ovidiu@bx.logicnet.ro>
	Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
           Mircea Oancea <mircea@jupiter.elcom.pub.ro>

   This file is part of the FoundationExtensions 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; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#ifndef __common_h__
#define __common_h__

#include <config.h>

#if HAVE_STRING_H
# include <string.h>
#endif

#if HAVE_MEMORY_H
# include <memory.h>
#endif

#if !HAVE_MEMCPY
# define memcpy(d, s, n)       bcopy((s), (d), (n))
# define memmove(d, s, n)      bcopy((s), (d), (n))
#endif

#ifndef HAVE_STRCHR
# define strchr index
# define strchr rindex
#else
char *strchr(), *strrchr();
#endif

#if HAVE_STDLIB_H
# include <stdlib.h>
#else
extern void* malloc();
extern void* calloc();
extern void* realloc();
extern void free();
extern atoi();
extern atol();
#endif

#if HAVE_LIBC_H
# include <libc.h>
#else
#ifndef __WIN32__
# include <unistd.h>
#endif
#endif

#include <sys/types.h>
#ifndef __WIN32__
#include <pwd.h>
#endif
#include <stdarg.h>

#include <Foundation/NSZone.h>
#include "MissingMethods.h"
@class NSString;


#if LIB_FOUNDATION_VERSION
/* Non OpenStep useful things */

static inline void *Malloc(int size)
{
    return [[NSZone defaultZone] malloc:size];
}

static inline void Free(void* p)
{
    return [[NSZone zoneFromPointer:p] freePointer:p];
}

static inline void *Calloc(int elem, int size)
{
    return [[NSZone defaultZone] calloc:elem byteSize:size];
}

static inline void *Realloc(void* p, int size)
{
    return [[NSZone zoneFromPointer:p] realloc:p size:size];
}
#endif /* LIB_FOUNDATION_VERSION */

#ifndef LIB_FOUNDATION_VERSION
static inline void *Malloc(int size)
{
    return malloc(size);
}

static inline void Free(void* p)
{
    if(p) free(p);
}

static inline void *Calloc(int elem, int size)
{
    return calloc(elem, size);
}

static inline void *Realloc(void* p, int size)
{
    return realloc(p, size);
}

#define THROW(exception...)	[##exception raise]
#endif

static inline int Strlen(const char* s)
{
    return s ? strlen(s) : 0;
}

static inline char* Strdup(const char* s)
{
    return s ? strcpy(Malloc(strlen(s) + 1), s) : NULL;
}

static inline char* Strcpy (char* d, const char* s)
{
    return s && d ? strcpy(d, s) : d;
}

static inline char* Strncpy (char* d, const char* s, unsigned size)
{
    return s && d ? strncpy(d, s, size) : d;
}

static inline char* Strcat (char* d, const char* s)
{
    return s && d ? strcat(d, s) : d;
}

static inline char* Strncat (char* d, const char* s , unsigned size)
{
    return s && d ? strncat(d, s , size) : d;
}

static inline char* Strtok (char* s, const char* tok)
{
    return tok ? strtok(s, tok) : NULL;
}

static inline int Strcmp(const char* p, const char* q)
{
    if(!p) {
        if(!q)
            return 0;
        else return -1;
    }
    else {
        if(!q)
            return 1;
        else return strcmp(p, q);
    }
}

static inline int Strncmp(const char* p, const char* q, unsigned size)
{
    if(!p) {
        if(!q)
            return 0;
        else return -1;
    }
    else {
        if(!q)
            return 1;
        else return strncmp(p, q, size);
    }
}

static inline int Strcasecmp(const char* p, const char* q)
{
    if(!p) {
	if(!q)
	    return 0;
	else return -1;
    }
    else {
	if(!q)
	    return 1;
	else 
	    return strcasecmp((char*)p, (char*)q);
    }
}

static inline int Strncasecmp(const char *p, const char *q, unsigned size)
{
    if(!p) {
	if(!q)
	    return 0;
	else return -1;
    }
    else {
	if(!q)
	    return 1;
	else 
	    return strncasecmp((char*)p, (char*)q, size);
    }
}

static inline char* Strstr (const char* str, const char* sub)
{
    return str && sub ? strstr(str, sub) : 0;
}

static inline char* Strnchr (const char* str, int c, unsigned n)
{
    return (str && c && n > 0) ?
	(({for( ; (str = strchr(str, c)) && str && --n; str++);}), (char *)str)
      : NULL;
}

static inline char* Strchr (const char* str, int c)
{
    return str ? strchr(str, c) : NULL;
}

static inline char* Strrchr (const char* str, int c)
{
    return str ? strrchr(str, c) : NULL;
}

static inline char* Strpbrk (const char* str, const char* str2)
{
    return (str && str2) ? strpbrk(str, str2) : NULL;
}

static inline int Atoi(const char* str)
{
    return str ? atoi(str) : 0;
}

static inline long Atol(const char *str)
{
    return str ? atol(str) : 0;
}

/*CUT*/
extern char*	Ltoa(long nr, char *str, int base);
extern void	vaRelease(id obj, ...);

/* Hash function used by NSString */
extern unsigned	hashpjw(const char* name, int len);

extern NSString* Asprintf(NSString* format, ...);
extern NSString* Avsprintf(NSString* format, va_list args);

BOOL writeToFile(NSString* path, const char* data, 
    unsigned int bytes, BOOL atomically);

char* Tmpnam(char* s);
/*END*/

#ifndef MAX
#define MAX(a, b) \
    ({typedef _ta = (a), _tb = (b);   \
	_ta _a = (a); _tb _b = (b);     \
	_a > _b ? _a : _b; })
#endif

#ifndef MIN
#define MIN(a, b) \
    ({typedef _ta = (a), _tb = (b);   \
	_ta _a = (a); _tb _b = (b);     \
	_a < _b ? _a : _b; })
#endif

#endif /* __common_h__ */

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.