crlf.anubis 3.45 KB
/*
 * 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 xlib/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)
.