Untitled
unknown
pascal
a year ago
6.9 kB
6
Indexable
using System.Text.RegularExpressions;
namespace EditFormatInserter
{
internal class Program
{
static Regex GetComponentNameRegex = new Regex(@"object ([A-Z0-9\._]+): \w+", RegexOptions.IgnoreCase);
static Regex GetPropRegex = new Regex(@"([A-Z0-9\.]+) = (.+)", RegexOptions.IgnoreCase);
static string GetLeadSpaces(string line) => new string(line.TakeWhile(c => c == ' ').ToArray());
static bool IsEmptyValue(string val) => string.IsNullOrWhiteSpace(val) || val == "''";
static void DropComponent(List<string> content, int start, int end)
{
for (int i = end; i >= start; --i)
content.RemoveAt(i);
}
static bool HasProperty(List<string> content, int start, int end, string propertyName)
{
for (int i = start; i < end; ++i)
{
if (content[i].TrimStart().StartsWith(propertyName))
return true;
}
return false;
}
static bool AddEditFormatToEh(List<string> content, int start, int end)
{
var foundKind = false;
for (int i = start; i < end; i++)
{
var line = content[i];
if (line.TrimStart().StartsWith("Kind = dtkDateEh"))
{
foundKind = true;
break;
}
}
if (!foundKind)
return false;
for (int i = start; i < end; i++)
{
var line = content[i];
if (line.TrimStart().StartsWith("EditFormat ="))
return false;
}
var empt = GetLeadSpaces(content[end - 1]);
content.Insert(end, empt + "EditFormat = 'DD.MM.YYYY'");
return true;
}
static readonly HashSet<string> defProps = [
"Left", "Top", "Width", "Height", "TabOrder", "TabStop",
"Ctl3D", "Visible", "OnChange", "OnCloseUp", "ParentShowHint", "ShowHint", "Hint"
];
static bool ConvertDtComponent(List<string> content, int start, int end)
{
if (HasProperty(content, start, end, "ShowCheckbox = True"))
return false;
//if (HasProperty(content, start, end, "LabelCaption ="))
// return false;
var empt = GetLeadSpaces(content[start]);
var componentName = GetComponentNameRegex.Match(content[start]).Groups[1].Value;
var newComp = new List<string>();
newComp.Add($"{empt}object {componentName}: TDBDateTimeEditEh");
// AdvDateTimePicker =>
bool hasFormat = false;
bool hasLabel = false;
for (int i = start; i < end; ++i)
{
var line = content[i];
var name = content[i].TrimStart();
var propMatch = GetPropRegex.Match(line);
var val = propMatch.Groups[2].Value;
var prop = propMatch.Groups[1].Value;
if (defProps.Contains(prop))
{
newComp.Add(line);
}
else if (prop == "Format" && !IsEmptyValue(val))
{
newComp.Add($"{empt} EditFormat = {val.ToUpper()}");
hasFormat = true;
}
else if (prop == "LabelCaption" && !IsEmptyValue(val))
{
newComp.Add($"{empt} ControlLabel.Caption = {val}");
newComp.Add($"{empt} ControlLabel.Visible = True");
hasLabel = true;
}
else if (prop == "LabelPosition" && val == "lpTopLeft" && hasLabel)
{
//Console.WriteLine("LabelPosition: " + val);
newComp.Add($"{empt} ControlLabelLocation.Position = lpAboveLeftEh");
}
else if (prop == "LabelPosition" && val == "lpLeftCenter" && hasLabel)
{
//Console.WriteLine("LabelPosition: " + val);
newComp.Add($"{empt} ControlLabelLocation.Position = lpLeftCenterEh");
}
}
if (!hasFormat)
newComp.Add($"{empt} EditFormat = 'DD.MM.YYYY'");
newComp.Add($"{empt}end");
DropComponent(content, start, end);
content.InsertRange(start, newComp);
return true;
}
static bool ProcessComponents(List<string> content, string componentName, Func<List<string>, int, int, bool> func)
{
bool result = false;
int start = 0;
int end = 0;
for (int i = 0; i < content.Count; ++i)
{
var line = content[i];
if (line.TrimStart().StartsWith("object") && line.EndsWith(componentName))
{
start = i;
}
else if (start > 0 && line.Trim() == "end")
{
end = i;
}
if (start > 0 && end > 0)
{
result |= func?.Invoke(content, start, end) ?? false;
start = 0;
end = 0;
}
}
return result;
}
static void Main(string[] args)
{
var dir = "D:\\proj\\VagTK\\VagTK";
var files = Directory.GetFiles(dir, "*.dfm", SearchOption.AllDirectories);
foreach (var file in files)
{
var content = File.ReadAllLines(file).ToList();
//if (ProcessComponents(content, "TDBDateTimeEditEh", AddEditFormatToEh))
//{
// File.WriteAllLines(file, content);
// Console.WriteLine("Done: " + file);
//}
if (ProcessComponents(content, "TAdvDateTimePicker", ConvertDtComponent))
{
File.WriteAllLines(file, content);
var pas = file.Substring(0, file.Length - 3) + "pas";
var pasContent = File.ReadAllText(pas);
pasContent = pasContent.Replace("TAdvDateTimePicker", "TDBDateTimeEditEh");
if (pasContent.IndexOf("DBCtrlsEh") == -1)
pasContent = pasContent.Replace("AdvDateTimePicker", "DBCtrlsEh");
File.WriteAllText(pas, pasContent);
Console.WriteLine("Done: " + file);
}
}
Console.WriteLine("Hello, World!");
}
}
}
Editor is loading...
Leave a Comment