Commit f60d92b8d3dff55dbcbd8a53e219e1482f513b66
1 parent
2ac800d6
add to_CRLF_String call in json adl
Showing
2 changed files
with
148 additions
and
0 deletions
Show diff stats
extensions/json.anubis
| @@ -57,3 +57,38 @@ public define String | @@ -57,3 +57,38 @@ public define String | ||
| 57 | ok(ba) then to_string(ba) | 57 | ok(ba) then to_string(ba) |
| 58 | }. | 58 | }. |
| 59 | 59 | ||
| 60 | +public define String | ||
| 61 | + to_CRLF_String | ||
| 62 | + ( | ||
| 63 | + Library json_lib, | ||
| 64 | + String unchecked_string | ||
| 65 | + )= | ||
| 66 | + if unchecked_string = "" then | ||
| 67 | + "" | ||
| 68 | + else | ||
| 69 | + if extension_library_call(json_lib, "to_CRLF_String", serialize(unchecked_string), length(unchecked_string)+300) is | ||
| 70 | + { | ||
| 71 | + error(err) then | ||
| 72 | + println("Can't call to_CRLF_String"); | ||
| 73 | + println(" --> " + lib_error_to_string(err)); | ||
| 74 | + "\"\"", | ||
| 75 | + ok(ba) then to_string(ba) | ||
| 76 | + }. | ||
| 77 | + | ||
| 78 | +public define ByteArray | ||
| 79 | + to_CRLF_ByteArray | ||
| 80 | + ( | ||
| 81 | + Library json_lib, | ||
| 82 | + String unchecked_string | ||
| 83 | + )= | ||
| 84 | + if unchecked_string = "" then | ||
| 85 | + to_byte_array("") | ||
| 86 | + else | ||
| 87 | + if extension_library_call(json_lib, "to_CRLF_String", serialize(unchecked_string), length(unchecked_string)+300) is | ||
| 88 | + { | ||
| 89 | + error(err) then | ||
| 90 | + println("Can't call to_CRLF_String"); | ||
| 91 | + println(" --> " + lib_error_to_string(err)); | ||
| 92 | + to_byte_array(unchecked_string), | ||
| 93 | + ok(ba) then ba | ||
| 94 | + }. |
| 1 | +/* | ||
| 2 | + * Created by PyramIDE. | ||
| 3 | + * User: フランスのトトロ aka (David RENÉ) | ||
| 4 | + * Date: 25/06/2016 | ||
| 5 | + * Time: 18:14 | ||
| 6 | + * © Calexium | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +read tools/basis.anubis | ||
| 10 | +transmit system/files.anubis | ||
| 11 | +transmit calexium_lib/extensions/json.anubis | ||
| 12 | + | ||
| 13 | +public define String | ||
| 14 | +/** normalize_to_CRLF will search for alone CR or LF and transform it to CRLF | ||
| 15 | + * output is the input string normalized | ||
| 16 | + */ | ||
| 17 | + to_CRLF_String | ||
| 18 | + ( | ||
| 19 | + String unchecked_string | ||
| 20 | + )= | ||
| 21 | + //try to use adl version of json escape string | ||
| 22 | + if load_library("json") is | ||
| 23 | + { | ||
| 24 | + error(err) then | ||
| 25 | + println("load lib error:"); | ||
| 26 | + println("--> can't load extension lib. Hence return input string"); | ||
| 27 | + unchecked_string | ||
| 28 | + ok(json_lib) then | ||
| 29 | + to_CRLF_String(json_lib, unchecked_string) | ||
| 30 | + }. | ||
| 31 | + | ||
| 32 | +public define Maybe(ByteArray -> ByteArray) | ||
| 33 | +/** normalize_to_CRLF will search for alone CR or LF and transform it to CRLF | ||
| 34 | + * output is the input string normalized | ||
| 35 | + */ | ||
| 36 | + get_CRLF_String_converter | ||
| 37 | + = | ||
| 38 | + //try to use adl version of json escape string | ||
| 39 | + if load_library("json") is | ||
| 40 | + { | ||
| 41 | + error(err) then | ||
| 42 | + println("load lib error:"); | ||
| 43 | + println("--> can't load extension lib. Hence return input string"); | ||
| 44 | + failure | ||
| 45 | + ok(json_lib) then | ||
| 46 | + success(((ByteArray) ba_input) |-> to_CRLF_ByteArray(json_lib, to_string(ba_input))) | ||
| 47 | + }. | ||
| 48 | + | ||
| 49 | +define ResultCopy | ||
| 50 | + _copy_file_and_convert | ||
| 51 | + ( | ||
| 52 | + RStream source, | ||
| 53 | + WStream target, | ||
| 54 | + ByteArray -> ByteArray converter, | ||
| 55 | + Int so_far | ||
| 56 | + ) = | ||
| 57 | + | ||
| 58 | + if read(source, 65536, 10) is | ||
| 59 | + { | ||
| 60 | + error then copy_error, | ||
| 61 | + timeout then _copy_file_and_convert(source, target, converter, so_far), | ||
| 62 | + ok(buffer)then | ||
| 63 | + with converted = converter(buffer), | ||
| 64 | + with len = length(converted), | ||
| 65 | + if len = 0 then | ||
| 66 | + copy_ok(so_far) | ||
| 67 | + else | ||
| 68 | + if flush(converted, target) is | ||
| 69 | + { | ||
| 70 | + failure then println("=> flush error");copy_error, | ||
| 71 | + success(_) then _copy_file_and_convert(source, target, converter, so_far + len) | ||
| 72 | + } | ||
| 73 | + }. | ||
| 74 | + | ||
| 75 | + | ||
| 76 | +public define ResultCopy | ||
| 77 | + copy_file_and_convert_to_CRLF | ||
| 78 | + ( | ||
| 79 | + String source_file, | ||
| 80 | + String target_file, | ||
| 81 | + ByteArray -> ByteArray converter | ||
| 82 | + ) = | ||
| 83 | + //open the source file | ||
| 84 | + if (Maybe(RStream))file(source_file, read) is | ||
| 85 | + { | ||
| 86 | + failure then cant_read_file, | ||
| 87 | + success(source) then | ||
| 88 | + //open the target file | ||
| 89 | + if (Maybe(RWStream))file(target_file, new) is | ||
| 90 | + { | ||
| 91 | + failure then println("can't create target file "+target_file);cant_create_file, //nothing to write | ||
| 92 | + success(target) then _copy_file_and_convert(source, weaken(target), converter, 0), | ||
| 93 | + //with start = unow, | ||
| 94 | + //result = _copy_file_and_convert(source, weaken(target), converter, 0), | ||
| 95 | + //println(show_duration_string("copy_file_data_and_convert_to_CRLF with extension ",start)); | ||
| 96 | + //result | ||
| 97 | + } | ||
| 98 | + }. | ||
| 99 | + | ||
| 100 | +public define ResultCopy | ||
| 101 | + copy_file_and_convert_to_CRLF | ||
| 102 | + ( | ||
| 103 | + String file, | ||
| 104 | + ByteArray -> ByteArray converter | ||
| 105 | + )= | ||
| 106 | + if rename(file, file+".tmp") then | ||
| 107 | + with result = copy_file_and_convert_to_CRLF(file+".tmp", file, converter), | ||
| 108 | + forget(remove(file+".tmp")); | ||
| 109 | + result | ||
| 110 | + else | ||
| 111 | + cant_create_file. | ||
| 112 | + | ||
| 113 | + |