<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

using System.Diagnostics;

namespace SAMP_Ex
{
    class FavFile
    {
        public static bool Load(string file)
        {
            try
            {
                _FavFile = XDocument.Load(file);
                _filepath = file;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static void Save()
        {
            _FavFile.Save(_filepath);
        }

        public static bool AddServer(Server server, string nickname)
        {
            if (!server.IsValid)
                return false;

            var servip = _FavFile.Root.Descendants("server").Where(x => x.Element("ip").Value == (server.Ip.ToString()+":"+server.Port));
           

            if (nickname == String.Empty)
                nickname = ConfigFile.GetUserConfig("defaultnick");

            if (servip == null)
            {
                _FavFile.Element("favorites").Add(                   
                    new XElement("server",
                            new XElement("ip", server.Ip.ToString() + ":" + server.Port), 
                            new XElement("nickname", nickname),
                            new XElement("password", server.Password)));
                return true;
            }
            return false;
        }

        public static bool DeleteServer(Server server)
        {
            if (!server.IsValid)
                return false;
              
            var serv = _FavFile.Root.Descendants("server").Where(x => x.Element("ip").Value == (server.Ip.ToString() + ":" + server.Port)).First();

            if(serv != null)
            {
                serv.Remove();
                return true;
            }
            return false;
        }

        public static List<Server> GetFavoritesList()
        {
            try
            {
                List<Server> tmpList = new List<Server>();
                foreach (XElement xel in _FavFile.Root.Descendants("server"))
                {
                    try
                    {
                        tmpList.Add(new Server(xel.Element("ip").Value, xel.Element("nickname").Value));
                    }
                    catch (Exception ex) { }
                      
                }
                return tmpList;
            }
            catch (Exception ex)
            {
                return new List<Server>();
            }
        }

        #region Attributes       
        private static string _filepath;
        private static XDocument _FavFile;
        #endregion
    }<commit_msg>Fix for a non-exception safe code<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

using System.Diagnostics;

namespace SAMP_Ex
{
    class FavFile
    {
        public static bool Load(string file)
        {
            try
            {
                _FavFile = XDocument.Load(file);
                _filepath = file;
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public static void Save()
        {
            _FavFile.Save(_filepath);
        }

        public static bool AddServer(Server server, string nickname)
        {
            if (!server.IsValid)
                return false;

            var servip = _FavFile.Root.Descendants("server").Where(x => x.Element("ip").Value == (server.Ip.ToString()+":"+server.Port));
           

            if (nickname == String.Empty)
                nickname = ConfigFile.GetUserConfig("defaultnick");

            if (servip == null)
            {
                _FavFile.Element("favorites").Add(                   
                    new XElement("server",
                            new XElement("ip", server.Ip.ToString() + ":" + server.Port), 
                            new XElement("nickname", nickname),
                            new XElement("password", server.Password)));
                return true;
            }
            return false;
        }

        public static bool DeleteServer(Server server)
        {
            if (!server.IsValid)
                return false;
              
            try
            {
                var serv = _FavFile.Root.Descendants("server").Where(x => x.Element("ip").Value == (server.Ip.ToString() + ":" + server.Port)).First();
                if (serv != null)
                {
                    serv.Remove();
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }           
        }

        public static List<Server> GetFavoritesList()
        {
            try
            {
                List<Server> tmpList = new List<Server>();
                foreach (XElement xel in _FavFile.Root.Descendants("server"))
                {
                    try
                    {
                        tmpList.Add(new Server(xel.Element("ip").Value, xel.Element("nickname").Value));
                    }
                    catch (Exception ex) { }
                      
                }
                return tmpList;
            }
            catch (Exception ex)
            {
                return new List<Server>();
            }
        }

        #region Attributes       
        private static string _filepath;
        private static XDocument _FavFile;
        #endregion
    }
{'fraction_non_alphanumeric': 0.09799168975069251, 'fraction_numerical': 0.0, 'mean_word_length': 1.1285924834193073, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '223368', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1376, 'n_words': 302}