From 5e75552199ad731969a79ccfb89130e9b0f320dd Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 19 Jan 2022 20:13:19 +0300 Subject: [PATCH] quick fix of files path --- .gitignore | 3 +- .vscode/launch.json | 26 ----------------- .vscode/tasks.json | 42 ---------------------------- DTLib/Filesystem/Directory.cs | 12 ++++---- DTLib/Filesystem/File.cs | 16 +++++------ DTLib/Filesystem/OldFilework.cs | 9 ------ DTLib/Filesystem/Path.cs | 5 ++-- TestProgram/DtsodV2X/TestDtsodV23.cs | 12 ++++---- TestProgram/Program.cs | 1 + TestProgram/TestProgram.csproj | 6 ++-- 10 files changed, 29 insertions(+), 103 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/tasks.json diff --git a/.gitignore b/.gitignore index 9c9cd3d..294ef06 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ [Ll]og/ [Ll]ogs/ .vs/ -.vshistory/ \ No newline at end of file +.vshistory/ +.idea/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index cdc88bc..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md - "name": ".NET Core Launch (console)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/TestProgram/bin/Debug/net6.0/TestProgram.dll", - "args": [], - "cwd": "${workspaceFolder}/TestProgram", - // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console - "console": "internalConsole", - "stopAtEntry": false - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach" - } - ] -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 3fd6915..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/TestProgram/TestProgram.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/TestProgram/TestProgram.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "${workspaceFolder}/TestProgram/TestProgram.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file diff --git a/DTLib/Filesystem/Directory.cs b/DTLib/Filesystem/Directory.cs index 3da2da7..94b9e80 100644 --- a/DTLib/Filesystem/Directory.cs +++ b/DTLib/Filesystem/Directory.cs @@ -10,8 +10,8 @@ public static class Directory if (!Directory.Exists(dir)) { // проверяет существование папки, в которой нужно создать dir - if (dir.Contains('\\') && !Directory.Exists(dir.Remove(dir.LastIndexOf('\\')))) - Create(dir.Remove(dir.LastIndexOf('\\'))); + if (dir.Contains(Path.Sep) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Path.Sep)))) + Create(dir.Remove(dir.LastIndexOf(Path.Sep))); System.IO.Directory.CreateDirectory(dir); } } @@ -100,8 +100,8 @@ public static class Directory public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains("\\")) - Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\'))); + if (symlinkName.Contains(Path.Sep)) + Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); } @@ -110,8 +110,8 @@ public static class Directory public static int SymCopy(string srcdir, string newdir) { List files = Directory.GetAllFiles(srcdir); - if (!srcdir.EndsWith('\\')) srcdir += '\\'; - if (!newdir.EndsWith('\\')) newdir += '\\'; + if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep; + if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep; int i = 0; for (; i < files.Count; i++) File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index cdc10b7..5cfdb6e 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -7,14 +7,12 @@ public static class File public static bool Exists(string file) => System.IO.File.Exists(file); // если файл не существует, создаёт файл, создаёт папки из его пути - public static void Create(string file, bool delete_old = false) + public static void Create(string file) { - if (delete_old && File.Exists(file)) - File.Delete(file); if (!File.Exists(file)) { - if (file.Contains("\\")) - Directory.Create(file.Remove(file.LastIndexOf('\\'))); + if (file.Contains(Path.Sep)) + Directory.Create(file.Remove(file.LastIndexOf(Path.Sep))); using System.IO.FileStream stream = System.IO.File.Create(file); stream.Close(); } @@ -64,7 +62,9 @@ public static class File Exists(file) ? System.IO.File.OpenRead(file) : throw new Exception($"file not found: <{file}>"); public static System.IO.FileStream OpenWrite(string file) { - File.Create(file, true); + if (File.Exists(file)) + File.Delete(file); + File.Create(file); return System.IO.File.Open(file, System.IO.FileMode.OpenOrCreate); } public static System.IO.FileStream OpenAppend(string file) @@ -75,8 +75,8 @@ public static class File public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains("\\")) - Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf('\\'))); + if (symlinkName.Contains(Path.Sep)) + Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File)) throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})"); } diff --git a/DTLib/Filesystem/OldFilework.cs b/DTLib/Filesystem/OldFilework.cs index 8372c67..c771425 100644 --- a/DTLib/Filesystem/OldFilework.cs +++ b/DTLib/Filesystem/OldFilework.cs @@ -5,15 +5,6 @@ // public static class OldFilework { - // записывает текст в файл и закрывает файл - /*public static void LogToFile(string logfile, string msg) - { - lock (new object()) - { - File.AppendAllText(logfile, msg); - } - }*/ - // чтение параметров из конфига public static string ReadFromConfig(string configfile, string key) { diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs index 8327222..17142cd 100644 --- a/DTLib/Filesystem/Path.cs +++ b/DTLib/Filesystem/Path.cs @@ -2,15 +2,16 @@ namespace DTLib.Filesystem; static public class Path { + static public readonly char Sep = OperatingSystem.IsWindows() ? '\\' : '/'; public static string CorrectSeparator(string path) { - if (System.IO.Path.PathSeparator == '\\') + if (Sep == '\\') { if (path.Contains('/')) path = path.Replace('/', '\\'); } - else if (System.IO.Path.PathSeparator == '/') + else if (Sep == '/') { if (path.Contains('\\')) path = path.Replace('\\', '/'); diff --git a/TestProgram/DtsodV2X/TestDtsodV23.cs b/TestProgram/DtsodV2X/TestDtsodV23.cs index 256e058..798b67d 100644 --- a/TestProgram/DtsodV2X/TestDtsodV23.cs +++ b/TestProgram/DtsodV2X/TestDtsodV23.cs @@ -20,7 +20,7 @@ public static class TestDtsodV23 public static void TestBaseTypes() { Info.Log("b", "[TestDtsodV23/TestBaseTypes]"); - DtsodV23 dtsod = new(File.ReadAllText("DtsodV2X\\base_types.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}base_types.dtsod")); foreach (var pair in dtsod) Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); Info.Log("g", "[test completed]"); @@ -28,7 +28,7 @@ public static class TestDtsodV23 public static void TestLists() { Info.Log("b", "[TestDtsodV23/TestLists]"); - DtsodV23 dtsod = new(File.ReadAllText("DtsodV2X\\lists.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}lists.dtsod")); foreach (var pair in dtsod) { Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", @@ -42,7 +42,7 @@ public static class TestDtsodV23 public static void TestComplexes() { Info.Log("b", "[TestDtsodV23/TestComplexes]"); - DtsodV23 dtsod = new(File.ReadAllText("DtsodV2X\\complexes.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")); foreach (var pair in dtsod) { Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, @@ -56,7 +56,7 @@ public static class TestDtsodV23 { Info.Log("b", "[TestDtsodV23/TestReSerialization]"); DtsodV23 dtsod = new DtsodV23(new DtsodV23(new DtsodV23( - new DtsodV23(File.ReadAllText("DtsodV2X\\complexes.dtsod")).ToString()).ToString()).ToString()); + new DtsodV23(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString()); Info.Log("y", dtsod.ToString()); Info.Log("g", "[test completed]"); } @@ -65,7 +65,7 @@ public static class TestDtsodV23 { Info.Log("b", "[TestDtsodV23/TestSpeed]"); IDtsod dtsod=null; - string text = File.ReadAllText("DtsodV2X\\messages.dtsod"); + string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); Tester.LogOperationTime("V21 deserialization",100,()=>dtsod=new DtsodV21(text)); Tester.LogOperationTime("V21 serialization", 100, () => _=dtsod.ToString()); Tester.LogOperationTime("V23 deserialization", 100, () => dtsod = new DtsodV23(text)); @@ -76,7 +76,7 @@ public static class TestDtsodV23 public static void TestMemoryConsumption() { Info.Log("b", "[TestDtsodV23/TestMemoryConsumption]"); - string text = File.ReadAllText("DtsodV2X\\messages.dtsod"); + string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); var a = GC.GetTotalMemory(true); DtsodV23[] dtsods = new DtsodV23[100]; for (int i = 0; i < dtsods.Length; i++) diff --git a/TestProgram/Program.cs b/TestProgram/Program.cs index 087376f..377eedf 100644 --- a/TestProgram/Program.cs +++ b/TestProgram/Program.cs @@ -27,6 +27,7 @@ static class Program Console.Title="tester"; try { + Info.Log("g","-------[DTLib tester]-------"); TestDtsodV23.TestAll(); } catch (Exception ex) diff --git a/TestProgram/TestProgram.csproj b/TestProgram/TestProgram.csproj index 7a8d794..1b23c27 100644 --- a/TestProgram/TestProgram.csproj +++ b/TestProgram/TestProgram.csproj @@ -1,4 +1,4 @@ - + Exe net6.0 @@ -14,10 +14,10 @@ - + Always - + Always