Merge from trunk
[plcapi.git] / trunk / psycopg2 / psycopg / config.h
1 /* config.h - general config and Dprintf macro
2  *
3  * Copyright (C) 2003 Federico Di Gregorio <fog@debian.org>
4  *
5  * This file is part of psycopg.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2,
10  * or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef PSYCOPG_CONFIG_H
23 #define PSYCOPG_CONFIG_H 1
24
25 /* debug printf-like function */
26 #if defined( __GNUC__) && !defined(__APPLE__)
27 #ifdef PSYCOPG_DEBUG
28 #include <sys/types.h>
29 #include <unistd.h>
30 #define Dprintf(fmt, args...) \
31     fprintf(stderr, "[%d] " fmt "\n", getpid() , ## args)
32 #else
33 #define Dprintf(fmt, args...)
34 #endif
35 #else /* !__GNUC__ or __APPLE__ */
36 #ifdef PSYCOPG_DEBUG
37 #include <stdarg.h>
38 static void Dprintf(const char *fmt, ...)
39 {
40     va_list ap;
41     va_start(ap, fmt);
42     vprintf(fmt, ap);
43     va_end(ap);
44     printf("\n");
45 }
46 #else
47 static void Dprintf(const char *fmt, ...) {}
48 #endif
49 #endif
50
51 /* pthreads work-arounds for mutilated operating systems */
52 #if defined(_WIN32) || defined(__BEOS__)
53
54 #ifdef _WIN32
55 #include <winsock2.h>
56 #define pthread_mutex_t HANDLE
57 #define pthread_condvar_t HANDLE
58 #define pthread_mutex_lock(object) WaitForSingleObject(object, INFINITE)
59 #define pthread_mutex_unlock(object) ReleaseMutex(object)
60 #define pthread_mutex_destroy(ref) (CloseHandle(*(ref)))
61 /* convert pthread mutex to native mutex */
62 static int pthread_mutex_init(pthread_mutex_t *mutex, void* fake)
63 {
64   *mutex = CreateMutex(NULL, FALSE, NULL);
65   return 0;
66 }
67 #endif /* _WIN32 */
68
69 #ifdef __BEOS__
70 #include <OS.h>
71 #define pthread_mutex_t sem_id
72 #define pthread_mutex_lock(object) acquire_sem(object)
73 #define pthread_mutex_unlock(object) release_sem(object)
74 #define pthread_mutex_destroy(ref) delete_sem(*ref)
75 static int pthread_mutex_init(pthread_mutex_t *mutex, void* fake)
76 {
77         *mutex = create_sem(1, "psycopg_mutex");
78         if (*mutex < B_OK)
79                 return *mutex;
80         return 0;
81 }
82 #endif /* __BEOS__ */
83
84 #else /* pthread is available */
85 #include <pthread.h>
86 #endif
87
88 /* to work around the fact that Windows does not have a gmtime_r function, or
89    a proper gmtime function */
90 #ifdef _WIN32
91 static struct tm *gmtime_r(time_t *t, struct tm *tm)
92 {
93   tm = gmtime(t);
94   return tm;
95 }
96 static struct tm *localtime_r(time_t *t, struct tm *tm)
97 {
98   tm = localtime(t);
99   return tm;
100 }
101 /* remove the inline keyword, since it doesn't work unless C++ file */
102 #define inline
103 #endif
104
105 #if defined(__FreeBSD__) || defined(_WIN32) || defined(__sun__)
106 /* what's this, we have no round function either? */
107 static double round(double num)
108 {
109   return (num >= 0) ? floor(num + 0.5) : ceil(num - 0.5);
110 }
111 #endif
112
113 /* postgresql < 7.4 does not have PQfreemem */
114 #ifndef HAVE_PQFREEMEM
115 #define PQfreemem free
116 #endif
117
118 #endif /* !defined(PSYCOPG_CONFIG_H) */