added file merge functionality

This commit is contained in:
Valerie Wolfe 2024-03-06 19:51:24 -05:00
parent c8a0606ae5
commit 3902f5e3b7
2 changed files with 35 additions and 4 deletions

View file

@ -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");

29
src/Util.cs Normal file
View file

@ -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<string>();
// 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);
}
}
}
}