diff --git a/delphi2csharp.sln b/delphi2csharp.sln new file mode 100644 index 0000000..7873bb9 --- /dev/null +++ b/delphi2csharp.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31424.327 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "delphi2csharp", "delphi2csharp\delphi2csharp.csproj", "{D722F27B-693F-471A-8166-8A7912531A4F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D722F27B-693F-471A-8166-8A7912531A4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D722F27B-693F-471A-8166-8A7912531A4F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D722F27B-693F-471A-8166-8A7912531A4F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D722F27B-693F-471A-8166-8A7912531A4F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {61D41C58-CBDF-4C21-BD22-DEFBD590FEF7} + EndGlobalSection +EndGlobal diff --git a/delphi2csharp/TStringList.cs b/delphi2csharp/TStringList.cs new file mode 100644 index 0000000..8630ffa --- /dev/null +++ b/delphi2csharp/TStringList.cs @@ -0,0 +1,159 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace delphi2csharp +{ + public class TStringList + { + private List FLines; + private List FObjects; + private TStringListType type = TStringListType.sltBoth; + public TStringListType Type { get => type; set => type = value; } + public int Count => FLines.Count; + public override bool Equals(object obj) + { + return obj is TStringList list && + Count == list.Count; + } + public override int GetHashCode() + { + return HashCode.Combine(Count); + } + public void Add(string AString) + { + if (!(type == TStringListType.sltObjectsOnly)) + { + FLines.Add(AString); + if (!(type == TStringListType.sltStringsOnly)) + { + FObjects.Add(AString); + } + } + } + public void Add(object AObject) + { + if (!(type == TStringListType.sltStringsOnly)) + { + if (!(type == TStringListType.sltObjectsOnly)) + { + FLines.Add(AObject.ToString()); + + } + FObjects.Add(AObject); + } + } + public void Add(string AString, object AObject) + { + if (!(type == TStringListType.sltStringsOnly) && !(type == TStringListType.sltObjectsOnly)) + { + FLines.Add(AString); + FObjects.Add(AObject); + } + } + public (string, object) Get(int Index) => (FLines[Index], FObjects[Index]); + public string GetString(int Index) + { + string result = ""; + if (!(type == TStringListType.sltObjectsOnly)) + { + result = FLines[Index]; + } + return result; + } + public object GetObject(int Index) + { + object result = null; + if (!(type == TStringListType.sltStringsOnly)) + { + result = FObjects[Index]; + } + return result; + } + public void Delete(int Index) + { + if (!(type == TStringListType.sltObjectsOnly)) + { + FLines.RemoveAt(Index); + } + if (!(type == TStringListType.sltStringsOnly)) + { + FObjects.RemoveAt(Index); + } + } + public int IndexOf(string AString) => FLines.IndexOf(AString); + public int IndexOf(object AObject) => FObjects.IndexOf(AObject); + public void Sort() + { + switch (type) + { + case TStringListType.sltStringsOnly: + FLines.Sort(); + break; + case TStringListType.sltObjectsOnly: + FObjects.Sort(); + break; + case TStringListType.sltBoth: + break; + default: + break; + } + } + public void Clear() + { + FLines.Clear(); + FObjects.Clear(); + } + public void Assign(List ALines) + { + if (!(type == TStringListType.sltStringsOnly)) + { + throw new ArgumentException("Assign method works in TStringList only for the sltStringsOnly type. Your changes are not applied!"); + } + Clear(); + FLines.AddRange(ALines); + } + public string DelimetedText(char Delimeter) + { + string result = ""; + if (!(type == TStringListType.sltObjectsOnly)) + { + for (int i = 0; i < FLines.Count; i++) + { + result = $"{result}{Delimeter}{FLines[i]}"; + } + } + return result; + } + public string Text() + { + char Delim = '\n'; + return DelimetedText(Delim); + } + public void LoadFromFile(string FileName) + { + if (!File.Exists(FileName)) + { + throw new FileNotFoundException(); + } + StreamReader sr = new StreamReader(FileName); + string line = sr.ReadLine(); + while (line != null) + { + FLines.Add(line); + line = sr.ReadLine(); + } + sr.Close(); + } + public void SaveToFile(string FileName) + { + StreamWriter sw = new StreamWriter(FileName); + for (int i = 0; i < FLines.Count; i++) + { + sw.WriteLine(FLines[i]); + } + sw.Close(); + } + } +} diff --git a/delphi2csharp/TStringListType.cs b/delphi2csharp/TStringListType.cs new file mode 100644 index 0000000..1c0c648 --- /dev/null +++ b/delphi2csharp/TStringListType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace delphi2csharp +{ + public enum TStringListType + { + sltStringsOnly, //Только строки -- сортировка по строкам включена, имеющиеся объекты удалит + sltObjectsOnly, //Только объекты -- сортировка по объектам включена + sltBoth //И строки, и объекты -- сортировка отключена + } +} diff --git a/delphi2csharp/delphi2csharp.csproj b/delphi2csharp/delphi2csharp.csproj new file mode 100644 index 0000000..86a736d --- /dev/null +++ b/delphi2csharp/delphi2csharp.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + 0.1.0 + Alexander Babaev + delphi2c# + Library to import some useful functions from Delphi language to C# language. + + + + diff --git a/delphi2csharp/delphimethods.cs b/delphi2csharp/delphimethods.cs new file mode 100644 index 0000000..0eeb9c2 --- /dev/null +++ b/delphi2csharp/delphimethods.cs @@ -0,0 +1,29 @@ +using System; + +namespace delphi2csharp +{ + static public class delphi + { + public static string IncludeTrailingBackslash(string Path) + { + string result = Path; + int Index = Path.Length - 1; + if (Path[Index] != '\\') + { + result = $"{Path}\\"; + } + return result; + } + public static string IntToStr(int AInt) => AInt.ToString(); + public static string IntToStr(long AInt) => AInt.ToString(); + public static long StrToInt(string AStr) + { + long result; + if (!long.TryParse(AStr, out result)) + { + result = 0; + } + return result; + } + } +}