2011-07-14 15:52:42 +00:00

211 lines
8.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
namespace fmtxml
{
public partial class FmtFileToXml
{
private bool _DoingFields = false;
private int NumBoxes = 0;
private int NumTexts = 0;
private int NumFields = 0;
private int CountForLists = 0;
private Regex removeComment = new Regex("/[*].*?[*]/");
private Regex removeComment1 = new Regex("/[*].*?$");
private Regex removeComment2 = new Regex("[*]/");
private string RemoveComments(string inStr)
{
string str = removeComment.Replace(inStr, "");
str = removeComment1.Replace(str, "");
str = removeComment2.Replace(str, "");
return str.Trim();
}
public void PsiIniToXml(ref FormatData fmtdata, string path)
{
string sLine;
StreamReader myReader = new StreamReader(path);// Open file
while ((sLine = myReader.ReadLine()) != null)// read line-by-line
{
try
{
if (sLine.Length > 0)
{
switch (sLine.Substring(0, 1))
{
case "[":
if (sLine.Contains("[OBJECT COUNT]"))
{
// read in the count of objects - these may not be in order, fix this:
sLine = myReader.ReadLine();
while(sLine.StartsWith(";")) sLine = myReader.ReadLine(); // Skip Comments
int indx = sLine.IndexOf("=");
string tmp = sLine.Substring(indx + 1, sLine.Length - indx - 1);
if (sLine.Contains("BOXES"))
NumBoxes = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("TEXT"))
NumTexts = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("FIELDS"))
NumFields = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
sLine = myReader.ReadLine();
while (sLine.StartsWith(";")) sLine = myReader.ReadLine(); // Skip Comments
indx = sLine.IndexOf("=");
tmp = sLine.Substring(indx + 1, sLine.Length -indx - 1);
if (sLine.Contains("BOXES"))
NumBoxes = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("TEXT"))
NumTexts = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("FIELDS"))
NumFields = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
sLine = myReader.ReadLine();
while (sLine.StartsWith(";")) sLine = myReader.ReadLine(); // Skip Comments
indx = sLine.IndexOf("=");
tmp = sLine.Substring(indx + 1, sLine.Length -indx - 1);
if (sLine.Contains("BOXES"))
NumBoxes = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("TEXT"))
NumTexts = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
else if (sLine.Contains("FIELDS"))
NumFields = tmp == "" ? 1 : Convert.ToInt32(RemoveComments(tmp)) + 1;
if (NumBoxes == 0) NumBoxes++;
if (NumTexts == 0) NumTexts++;
if (NumFields == 0) NumFields++;
fmtdata.ProcData.Psi.Boxes = new PsiBox[NumBoxes];
fmtdata.ProcData.Psi.Labels = new PsiLabel[NumTexts];
fmtdata.ProcData.Psi.Fields = new PsiField[NumFields];
break;
}
if (sLine.Contains("[DIALOG]") || sLine.Contains("[BOXES]") || sLine.Contains("[TEXT]") || sLine.Contains("[FIELDS]"))
{
_DoingFields = sLine.Contains("[FIELDS]");
CountForLists = 0;
break;
}
break;
case ";":
break;
case "/":
if (sLine[1] == '*') break; // if it isn't a comment, fall thru to the default.
if (sLine.IndexOf("=") >= 0)
{
AddParam(ref fmtdata, RemoveComments(sLine));
}
break;
default:
if (sLine.IndexOf("=") >= 0)
{
AddParam(ref fmtdata, RemoveComments(sLine));
}
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("error parsing .INI file: {0}, message: {1}", path, ex.Message));
}
}
myReader.Close();
}
private Regex findBOX = new Regex("^BOX[0-9]+$");
private Regex findSIZE = new Regex("^SIZE$");
private Regex findFONT = new Regex("^FONT$");
private Regex findCAPTION = new Regex("^CAPTION$");
private Regex findBUTTONSONBOTTOM = new Regex("^BUTTONSONBOTTOM$");
private Regex findTEXT = new Regex("^TEXT[0-9]+$");
private void AddParam(ref FormatData fmtdata, string sParam)
{
int i = sParam.IndexOf("=");
string attrname = sParam.Substring(0, i);
if (findSIZE.IsMatch(attrname))
{
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
string sb = sParam.Substring(i + 1, end - i - 1);
string[] split = sb.Split(new char[] { ',' });
fmtdata.ProcData.Psi.x = Convert.ToInt32(split[0].Trim());
fmtdata.ProcData.Psi.y = Convert.ToInt32(split[1].Trim());
return;
}
if (findFONT.IsMatch(attrname))
{
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
string sb = sParam.Substring(i + 1, end - i - 1);
string[] split = sb.Split(new char[] { ',' });
fmtdata.ProcData.Psi.font = split[0].Trim();
return;
}
if (findCAPTION.IsMatch(attrname))
{
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
fmtdata.ProcData.Psi.Caption = sParam.Substring(i + 1, end-i-1).Replace("\"","");
return;
}
if (findBUTTONSONBOTTOM.IsMatch(attrname))
{
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
fmtdata.ProcData.Psi.ButtonsOnBottom = sParam.Substring(i + 1, end - i - 1);
return;
}
if (findBOX.IsMatch(attrname)) // attrname.StartsWith("BOX"))
{
//<Box style="BLACKFRAME" x="4" y="4" width="192" height="112"/>
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
string sb = sParam.Substring(i + 1, end - i - 1);
string[] split = sb.Split(new char[] { ',' });
if (CountForLists <= NumBoxes - 1)
{
fmtdata.ProcData.Psi.Boxes[CountForLists].style = split[0].Trim();
fmtdata.ProcData.Psi.Boxes[CountForLists].x = Convert.ToInt32(split[1].Trim());
fmtdata.ProcData.Psi.Boxes[CountForLists].y = Convert.ToInt32(split[2].Trim());
fmtdata.ProcData.Psi.Boxes[CountForLists].width = Convert.ToInt32(split[3].Trim());
fmtdata.ProcData.Psi.Boxes[CountForLists].height = Convert.ToInt32(split[4].Trim());
}
CountForLists++;
return;
}
if (findTEXT.IsMatch(attrname))
{
//<Label text="Responsible Manager's Title:" Justify="LEFT" x="12" y="11" width="100" height="8" />
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
string sb = sParam.Substring(i + 1, end - i - 1);
string[] split = sb.Split(new char[] { ',' });
fmtdata.ProcData.Psi.Labels[CountForLists].text = split[0].Trim();
fmtdata.ProcData.Psi.Labels[CountForLists].Justify = split[1].Trim();
fmtdata.ProcData.Psi.Labels[CountForLists].x = Convert.ToInt32(split[2].Trim());
fmtdata.ProcData.Psi.Labels[CountForLists].y = Convert.ToInt32(split[3].Trim());
fmtdata.ProcData.Psi.Labels[CountForLists].width = Convert.ToInt32(split[4].Trim());
fmtdata.ProcData.Psi.Labels[CountForLists].height = Convert.ToInt32(split[5].Trim());
CountForLists++;
return;
}
if (_DoingFields)
{
//<Field type="TEXT" name="CLASSIFICATION" length="51" x="12" y="20" width="180" height="12" />
//<Field type ="LOGICAL" name="USESETID" text="Some optional text" x="12" y="20" />
int end = (sParam.IndexOf(";") > -1) ? sParam.IndexOf(";") : sParam.Length;
string sb = sParam.Substring(i + 1, end - i - 1);
string[] split = sb.Split(new char[] { ',' });
// first check whether it is a text field or a logical (checkbox) field:
fmtdata.ProcData.Psi.Fields[CountForLists].name = attrname;
fmtdata.ProcData.Psi.Fields[CountForLists].type = split[0].Trim();
string typ = fmtdata.ProcData.Psi.Fields[CountForLists].type;
fmtdata.ProcData.Psi.Fields[CountForLists].text = split[1] == null ? null : split[1].Trim();
if (typ.ToUpper() == "TEXT")
fmtdata.ProcData.Psi.Fields[CountForLists].Length = Convert.ToInt32(split[2].Trim());
else
fmtdata.ProcData.Psi.Fields[CountForLists].Length = 0;
fmtdata.ProcData.Psi.Fields[CountForLists].x = Convert.ToInt32(split[3].Trim());
fmtdata.ProcData.Psi.Fields[CountForLists].y = Convert.ToInt32(split[4].Trim());
fmtdata.ProcData.Psi.Fields[CountForLists].width = Convert.ToInt32(split[5].Trim());
fmtdata.ProcData.Psi.Fields[CountForLists].height = Convert.ToInt32(split[6].Trim());
CountForLists++;
}
}
}
}