<commit_before>﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace WOptiPng
{
    public static class OptiPngWrapper
    {
        public static bool OptiPngExists()
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo("optipng")
                {
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            try
            {
                process.Start();
            }
            catch (Win32Exception)
            {
                return false;
            }
            return true;
        }

        public static int Optimize(string inputPath, string outputPath, Settings settings,
            Action<string> standardErrorCallback)
        {
            var p = new Process
            {
                StartInfo = new ProcessStartInfo("optipng")
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    Arguments = FormatArguments(outputPath, inputPath, settings.OptLevel),
                    RedirectStandardError = true,
                }
            };
            p.Start();
            p.PriorityClass = settings.ProcessPriority;
            p.ErrorDataReceived += (sender, e) =>
            {
                if (standardErrorCallback != null)
                {
                    standardErrorCallback(e.Data);
                }
            };
            p.BeginErrorReadLine();
            p.WaitForExit();
            return p.ExitCode;
        }

        private static string FormatArguments(string outputPath, string inputPath, int optLevel)
        {
            return string.Format("-clobber -preserve -out \"{0}\" -o {1} \"{2}\"", outputPath, optLevel, inputPath);
        }
    }
}<commit_msg>Fix Process class not being disposed<commit_after>﻿using System;
using System.ComponentModel;
using System.Diagnostics;

namespace WOptiPng
{
    public static class OptiPngWrapper
    {
        public static bool OptiPngExists()
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo("optipng")
                {
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            try
            {
                process.Start();
            }
            catch (Win32Exception)
            {
                return false;
            }
            return true;
        }

        public static int Optimize(string inputPath, string outputPath, Settings settings,
            Action<string> standardErrorCallback)
        {
            using (var p = new Process
            {
                StartInfo = new ProcessStartInfo("optipng")
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    Arguments = FormatArguments(outputPath, inputPath, settings.OptLevel),
                    RedirectStandardError = true,
                }
            })
            {
                p.Start();
                p.PriorityClass = settings.ProcessPriority;
                p.ErrorDataReceived += (sender, e) =>
                {
                    if (standardErrorCallback != null)
                    {
                        standardErrorCallback(e.Data);
                    }
                };
                p.BeginErrorReadLine();
                p.WaitForExit();
                return p.ExitCode;
            }
        }

        private static string FormatArguments(string outputPath, string inputPath, int optLevel)
        {
            return string.Format("-clobber -preserve -out \"{0}\" -o {1} \"{2}\"", outputPath, optLevel, inputPath);
        }
    }
}
{'fraction_non_alphanumeric': 0.078395385422129, 'fraction_numerical': 0.0026219192448872575, 'mean_word_length': 1.1762692527096406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1749849', 'n_tokens_mistral': 911, 'n_tokens_neox': 858, 'n_words': 200}