/*

CLASS {Text_file|_Java}, STOPPING, NATIVE, UNTESTED :

IMPORT: {System_functions}, {File_path}, {String_functions} ;

*/


    public class Text_file
    {                                                               

        /*
        STR Path|_to_text_file, INIT
        */
        public string Path;

        public Text_file(string path) {
            Path = path;
        }

        /*
        {String_functions}.{encoding} Encoding

        METHOD Load|_strings_appending_lines(
        STR Lines|_to_load[]) ==> BOOL
            :
                 RESULT = do_Load(Path, Lines[], Encoding.encoding_as_str) .
                 
		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[]) .
		         
        FUN Text_load|_first_clear(
        STR Path|_file, STR Lines|_array[] )
            :
            Lines[].Clear
            temp|_loader = {Text_file}(Path = Path)
            NONE = temp.Load(Lines[]) .
            
        FUN Load_text|_first_clear(
        STR Lines|_array[], STR Path|_from)
            :
			Text_load(Path, Lines[]) .
            

        FUNCTION encoding_as_str|ing({encoding} E|ncoding) ==> STR :
                 CASE E ?
                 ['DEFAULT']: NONE
                 ['ASCII']: RESULT = "ASCII"
                 ['UTF8']: RESULT = "UTF-8"
                 ['UTF16']: RESULT = "UTF-16"
                 ['UTF16_BIG']: RESULT = "UTF-16BE"
                 ['UTF7']: RESULT = "UTF-7"
                 ['UTF32']: RESULT = "UTF-32" 
                 ['UTF32_BIG']: RESULT = "UTF-32BE" ; .

        FUN do_Load|_strings(STR Path|_to_file, STR Lines|_to_load[],
                 STR E|ncoding) ==> BOOL, NATIVE :
           @"    if (X_Path == " #QU #QU ") return false;"
            "    try {"
            "        if (!new java.io.File(X_Path).isFile()) return false;"
            "        java.io.BufferedReader br = null;"
            "        java.io.FileInputStream fis = new java.io.FileInputStream("
            "            X_Path);"
            "        if (!X_E.equals(" #QU #QU "))"
            "           br = new java.io.BufferedReader("
            "               new java.io.InputStreamReader("
            "               fis, X_E));"
            "        else"
            "           br = new java.io.BufferedReader("
            "               new java.io.InputStreamReader("
            "               fis));"
            "        try {"
            "            String line = br.readLine();"
            "            java.io.Console con = System.console();"
            "            if (line.length() >= 1"
            "               && (byte)line.charAt(0) == (byte)0xFF) {"
            "               line = line.substring(1);"
            //"               con.printf(" #QU "\r\n" #QU " + line + "
            //"                   " #QU "\r\n" #QU ");"
            "            }"
            "            while (line != null) {"
            "                X_Lines.Add(line);"
            "                line = br.readLine();"
            "            }"
            "            RESULT = true;"
            "        }"
            "        finally {"
            "            br.close();"
            "        }"
            "    }"
            "    catch (Exception E_io) {"
            "        RESULT = false;"
            "    }" .
        
        METHOD Save|_strings(STR Lines|_to_save[]) ==> BOOL :
                 Stop_if_test(Path)
                 STR dir|ectory_path = Path.Extract_path
                 CASE !dir.Directory_exists ?
                      dir.Create_dir ;
                      
                 RESULT = save(Path, Lines[], Encoding.encoding_as_str,
                 	Line_end= Line_end) .
                 
        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[]) .
            
        FUN save|_strings(STR Path|_to_save, STR Lines|_to_save[],
                 STR E|ncoding, STR Line_end) ==> BOOL, NATIVE :
			NATIVE @
            "    String nl = X_Line_end;"
            ''	 if (nl == "")''
            "		nl = System.getProperty(" #QU "line.separator" #QU ");"
            "    try {"
            "        java.io.BufferedWriter bw = null;"
            "        if (!X_E.equals(" #QU #QU "))"
            "           bw = new java.io.BufferedWriter("
            "               new java.io.OutputStreamWriter("
            "               new java.io.FileOutputStream(X_Path, false), X_E));"
            "        else"
            "           bw = new java.io.BufferedWriter("
            "               new java.io.OutputStreamWriter("
            "               new java.io.FileOutputStream(X_Path, false)));"
            "        try {"
            "            for (int i = 0; i < X_Lines.Count(); i++)"
            "                bw.append(X_Lines.Get_item(i)).append(nl);"
            "            RESULT = true;"
            "        }"
            "        finally {"
            "            bw.close();"
            "        }"
            "    }"
            "    catch (Exception E_io) {"
            "        RESULT = false;"
            "    }" .
            
        FUN Unix_text_save( STR Path|_file, STR Lines|_array[] ) :
            temp|_saver = {Text_file}(Path = Path)
            PUSH temp.Line_end = #10 :
            	 NONE = temp.Save(Lines[]) ; .
            
        STR Line_end|ing_sequence
            
            
        */

    }

/*
END
*/

