<commit_before>using System;
using System.Threading;
using System.Windows.Forms;
using EveOPreview.Configuration;
using EveOPreview.UI;

namespace EveOPreview
{
	static class Program
	{
		private static string MutexName = "EVE-O Preview Single Instance Mutex";
		private static string ConfigParameterName = "--config:";

		/// <summary>The main entry point for the application.</summary>
		[STAThread]
		static void Main(string[] args)
		{
			// The very usual Mutex-based single-instance screening
			// 'token' variable is used to store reference to the instance Mutex
			// during the app lifetime
			object token = Program.GetInstanceToken();

			// If it was not possible to aquite the app token then another app instance is already running
			// Nothing to do here
			if (token == null)
			{
				return;
			}

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			IIocContainer container = new LightInjectContainer();

			// UI classes
			IApplicationController controller = new ApplicationController(container)
				.RegisterView<IMainView, MainForm>()
				.RegisterView<IThumbnailView, ThumbnailView>()
				.RegisterView<IThumbnailDescriptionView, ThumbnailDescriptionView>()
				.RegisterInstance(new ApplicationContext());

			// Application services
			controller.RegisterService<IThumbnailManager, ThumbnailManager>()
				.RegisterService<IThumbnailViewFactory, ThumbnailViewFactory>()
				.RegisterService<IThumbnailDescriptionViewFactory, ThumbnailDescriptionViewFactory>()
				.RegisterService<IConfigurationStorage, ConfigurationStorage>()
				.RegisterInstance<IAppConfig>(new AppConfig())
				.RegisterInstance<IThumbnailConfig>(new ThumbnailConfig());

			controller.Create<IAppConfig>().ConfigFileName = Program.GetCustomConfigFile(args);

			controller.Run<MainPresenter>();

			token = null;
		}

		// Parse startup parameters
		// Simple approach is used because something like NParams would be an overkill here
		private static string GetCustomConfigFile(string[] args)
		{
			string configFile = null;
			foreach (string arg in args)
			{
				if ((arg.Length <= Program.ConfigParameterName.Length) || !arg.StartsWith(Program.ConfigParameterName, StringComparison.Ordinal))
				{
					continue;
				}

				configFile = arg.Substring(Program.ConfigParameterName.Length);
				break;
			}

			if (string.IsNullOrEmpty(configFile))
			{
				return "";
			}

			// One more check to drop trailing "
			if ((configFile.Length > 3) && (configFile[0] == '"') && (configFile[configFile.Length - 1] == '"'))
			{
				configFile = configFile.Substring(1, configFile.Length - 2);
			}

			return configFile;
		}

		private static object GetInstanceToken()
		{
			// The code might look overcomplicated here for a single Mutex operation
			// Yet we had already experienced a Windows-level issue
			// where .NET finalizer theread was literally paralyzed by
			// a failed Mutex operation. That did lead to weird OutOfMemory
			// exceptions later
			try
			{
				Mutex mutex = Mutex.OpenExisting(Program.MutexName);
				// if that didn't fail then anotherinstance is already running
				return null;
			}
			catch (UnauthorizedAccessException)
			{
				return null;
			}
			catch (Exception)
			{
				bool result;
				Mutex token = new Mutex(true, Program.MutexName, out result);
				return result ? token : null;
			}
		}
	}
}<commit_msg>Refactor the program startup code<commit_after>using System;
using System.Threading;
using System.Windows.Forms;
using EveOPreview.Configuration;
using EveOPreview.UI;

namespace EveOPreview
{
	static class Program
	{
		private static string MutexName = "EVE-O Preview Single Instance Mutex";
		private static string ConfigParameterName = "--config:";

		/// <summary>The main entry point for the application.</summary>
		[STAThread]
		static void Main(string[] args)
		{
			// The very usual Mutex-based single-instance screening
			// 'token' variable is used to store reference to the instance Mutex
			// during the app lifetime
			object token = Program.GetInstanceToken();

			// If it was not possible to aquite the app token then another app instance is already running
			// Nothing to do here
			if (token == null)
			{
				return;
			}

			Program.InitializeWinFormsGui();

			IApplicationController controller = Program.InitializeApplicationController();

			Program.SetupApplicationConttroller(controller, Program.GetCustomConfigFile(args));

			controller.Run<MainPresenter>();

			token = null;
		}

		private static string GetCustomConfigFile(string[] args)
		{
			// Parse startup parameters
			// Simple approach is used because something like NParams would be an overkill here
			string configFile = null;
			foreach (string arg in args)
			{
				if ((arg.Length <= Program.ConfigParameterName.Length) || !arg.StartsWith(Program.ConfigParameterName, StringComparison.Ordinal))
				{
					continue;
				}

				configFile = arg.Substring(Program.ConfigParameterName.Length);
				break;
			}

			if (string.IsNullOrEmpty(configFile))
			{
				return "";
			}

			// One more check to drop trailing "
			if ((configFile.Length > 3) && (configFile[0] == '"') && (configFile[configFile.Length - 1] == '"'))
			{
				configFile = configFile.Substring(1, configFile.Length - 2);
			}

			return configFile;
		}

		private static object GetInstanceToken()
		{
			// The code might look overcomplicated here for a single Mutex operation
			// Yet we had already experienced a Windows-level issue
			// where .NET finalizer theread was literally paralyzed by
			// a failed Mutex operation. That did lead to weird OutOfMemory
			// exceptions later
			try
			{
				Mutex mutex = Mutex.OpenExisting(Program.MutexName);
				// if that didn't fail then anotherinstance is already running
				return null;
			}
			catch (UnauthorizedAccessException)
			{
				return null;
			}
			catch (Exception)
			{
				bool result;
				Mutex token = new Mutex(true, Program.MutexName, out result);
				return result ? token : null;
			}
		}

		private static void InitializeWinFormsGui()
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
		}

		private static IApplicationController InitializeApplicationController()
		{
			IIocContainer container = new LightInjectContainer();

			// UI classes
			IApplicationController controller = new ApplicationController(container)
				.RegisterView<IMainView, MainForm>()
				.RegisterView<IThumbnailView, ThumbnailView>()
				.RegisterView<IThumbnailDescriptionView, ThumbnailDescriptionView>()
				.RegisterInstance(new ApplicationContext());

			// Application services
			controller.RegisterService<IThumbnailManager, ThumbnailManager>()
				.RegisterService<IThumbnailViewFactory, ThumbnailViewFactory>()
				.RegisterService<IThumbnailDescriptionViewFactory, ThumbnailDescriptionViewFactory>()
				.RegisterService<IConfigurationStorage, ConfigurationStorage>()
				.RegisterInstance<IAppConfig>(new AppConfig())
				.RegisterInstance<IThumbnailConfig>(new ThumbnailConfig());

			return controller;
		}

		private static void SetupApplicationConttroller(IApplicationController controller, string configFile)
		{
			controller.Create<IAppConfig>().ConfigFileName = configFile;
		}
	}
}
{'fraction_non_alphanumeric': 0.09221029452243325, 'fraction_numerical': 0.0013762730525736307, 'mean_word_length': 3.8903095558546434, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '4405410', 'n_tokens_mistral': 2362, 'n_tokens_neox': 1957, 'n_words': 557}