2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
7 using System.Threading;
19 protected internal string exe_path;
20 protected internal string load_from_STDIN_option;
22 public DesktopService(
string exe_path) => this.exe_path = exe_path;
24 public virtual string ExePath {
get => exe_path;
set => this.exe_path = value; }
26 protected internal abstract Output GetOutput(
string output,
string error);
28 public void StartAsync(ICallback callback, IList<InputProgram> programs, IList<OptionDescriptor> options)
32 callback.Callback(StartSync(programs, options));
36 public virtual Output StartSync(IList<InputProgram> programs, IList<OptionDescriptor> options)
39 foreach (OptionDescriptor o
in options)
44 option += o.Separator;
47 Console.Error.WriteLine(
"Warning : wrong " + typeof(OptionDescriptor).FullName);
50 string files_paths =
"";
51 string final_program =
"";
53 foreach (InputProgram p
in programs)
57 final_program += p.Programs;
58 foreach (String program_file
in p.FilesPaths)
60 FileAttributes f = File.GetAttributes(@program_file);
61 if (File.Exists(program_file) && !f.HasFlag(FileAttributes.Directory))
63 files_paths += program_file;
67 Console.Error.WriteLine(
"Warning : the file " + Path.GetFullPath(@program_file) +
" does not exists.");
71 Console.Error.WriteLine(
"Warning : wrong " + typeof(InputProgram).FullName);
74 string solverOutput =
"EMPTY_OUTPUT";
75 string solverError =
"EMPTY_ERROR";
79 var watch = System.Diagnostics.Stopwatch.StartNew();
81 StringBuilder stringBuffer =
new StringBuilder();
82 StringBuilder options_string =
new StringBuilder();
85 return new Output(
"",
"Error: executable not found");
88 Process solver_process =
new Process();
89 stringBuffer.Append(exe_path).Append(
" ").Append(option).Append(
" ").Append(files_paths);
90 solver_process.StartInfo.FileName = exe_path;
91 options_string.Append(option).Append(
" ").Append(files_paths);
93 if (final_program.Length > 0)
95 options_string.Append(this.load_from_STDIN_option);
96 stringBuffer.Append(this.load_from_STDIN_option);
99 Console.Error.WriteLine(stringBuffer.ToString());
100 solver_process.EnableRaisingEvents =
true;
101 solver_process.StartInfo.Arguments = @options_string.ToString();
102 solver_process.StartInfo.UseShellExecute =
false;
103 solver_process.StartInfo.CreateNoWindow =
true;
104 solver_process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
105 solver_process.StartInfo.RedirectStandardInput =
true;
106 solver_process.StartInfo.RedirectStandardOutput =
true;
107 solver_process.StartInfo.RedirectStandardError =
true;
108 solver_process.Start();
110 if (final_program.Length > 0)
112 StreamWriter writer = solver_process.StandardInput;
113 writer.WriteLine(final_program);
120 solverOutput = solver_process.StandardOutput.ReadToEnd().ToString();
121 solverError = solver_process.StandardError.ReadToEnd().ToString();
123 solver_process.WaitForExit();
124 solver_process.Close();
128 Console.Error.WriteLine(
"Total time : " + watch.ElapsedMilliseconds);
130 return GetOutput(solverOutput.ToString(), solverError.ToString());
132 catch (Win32Exception e2)
134 Console.Error.WriteLine(e2.ToString());
135 Console.Error.Write(e2.StackTrace);
138 return GetOutput(
"",
"");