summaryrefslogtreecommitdiff
path: root/Analizator9000/Analizator9000/DealerWrapper.cs
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2012-11-14 22:44:39 +0000
committeremkael <emkael@tlen.pl>2012-11-14 22:44:39 +0000
commitbf0f197187b2d4403d4e80da5d36048fab3834b3 (patch)
treed07e7bc75b7ddf653416ffbc74d08d1101a529ae /Analizator9000/Analizator9000/DealerWrapper.cs
parent258f973cf4e3f46f6983c2d6a1218f87b8fa3d0d (diff)
* deal generating and writing to file
git-svn-id: https://svn.emkael.info/an9k@6 05ec0d5d-773b-4d93-9e23-c81a7ac79feb
Diffstat (limited to 'Analizator9000/Analizator9000/DealerWrapper.cs')
-rw-r--r--Analizator9000/Analizator9000/DealerWrapper.cs106
1 files changed, 106 insertions, 0 deletions
diff --git a/Analizator9000/Analizator9000/DealerWrapper.cs b/Analizator9000/Analizator9000/DealerWrapper.cs
new file mode 100644
index 0000000..b17c9ab
--- /dev/null
+++ b/Analizator9000/Analizator9000/DealerWrapper.cs
@@ -0,0 +1,106 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows.Forms;
+using System.Diagnostics;
+using System.IO;
+using System.Text.RegularExpressions;
+
+namespace Analizator9000
+{
+ class DealerWrapper
+ {
+ private Form1 debugForm;
+ private String scriptname;
+ private StreamWriter outputFile;
+ private bool running = false;
+ private long produce = 0;
+ private long lineCount = 0;
+
+ public DealerWrapper(String scriptname, Form1 debugForm, long produce)
+ {
+ if (!File.Exists(scriptname))
+ {
+ throw new Exception("Nie znaleziono pliku: " + scriptname);
+ }
+ if (produce < 1)
+ {
+ throw new Exception("Nieprawidłowa liczba rozdań do wyprodukowania");
+ }
+ this.scriptname = scriptname;
+ this.debugForm = debugForm;
+ this.produce = produce;
+ }
+
+ private void debugWriteLine(String line) {
+ this.debugForm.addStatusLine(line);
+ }
+
+ private bool handleData(string data)
+ {
+ if (data != null)
+ {
+ String[] dataLines = data.Split('\n');
+ foreach (String line in dataLines)
+ {
+ Match lineMatch = Regex.Match(line.Trim(), "^n\\s*(\\S*)\\s*e\\s*(\\S*)\\s*s\\s*(\\S*)\\s*w\\s*(\\S*)$");
+ if (lineMatch.Success)
+ {
+ this.outputFile.WriteLine(lineMatch.Result("${1} ${2} ${3} ${4}"));
+ this.lineCount++;
+ }
+ }
+ int progress = ((int)(Convert.ToDouble(this.lineCount) / Convert.ToDouble(this.produce) * 100));
+ this.debugForm.setProgress(progress);
+ this.debugWriteLine(data);
+ return true;
+ }
+ return false;
+ }
+
+ public void closeFile()
+ {
+ this.outputFile.Close();
+ }
+
+ private delegate void onEndDelegate(String filename);
+ public void run(Action<String> onEnd)
+ {
+ if (!this.running)
+ {
+ this.running = true;
+ this.lineCount = 0;
+ String filename = "an9k-" + DateTime.Now.ToString("yyyyMMddHHmmssFFF") + ".deals";
+ this.outputFile = new StreamWriter("files\\"+filename);
+ ProcessStartInfo pInfo = new ProcessStartInfo();
+ pInfo.FileName = "bin\\dealer.exe";
+ pInfo.WindowStyle = ProcessWindowStyle.Hidden;
+ pInfo.CreateNoWindow = true;
+ pInfo.Arguments = "\"" + this.scriptname + "\"";
+ pInfo.UseShellExecute = false;
+ pInfo.RedirectStandardOutput = true;
+ pInfo.RedirectStandardError = true;
+ this.debugWriteLine(pInfo.FileName + " " + pInfo.Arguments);
+ Process dealerProc = new Process();
+ dealerProc.StartInfo = pInfo;
+ dealerProc.OutputDataReceived += (sender, output) =>
+ {
+ if (output != null)
+ {
+ if (!this.handleData(output.Data))
+ {
+ this.closeFile();
+ (new onEndDelegate(onEnd)).Invoke(filename);
+ }
+ }
+ };
+ dealerProc.ErrorDataReceived += (sender, error) => { if (error != null) { this.debugWriteLine(error.Data); } };
+ dealerProc.Start();
+ dealerProc.BeginErrorReadLine();
+ dealerProc.BeginOutputReadLine();
+ }
+ }
+
+ }
+}