]> err.no Git - sope/blob - sope-core/EOControl/common.h
synced with latest additions and bumped framework versions
[sope] / sope-core / EOControl / common.h
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #ifndef __EOControl_COMMON_H__
23 #define __EOControl_COMMON_H__
24
25 #include <stdlib.h>
26 #include <objc/Protocol.h>
27
28 #import <Foundation/Foundation.h>
29 #import <Foundation/NSObjCRuntime.h>
30
31 #if NeXT_RUNTIME || APPLE_RUNTIME
32 #  define objc_free(__mem__)    free(__mem__)
33 #  define objc_malloc(__size__) malloc(__size__)
34 #  define objc_calloc(__cnt__, __size__) calloc(__cnt__, __size__)
35 #  define objc_realloc(__ptr__, __size__) realloc(__ptr__, __size__)
36 #  ifndef sel_eq
37 #    define sel_eq(sela,selb) (sela==selb?YES:NO)
38 #  endif
39 #endif
40
41 #ifndef ASSIGN
42 #  define ASSIGN(object, value) \
43        ({id __object = (id)object;    \
44          id __value = (id)value;      \
45          if (__value != __object) { if (__value) [__value retain]; \
46           if (__object) [__object release]; \
47           object = __value;}})
48 #endif
49 #ifndef ASSIGNCOPY
50 #  define ASSIGNCOPY(object, value) \
51        ({id __object = (id)object;    \
52          id __value = (id)value;      \
53          if (__value != __object) { if (__value) __value = [__value copy];   \
54           if (__object) [__object release]; \
55           object = __value;}})
56 #endif
57
58 // ******************** common functions ********************
59
60 static inline char* Strdup(const char*) __attribute__((unused));
61 static inline char* Strcpy (char*, const char*) __attribute__((unused));
62 static inline char* Strncpy (char*, const char*, unsigned)
63     __attribute__((unused));
64 static inline char* Strcat (char*, const char*) __attribute__((unused));
65 static inline char* Strncat (char*, const char*, unsigned)
66     __attribute__((unused));
67 static inline int Strcmp(const char*, const char*) __attribute__((unused));
68 static inline int Strncmp(const char*, const char*, unsigned)
69     __attribute__((unused));
70 static inline int Atoi(const char*) __attribute__((unused));
71 static inline long Atol(const char*) __attribute__((unused));
72
73 #define Malloc  malloc
74 #define Calloc  calloc
75 #define Realloc realloc
76 #define Free(__p__)  if(__p__) { free(__p__); __p__ = NULL; }
77
78 #define Strlen(__s__) (__s__?strlen(__s__):0)
79
80 static inline char *Strdup(const char *s) {
81   return s ? strcpy(Malloc(strlen(s) + 1), s) : NULL;
82 }
83 static inline char* Strcpy (char *d, const char *s) {
84   return s && d ? strcpy(d, s) : d;
85 }
86 static inline char* Strncpy (char* d, const char* s, unsigned size) {
87   return s && d ? strncpy(d, s, size) : d;
88 }
89 static inline char* Strcat (char* d, const char* s) {
90   return s && d ? strcat(d, s) : d;
91 }
92 static inline char* Strncat (char* d, const char* s , unsigned size) {
93   return s && d ? strncat(d, s , size) : d;
94 }
95
96 static inline int Strcmp(const char* p, const char* q) {
97   if(p == NULL) {
98     if(!q)
99       return 0;
100     else return -1;
101   }
102   else {
103     if(!q)
104       return 1;
105     else return strcmp(p, q);
106   }
107 }
108
109 static inline int Strncmp(const char* p, const char* q, unsigned size) {
110   if(!p) {
111       if(!q)
112           return 0;
113       else return -1;
114   }
115   else {
116       if(!q)
117           return 1;
118       else return strncmp(p, q, size);
119   }
120 }
121
122 static inline int Atoi(const char* str) {
123   return str ? atoi(str) : 0;
124 }
125 static inline long Atol(const char *str) {
126   return str ? atol(str) : 0;
127 }
128
129 #ifndef MAX
130 #define MAX(a, b) \
131     ({typedef _ta = (a), _tb = (b);   \
132         _ta _a = (a); _tb _b = (b);     \
133         _a > _b ? _a : _b; })
134 #endif
135
136 #ifndef MIN
137 #define MIN(a, b) \
138     ({typedef _ta = (a), _tb = (b);   \
139         _ta _a = (a); _tb _b = (b);     \
140         _a < _b ? _a : _b; })
141 #endif
142
143 static inline char *Ltoa(long nr, char *str, int base) {
144   char buff[34], rest, is_negative;
145   int ptr;
146
147   ptr = 32;
148   buff[33] = '\0';
149   if (nr < 0) {
150     is_negative = 1;
151     nr = -nr;
152   }
153   else
154     is_negative = 0;
155
156   while (nr != 0) {
157     rest = nr % base;
158     if (rest > 9)
159       rest += 'A' - 10;
160     else
161       rest += '0';
162     buff[ptr--] = rest;
163     nr /= base;
164   }
165   if (ptr == 32)
166     buff[ptr--] = '0';
167   if (is_negative)
168     buff[ptr--] = '-';
169
170   Strcpy(str, &buff[ptr+1]);
171
172   return(str);
173 }
174
175 #endif /* __EOControl_COMMON_H__ */