]> gitweb.factorcode.org Git - factor.git/blob - extra/size-of/size-of.factor
4f4743c6b699e8caf18a7b2583998d4718d048da
[factor.git] / extra / size-of / size-of.factor
1
2 USING: io io.encodings.ascii io.files io.files.temp io.launcher
3        locals math.parser sequences sequences.deep
4        help.syntax
5        easy-help ;
6
7 IN: size-of
8
9 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10
11 DEFER: size-of
12
13 HELP: size-of
14
15 Values: HEADERS sequence  TYPE string  n integer ..
16
17 Description:
18
19     Use 'size-of' to find out the size in bytes of a C type. 
20
21     The 'headers' argument is a list of header files to use. You may 
22     pass 'f' to only use 'stdio.h'. ..
23
24 Example:
25
26     ! Find the size of 'int'
27
28     f "int" size-of .    ..
29
30 Example:
31
32     ! Find the size of the 'XAnyEvent' struct from Xlib.h
33
34     { "X11/Xlib.h" } "XAnyEvent" size-of .    ..
35
36 ;
37
38 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39
40 :: size-of ( HEADERS TYPE -- n )
41
42   [let | C-FILE   [ "size-of.c" temp-file ]
43          EXE-FILE [ "size-of"   temp-file ]
44          INCLUDES [ HEADERS [| FILE | { "#include <" FILE ">" } concat ] map ] |
45
46     {
47       "#include <stdio.h>"
48       INCLUDES
49       "main() { printf( \"%i\" , sizeof( " TYPE " ) ) ; }"
50     }
51
52     flatten C-FILE  ascii  set-file-lines
53
54     { "gcc" C-FILE "-o" EXE-FILE } try-process
55
56     EXE-FILE ascii <process-reader> contents string>number ] ;
57
58 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
59
60