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

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

/* 
   common.h

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>
	   Mircea Oancea <mircea@jupiter.elcom.pub.ro>
	   Florin Mihaila <phil@pathcom.com>
	   Bogdan Baliuc <stark@protv.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#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

#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
# include <unistd.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <fcntl.h>                     
#include <pwd.h>
#include <stdarg.h>

#include <Foundation/NSZone.h>

#if (__GNUC__ == 2) && (__GNUC_MINOR__ <= 6) && !defined(__attribute__)
#  define __attribute__(x)
#endif

@class NSString;
@class NSData;

static inline void *Malloc(int) __attribute__((unused));
static inline void Free(void*) __attribute__((unused));
static inline void *Calloc(int, int) __attribute__((unused));
static inline void *Realloc(void*, int) __attribute__((unused));
static inline int Strlen(const char*) __attribute__((unused));
static inline char* Strdup(const char*) __attribute__((unused));
static inline char* Strcpy (char*, const char*) __attribute__((unused));
static inline char* Strncpy (char*, const char*, unsigned)
    __attribute__((unused));
static inline char* Strcat (char*, const char*) __attribute__((unused));
static inline char* Strncat (char*, const char*, unsigned)
    __attribute__((unused));
static inline int Strcmp(const char*, const char*) __attribute__((unused));
static inline int Strncmp(const char*, const char*, unsigned)
    __attribute__((unused));
static inline int Atoi(const char*) __attribute__((unused));
static inline long Atol(const char*) __attribute__((unused));

/* 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];
}

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 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 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, NSData* data, 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.