/*)
CLASS {Text_file|_CSharp_4PrimaryOnly}, UNTESTED, STOPPING, NATIVE :

IMPORT: {System_functions} ;

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;
        }

        /******************************************)
        ENUM {encoding}: 'DEFAULT|_ENCODING',
                         'ASCII|_ENCODING',
                         'UTF8|_ENCODING',
                         'UTF16|_UNICODE',
                         'UTF16_BIG|_ENDIAN',
                         'UTF7|_ENCODING',
                         'UTF32|_ENCODING' .

        {encoding} Encoding

        METHOD Load|_strings(STR Lines|_to_load[]) ==> BOOL :
        	RESULT = do_Load(Path, Lines[], Encoding) .
                 
        FUNCTION Text_load( STR Path|_file, STR Lines|_array[] ) :
            Lines[].Clear
            temp|_loader = {Text_file}(Path = Path)
            NONE = temp.Load(Lines[]) .

        FUNCTION do_Load|_strings(STR Path|_of_text_file,
        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 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);"
            "            }"
            "            return true;"
            "        }"
            "        finally { sr.Close(); }"
            "    }"
            "    catch {"
            "        return 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 = save(Path, Lines[]) .

        FUNCTION save|_strings(STR Path|_of_file,
        STR Lines|_to_save[]) ==> BOOL, NATIVE :
           @"    string dir = System.IO.Path.GetDirectoryName(X_Path);"
            "    if (!Directory.Exists(dir))"
            "        Directory.CreateDirectory(dir);"
            "    if (!Directory.Exists(dir)) return false;"
            "    try {"
            "        StreamWriter sw = new StreamWriter(X_Path, false);"
            "        try {"
            "            for (int j = 0; j < X_Lines.Count; j++)"
            "			 {"
            "            	 string S = X_Lines[j];"
            "                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(); }"
            "    }"
            "    catch { return false; }" .
        (*************************/
        public bool Save_strings(List<string> X_Lines)
        {
            string dir = System.IO.Path.GetDirectoryName(Path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            if (!Directory.Exists(dir)) return false;
            StreamWriter sw = new StreamWriter(Path);
            try
            {
                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
(*********************************/
// *)
