Save extra code to json

This commit is contained in:
2015-01-22 08:33:46 +01:00
parent a455e7d4f0
commit de557eb04a
2 changed files with 46 additions and 57 deletions

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
namespace CobXmlSupport
@ -18,6 +19,7 @@ namespace CobXmlSupport
public readonly string CSV = ".csv";
public readonly string DIF = ".dif";
public readonly string JSON = ".json";
string oldTxt;
bool bSynch;
@ -89,75 +91,44 @@ namespace CobXmlSupport
private void btnSynch_Click(object sender, EventArgs e)
{
Dictionary<string, string> wrkDic = null;
Dictionary<string, string> wrkDic = null, newDic = 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;
// 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;
if (File.Exists(inPath + codeFile))
if (File.Exists(inPath + jsonFile))
{
codeComplement = File.ReadAllText(inPath + codeFile);
codeComplement = File.ReadAllText(inPath + jsonFile);
}
if (codeComplement.Length > 0)
{
// wrkDic = moveFieldDictionary(codeComplement);
wrkDic = moveDifFieldDictionary(codeComplement);
wrkDic = moveJsonFieldDictionary(codeComplement);
}
newDic = 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 (wrkDic.TryGetValue(cr.FieldName, out fieldCompl))
{
cr.MoveCode = deQuote( fieldCompl);
cr.MoveCode = 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++;
newDic.Add(cr.FieldName, cr.MoveCode);
}
}
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 njsonObj = createJsonFromDic(newDic);
string backupfile;
if (S.ettingUserCodeBcup)
{
backupfile = inPath + inFile.Substring(0, inFile.LastIndexOf(".")) + "." +
@ -168,22 +139,35 @@ namespace CobXmlSupport
DateTime.Now.Minute.ToString("00") +
DateTime.Now.Second.ToString("00") + DIF;
}
else {
backupfile = inPath + inFile.Substring(0, inFile.LastIndexOf(".")) + ".backup"+DIF;
else
{
backupfile = inPath + inFile.Substring(0, inFile.LastIndexOf(".")) + ".backup" + JSON;
File.Delete(backupfile);
}
if (File.Exists(inPath + codeFile))
if (File.Exists(inPath + jsonFile))
{
File.Move(inPath + codeFile, backupfile);//+ DateTime.Now.ToString());
File.Move(inPath + jsonFile, backupfile);
}
File.WriteAllText(inPath + codeFile, codeComplInit);
File.WriteAllText(inPath + jsonFile, njsonObj);
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;
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;
}
private Dictionary<string, string> moveJsonFieldDictionary(string ccIn)
{
Dictionary<string, string> tmpDic = JsonConvert.DeserializeObject<Dictionary<string, string>>(ccIn);
return tmpDic;
}
private string createJsonFromDic(Dictionary<string, string> wrkDic)
{
string tmpJson = JsonConvert.SerializeObject(wrkDic);
return tmpJson;
}
private string deQuote(string inString)
{
string tmpStr = "";
@ -220,7 +204,7 @@ namespace CobXmlSupport
}
else tmpStr += x[i];
}
return "\""+tmpStr+"\"";
return "\"" + tmpStr + "\"";
}
@ -245,9 +229,9 @@ namespace CobXmlSupport
int rows = 0, rowCount = 0;
Dictionary<string, string> tmpDic = new Dictionary<string, string>();
string[] splits = { "\r\n" };
char[] csplit = {','};
char[] csplit = { ',' };
string[] items = inFil.Split(splits, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < items.Count();i++ )
for (int i = 0; i < items.Count(); i++)
{
string item = items[i];
switch (item.ToUpper())
@ -282,7 +266,7 @@ namespace CobXmlSupport
{
if (rowCount < rows)
{
tmpDic.Add(deQuote(items[i + 2]), items[i+4].Replace("\n", "\r\n"));
tmpDic.Add(deQuote(items[i + 2]), items[i + 4].Replace("\n", "\r\n"));
rowCount++;
i = i + 4;
}
@ -291,7 +275,7 @@ namespace CobXmlSupport
}
case "EOD":
{
i= items.Count();
i = items.Count();
break;
}
}