Skip to content

Commit

Permalink
Add Localidiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Belyaev committed Jan 23, 2024
1 parent b41bdaa commit 5deac6e
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
bin

obj
.vs
*.csproj.user
10 changes: 10 additions & 0 deletions Localidiff/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*.cs]
indent_style = space
indent_size = 2
tab_width = 2
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
charset = utf-8
16 changes: 16 additions & 0 deletions Localidiff/Localidiff.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>localidiff</AssemblyName>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SelfContained>true</SelfContained>
<LangVersion>latest</LangVersion>
<PublishTrimmed>true</PublishTrimmed>
<PublishAot>true</PublishAot>
<PublishSingleFile>false</PublishSingleFile>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions Localidiff/Localidiff.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Localidiff", "Localidiff.csproj", "{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8C40D30-8EE5-4DC4-A5DD-9215BC528B21}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7A4DFED6-0E8C-4114-8B68-61F1BC918581}
EndGlobalSection
EndGlobal
129 changes: 129 additions & 0 deletions Localidiff/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
static Dictionary<string, string[]> LoadFromFile(string path)
{
var dict = new Dictionary<string, string[]>();
foreach (var strings in File.ReadAllLines(path).Select(line => line.Split('\t')))
{
switch (strings[0])
{
case "//":
dict["//"] = strings;
break;
case "{":
dict[strings[1]] = strings;
break;
case var _ when (strings.All(string.IsNullOrWhiteSpace)):
break;
default:
throw new NotImplementedException(strings[0]);
}
}
return dict;
}

static string FormatLine(string[] values) => string.Join('\t', values);

static void Compare(string oldPath, string newPath)
{
var oldDict = LoadFromFile(oldPath);
var newDict = LoadFromFile(newPath);

foreach (var key in oldDict.Keys.Union(newDict.Keys).OrderBy(x => x))
{
var haveOld = oldDict.TryGetValue(key, out var oldValue);
var haveNew = newDict.TryGetValue(key, out var newValue);
if (!haveOld)
Console.WriteLine($"ADD\t{FormatLine(newValue!)}");
else if (!haveNew)
Console.WriteLine($"REMOVE\t{FormatLine(oldValue!)}");
else
{
if (newValue!.Length > oldValue!.Length)
for (int i = oldValue.Length - 1; i < newValue.Length - 1; i++)
Console.WriteLine($"ADD-LANGUAGE\t{key}\t{i}\t{newValue[i]}");
else if(newValue.Length < oldValue.Length)
for (int i = newValue.Length - 1; i < oldValue.Length - 1; i++)
Console.WriteLine($"REMOVE-LANGUAGE\t{key}\t{i}\t{oldValue[i]}");

for (int i = 1; i < Math.Min(oldValue!.Length, newValue!.Length) - 1; i++)
if (oldValue[i] != newValue![i])
Console.WriteLine($"CHANGE\t{key}\t{i}\t{oldValue[i]}\t{newValue[i]}");
}
}
}


if (args.Length == 7)
{
// localidiff old.txt new.txt
Console.WriteLine($"FILE\t{args[0]}");
Compare(args[1], args[4]);
}
else if (args.Length == 2 && args[0] == "apply")
{
// localidiff apply patch
var patch = File.ReadAllLines(args[1]);

Dictionary<string, string[]>? dict = null;
string? path = null;
foreach (var line in patch)
{
var items = line.Split('\t');
switch (items[0])
{
case "FILE":
if (dict != null && path != null)
{
Console.WriteLine($"Writing {path}");
File.WriteAllLines(path, dict.OrderByDescending(pair => pair.Key == "//").ThenBy(pair => pair.Key).Select(pair => FormatLine(pair.Value) + "\r"), System.Text.Encoding.Unicode);
}
path = items[1];
Console.WriteLine($"Loading {path}");
dict = LoadFromFile(path);
break;

case "ADD":
Console.WriteLine($"Adding {FormatLine(items.Skip(1).ToArray())}");
dict![items[2]] = items.Skip(1).ToArray();
break;

case "REMOVE":
Console.WriteLine($"Removing {FormatLine(dict![items[2]])}");
dict!.Remove(items[2]);
break;

case "CHANGE":
if (dict!.ContainsKey(items[1]))
{
Console.WriteLine($"Replacing {dict![items[1]][int.Parse(items[2])]} with {items[4]}");
dict![items[1]][int.Parse(items[2])] = items[4];
}
else
Console.WriteLine($"{items[1]} ({items[3]} -> {items[4]}) not found for replace!");
break;

default:
break;
}
}
Console.WriteLine($"Writing {path}");
File.WriteAllLines(path!, dict!.OrderByDescending(pair => pair.Key == "//").ThenBy(pair => pair.Key).Select(pair => FormatLine(pair.Value) + "\r"), System.Text.Encoding.Unicode);
}
else if (args.Length == 2)
{
Console.WriteLine($"FILE\t{Path.GetFileName(args[0])}");
Compare(args[0], args[1]);
}
else
{
Console.WriteLine("""
Usage:
Localidiff 1.txt 2.txt
Compares 2.txt to 1.txt and outputs the patch into the standard output
Localidiff unused 1.txt unused unused 2.txt unused unused
The same but for usage as a git difftool
Localidiff apply patch.patch
Applies patch.patch
""");
}
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
all: bin/awimage bin/add_checksum bin/minfs
all: bin/awimage bin/add_checksum bin/minfs bin/localidiff

.PHONY: clean

bin:
mkdir bin
Expand All @@ -18,6 +20,9 @@ awutils/add_checksum:
bin/minfs: bin lindenis-v833-RTOS-melis-4.0/source/utility/host-tool/minfs_tool/minfs
cp lindenis-v833-RTOS-melis-4.0/source/utility/host-tool/minfs_tool/minfs bin

bin/localidiff: bin Localidiff/Localidiff.csproj Localidiff/Program.cs
dotnet publish -c Release Localidiff/Localidiff.csproj -o bin

lindenis-v833-RTOS-melis-4.0/source/utility/host-tool/minfs_tool/minfs:
$(MAKE) -C lindenis-v833-RTOS-melis-4.0/source/utility/host-tool/minfs_tool

Expand Down

0 comments on commit 5deac6e

Please sign in to comment.