using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;    // File
using System.Text.RegularExpressions;   // Regex

namespace Post
{
    public abstract class AllFile
    {
        protected string[] LoadFile(string path)
        {
            ConstValues constValues = new ConstValues();
            string[] arrData = null;
            string homePath = Environment.GetEnvironmentVariable("POST_HOME");

            try
            {
                if (homePath == null)
                {
                    Console.WriteLine("POST_HOME 환경변수가 없습니다.");
                    return null;
                }

                bool isExist = File.Exists(path);
                if (!isExist)
                {
                    Console.WriteLine("file not found");
                    return null;
                }

                StreamReader reader = new StreamReader(path);
                string rowData = reader.ReadToEnd();
                arrData = Regex.Split(rowData, "\r\n");

                for (int i = 0; i < arrData.Length; i++)
                {
                    arrData[i] = arrData[i].Replace('"', ' ').Replace(';', ' ');
                    Console.WriteLine(">> " + arrData[i]);
                    string[] arr = Regex.Split(arrData[i], "=");
                    string[] arrTemp = new string[2];

                    if (arr.Length > 1)
                    {
                        arrTemp[0] = arr[0];
                        arrTemp[1] = arr[1].Trim();
                    }
                    else
                    {
                        arrTemp[0] = arr[0];
                        arrTemp[1] = "";
                    }
                }
                    reader.Close();

            } catch(Exception e)
            {
                Console.WriteLine("allfile : " + e.ToString());
                return null;
            }

            return arrData;
        }
    }
}
