forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.mpl
146 lines (132 loc) · 4.44 KB
/
file.mpl
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (C) Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"Array.Array" use
"String.String" use
"String.addTerminator" use
"String.assembleString" use
"String.makeStringView" use
"String.makeStringViewByAddress" use
"String.toString" use
"algorithm.cond" use
"control.&&" use
"control.Int32" use
"control.Nat8" use
"control.Natx" use
"control.Text" use
"control.drop" use
"control.||" use
"conventions.cdecl" use
{stream: Natx;} Int32 {convention: cdecl;} "fclose" importFunction
{stream: Natx;} Int32 {convention: cdecl;} "ferror" importFunction
{stream: Natx;} Int32 {convention: cdecl;} "fflush" importFunction
{filename: Text; mode: Text;} Natx {convention: cdecl;} "fopen" importFunction
{buffer: Natx; size: Natx; count: Natx; stream: Natx;} Natx {convention: cdecl;} "fread" importFunction
{stream: Natx; offset: Int32; origin: Int32;} Int32 {convention: cdecl;} "fseek" importFunction
{stream: Natx;} Int32 {convention: cdecl;} "ftell" importFunction
{buffer: Natx; size: Natx; count: Natx; stream: Natx;} Natx {convention: cdecl;} "fwrite" importFunction
{errnum: Int32;} Natx {convention: cdecl;} "strerror" importFunction
SEEK_SET: [0i32];
SEEK_CUR: [1i32];
SEEK_END: [2i32];
private errno: [
"errno.errno" use
errno
];
getErrnoText: [
strerror makeStringViewByAddress
];
loadFile: [
name: addTerminator makeStringView;
result: {
result: String;
data: Nat8 Array;
};
file: Natx;
() (
[
drop
"rb\00" name.data storageAddress Text addressToReference fopen @file set
file 0nx =
] [("fopen failed, " errno new getErrnoText) assembleString @result.!result]
[
drop
SEEK_END 0 file fseek drop
size: file ftell Natx cast;
size Int32 cast @[email protected]
SEEK_SET 0 file fseek drop
file size 1nx result.data.data storageAddress fread size = ~
] [("fread failed, " file ferror getErrnoText) assembleString @result.!result]
[
file fclose drop
]
) cond
result
];
saveFile: [
data: name: addTerminator makeStringView;;
file: Natx;
() (
[
drop
"wb\00" name.data storageAddress Text addressToReference fopen @file set
file 0nx =
] [("fopen failed, " errno new getErrnoText) assembleString]
[
drop
size: data.size Natx cast;
file size 1nx data.data storageAddress fwrite size = ~
] [("fwrite failed, " file ferror getErrnoText) assembleString]
[
file fclose drop
"" toString
]
) cond
];
loadString: [
name: addTerminator makeStringView;
result: {
success: TRUE;
data: String;
};
size: 0nx dynamic;
f: "rb\00" name.data storageAddress Text addressToReference fopen;
f 0nx = ~ [
SEEK_END 0 f fseek 0 =
[f ftell Natx cast @size set
SEEK_SET 0 f fseek 0 =] &&
[size 0ix cast 0 cast @result.@[email protected]
f size 1nx @[email protected] storageAddress fread size =] &&
[0n8 @result.@[email protected] TRUE] &&
f fclose 0 = and
] &&
@result.@success set
result
];
saveString: [
stringView: makeStringView;
name: addTerminator makeStringView;
size: stringView.size;
f: "wb\00" name.data storageAddress Text addressToReference fopen;
f 0nx = ~
[
size 0 = [f size Natx cast 1nx stringView.data storageAddress fwrite size Natx cast =] ||
f fflush 0 = and
f fclose 0 = and
] &&
];
appendString: [
stringView: makeStringView;
name: addTerminator makeStringView;
size: stringView.size;
f: "ab\00" name.data storageAddress Text addressToReference fopen;
f 0nx = ~
[
size 0 = [f size Natx cast 1nx stringView.data storageAddress fwrite size Natx cast =] ||
f fflush 0 = and
f fclose 0 = and
] &&
];