FreeBASIC  0.91.0
fb.h
Go to the documentation of this file.
1 #ifndef __FB_H__
2 #define __FB_H__
3 
4 /* Must be included before any system headers due to certain #defines */
5 #include "fb_config.h"
6 
7 /* Minimum headers needed for fb.h alone, more in system-specific sections
8  below. These can be relied upon and don't need to be #included again. */
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 
15 #define FB_TRUE (-1)
16 #define FB_FALSE 0
17 
18 #ifndef FALSE
19 #define FALSE 0
20 #endif
21 #ifndef TRUE
22 #define TRUE 1
23 #endif
24 #ifndef NULL
25 #define NULL 0
26 #endif
27 
28 /* Defines the ASCII code that indicates a two-byte key code.
29  A two-byte key code will be returned by GET on SCRN: or INKEY$. */
30 #define FB_EXT_CHAR ((char)255)
31 
32 /* Maximum number of temporary string descriptors. */
33 #define FB_STR_TMPDESCRIPTORS 256
34 
35 /* Maximum number of array dimensions. */
36 #define FB_MAXDIMENSIONS 8
37 
38 /* Maximum number of temporary array descriptors. */
39 #define FB_ARRAY_TMPDESCRIPTORS (FB_STR_TMPDESCRIPTORS / 4)
40 
41 /* The padding width (for PRINT ,). */
42 #define FB_TAB_WIDTH 14
43 
44 #if FB_TAB_WIDTH == 8
45 #define FB_NATIVE_TAB 1
46 #endif
47 
48 /* Screen width/height returned by default when native console function failed.
49  This is required when an applications output is redirected. */
50 #define FB_SCRN_DEFAULT_WIDTH 80
51 #define FB_SCRN_DEFAULT_HEIGHT 25
52 
53 /* Default colors for console color() function */
54 #define FB_COLOR_FG_DEFAULT 0x1
55 #define FB_COLOR_BG_DEFAULT 0x2
56 
57 /* Number of reserved file handles. 0: SCRN, 1: LPT1 */
58 #define FB_RESERVED_FILES 2
59 
60 /* Maximum number of file handles. */
61 #define FB_MAX_FILES (FB_RESERVED_FILES + 255)
62 
63 /* File buffer size (for buffered read?). */
64 #define FB_FILE_BUFSIZE 8192
65 
66 /* Max length to allocated for a temporary buffer on stack */
67 #define FB_LOCALBUFF_MAXLEN 32768
68 
69 #ifndef HOST_WIN32
70  /* Maximum path length for Non-Win32 targets. For Win32 targets, this
71  value will be set automatically by windows.h. */
72  #define MAX_PATH 1024
73 #endif
74 
75 /* Convert char to int without sign-extension. */
76 #define FB_CHAR_TO_INT(ch) ((int) ((unsigned) (unsigned char) (ch)))
77 
78 /* Build an extended 2 byte key code like those returned by getkey()
79  (inkey() returns a string like &hFF &h49 [page up key code],
80  getkey() returns the same but in a little-endian integer: &h49FF
81  where &hFF is the FB_EXT_CHAR */
82 #define FB_MAKE_EXT_KEY(ch) \
83  ((int) ((((unsigned) (unsigned char) (ch)) << 8) + \
84  (unsigned) (unsigned char) (FB_EXT_CHAR)))
85 
86 #define MIN(a,b) ((a) < (b) ? (a) : (b))
87 #define MAX(a,b) ((a) > (b) ? (a) : (b))
88 #define MID(a,b,c) MIN(MAX((a), (b)), (c))
89 
90 #define SWAP(a,b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
91 
92 #if defined HOST_DOS
93  #include "dos/fb_dos.h"
94 #elif defined HOST_UNIX
95  #include "unix/fb_unix.h"
96 #elif defined HOST_WIN32
97  #include "win32/fb_win32.h"
98 #elif defined HOST_XBOX
99  #include "xbox/fb_xbox.h"
100 #endif
101 
102 #if defined ENABLE_MT && !defined HOST_DOS && !defined HOST_XBOX
103  FBCALL void fb_Lock( void );
104  FBCALL void fb_Unlock( void );
105  FBCALL void fb_StrLock( void );
106  FBCALL void fb_StrUnlock( void );
107  #define FB_LOCK() fb_Lock()
108  #define FB_UNLOCK() fb_Unlock()
109  #define FB_STRLOCK() fb_StrLock()
110  #define FB_STRUNLOCK() fb_StrUnlock()
111 #else
112  #define FB_LOCK()
113  #define FB_UNLOCK()
114  #define FB_STRLOCK()
115  #define FB_STRUNLOCK()
116 #endif
117 
118 /* CPU-dependent macros and inline functions */
119 #ifdef HOST_X86
120  static __inline__ int FB_MEMCMP( const void *p1, const void *p2, size_t len )
121  {
122  int res;
123  if( len==0 )
124  return 0;
125  __asm volatile (
126  " pushl %%esi \n"
127  " pushl %%edi \n"
128  " repe \n"
129  " cmpsb \n"
130  " je 0f \n"
131  " movl $1, %%ecx \n"
132  " ja 0f \n"
133  " neg %%ecx \n"
134  "0: \n"
135  " popl %%edi \n"
136  " popl %%esi \n"
137  : "=c" (res)
138  : "c" (len), "S" (p1), "D" (p2)
139  );
140  return res;
141  }
142 
143  static __inline__ void *FB_MEMCPY( void *dest, const void *src, size_t n )
144  {
145  __asm volatile (
146  " pushl %%ecx \n"
147  " pushl %%esi \n"
148  " pushl %%edi \n"
149  " pushl %%ecx \n"
150  " shrl $2,%%ecx \n"
151  " rep \n"
152  " movsd \n"
153  " popl %%ecx \n"
154  " andl $3,%%ecx \n"
155  " rep \n"
156  " movsb \n"
157  " popl %%edi \n"
158  " popl %%esi \n"
159  " popl %%ecx \n"
160  :
161  : "c" (n), "S" (src), "D" (dest)
162  );
163  return dest;
164  }
165 
168  static __inline__ void *FB_MEMCPYX( void *dest, const void *src, size_t n )
169  {
170  __asm volatile (
171  " pushl %%ecx \n"
172  " pushl %%esi \n"
173  " pushl %%ecx \n"
174  " shrl $2,%%ecx \n"
175  " rep \n"
176  " movsd \n"
177  " popl %%ecx \n"
178  " andl $3,%%ecx \n"
179  " rep \n"
180  " movsb \n"
181  " popl %%esi \n"
182  " popl %%ecx \n"
183  : "=D" (dest)
184  : "c" (n), "S" (src), "D" (dest)
185  );
186  return dest;
187  }
188 
189  static __inline__ const void *FB_MEMCHR( const void *s, int c, size_t n )
190  {
191  const void *dst;
192  if( n==0 )
193  return NULL;
194  __asm volatile (
195  " pushl %%ecx \n"
196  " pushf \n"
197  " cld \n"
198  " repne \n"
199  " scasb \n"
200  " jne 0f \n"
201  " dec %%edi \n"
202  " jmp 1f \n"
203  "0: \n"
204  " xorl %%edi, %%edi \n"
205  "1: \n"
206  " popf \n"
207  " popl %%ecx \n"
208  : "=D" (dst)
209  : "c" (n), "a" (c), "D" (s)
210  );
211  return dst;
212  }
213 
216  static __inline__ size_t FB_MEMLEN( const void *s, int c, size_t n )
217  {
218  size_t len;
219  if( n==0 )
220  return 0;
221  __asm volatile (
222  " pushl %%edi \n"
223  " pushf \n"
224  " std \n" /* DF = 1 -> from hi to lo */
225  " repe \n"
226  " scasb \n"
227  " je 0f \n"
228  " inc %%ecx \n"
229  "0: \n"
230  " popf \n"
231  " popl %%edi \n"
232  : "=c" (len)
233  : "c" (n), "a" (c), "D" ((const char*) s + n - 1)
234  );
235  return len;
236  }
237 
238  #define RORW(num, bits) __asm__ __volatile__("rorw %1, %0" : "=m"(num) : "c"(bits) : "memory")
239  #define RORW1(num) __asm__ __volatile__("rorw $1, %0" : "=m"(bit) : : "memory");
240 #else
241  /* We use memcmp from C because the compiler might replace this by a built-in
242  * function which will definately be faster than our own implementation in C. */
243  #define FB_MEMCMP(p1, p2, len) memcmp( p1, p2, len )
244  #define FB_MEMCPY( dest, src, n ) memcpy(dest, src, n)
245  #define FB_MEMCHR( s, c, n ) memchr( s, c, n )
246 
247  /* We have to wrap memcpy here because our MEMCPYX should return the position
248  * after the destination string. */
249  static __inline__ void *FB_MEMCPYX( void *dest, const void *src, size_t n )
250  {
251  memcpy(dest, src, n);
252  return ((char *)dest)+n;
253  }
254 
255  static __inline__ size_t FB_MEMLEN( const void *s, int c, size_t n )
256  {
257  const char *pachData = (const char*) s;
258  while (n--) {
259  if( pachData[n]!=(char)c )
260  return n+1;
261  }
262  return 0;
263  }
264 
265  #define RORW(num, bits) num = ( (num) >> (bits) ) | (num << (16 - bits) )
266  #define RORW1(num) RORW(num, 1)
267 #endif
268 
269 #ifdef DEBUG
270  #include <assert.h>
271  #define DBG_ASSERT(e) assert(e)
272 #else
273  #define DBG_ASSERT(e) ((void)0)
274 #endif
275 
276 #define fb_hSign( x ) (((x) < 0) ? -1 : 1)
277 
278 /* internal lists */
279 typedef struct _FB_LISTELEM {
282 } FB_LISTELEM;
283 
284 typedef struct _FB_LIST {
285  int cnt; /* Number of used elements */
286  FB_LISTELEM *head; /* First used element */
287  FB_LISTELEM *tail; /* Last used element */
288  FB_LISTELEM *fhead; /* First free element */
289 } FB_LIST;
290 
291 void fb_hListInit ( FB_LIST *list, void *table, size_t elem_size, size_t size );
293 void fb_hListFreeElem ( FB_LIST *list, FB_LISTELEM *elem );
294 void fb_hListDynInit ( FB_LIST *list );
295 void fb_hListDynElemAdd ( FB_LIST *list, FB_LISTELEM *elem );
297 
298 #include "fb_unicode.h"
299 #include "fb_error.h"
300 #include "fb_string.h"
301 #include "fb_array.h"
302 #include "fb_system.h"
303 #include "fb_math.h"
304 #include "fb_data.h"
305 #include "fb_console.h"
306 #include "fb_file.h"
307 #include "fb_print.h"
308 #include "fb_device.h"
309 #include "fb_serial.h"
310 #include "fb_printer.h"
311 #include "fb_datetime.h"
312 #include "fb_thread.h"
313 #include "fb_hook.h"
314 #include "fb_oop.h"
315 #include "fb_legacy.h"
316 
317 /* DOS keyboard scancodes */
318 #define SC_ESCAPE 0x01
319 #define SC_1 0x02
320 #define SC_2 0x03
321 #define SC_3 0x04
322 #define SC_4 0x05
323 #define SC_5 0x06
324 #define SC_6 0x07
325 #define SC_7 0x08
326 #define SC_8 0x09
327 #define SC_9 0x0A
328 #define SC_0 0x0B
329 #define SC_MINUS 0x0C
330 #define SC_EQUALS 0x0D
331 #define SC_BACKSPACE 0x0E
332 #define SC_TAB 0x0F
333 #define SC_Q 0x10
334 #define SC_W 0x11
335 #define SC_E 0x12
336 #define SC_R 0x13
337 #define SC_T 0x14
338 #define SC_Y 0x15
339 #define SC_U 0x16
340 #define SC_I 0x17
341 #define SC_O 0x18
342 #define SC_P 0x19
343 #define SC_LEFTBRACKET 0x1A
344 #define SC_RIGHTBRACKET 0x1B
345 #define SC_ENTER 0x1C
346 #define SC_CONTROL 0x1D
347 #define SC_A 0x1E
348 #define SC_S 0x1F
349 #define SC_D 0x20
350 #define SC_F 0x21
351 #define SC_G 0x22
352 #define SC_H 0x23
353 #define SC_J 0x24
354 #define SC_K 0x25
355 #define SC_L 0x26
356 #define SC_SEMICOLON 0x27
357 #define SC_QUOTE 0x28
358 #define SC_TILDE 0x29
359 #define SC_LSHIFT 0x2A
360 #define SC_BACKSLASH 0x2B
361 #define SC_Z 0x2C
362 #define SC_X 0x2D
363 #define SC_C 0x2E
364 #define SC_V 0x2F
365 #define SC_B 0x30
366 #define SC_N 0x31
367 #define SC_M 0x32
368 #define SC_COMMA 0x33
369 #define SC_PERIOD 0x34
370 #define SC_SLASH 0x35
371 #define SC_RSHIFT 0x36
372 #define SC_MULTIPLY 0x37
373 #define SC_ALT 0x38
374 #define SC_SPACE 0x39
375 #define SC_CAPSLOCK 0x3A
376 #define SC_F1 0x3B
377 #define SC_F2 0x3C
378 #define SC_F3 0x3D
379 #define SC_F4 0x3E
380 #define SC_F5 0x3F
381 #define SC_F6 0x40
382 #define SC_F7 0x41
383 #define SC_F8 0x42
384 #define SC_F9 0x43
385 #define SC_F10 0x44
386 #define SC_NUMLOCK 0x45
387 #define SC_SCROLLLOCK 0x46
388 #define SC_HOME 0x47
389 #define SC_UP 0x48
390 #define SC_PAGEUP 0x49
391 #define SC_LEFT 0x4B
392 #define SC_CLEAR 0x4C
393 #define SC_RIGHT 0x4D
394 #define SC_PLUS 0x4E
395 #define SC_END 0x4F
396 #define SC_DOWN 0x50
397 #define SC_PAGEDOWN 0x51
398 #define SC_INSERT 0x52
399 #define SC_DELETE 0x53
400 #define SC_F11 0x57
401 #define SC_F12 0x58
402 #define SC_LWIN 0x5B
403 #define SC_RWIN 0x5C
404 #define SC_MENU 0x5D
405 #define SC_ALTGR 0x64
406 
407 #define KEY_BACKSPACE 8
408 #define KEY_TAB '\t'
409 #define KEY_F1 FB_MAKE_EXT_KEY( ';' )
410 #define KEY_F2 FB_MAKE_EXT_KEY( '<' )
411 #define KEY_F3 FB_MAKE_EXT_KEY( '=' )
412 #define KEY_F4 FB_MAKE_EXT_KEY( '>' )
413 #define KEY_F5 FB_MAKE_EXT_KEY( '?' )
414 #define KEY_F6 FB_MAKE_EXT_KEY( '@' )
415 #define KEY_F7 FB_MAKE_EXT_KEY( 'A' )
416 #define KEY_F8 FB_MAKE_EXT_KEY( 'B' )
417 #define KEY_F9 FB_MAKE_EXT_KEY( 'C' )
418 #define KEY_F10 FB_MAKE_EXT_KEY( 'D' )
419 #define KEY_HOME FB_MAKE_EXT_KEY( 'G' )
420 #define KEY_UP FB_MAKE_EXT_KEY( 'H' )
421 #define KEY_PAGE_UP FB_MAKE_EXT_KEY( 'I' )
422 #define KEY_LEFT FB_MAKE_EXT_KEY( 'K' )
423 #define KEY_CLEAR FB_MAKE_EXT_KEY( 'L' )
424 #define KEY_RIGHT FB_MAKE_EXT_KEY( 'M' )
425 #define KEY_END FB_MAKE_EXT_KEY( 'O' )
426 #define KEY_DOWN FB_MAKE_EXT_KEY( 'P' )
427 #define KEY_PAGE_DOWN FB_MAKE_EXT_KEY( 'Q' )
428 #define KEY_INS FB_MAKE_EXT_KEY( 'R' )
429 #define KEY_DEL FB_MAKE_EXT_KEY( 'S' )
430 #define KEY_QUIT FB_MAKE_EXT_KEY( 'k' )
431 
432 FBSTRING *fb_hMakeInkeyStr( int ch );
433 int fb_hScancodeToExtendedKey( int scancode );
434 
435 /* This should match fbc's lang enum */
436 enum FB_LANG {
442 };
443 
444 typedef struct FB_RTLIB_CTX_ {
445  int argc;
446  char **argv;
448  char *errmsg;
452  int lang;
453 } FB_RTLIB_CTX;
454 
455 extern FB_RTLIB_CTX __fb_ctx;
456 
457 #endif /*__FB_H__*/