From 3902f5e3b791e9f983aac9b068df3b20421d79bd Mon Sep 17 00:00:00 2001 From: Valerie Date: Wed, 6 Mar 2024 19:51:24 -0500 Subject: [PATCH] added file merge functionality --- src/Program.cs | 10 ++++++---- src/Util.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/Util.cs diff --git a/src/Program.cs b/src/Program.cs index 6dd8bbe..60cae9b 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -2,6 +2,12 @@ using System; using System.IO; using System.Security.Cryptography; +// use merge function if given multiple arguments +if(args.Length > 0) { + Utilities.Merge(args); + return 0; +} + // make sure fortune directory exists var resourcePath = "/usr/share/fortune-cs/"; if(!Directory.Exists(resourcePath)) { @@ -12,15 +18,11 @@ if(!Directory.Exists(resourcePath)) { // pull file list var files = Directory.GetFiles(resourcePath, "*.txt"); -var prng = RandomNumberGenerator.Create(); - // choose a file and line var file = files[RandomNumberGenerator.GetInt32(files.Length)]; var lines = File.ReadAllLines(file); var line = lines[RandomNumberGenerator.GetInt32(lines.Length)]; -prng.Dispose(); - // process escape codes line = line.Replace("\\n", "\n"); diff --git a/src/Util.cs b/src/Util.cs new file mode 100644 index 0000000..14f7f7c --- /dev/null +++ b/src/Util.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.IO; + +public static class Utilities { + + public static void Merge(params string[] files) { + // hashset to prevent duplicates + var members = new HashSet(); + // iterate over all paths given + foreach(var file in files) { + // skip nonexistent files gracefully + if(!File.Exists(file)) + continue; + // iterate over lines + var lines = File.ReadAllLines(file); + foreach(var line in lines) { + // prevent duplicates + if(members.Contains(line)) + continue; + members.Add(line); + // emit to stdout + Console.WriteLine(line); + } + } + } + +} +