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