20210706
This commit is contained in:
23
anbs_cp/LikeDelphi.cs
Normal file
23
anbs_cp/LikeDelphi.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace anbs_cp
|
||||
{
|
||||
public class LikeDelphi
|
||||
{
|
||||
/*
|
||||
* Аналог функции "IncludeTrailingBackslash
|
||||
*/
|
||||
public static string IncludeTrailingBackslash(string Path)
|
||||
{
|
||||
string result = Path;
|
||||
int Index = Path.Length - 1;
|
||||
if (Path[Index] != '\\')
|
||||
{
|
||||
result = $"{Path}\\";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
164
anbs_cp/TStringList.cs
Normal file
164
anbs_cp/TStringList.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace anbs_componentspack
|
||||
{
|
||||
public enum TStringListType
|
||||
{
|
||||
sltStringsOnly, //Только строки -- сортировка по строкам включена, имеющиеся объекты удалит
|
||||
sltObjectsOnly, //Только объекты -- сортировка по объектам включена
|
||||
sltBoth //И строки, и объекты -- сортировка отключена
|
||||
}
|
||||
public class TStringList
|
||||
{
|
||||
private List<string> FLines;
|
||||
private List<object> 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<string> 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();
|
||||
}
|
||||
}
|
||||
}
|
26
anbs_cp/TypeConverter.cs
Normal file
26
anbs_cp/TypeConverter.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace anbs_componentspack
|
||||
{
|
||||
public static class TypeConverter
|
||||
{
|
||||
public static string IntToStr(int AInt) => AInt.ToString();
|
||||
public static string IntToStr(long AInt) => AInt.ToString();
|
||||
public static int StrToInt(string AStr)
|
||||
{
|
||||
if (!int.TryParse(AStr, out int result))
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static long StrToInt64(string AStr)
|
||||
{
|
||||
if (!long.TryParse(AStr, out long result))
|
||||
{
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
12
anbs_cp/anbs_cp.csproj
Normal file
12
anbs_cp/anbs_cp.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Version>0.1.0</Version>
|
||||
<Authors>Alexander Babaev</Authors>
|
||||
<Product>delphi2c#</Product>
|
||||
<Description>Library to import some useful functions from Delphi language to C# language.</Description>
|
||||
<Copyright />
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user