Exemple #1
0
        public static (IniData, bool) FileINIRead(string _URL)
        {
            bool status = FileValidate(_URL);

            if (!status)
            {
                HEVConsole.Print("FileINIRead() Missing file at " + _URL, EPrintType.eError);
                return(null, false);
            }

            FileIniDataParser parser = new FileIniDataParser();

            parser.Parser.Configuration.ThrowExceptionsOnError = true;
            IniData data = null;

            try {
                data   = parser.ReadFile(_URL);
                status = true;
            } catch {
                status = false;
            }
            if (!status)
            {
                HEVConsole.Print("FileINIRead() Parser file read.", EPrintType.eError);
                return(null, false);
            }
            return(data, true);
        }
Exemple #2
0
        public static bool FileINIWrite(string _URL, IniData _Data)
        {
            bool status = FileValidate(_URL);

            if (!status)
            {
                HEVConsole.Print("FileINIWrite() Missing file. Creating one at " + _URL, EPrintType.eWarning);
                return(false);
            }

            FileIniDataParser parser = new FileIniDataParser();

            parser.Parser.Configuration.ThrowExceptionsOnError = true;
            IniData data = _Data;

            try {
                parser.WriteFile(_URL, data, Encoding.UTF8);
                status = true;
            } catch {
                status = false;
            }
            if (!status)
            {
                HEVConsole.Print("FileINIWrite() Parser file write.", EPrintType.eError);
                return(false);
            }
            return(true);
        }
Exemple #3
0
        public static (IniData, bool) INIRead(string _String)
        {
            if (!HEVText.Validate(_String))
            {
                HEVConsole.Print("INIRead() Empty string.", EPrintType.eError);
                return(null, false);
            }
            bool status = false;
            FileIniDataParser parser = new FileIniDataParser();

            parser.Parser.Configuration.ThrowExceptionsOnError = true;
            IniData data = null;

            try {
                data   = parser.Parser.Parse(_String);
                status = true;
            } catch {
                status = false;
            }
            if (!status)
            {
                HEVConsole.Print("INIRead() Parser to data.", EPrintType.eError);
                return(null, false);
            }
            return(data, true);
        }
Exemple #4
0
        public static (string[], bool) GetStringArrayLines(string[] _String, int _StartLine = 0, int _EndLine = -1)
        {
            string[] lines    = _String;
            int      lastLine = 0;

            if (lines.Length < 1)
            {
                HEVConsole.Print("GetStringArrayLines() Empty string array.", EPrintType.eError);
                return(lines, false);
            }
            lastLine = lines.Length - 1;
            if (_StartLine == 0 && _EndLine == -1)
            {
            }
            else if (_StartLine == -1)
            {
                lines = new string[] { lines[lastLine] };
            }
            else
            {
                int startLine = HEVMath.Max(_StartLine, 0);
                startLine = HEVMath.Min(startLine, lastLine);
                int endLine = HEVMath.Min(_EndLine, lastLine);
                endLine = HEVMath.Max(_StartLine, _EndLine);
                List <string> linesList = new List <string>();
                for (int i = startLine; i < endLine; i++)
                {
                    linesList.Add(lines[i]);
                }
                lines = linesList.ToArray();
            }
            return(lines, true);
        }
Exemple #5
0
        public static bool JSONParseClassTry <T>(this string _String, out T _Result)
        {
            bool status = true;
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                Error = (sender, args) => { status = false; args.ErrorContext.Handled = true; },
                MissingMemberHandling = MissingMemberHandling.Error
            };

            _Result = JsonConvert.DeserializeObject <T>(_String, settings);
            if (!status)
            {
                HEVConsole.Print("JSONParseClassTry().", EPrintType.eError);
            }

            return(status);
        }
Exemple #6
0
        public static (List <T>, bool) JSONReadClassList <T>(string _String) where T : class, new()
        {
            if (!typeof(T).IsClass)
            {
                throw new ArgumentException("Error - JSONReadClassList() must have a valid class.");
            }
            List <T> localClass = new List <T>();
            string   fileText   = _String;
            bool     status     = false;

            if (!HEVText.Validate(_String))
            {
                HEVConsole.Print("JSONReadClassList() Empty string.", EPrintType.eWarning);
                return(localClass, false);
            }
            status = fileText.JSONParseClassTry <List <T> >(out localClass);
            return(localClass, status);
        }
Exemple #7
0
        public static (List <T>, bool) FileJSONReadClassList <T>(string _URL) where T : class, new()
        {
            if (!typeof(T).IsClass)
            {
                throw new ArgumentException("Error - FileJSONReadClassList() " + _URL +
                                            " must have a valid class.");
            }
            List <T> localClass = new List <T>();
            string   fileText   = "";
            bool     status     = false;

            (fileText, status) = FileTextReadString(_URL);
            if (!status)
            {
                HEVConsole.Print("FileJSONReadClassList() Missing file at " + _URL, EPrintType.eError);
                return(localClass, false);
            }

            (localClass, status) = JSONReadClassList <T>(fileText);
            return(localClass, status);
        }
