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