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