Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
[gnokii.git] / include / misc.h
1 /*
2
3   $Id$
4
5   G N O K I I
6
7   A Linux/Unix toolset and driver for Nokia mobile phones.
8
9   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
10
11   Released under the terms of the GNU GPL, see file COPYING for more details.
12
13   Header file for miscellaneous defines, typedefs etc.
14
15   $Log$
16   Revision 1.1.1.1.8.1  2001/11/27 21:30:44  short
17   Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
18
19   Revision 1.1.1.1.2.1  2001/11/27 04:37:59  short
20   Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
21
22   Revision 1.1.1.2  2001/11/27 04:19:40  short
23   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 05:17 CET 2001
24
25   Revision 1.27  2001/11/26 18:06:08  pkot
26   Checking for *printf functions, N_(x) for localization, generic ARRAY_LEN, SAFE_STRNCPY, G_GNUC_PRINTF (Jan Kratochvil)
27
28   Revision 1.26  2001/11/14 10:46:12  pkot
29   Small cleanup with __unices__
30
31   Revision 1.25  2001/07/03 15:27:14  pkot
32   AT commands for SMS handling support (Tamas Bondar)
33   Small at-emulator code cleanup (me)
34
35   Revision 1.24  2001/06/28 00:28:46  pkot
36   Small docs updates (Pawel Kot)
37
38
39 */
40
41 #ifndef __misc_h
42 #define __misc_h    
43
44 #include "config.h"
45
46 /* Some general defines. */
47
48 #ifndef false
49 #  define false (0)
50 #endif
51
52 #ifndef true
53 #  define true (!false)
54 #endif
55
56 #ifndef bool    
57 #  define bool int
58 #endif
59
60 #define ARRAY_LEN(x) (sizeof((x)) / sizeof((x)[0]))
61
62 #define SAFE_STRNCPY(dest, src, n) do { \
63         strncpy((dest), (src), (n)); \
64         if ((n) > 0) \
65                 (dest)[(n)-1] = '\0'; \
66         } while (0)
67
68 #define SAFE_STRNCPY_SIZEOF(dest,src) \
69         SAFE_STRNCPY((dest), (src), sizeof((dest)))
70
71 /* Stolen from <glib.h>: */
72 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
73 #  define G_GNUC_PRINTF( format_idx, arg_idx )  \
74     __attribute__((format (printf, format_idx, arg_idx)))
75 #else   /* !__GNUC__ */
76 #  define G_GNUC_PRINTF( format_idx, arg_idx )
77 #endif  /* !__GNUC__ */
78
79 #define GNOKII_MAX(a, b)  (((a) > (b)) ? (a) : (b))
80 #define GNOKII_MIN(a, b)  (((a) < (b)) ? (a) : (b))
81
82 /* A define to make debug printfs neat */
83 #ifndef DEBUG
84 #  define dprintf(a...) do { } while (0)
85 #else
86 #  define dprintf(a...) do { fprintf(stderr, a); fflush(stderr); } while (0) 
87 #endif
88
89 /* Use gsprintf instead of sprintf and sprintf */
90 #ifdef HAVE_SNPRINTF
91 #  define gsprintf(a, b, c...) snprintf(a, b, c)
92 #else
93 #  define gsprintf(a, b, c...) sprintf(a, c)
94 #endif
95 #ifdef HAVE_VSNPRINTF
96 #  define gvsprintf(a, b, c...) vsnprintf(a, b, c)
97 #else
98 #  define gvsprintf(a, b, c...) vsprintf(a, c)
99 #endif
100 #ifdef HAVE_ASPRINTF
101 #  define gasprintf(a...) asprintf(a)
102 #else
103 #include <stdarg.h>
104 extern int gasprintf(char **destp, const char *fmt,...);
105 #endif
106 #ifdef HAVE_VASPRINTF
107 #  define gvasprintf(a...) vasprintf(a)
108 #else
109 #include <stdarg.h>
110 extern int gvasprintf(char **destp, const char *fmt, va_list ap);
111 #endif
112
113 /* Get rid of long defines. Use #if __unices__ */
114 #define __unices__ defined(__svr4__) || defined(__FreeBSD__) || defined(__bsdi__)
115 #if __unices__
116 #  include <strings.h>
117 #  include <sys/file.h>
118 #endif
119
120 /* This one is for NLS. */
121 #ifdef USE_NLS
122 #  include <libintl.h>
123 #  define _(x) gettext(x)
124 #  define N_(x) gettext_noop(x)
125 #else
126 #  define _(x) (x)
127 #  define N_(x) (x)
128 #endif /* USE_NLS */
129
130 /* Definitions for u8, u16, u32 and u64, borrowed from
131    /usr/src/linux/include/asm-i38/types.h */
132
133 #ifndef u8
134         typedef unsigned char u8;
135 #endif
136
137 #ifndef u16
138         typedef unsigned short u16;
139 #endif
140
141 #ifndef u32
142         typedef unsigned int u32;
143 #endif
144
145 #ifndef s32
146         typedef int s32;
147 #endif
148
149 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
150 #  ifndef u64
151         typedef unsigned long long u64;
152 #  endif
153
154 #  ifndef s64
155         typedef signed long long s64;
156 #  endif
157 #endif 
158
159 /* This one is for FreeBSD and similar systems without __ptr_t_ */
160 /* FIXME: autoconf should take care of this. */
161 #ifndef __ptr_t
162         typedef void * __ptr_t;
163 #endif /* __ptr_t */
164
165
166 /* Add here any timer operations which are not supported by libc5 */
167
168 #ifndef HAVE_TIMEOPS
169 #  ifdef WIN32
170
171 #    include <windows.h>
172 #    include <sys/timeb.h>
173 #    define timersub(a, b, result)
174         do {
175                 (result)->tv_sec = (a)->time - (b)->time;
176                 (result)->tv_usec = ((a)->millitm - (b)->millitm) * 1000;
177                 if ((result)->tv_usec < 0) {
178                         --(result)->tv_sec;
179                         (result)->tv_usec += 1000000;
180                 }
181         } while (0)
182 #    define gettimeofday(a, b) _ftime(a)
183
184 #  else /* !WIN32 */
185 #    include <sys/time.h>
186
187 #    ifndef timersub
188 #      define timersub(a, b, result)                               \
189         do {                                                       \
190                 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;      \
191                 (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;   \
192                 if ((result)->tv_usec < 0) {                       \
193                         --(result)->tv_sec;                        \
194                         (result)->tv_usec += 1000000;              \
195                 }                                                  \
196         } while (0)
197 #    endif /* timersub */
198
199 #  endif /* WIN32 */
200 #endif /* HAVE_TIMEOPS */
201
202
203 #include <stdio.h>
204 extern int GetLine(FILE *File, char *Line, int count);
205
206 /* For models table */
207 typedef struct {
208         char *model;
209         char *number;
210         int flags;
211 } PhoneModel;
212
213 #define PM_CALLERGROUP    0x0001
214 #define PM_NETMONITOR     0x0002
215 #define PM_KEYBOARD       0x0004
216 #define PM_SMS            0x0008
217 #define PM_CALENDAR       0x0010
218 #define PM_DTMF           0x0020
219 #define PM_DATA           0x0040
220 #define PM_SPEEDDIAL      0x0080
221 #define PM_EXTPBK         0x0100
222 #define PM_AUTHENTICATION 0x0200
223
224 extern char *GetModel (const char *);
225 extern PhoneModel *GetPhoneModel (const char *);
226
227 #endif /* __misc_h */
228