/
legend.monkey
100 lines (77 loc) · 2.55 KB
/
legend.monkey
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
Strict
Public
' Preprocessor related:
' Nothing so far.
' Imports (Internal):
Import core
Private
#If VIRTUALOS_EXTENSION_LOADBUFFER Or VIRTUALOS_EXTENSION_SAVEBUFFER
Import brl.databuffer
#End
Import filesystem
Public
' Imports (Other):
#If Not VIRTUALOS_IMPLEMENTED
#If VIRTUALOS_REAL
#If Not VIRTUALOS_REAL_USE_BRL
Import os
#End
#End
#Else
#If Not VIRTUALOS_TRADITIONAL
Import "native/legend.${LANG}"
#End
#End
#If VIRTUALOS_IMPLEMENTED
' External bindings:
Extern
' Functions (External):
' API:
Function HostOS:String()
Function LoadString:String(Path:String)
Function SaveString:Int(Str:String, Path:String)
' Extensions:
#If VIRTUALOS_EXTENSION_UNSAFE_LOADARRAY
' This provides an array of integers in a native format, which is ideally used like a normal array.
' The resulting array is symbolic, and may or may not behave appropriately.
' If you are unsure, use 'LoadString'. (Binary data may still need to use this extension)
Function __OS_Unsafe__LoadArray:Int[](RealPath:String)="__os_LoadArray" ' UNSAFE
#End
#If VIRTUALOS_EXTENSION_LOADBUFFER
' This natively binds the file-data of 'RealPath' to 'Buffer'.
' This data may or may not represent the native entry.
' Mutation of this buffer will result in undefined behavior.
Function __OS_LoadBufferTo:BBDataBuffer(RealPath:String, Buffer:BBDataBuffer)="__os_LoadBufferTo"
#End
#If VIRTUALOS_EXTENSION_SAVEBUFFER
' This takes the contents of 'Buffer' and creates a file-system entry using 'RealPath'.
' This will only override the contents of an existing file-entry if 'Override' is enabled.
Function __OS_SaveBuffer:Bool(RealPath:String, Buffer:BBDataBuffer, Override:Bool=True)="__os_SaveBuffer"
#End
Public
' Functions (Public):
#If VIRTUALOS_EXTENSION_LOADBUFFER
' This allocates a 'DataBuffer' using the file-data at 'RealPath'.
' If the file does not exist, this will return 'Null'.
' The output-buffer of this method should not be modified
' unless 'VIRTUALOS_EXTENSION_LOADBUFFER_SAFE' is defined.
Function __OS_LoadBuffer:DataBuffer(RealPath:String)
Local B:= New DataBuffer()
If (__OS_LoadBufferTo(RealPath, B) <> B) Then
Return Null
Endif
' Apply safety behavior:
#If VIRTUALOS_EXTENSION_LOADBUFFER_SAFE
#If VIRTUALOS_EXTENSION_ENCODING_INFO
If (__OS_GetFileSystemEncoding() <> FILESYSTEM_ENCODING_ARRAYBUFFER) Then
Return B
Endif
#End
Local Out:= New DataBuffer(B.Length)
B.CopyBytes(0, Out, 0, Out.Length) ' B.Length
Return Out
#End
Return B
End
#End
#End