]> err.no Git - sope/blob - sope-core/EOControl/common.h
Drop apache 1 build-dependency
[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 <string.h>
27 #include <objc/Protocol.h>
28
29 #import <Foundation/Foundation.h>
30 #import <Foundation/NSObjCRuntime.h>
31
32 #if NeXT_RUNTIME || APPLE_RUNTIME
33 #  define objc_free(__mem__)    free(__mem__)
34 #  define objc_malloc(__size__) malloc(__size__)
35 #  define objc_calloc(__cnt__, __size__) calloc(__cnt__, __size__)
36 #  define objc_realloc(__ptr__, __size__) realloc(__ptr__, __size__)
37 #  ifndef sel_eq
38 #    define sel_eq(sela,selb) (sela==selb?YES:NO)
39 #  endif
40 #endif
41
42 #ifndef ASSIGN
43 #  define ASSIGN(object, value) \
44        ({id __object = (id)object;    \
45          id __value = (id)value;      \
46          if (__value != __object) { if (__value) [__value retain]; \
47           if (__object) [__object release]; \
48           object = __value;}})
49 #endif
50 #ifndef ASSIGNCOPY
51 #  define ASSIGNCOPY(object, value) \
52        ({id __object = (id)object;    \
53          id __value = (id)value;      \
54          if (__value != __object) { if (__value) __value = [__value copy];   \
55           if (__object) [__object release]; \
56           object = __value;}})
57 #endif
58
59 // ******************** common functions ********************
60
61 static inline char* Strdup(const char*) __attribute__((unused));
62 static inline char* Strcpy (char*, const char*) __attribute__((unused));
63 static inline char* Strncpy (char*, const char*, unsigned)
64     __attribute__((unused));
65 static inline char* Strcat (char*, const char*) __attribute__((unused));
66 static inline char* Strncat (char*, const char*, unsigned)
67     __attribute__((unused));
68 static inline int Strcmp(const char*, const char*) __attribute__((unused));
69 static inline int Strncmp(const char*, const char*, unsigned)
70     __attribute__((unused));
71 static inline int Atoi(const char*) __attribute__((unused));
72 static inline long Atol(const char*) __attribute__((unused));
73
74 #define Malloc  malloc
75 #define Calloc  calloc
76 #define Realloc realloc
77 #define Free(__p__)  if(__p__) { free(__p__); __p__ = NULL; }
78
79 #define Strlen(__s__) (__s__?strlen(__s__):0)
80
81 static inline char *Strdup(const char *s) {
82   return s ? strcpy(Malloc(strlen(s) + 1), s) : NULL;
83 }
84 static inline char* Strcpy (char *d, const char *s) {
85   return s && d ? strcpy(d, s) : d;
86 }
87 static inline char* Strncpy (char* d, const char* s, unsigned size) {
88   return s && d ? strncpy(d, s, size) : d;
89 }
90 static inline char* Strcat (char* d, const char* s) {
91   return s && d ? strcat(d, s) : d;
92 }
93 static inline char* Strncat (char* d, const char* s , unsigned size) {
94   return s && d ? strncat(d, s , size) : d;
95 }
96
97 static inline int Strcmp(const char* p, const char* q) {
98   if(p == NULL) {
99     if(!q)
100       return 0;
101     else return -1;
102   }
103   else {
104     if(!q)
105       return 1;
106     else return strcmp(p, q);
107   }
108 }
109
110 static inline int Strncmp(const char* p, const char* q, unsigned size) {
111   if(!p) {
112       if(!q)
113           return 0;
114       else return -1;
115   }
116   else {
117       if(!q)
118           return 1;
119       else return strncmp(p, q, size);
120   }
121 }
122
123 static inline int Atoi(const char* str) {
124   return str ? atoi(str) : 0;
125 }
126 static inline long Atol(const char *str) {
127   return str ? atol(str) : 0;
128 }
129
130 #ifndef MAX
131 #define MAX(a, b) \
132     ({typedef _ta = (a), _tb = (b);   \
133         _ta _a = (a); _tb _b = (b);     \
134         _a > _b ? _a : _b; })
135 #endif
136
137 #ifndef MIN
138 #define MIN(a, b) \
139     ({typedef _ta = (a), _tb = (b);   \
140         _ta _a = (a); _tb _b = (b);     \
141         _a < _b ? _a : _b; })
142 #endif
143
144 static inline char *Ltoa(long nr, char *str, int base) {
145   char buff[34], rest, is_negative;
146   int ptr;
147
148   ptr = 32;
149   buff[33] = '\0';
150   if (nr < 0) {
151     is_negative = 1;
152     nr = -nr;
153   }
154   else
155     is_negative = 0;
156
157   while (nr != 0) {
158     rest = nr % base;
159     if (rest > 9)
160       rest += 'A' - 10;
161     else
162       rest += '0';
163     buff[ptr--] = rest;
164     nr /= base;
165   }
166   if (ptr == 32)
167     buff[ptr--] = '0';
168   if (is_negative)
169     buff[ptr--] = '-';
170
171   Strcpy(str, &buff[ptr+1]);
172
173   return(str);
174 }
175
176 #endif /* __EOControl_COMMON_H__ */