-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.f
executable file
·108 lines (73 loc) · 3.48 KB
/
Config.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
marker Config.f \ For saving data, variables and strings in a file.
needs table_sort.f
0 value /ConfigDef \ Keeps how big the size of the config file should be.
create ConfigFile$ maxcounted allot s" Config.dat" ConfigFile$ place
S" win32forth" ENVIRONMENT? [IF] DROP
map-handle config-mhndl
: map-config-file ( - ) ConfigFile$ count config-mhndl open-map-file throw ;
: map-hndl>vadr ( m_hndl - vadr ) >hfileAddress @ ;
: vadr-config ( - vadr-config ) config-mhndl map-hndl>vadr ;
: DisableConfigFile ( - )
config-mhndl dup flush-view-file drop close-map-file drop ;
[THEN]
S" gforth" ENVIRONMENT? [IF] 2drop
2variable config-ghndl \ vadr size
: map-config-file ( - ) ConfigFile$ count r/w map-file config-ghndl 2! ;
: map-hndl>vadr ( m_hndl - vadr ) cell+ @ ;
: vadr-config ( - vadr-config ) config-ghndl map-hndl>vadr ;
: DisableConfigFile ( - ) config-ghndl 2@ 2dup MS_SYNC msync drop unmap ;
[THEN]
: file-exist? ( adr len -- true-if-file-exist ) file-status nip 0= ;
: file-size>s ( fileid -- len ) file-size drop d>s ;
: CreateConfigFile ( - )
/ConfigDef
ConfigFile$ count r/w bin create-file abort" Can't create configuration file"
extend-file
;
: check-config ( -- ) \ creates a config-file with the right size.
ConfigFile$ count file-exist?
if ConfigFile$ count r/w bin open-file abort" Can't open the cofiguration file"
/ConfigDef over file-size>s 2dup > \ Extend it when it is needed.
if - swap extend-file \ Keep the extisting data.
else 2drop CloseFile \ Do nothing when it is right.
then
else CreateConfigFile
then
;
: AllotConfigDef ( size - ) /ConfigDef dup , + to /ConfigDef ;
: OffsetInConfigDef ( adr - ) @ vadr-config + ;
\ A ConfigVariable directly acceses the config file.
\ They only work when the config file is mapped.
: ConfigVariable \ Allocates variables in a configuration file
create cell AllotConfigDef \ Compiletime: ( -< name >- )
does> OffsetInConfigDef \ Runtime: ( - AdrInMappedConfigFile )
;
: Config$: \ Allocates strings in a configuration file
create maxcounted AllotConfigDef \ Compiletime: ( -< name >- )
does> OffsetInConfigDef \ Runtime: ( - AdrInMappedConfigFile )
;
: DataArea: \ Allocates a data area in a configuration file
create AllotConfigDef \ Compiletime: ( size -< name >- )
does> OffsetInConfigDef \ Runtime: ( - AdrInMappedConfigFile )
;
: EnableConfigFile ( - ) check-config map-config-file ;
\s Disable this line to see it's use:
\ Define ConfigVariables to access the mapped file.
ConfigVariable LBs/Inches-
ConfigVariable SingCutoff-
Config$: DataFile$
ConfigVariable ShowObese-
8 DataArea: Test
EnableConfigFile \ Make sure there is a config file with the right size and map it
1 LBs/Inches- !
2 SingCutoff- !
s" c:\appl\test.dat" DataFile$ place
3 ShowObese- !
-1 Test !
DisableConfigFile \ When you are ready.
EnableConfigFile \ To use the config file again.
cr .( The saved values are: ) LBs/Inches- ? SingCutoff- ? ShowObese- ?
cr .( The name of the DataFile$ in the config file is: ) DataFile$ count type
vadr-config /ConfigDef dump
DisableConfigFile \ When you are ready.
\s