using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace CobXmlSupport { public partial class ShowCode : Form { public readonly string CSV = ".csv"; public readonly string DIF = ".dif"; string oldTxt; bool bSynch; public ShowCode() { InitializeComponent(); oldTxt = ""; bSynch = false; } public bool BSynch { get { return bSynch; } set { bSynch = value; btnSynch.Visible = bSynch; } } public TextBox CodeShower { get { return txtCode; } set { txtCode = value; } } public string Labeltext { get { return lblCodeType.Text; } set { lblCodeType.Text = value; } } private GenCobCode parentWindow; public GenCobCode Parent { get { return parentWindow; } set { parentWindow = value; } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } private void txtCode_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) { txtCode.SelectAll(); } } private void txtCode_TextChanged(object sender, EventArgs e) { if (txtCode.Text.Length > oldTxt.Length) { string tmpNewText = txtCode.Text.Substring(oldTxt.Length); // Debug.Write(tmpNewText); // Tag bort denna kommentering för att testa på vad som precis skrivs på log-fönster. oldTxt = txtCode.Text; } } private void btnSynch_Click(object sender, EventArgs e) { Dictionary wrkDic = null; string codeComplement = "", fieldCompl = "", codeComplInit = ""; int actRows = 0; string inFile = parentWindow.CmbLastFile.Text.Substring(parentWindow.CmbLastFile.Text.LastIndexOf("\\") + 1); string inPath = parentWindow.CmbLastFile.Text.Substring(0, parentWindow.CmbLastFile.Text.LastIndexOf("\\") + 1); //string codeFile = inFile.Substring(0, inFile.LastIndexOf(".")) + CSV; string codeFile = inFile.Substring(0, inFile.LastIndexOf(".")) + DIF; this.CodeShower.Text = " InPath = " + inPath + "\r\n InFile = " + inFile + "\r\n codeFile = " + codeFile + "\r\n codePath = " + inPath + codeFile + "\r\n -------------- \r\n" + this.CodeShower.Text; if (File.Exists(inPath + codeFile)) { codeComplement = File.ReadAllText(inPath + codeFile); } if (codeComplement.Length > 0) { // wrkDic = moveFieldDictionary(codeComplement); wrkDic = moveDifFieldDictionary(codeComplement); } foreach (CobRow cr in parentWindow.RowList) { if (!cr.isAttribute && !cr.SampleStr.Equals(parentWindow.NOMOVE)) { if (wrkDic != null && wrkDic.Count > 0) { if (wrkDic.TryGetValue(cr.FieldName, out fieldCompl)) { cr.MoveCode = deQuote( fieldCompl); } } codeComplInit += "-1,0\r\n"; codeComplInit += "BOT\r\n"; codeComplInit += "1,0\r\n"; if (cr.MoveCode == null) { codeComplInit += enQuote(cr.FieldName) + "\r\n"; codeComplInit += "1,0\r\n"; codeComplInit += enQuote(" ") + "\r\n"; } else { codeComplInit += enQuote(cr.FieldName) + "\r\n"; codeComplInit += "1,0\r\n"; codeComplInit += enQuote(cr.MoveCode.Replace("\r\n", "\n")) + "\r\n"; } actRows++; } } codeComplInit = "TABLE\r\n" + "0,1\r\n" + "\"EXCEL\"\r\n" + "VECTORS\r\n" + "0"+actRows.ToString()+"\r\n" + "\"\"\r\n" + "TUPLES\r\n" + "0,2\r\n" + "\"\"\r\n" + "DATA\r\n" + "0,0\r\n" + "\"\"\r\n" + codeComplInit+ "-1,0\r\n"+ "EOD\r\n"; string backupfile; if (S.ettingUserCodeBcup) { backupfile = inPath + inFile.Substring(0, inFile.LastIndexOf(".")) + "." + (DateTime.Now.Year - 2000).ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + "T" + DateTime.Now.Hour.ToString("00") + DateTime.Now.Minute.ToString("00") + DateTime.Now.Second.ToString("00") + DIF; } else { backupfile = inPath + inFile.Substring(0, inFile.LastIndexOf(".")) + ".backup"+DIF; File.Delete(backupfile); } if (File.Exists(inPath + codeFile)) { File.Move(inPath + codeFile, backupfile);//+ DateTime.Now.ToString()); } File.WriteAllText(inPath + codeFile, codeComplInit); refreshWindow(); this.CodeShower.Text = " InPath = " + inPath + "\r\n InFile = " + inFile + "\r\n codeFile = " + codeFile + "\r\n codePath = " + inPath + codeFile + "\r\n -------------- \r\n" + this.CodeShower.Text; } private string deQuote(string inString) { string tmpStr = ""; char[] x = inString.ToArray(); for (int i = 0; i < x.Length; i++) { if (x[i] == '\"') { if (i < x.Length - 1) { if (x[i + 1] == '\"') { i++; tmpStr += "\""; } } } else tmpStr += x[i]; } return tmpStr; } private string enQuote(string inString) { string tmpStr = ""; char[] x = inString.ToArray(); for (int i = 0; i < x.Length; i++) { if (x[i] == '\"') { { tmpStr += "\"\""; } } else tmpStr += x[i]; } return "\""+tmpStr+"\""; } private Dictionary moveFieldDictionary(string inFil) { Dictionary tmpDic = new Dictionary(); string[] splits = { "\r\n" }; string[] itemPairs = inFil.Split(splits, StringSplitOptions.RemoveEmptyEntries); char[] newSplits = { '\t' }; foreach (string itemPair in itemPairs) { string[] items = itemPair.Split(newSplits); tmpDic.Add(items[0], items[1].Replace("\n", "\r\n")); } return tmpDic; } private Dictionary moveDifFieldDictionary(string inFil) { bool TABLE = false, VECTORS = false, TUPLES = false, DATA = false; int rows = 0, rowCount = 0; Dictionary tmpDic = new Dictionary(); string[] splits = { "\r\n" }; char[] csplit = {','}; string[] items = inFil.Split(splits, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < items.Count();i++ ) { string item = items[i]; switch (item.ToUpper()) { case "TABLE": { TABLE = true; break; } case "VECTORS": { VECTORS = true; string[] nums = items[i + 1].Split(csplit); rows = int.Parse(nums[1]); break; } case "TUPLES": { TUPLES = true; break; } case "DATA": { DATA = true; break; } case "BOT": { if (TABLE && VECTORS && TUPLES && DATA) { if (rowCount < rows) { tmpDic.Add(deQuote(items[i + 2]), items[i+4].Replace("\n", "\r\n")); rowCount++; i = i + 4; } } break; } case "EOD": { i= items.Count(); break; } } } return tmpDic; } private void refreshWindow() { this.CodeShower.Text = ""; foreach (CobRow cr in parentWindow.RowList) { if (cr.CobLevel < 2) { string inFile = parentWindow.CmbLastFile.Text.Substring(parentWindow.CmbLastFile.Text.LastIndexOf("\\") + 1); parentWindow.CreateComment(this.CodeShower, "Moves reference (with loop) " + cr.FieldName, S.ettingUserName, S.ettingCompany, inFile); } // Header square created if (!cr.isAttribute && !cr.SampleStr.Equals(parentWindow.NOMOVE)) { if (this.CodeShower.Text == "") { } else this.CodeShower.Text += "\r\n"; if (cr.MoveCode == null) { this.CodeShower.Text += "".PadRight(30) + " " + cr.FieldName.PadRight(30); } else { this.CodeShower.Text += cr.MoveCode.PadRight(30) + " " + cr.FieldName.PadRight(30); } } } } } }