Exemplo n.º 1
0
        private void ProcessText()
        {
            try
            {
                var binCompiler = new BinCompiler(txtInput.Text);
                var result      = binCompiler.Compile();
                var s           = new StringBuilder();

                for (var i = 0; i < result.Length; i++)
                {
                    s.Append(result[i].ToString("X2"));
                    s.Append(i >= result.Length - 1 || i % 8 == 7 ? Environment.NewLine : " ");
                }

                _lastResult = s.ToString();
            }
            catch (Exception e)
            {
                _lastResult = $"Failed to compile string!{Environment.NewLine}{e.Message}";
            }
        }
Exemplo n.º 2
0
        private static int Main(string[] args)
        {
            var argumentParser = new ArgumentParser(args);

            if (!argumentParser.HasArguments || argumentParser.HasParameter("-nohide"))
            {
                var handle = GetConsoleWindow();
                ShowWindow(handle, 0);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainWindow());

                ShowWindow(handle, 5);
                return(0);
            }

            var sourceFilename = argumentParser.GetArgumentParameter("-source");
            var targetFilename = argumentParser.GetArgumentParameter("-target");
            var prompt         = argumentParser.GetArgument("-prompt");
            var help           = argumentParser.GetArgument("/?", "-?", "?", "help", "-help", "/help");

            if (!string.IsNullOrWhiteSpace(help))
            {
                Console.WriteLine(Arguments);
                return(0);
            }
            if (!string.IsNullOrWhiteSpace(prompt))
            {
                if (argumentParser.HasParameter("-source") || argumentParser.HasParameter("-target"))
                {
                    Console.WriteLine(@"Conflicting arguments.");
                    return(1);
                }

                do
                {
                    Console.Write(@"Text to bin: ");
                    var s = (Console.ReadLine() ?? "").Trim();
                    if (string.IsNullOrWhiteSpace(s))
                    {
                        return(0);
                    }
                    try
                    {
                        var comp = new BinCompiler(s);
                        var b    = comp.Compile();
                        for (var i = 0; i < b.Length; i++)
                        {
                            Console.Write(b[i].ToString("X2"));
                            Console.Write(i >= b.Length - 1 ? "\n" : " ");
                        }
                        Console.WriteLine(@"Ok.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($@"Failed. {e.Message}");
                    }
                } while (true);
            }
            if (string.IsNullOrWhiteSpace(sourceFilename))
            {
                Console.WriteLine(@"Missing argument: -source ""Filename""");
                return(1);
            }
            FileInfo sourceInfo;

            try
            {
                sourceInfo = new FileInfo(sourceFilename);
                if (!sourceInfo.Exists)
                {
                    Console.WriteLine(@"Source file does not exist.");
                    return(2);
                }
            }
            catch
            {
                Console.WriteLine($@"Invalid source file: {sourceFilename}");
                return(3);
            }
            if (string.IsNullOrWhiteSpace(targetFilename))
            {
                Console.WriteLine(@"Missing argument: -target ""Filename""");
                return(4);
            }
            FileInfo targetInfo;

            try
            {
                targetInfo = new FileInfo(targetFilename);
            }
            catch
            {
                Console.WriteLine($@"Invalid target file: {targetFilename}");
                return(7);
            }

            if (argumentParser.HasParameter("-totext"))
            {
                byte[] source;
                try
                {
                    source = File.ReadAllBytes(sourceFilename);
                }
                catch
                {
                    Console.WriteLine($@"Failed to load: {sourceInfo.FullName}");
                    return(8);
                }
                var    textGenerator = new TextGenerator(source);
                string result;
                try
                {
                    result = textGenerator.ToString();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(8);
                }
                try
                {
                    if (targetInfo.Exists)
                    {
                        targetInfo.Delete();
                    }
                }
                catch
                {
                    Console.WriteLine($@"Failed to delete existing target file: {targetInfo.FullName}");
                    return(9);
                }
                try
                {
                    using (var sw = new StreamWriter(targetFilename, false, Encoding.UTF8))
                    {
                        sw.Write(result);
                        sw.Flush();
                        sw.Close();
                    }
                }
                catch
                {
                    Console.WriteLine($@"Failed to write file: {targetInfo.FullName}");
                    return(10);
                }
            }
            else
            {
                string source;
                try
                {
                    using (var sw = new StreamReader(sourceInfo.FullName))
                    {
                        source = sw.ReadToEnd();
                        sw.Close();
                    }
                }
                catch
                {
                    Console.WriteLine($@"Failed to load: {sourceInfo.FullName}");
                    return(8);
                }
                var    compiler = new BinCompiler(source);
                byte[] bytes;
                try
                {
                    bytes = compiler.Compile();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    return(8);
                }
                try
                {
                    if (targetInfo.Exists)
                    {
                        targetInfo.Delete();
                    }
                }
                catch
                {
                    Console.WriteLine($@"Failed to delete existing target file: {targetInfo.FullName}");
                    return(9);
                }
                try
                {
                    using (var bw = new BinaryWriter(targetInfo.OpenWrite()))
                    {
                        bw.Write(bytes);
                        bw.Flush();
                        bw.Close();
                    }
                }
                catch
                {
                    Console.WriteLine($@"Failed to write file: {targetInfo.FullName}");
                    return(10);
                }
            }

            Console.WriteLine(@"Ok.");
            return(0);
        }