crlf.anubis
3.46 KB
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
/*
* Created by PyramIDE.
* User: フランスのトトロ aka (David RENÉ)
* Date: 25/06/2016
* Time: 18:14
* © David RENÉ
*/
read tools/basis.anubis
transmit system/files.anubis
transmit calexium_lib/extensions/json.anubis
public define String
/** normalize_to_CRLF will search for alone CR or LF and transform it to CRLF
* output is the input string normalized
*/
to_CRLF_String
(
String unchecked_string
)=
//try to use adl version of json escape string
if load_library("json") is
{
error(err) then
println("load lib error:");
println("--> can't load extension lib. Hence return input string");
unchecked_string
ok(json_lib) then
to_CRLF_String(json_lib, unchecked_string)
}.
public define Maybe(ByteArray -> ByteArray)
/** normalize_to_CRLF will search for alone CR or LF and transform it to CRLF
* output is the input string normalized
*/
get_CRLF_String_converter
=
//try to use adl version of json escape string
if load_library("json") is
{
error(err) then
println("load lib error:");
println("--> can't load extension lib. Hence return input string");
failure
ok(json_lib) then
success(((ByteArray) ba_input) |-> to_CRLF_ByteArray(json_lib, to_string(ba_input)))
}.
define ResultCopy
_copy_file_and_convert
(
RStream source,
WStream target,
ByteArray -> ByteArray converter,
Int so_far
) =
if read(source, 65536, 10) is
{
error then copy_error,
timeout then _copy_file_and_convert(source, target, converter, so_far),
ok(buffer)then
with converted = converter(buffer),
with len = length(converted),
if len = 0 then
copy_ok(so_far)
else
if flush(converted, target) is
{
failure then println("=> flush error");copy_error,
success(_) then _copy_file_and_convert(source, target, converter, so_far + len)
}
}.
public define ResultCopy
copy_file_and_convert_to_CRLF
(
String source_file,
String target_file,
ByteArray -> ByteArray converter
) =
//open the source file
if (Maybe(RStream))file(source_file, read) is
{
failure then cant_read_file,
success(source) then
//open the target file
if (Maybe(RWStream))file(target_file, new) is
{
failure then println("can't create target file "+target_file);cant_create_file, //nothing to write
success(target) then _copy_file_and_convert(source, weaken(target), converter, 0),
//with start = unow,
//result = _copy_file_and_convert(source, weaken(target), converter, 0),
//println(show_duration_string("copy_file_data_and_convert_to_CRLF with extension ",start));
//result
}
}.
public define ResultCopy
copy_file_and_convert_to_CRLF
(
String file,
ByteArray -> ByteArray converter,
Bool keep_org
)=
//remove the temporary file if exists
forget(remove(file+".tmp"));
if rename(file, file+".tmp") then
with result = copy_file_and_convert_to_CRLF(file+".tmp", file, converter),
(if keep_org then
unique
else
forget(remove(file+".tmp")));
result
else
cant_create_file
.
public define ResultCopy
copy_file_and_convert_to_CRLF
(
String file,
ByteArray -> ByteArray converter
)=
copy_file_and_convert_to_CRLF(file, converter, false)
.