/*

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

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

NATIVE: "using System.IO;" .
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

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

        FUN do_Load|_strings_LOCAL(
        STR Path|_load_from, 
        STR Lines|_to_load[], {encoding} Encoding) ==> BOOL, NATIVE :
           @"    if (X_Path == " #QU #QU ") return false;"
            "    try {"
            "        if (!File.Exists(X_Path)) return false;"
            "        StreamReader sr = null;"
            "        Encoding e = null;"
            "        switch (X_Encoding) {"
            "               case 0: e = Encoding.Default; break;"
            "               case 1: e = Encoding.ASCII; break;"
            "               case 2: e = Encoding.UTF8; break;"
            "               case 3: e = Encoding.Unicode; break;"
            "               case 4: e = Encoding.BigEndianUnicode; break;"
            "               case 5: e = Encoding.UTF7; break;"
            "               case 6: e = Encoding.UTF32; break;"
            "        }"
            "        if (e == null) sr = new StreamReader(X_Path);"
            "        else sr = new StreamReader(X_Path, e);"
            "        try"
            "        {"
            "            while (!sr.EndOfStream)"
            "            {"
            "                string s = sr.ReadLine();"
            "                X_Lines.Add(s);"
            "            }"
            "            RESULT = true;"
            "        }"
            "        finally { sr.Close(); }"
            "    }"
            "    catch {"
            "        RESULT = false;"
            "    }".
        */
        public bool Load_strings(List<string> Lines)
        {
            if (Path == "") return false;
            if (!File.Exists(Path)) return false;
            StreamReader sr = new StreamReader(Path);
            try
            {
                while (!sr.EndOfStream)
                {
                    string s = sr.ReadLine();
                    Lines.Add(s);
                }
                return true;
            }
            finally { sr.Close(); }
        }

        /*
        
        METHOD Save|_strings(STR Lines|_to_save[]) ==> BOOL :
                 Stop_if_test(Path)
                 RESULT = do_Save(Path, Lines[], Encoding, 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 Unicode_save( STR Path|_file, STR Lines|_array[] ) :
            temp|_saver = {Text_file}(Path = Path, Encoding= 'UTF8')
            NONE = temp.Save(Lines[]) .
            
        FUN Unix_text_save( STR Path|_file, STR Lines|_array[] ) :
            temp|_saver = {Text_file}(Path = Path)
            temp.Line_end = #10
            NONE = temp.Save(Lines[]) .
            
        STR Line_end|ing_sequence = #13#10

        FUN do_Save|_strings_LOCAL(
        STR Path|_file_save_to, STR Lines|_to_save[],
        {encoding} Encoding, STR Line_end) ==> BOOL, NATIVE :
			STR dir|ectory = Path.Extract_path
			CASE dir != "" && !dir.Dir_exists ?
			     dir.Create_dir ;
			CASE Path.Exists ?
			     Path.Delete_file ;
			NATIVE @
            "  try {"
            "      string dir = System.IO.Path.GetDirectoryName(X_Path);"
            ''      if (dir != "" && !Directory.Exists(dir))''
            "         Directory.CreateDirectory(dir);"
            ''      if (dir != "" && !Directory.Exists(dir)) return false;''
            "      Encoding e = null;"
            "      switch (X_Encoding) {"
            "             case 0: e = Encoding.Default; break;"
            "             case 1: e = Encoding.ASCII; break;"
            "             case 2: e = new UTF8Encoding(false); break;"
            " 					  // e = Encoding.UTF8; break;"
            "             case 3: e = Encoding.Unicode; break;"
            "             case 4: e = Encoding.BigEndianUnicode; break;"
            "             case 5: e = Encoding.UTF7; break;"
            "             case 6: e = Encoding.UTF32; break;"
            "      }"
            "      StreamWriter sw;"
            "      if (e == null) sw = new StreamWriter(X_Path, false);"
            "      else sw = new StreamWriter(X_Path, false, e);"
            "      try {"
            "          for (int jj = 0; jj < X_Lines.Count; jj++)"
            "          {"
            "              string S = X_Lines[jj];"
            "              if (X_Line_end != "#QU#QU" + (char)10)"
            "                 for (int i = S.Length - 2; i >= 0; i--)"
            "                     if (S[i] == (char)13 && S[i + 1] != (char)10)"
            "                        S = S.Substring(0, i + 1) + "
            "                           (char)10 + S.Substring(i + 1);"
            "              sw.Write(S + X_Line_end);"
            "          }"
            "          RESULT = true;"
            "      }"
            "      finally { sw.Close(); }"
            "  }"
            "  catch { RESULT = false; }" .
        */
        public bool Save_strings(List<string> X_Lines)
        {
            StreamWriter sw = null;
            try
            {
                string dir = System.IO.Path.GetDirectoryName(Path);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                if (!Directory.Exists(dir)) return false;
                sw = new StreamWriter(Path);
                foreach (string s in X_Lines)
                {
                    string S = s;
                    for (int i = S.Length - 2; i >= 0; i--)
                        if (S[i] == (char)13 && S[i + 1] != (char)10)
                            S = S.Substring(0, i + 1) + (char)10 + S.Substring(i + 1);
                    sw.WriteLine(S);
                }
                return true;
            }
            finally { sw.Close(); }
        }
        
        
    }
}
/*
END
*/