Exemple #8
0
 public static bool FileValidate(string _URL, bool _Critical = true)
 {
     if (!System.IO.File.Exists(_URL))
     {
         EPrintType val;
         if (_Critical)
         {
             val = EPrintType.eError;
         }
         else
         {
             val = EPrintType.eWarning;
         }
         HEVConsole.Print("FileValidate() Missing file: " + _URL, val, true);
         return(false);
     }
     else
     {
         return(true);
     }
 }
Exemple #9
0
        public static (string, bool) INIWrite(IniData _Data)
        {
            bool   status = false;
            string data   = "";

            if (_Data != null)
            {
                HEVConsole.Print("INIWrite() Empty data.", EPrintType.eError);
                return(null, false);
            }

            try {
                data   = _Data.ToString();
                status = true;
            } catch {
                status = false;
            }
            if (!status)
            {
                HEVConsole.Print("INIWrite() Parser to string.", EPrintType.eError);
                return(null, false);
            }
            return(data, true);
        }
Exemple #10
0
        public static (string[], bool) FileTextReadStringArray(string _URL, int _StartLine = 0, int _EndLine = -1)
        {
            string[] lines  = null;
            bool     status = false;

            if (!FileValidate(_URL))
            {
                return(lines, false);
            }
            if (!HEVText.Validate(System.IO.File.ReadAllText(_URL)))
            {
                HEVConsole.Print("FileTextRead() Invalid URL " + _URL, EPrintType.eWarning);
                lines = new string[] { "Empty" };
                return(lines, false);
            }
            lines = System.IO.File.ReadAllLines(_URL);

            (lines, status) = HEVText.GetStringArrayLines(lines, _StartLine, _EndLine);
            if (!status)
            {
                return(lines, false);
            }
            return(lines, true);
        }
Exemple #11
0
        public static bool FileTextWriteStringArray(string _URL, string[] _Text, bool _Replace = true,
                                                    int _LineIndex = -1)
        {
            bool file = true;

            if (!FileValidate(_URL, false))
            {
                HEVConsole.Print("FileTextWriteStringArray() Missing file. Creating new one at " +
                                 _URL, EPrintType.eWarning, true);
                file = false;
            }
            bool status = false;

            string[] linesPre   = null;
            string[] linesPost  = null;
            string[] linesFinal = null;

            if (file)
            {
                (linesPre, status) = FileTextReadStringArray(_URL);
                if (!status)
                {
                    return(false);
                }
                (linesPost, status) = HEVText.GetStringArrayLines(linesPre, _LineIndex, -1);
                (linesPre, status)  = HEVText.GetStringArrayLines(linesPre, 0, _LineIndex);
            }

            if (status == false)
            {
                linesFinal = _Text;
            }
            else if (_LineIndex == 0)
            {
                if (_Replace)
                {
                    linesFinal = _Text;
                }
                else
                {
                    linesFinal = _Text.Concat(linesPost).ToArray();
                }
            }
            else if (_LineIndex == -1)
            {
                if (_Replace)
                {
                    linesFinal = linesPre.Concat(_Text).ToArray();
                }
                else
                {
                    linesFinal = linesPre.Concat(_Text).Concat(linesPost).ToArray();
                }
            }
            else
            {
                if (_Replace)
                {
                    linesFinal = linesPre.Concat(_Text).ToArray();
                }
                else
                {
                    linesFinal = linesPre.Concat(_Text).Concat(linesPost).ToArray();
                }
            }

            try {
                if (!file)
                {
                    string dir = System.IO.Path.GetDirectoryName(_URL);
                    HEVConsole.Print(dir, EPrintType.eInfo);
                    if (!System.IO.Directory.Exists(dir))
                    {
                        DirectoryInfo di = System.IO.Directory.CreateDirectory(dir);
                        if (!di.Exists)
                        {
                            HEVConsole.Print("Administrator privileges are need in order to create the files.",
                                             EPrintType.eError);
                            return(false);
                        }
                    }
                    //System.IO.File.CreateText( _URL );
                }
                System.IO.File.WriteAllLines(_URL, linesFinal, System.Text.Encoding.UTF8);
                status = true;
            } catch {
                status = false;
            }
            if (!status)
            {
                HEVConsole.Print("FileTextWriteStringArray() At write." + _URL, EPrintType.eError);
                return(false);
            }
            else
            {
                return(true);
            }
        }