Skip to content

Commit

Permalink
update to .net 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Лиясов Илья Андреевич committed Nov 5, 2024
1 parent b96dce5 commit 06fd2d8
Show file tree
Hide file tree
Showing 17 changed files with 573 additions and 613 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,6 @@ _Pvt_Extensions

# FAKE - F# Make
.fake/

# VS Code
.vscode/
4 changes: 2 additions & 2 deletions Exceptions.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exceptions", "Exceptions\Exceptions.csproj", "{E837A77F-C3E7-4756-95DC-C41D6A46CF4B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Exceptions", "Exceptions\Exceptions.csproj", "{E837A77F-C3E7-4756-95DC-C41D6A46CF4B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
120 changes: 120 additions & 0 deletions Exceptions/Converter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NLog;

namespace Exceptions;

public class Converter
{
private static readonly Logger log = LogManager.GetCurrentClassLogger();

public static void ConvertFiles(params string[] filenames)
{
try
{
filenames = filenames.Length > 0 ? filenames : new[] { "text.txt" };
var settings = LoadSettings();
ConvertFiles(filenames, settings);
}
catch (Exception e)
{
log.Error(e);
}
}

private static void ConvertFiles(string[] filenames, Settings settings)
{
var tasks = filenames
.Select(fn => Task.Run(() => ConvertFile(fn, settings)))
.ToArray();
Task.WaitAll(tasks);
}

private static Settings LoadSettings()
{
using var stream = new FileStream("settings.json", FileMode.Open);
return JsonSerializer.Deserialize<Settings>(stream);
}

private static void ConvertFile(string filename, Settings settings)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(settings.SourceCultureName);
if (settings.Verbose)
{
log.Info("Processing file " + filename);
log.Info("Source Culture " + Thread.CurrentThread.CurrentCulture.Name);
}
IEnumerable<string> lines;
try
{
lines = PrepareLines(filename);
}
catch
{
log.Error($"File {filename} not found");
return;
}
var convertedLines = lines
.Select(ConvertLine)
.Select(s => s.Length + " " + s);
File.WriteAllLines(filename + ".out", convertedLines);
}

private static IEnumerable<string> PrepareLines(string filename)
{
var lineIndex = 0;
foreach (var line in File.ReadLines(filename))
{
if (line == string.Empty) continue;
yield return line.Trim();
lineIndex++;
}
yield return lineIndex.ToString();
}

public static string ConvertLine(string arg)
{
try
{
return ConvertAsDateTime(arg);
}
catch
{
try
{
return ConvertAsDouble(arg);
}
catch
{
return ConvertAsCharIndexInstruction(arg);
}
}
}

private static string ConvertAsCharIndexInstruction(string s)
{
var parts = s.Split();
if (parts.Length < 2) return null;
var charIndex = int.Parse(parts[0]);
if ((charIndex < 0) || (charIndex >= parts[1].Length))
return null;
var text = parts[1];
return text[charIndex].ToString();
}

private static string ConvertAsDateTime(string arg)
{
return DateTime.Parse(arg).ToString(CultureInfo.InvariantCulture);
}

private static string ConvertAsDouble(string arg)
{
return double.Parse(arg).ToString(CultureInfo.InvariantCulture);
}
}
122 changes: 0 additions & 122 deletions Exceptions/ConverterProgram.cs

This file was deleted.

Loading

0 comments on commit 06fd2d8

Please sign in to comment.