CodeCompleter class created

This commit is contained in:
2015-01-23 00:35:21 +01:00
parent 0337efacf0
commit fb0a2647b3
3 changed files with 213 additions and 47 deletions

View File

@ -49,6 +49,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CobRow.cs" />
<Compile Include="codeCompleter.cs" />
<Compile Include="frmSettings.cs">
<SubType>Form</SubType>
</Compile>

View File

@ -91,68 +91,34 @@ namespace CobXmlSupport
private void btnSynch_Click(object sender, EventArgs e)
{
Dictionary<string, string> 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<string, string> 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<string, string>();
// wrkDic = new Dictionary<string, string>();
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;
}

View File

@ -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<string, string> __wrkDicIn = null;
private Dictionary<string, string> __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<string, string>();
__wrkDicOut = new Dictionary<string, string>();
}
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<string, string> WorkDicIn
{
get { return __wrkDicIn; }
set { __wrkDicIn = value; }
}
public Dictionary<string, string> 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<Dictionary<string, string>>(fString);
else
__wrkDicOut = JsonConvert.DeserializeObject<Dictionary<string, string>>(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;
}
}
}