/*

CLASS {Text_file|_Delphi_KOL},
STOPPING, NATIVE, UNTESTED //, DEPRECATED('{File_stream}')
	:

IMPORT: {System_functions}, {String_functions}, {File_stream}, {Stream}, 
		{Long_int}, {Memory_stream_contig} .
IMPORT, TEST: {File_path} . 

NATIVE: "uses Windows;" .

STR Path|_to_text_file, INIT

{String_functions}.{encoding} Encoding

FUN Text_load|_first_clear_lines(
STR Path|_file, STR Lines|_array[] )
    :
    Lines[].Clear
    temp|_loader = {Text_file}(Path = Path)
    NONE = temp.Load(Lines[]) .
    
FUN Load_text|_first_clear_lines(
STR Lines|_array[], STR Path|_from)
    :
	Text_load(Path, Lines[]) .

FUN From_text_file(STR Lines|_cleared_first[],
	STR Path|_text_file, {encoding} Encoding) 
    :
    temp|_loader = {Text_file}(Path= Path, Encoding= Encoding)
    Lines[].Clear
    NONE = temp.Load(Lines[]) .
         
FUN To_text_file(
STR Lines|_to_save[], STR Path|_where, {encoding} Encoding)
    :
    temp|saver = {Text_file}(Path= Path, Encoding= Encoding)
    NONE = temp.Save(Lines[]) .
         
    

METHOD Load|_strings(STR Lines|_to_APPEND_to[]) ==> BOOL
	://= do_Load(Path, Lines[]) .
		Lines[].Clear
	{File_stream} fs|_to_load_temp = Read_file_stream(Path)
    //CASE Encoding=='DEFAULT' ? fs.Detect_encoding
    //ELSE fs.Set_encoding(Encoding) ;
	-------------------- 'new version - fast'
	mem|ory_stream_temp = {Memory_stream_contig}
	mem.Set_capacity(fs.Size.To_int)
	mem.Copy_stream(fs)
	mem.Rewind
	//mem.Set_encoding(fs.Encoding)
	fs.Close
	fs = NONE
	-------------------- 'check for faster ways' 
	CASE Encoding=='DEFAULT' ? mem.Detect_encoding
	ELSE mem.Set_encoding(Encoding) ;     
	//STR pinfo|_of_platform = Platform_info.Lower
    CASE ?
    [mem.Encoding IN ['DEFAULT', 'ASCII']] ?
		STR source|_as_single_string = mem.Memory[0 TO *].Bytes_to_ascii
    ELSE
		-------------------- 'old version - slow'
		FOR i|_dummy IN [0 TO mem.Size.To_int] :
		    CASE mem.Ended ? BREAK i ;
			Lines[] << mem.Get_line
			//DEBUG: << "[" Lines[*] "]"#NL ;
		;
		RESULT = TRUE ==>
    ;
	Lines[].Split(source, #LF)
	FOR s IN Lines[] : Lines[s.INDEX] = s.Remove_ending(#CR) ;
	RESULT = TRUE .

   
FUN Unix_text_save|_DUMMY_actually_text_is_saved_as_Windows_text(
	STR Path|_file, STR Lines|_array[] )
	:
    temp|_saver = {Text_file}(Path = Path)
    NONE = temp.Save(Lines[]) .
    
FUN Text_save( STR Path|_file, STR Lines|_array[] ) :
    temp|_saver = {Text_file}(Path = Path)
    NONE = temp.Save(Lines[]) .
    
FUN Save_to|_file(STR Lines|_array[], STR Path|_file) :
	Text_save(Path, Lines[]) .

METHOD Save|_strings_as_ANSI_or_UTF8_only(
    STR Lines|_to_save[]) ==> BOOL
    :
    Stop_if_test(Path)
    RESULT = do_Save(Path, Lines[], Encoding == 'UTF8') .
FUN do_Save|_strings(STR Path|_of_file_save_to, STR Lines|_to_save[],
BOOL Is_UTF8|_encoding) ==> BOOL, NATIVE : @
    "var dir: String;"
    "var SL: PStrList;"
    "var i: Integer;"
    "var WS: WideString;"
    "var size: Integer;"
    "var b: PByte; b_len: Integer;"
    ""
    "    Result := FALSE;"
    "	 if (X_Path <> '') then begin"
    "        dir := ExtractFilePath(X_Path);"
    "		 if dir <> '' then begin"
    "           if not DirectoryExists(dir) then"
    "               ForceDirectories(dir);"
    "           if not DirectoryExists(dir) then exit;"
    "		 end;"     
    "	 end;"     
    "    SL := NewStrList;"
    "	 b := nil;"     
    "    try"
    "        if X_is_utf8 then"
    "        begin"
    "           GetMem(b, 4096); b_len := 4096;"
    "           for i := 0 to X_Lines.Count-1 do"
    "           begin"
    "               WS := X_Lines.GetStrings(i);"
    "               size := WideCharToMultiByte(CP_UTF8, 0,"
    "                   PWideChar(WS), Length(WS), nil, 0, nil, nil);"
    "               if size = 0 then"
    "                  SL.Add('')"
    "               else"
    "               begin"
    "                   if b_len <= size then"
    "					begin"     
    "                      ReallocMem(b, size+1);"
    "					   b_len := size+1;"     
    "					end;"     
    "                   WideCharToMultiByte(CP_UTF8, 0, PWideChar(WS), "
    "                      Length(WS), PChar(b), size, nil, nil);"
    "                   PByte(Integer(b) + size)^ := 0;"
    "                   SL.Add(PChar(b));"
    "               end"
    "           end;"
    "           //FreeMem(b); b := nil;"
    "        end"
    "        else"
    "           for i := 0 to X_Lines.Count-1 do"
    "               SL.Add(X_Lines.GetStrings(i));"
    "        SL.SaveToFile(X_Path);"
    "        Result := TRUE;"
    "    finally SL.Free; FreeMem(b);"
    "    end;" .

TEST save_load :
     t|emp_text = {Text_file}(Path = Start_dir "$TEST\test.txt")
     STR lines|_to_save[] << "test1" << "test2" << "test3"
     Delete_file(t.Path)
     ASSERT !t.Path.Exists
     ASSERT t.Save(lines[])
     ASSERT t.Path.Exists
     ASSERT t.Load(lines[])
     .
        
END
*/

