From fb0a2647b3ce2fc621cda3478b205f22197a58b5 Mon Sep 17 00:00:00 2001 From: tfoman Date: Fri, 23 Jan 2015 00:35:21 +0100 Subject: [PATCH] CodeCompleter class created --- CobXmlSupport/CobXmlSupport.csproj | 1 + CobXmlSupport/ShowCode.cs | 60 ++------- CobXmlSupport/codeCompleter.cs | 199 +++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+), 47 deletions(-) create mode 100644 CobXmlSupport/codeCompleter.cs diff --git a/CobXmlSupport/CobXmlSupport.csproj b/CobXmlSupport/CobXmlSupport.csproj index 59f71c3..812df95 100644 --- a/CobXmlSupport/CobXmlSupport.csproj +++ b/CobXmlSupport/CobXmlSupport.csproj @@ -49,6 +49,7 @@ + Form diff --git a/CobXmlSupport/ShowCode.cs b/CobXmlSupport/ShowCode.cs index 6ff4ae0..df3b592 100644 --- a/CobXmlSupport/ShowCode.cs +++ b/CobXmlSupport/ShowCode.cs @@ -91,68 +91,34 @@ namespace CobXmlSupport private void btnSynch_Click(object sender, EventArgs e) { - Dictionary wrkDic = null, newDic = null; - string codeComplement = "", fieldCompl = "", codeComplInit = ""; - 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(".")) + DIF; - string jsonFile = inFile.Substring(0, inFile.LastIndexOf(".")) + JSON; - this.CodeShower.Text = " InPath = " + inPath + "\r\n InFile = " + inFile + "\r\n codeFile = " + jsonFile + "\r\n codePath = " + inPath + jsonFile + "\r\n -------------- \r\n" + this.CodeShower.Text; + Dictionary wrkDic = null; + codeCompleter Cc = new codeCompleter(parentWindow.CmbLastFile.Text); + string fieldCompl = ""; + this.CodeShower.Text = " InPath = " + Cc.PathId + "\r\n InFile = " + Cc.FileId + "\r\n codeFile = " + Cc.CcFileInId + "\r\n codePath = " + Cc.PathId+Cc.CcFileInId + "\r\n -------------- \r\n" + this.CodeShower.Text; + Cc.TryFill_In_Dic(); - if (File.Exists(inPath + jsonFile)) - { - codeComplement = File.ReadAllText(inPath + jsonFile); - } - - if (codeComplement.Length > 0) - { - wrkDic = moveJsonFieldDictionary(codeComplement); - } - - newDic = new Dictionary(); + // wrkDic = new Dictionary(); foreach (CobRow cr in parentWindow.RowList) { if (!cr.isAttribute && !cr.SampleStr.Equals(parentWindow.NOMOVE)) { - if (wrkDic != null && wrkDic.Count > 0) + if (Cc.WorkDicIn != null && Cc.WorkDicIn.Count > 0) { - if (wrkDic.TryGetValue(cr.FieldName, out fieldCompl)) + if (Cc.WorkDicIn.TryGetValue(cr.FieldName, out fieldCompl)) { cr.MoveCode = fieldCompl; } } - - newDic.Add(cr.FieldName, cr.MoveCode); + if (cr.MoveCode == null) { cr.MoveCode = " "; } + Cc.WorkDicIn[cr.FieldName]=cr.MoveCode; } } - string njsonObj = createJsonFromDic(newDic); - 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" + JSON; - File.Delete(backupfile); - } - - if (File.Exists(inPath + jsonFile)) - { - File.Move(inPath + jsonFile, backupfile); - } - File.WriteAllText(inPath + jsonFile, njsonObj); + Cc.Save_In_Dic(); refreshWindow(); - this.CodeShower.Text = " InPath = " + inPath + "\r\n InFile = " + inFile + "\r\n codeFile = " + jsonFile + "\r\n codePath = " + inPath + jsonFile + "\r\n -------------- \r\n" + this.CodeShower.Text; + + this.CodeShower.Text = " InPath = " + Cc.PathId + "\r\n InFile = " + Cc.FileId + "\r\n codeFile = " + Cc.CcFileInId + "\r\n codePath = " + Cc.PathId + Cc.CcFileInId + "\r\n -------------- \r\n" + this.CodeShower.Text; } diff --git a/CobXmlSupport/codeCompleter.cs b/CobXmlSupport/codeCompleter.cs new file mode 100644 index 0000000..03116a2 --- /dev/null +++ b/CobXmlSupport/codeCompleter.cs @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using Newtonsoft.Json; + +namespace CobXmlSupport +{ + public class codeCompleter + { + private Dictionary __wrkDicIn = null; + private Dictionary __wrkDicOut = null; + private string __filePathId; + private string __fileId; + private string __pathId; + private string __ccFileInId; + private string __ccFileOutId; + private string __ccFileInIdNoSuff; + private string __ccFileOutIdNoSuff; + public readonly string JSON = ".json"; + + public codeCompleter() + { + __filePathId = ""; + __fileId = ""; + __pathId = ""; + __ccFileInId = ""; + __ccFileOutId = ""; + __ccFileInIdNoSuff = ""; + __ccFileOutIdNoSuff = ""; + } + + public codeCompleter(string pathFileId) + { + __filePathId = pathFileId; + nameCreator(__filePathId); + __wrkDicIn = new Dictionary(); + __wrkDicOut = new Dictionary(); + } + + public string FilePathId + { + set + { + __filePathId = value; + nameCreator(__filePathId); + } + get { return __filePathId; } + } + + public string FileId + { + get { return __fileId; } + } + + public string PathId + { + get { return __pathId; } + } + public string CcFileInId + { + get { return __ccFileInId; } + } + public string CcFileOutId + { + get { return __ccFileOutId; } + } + + public Dictionary WorkDicIn + { + get { return __wrkDicIn; } + set { __wrkDicIn = value; } + } + + public Dictionary WorkDicOut + { + get { return __wrkDicOut; } + set { __wrkDicOut = value; } + } + + private void nameCreator(string inPathFname) + { + __fileId = inPathFname.Substring(inPathFname.LastIndexOf("\\") + 1); + __pathId = inPathFname.Substring(0, inPathFname.LastIndexOf("\\") + 1); + __ccFileInId = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".In" + JSON; + __ccFileOutId = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".Out" + JSON; + __ccFileInIdNoSuff = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".In"; + __ccFileOutIdNoSuff = __fileId.Substring(0, __fileId.LastIndexOf(".")) + ".Out"; + } + + private string backupSuffix() + { + string tmpSuff = ""; + if (S.ettingUserCodeBcup) + { + tmpSuff = "." + + (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") + JSON; + } + else + { + tmpSuff = ".backup" + JSON; + } + return tmpSuff; + } + + + public void TryFill_In_Dic() + { + string ccFileInId = __pathId + __ccFileInId; + if (__filePathId.Trim() == "") { } + else + { + if (File.Exists(ccFileInId)) + { + string fileStr = File.ReadAllText(ccFileInId); + moveJsonFieldDictionary(fileStr, "IN"); + } + } + } + + public void TryFill_Out_Dic() + { + string ccFileOutId = __pathId + __ccFileOutId; + if (__filePathId.Trim() == "") { } + else + { + if (File.Exists(ccFileOutId)) + { + string fileStr = File.ReadAllText(ccFileOutId); + moveJsonFieldDictionary(fileStr, "OUT"); + } + } + } + + private void moveJsonFieldDictionary(string fString, string inOut) + { + if (inOut == "IN") + __wrkDicIn = JsonConvert.DeserializeObject>(fString); + else + __wrkDicOut = JsonConvert.DeserializeObject>(fString); + } + + public void Save_In_Dic() + { + string ccFileInId = __pathId + __ccFileInId; + string ccFileInBackup = __pathId + __ccFileInIdNoSuff + backupSuffix(); + + if (__filePathId.Trim() == "") { } + else + { + if (File.Exists(ccFileInId)) + { + if (File.Exists(ccFileInBackup)) + { + File.Delete(ccFileInBackup); + } + File.Move(ccFileInId, ccFileInBackup); + } + File.WriteAllText(ccFileInId, createJsonFromDic("IN")); + } + } + + public void Save_Out_Dic() + { + string ccFileOutId = __pathId + __ccFileOutId; + string ccFileOutBackup = __pathId + __ccFileOutIdNoSuff + backupSuffix(); + + if (__filePathId.Trim() == "") { } + else + { + if (File.Exists(ccFileOutId)) + { + if (File.Exists(ccFileOutBackup)) + { + File.Delete(ccFileOutBackup); + } + File.Move(ccFileOutId, ccFileOutBackup); + } + File.WriteAllText(ccFileOutId, createJsonFromDic("OUT")); + } + } + + private string createJsonFromDic(string inOut) + { + string tmpJson = ""; + if (inOut == "IN") + tmpJson = JsonConvert.SerializeObject(__wrkDicIn); + else + tmpJson = JsonConvert.SerializeObject(__wrkDicOut); + return tmpJson; + } + } +}