From f60d92b8d3dff55dbcbd8a53e219e1482f513b66 Mon Sep 17 00:00:00 2001 From: totoro Date: Sun, 26 Jun 2016 01:57:03 +0200 Subject: [PATCH] add to_CRLF_String call in json adl --- extensions/json.anubis | 35 +++++++++++++++++++++++++++++++++++ string/crlf.anubis | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 0 deletions(-) create mode 100644 string/crlf.anubis diff --git a/extensions/json.anubis b/extensions/json.anubis index eff455b..55d5ecb 100644 --- a/extensions/json.anubis +++ b/extensions/json.anubis @@ -57,3 +57,38 @@ public define String ok(ba) then to_string(ba) }. +public define String + to_CRLF_String + ( + Library json_lib, + String unchecked_string + )= + if unchecked_string = "" then + "" + else + if extension_library_call(json_lib, "to_CRLF_String", serialize(unchecked_string), length(unchecked_string)+300) is + { + error(err) then + println("Can't call to_CRLF_String"); + println(" --> " + lib_error_to_string(err)); + "\"\"", + ok(ba) then to_string(ba) + }. + +public define ByteArray + to_CRLF_ByteArray + ( + Library json_lib, + String unchecked_string + )= + if unchecked_string = "" then + to_byte_array("") + else + if extension_library_call(json_lib, "to_CRLF_String", serialize(unchecked_string), length(unchecked_string)+300) is + { + error(err) then + println("Can't call to_CRLF_String"); + println(" --> " + lib_error_to_string(err)); + to_byte_array(unchecked_string), + ok(ba) then ba + }. diff --git a/string/crlf.anubis b/string/crlf.anubis new file mode 100644 index 0000000..503358a --- /dev/null +++ b/string/crlf.anubis @@ -0,0 +1,113 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 25/06/2016 + * Time: 18:14 + * © Calexium + */ + +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 + )= + if rename(file, file+".tmp") then + with result = copy_file_and_convert_to_CRLF(file+".tmp", file, converter), + forget(remove(file+".tmp")); + result + else + cant_create_file. + + -- libgit2 0.21.4