Skip to content

Commit

Permalink
Fixed error when cancelling the Select Input File dialog and matched …
Browse files Browse the repository at this point in the history
…the code to the upcoming Kotlin version.
  • Loading branch information
VictorPLopes committed Jul 26, 2023
1 parent bbc8a3b commit 5c29ea6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
1 change: 1 addition & 0 deletions FixGraphicEQ/FixGraphicEQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<AssemblyVersion>1.1.0</AssemblyVersion>
</PropertyGroup>

</Project>
81 changes: 46 additions & 35 deletions FixGraphicEQ/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,24 @@
using System.Runtime.InteropServices;
using FixGraphicEQ;

// I don't know what this does but according to StackOverflow, I need it to create an open file dialog
// Sources: https://stackoverflow.com/questions/68711769/how-to-make-a-openfile-dialog-box-in-console-application-net-core-c
// https://www.pinvoke.net/default.aspx/comdlg32/GetOpenFileName.html
// https://www.pinvoke.net/default.aspx/Structures/OPENFILENAME.html
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName(ref OpenFileName ofn);

// Shows an open file dialog and returns the path of the selected file, from the sources above
static string ShowDialog() {
var ofn = new OpenFileName();
ofn.lStructSize = Marshal.SizeOf(ofn);
// Define Filter for your extensions (Excel, ...)
ofn.lpstrFilter = "Plain Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = new string(new char[256]);
ofn.nMaxFile = ofn.lpstrFile.Length;
ofn.lpstrFileTitle = new string(new char[64]);
ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
ofn.lpstrTitle = "Open File Dialog...";
if (GetOpenFileName(ref ofn))
return ofn.lpstrFile;
return string.Empty;
}

// Pauses the program until a key is pressed, probably a stupid way to do it without sending a system pause command
void Pause() {
Console.ReadKey();
}

// App header
Console.WriteLine("FixGraphicEQ v1.0\n" +
" Made by VictorPL\n\n" +
@"This program will offset values in a GraphicEQ file generated by https:\\autoeq.app by a given amount." +
"\n\nPress ENTER to select an input file...");
Console.WriteLine("""
FixGraphicEQ (C# Version) v1.1.0
Made by VictorPL

This program will offset values in a GraphicEQ file generated by https:\\autoeq.app by a given amount.

Press ENTER to select an input file...
""");
Pause();

// Get input file path
var inputFilePath = ShowDialog();
if (inputFilePath == string.Empty) {
Console.WriteLine("\nOperation cancelled. Press any key to exit...");
Pause();
return;
}

// Gets the offset value from the user
float offset;
Expand All @@ -53,7 +34,7 @@ void Pause() {
List<FrequencyGain> frequenciesGains = new();

// Beginning of every GraphicEQ file
var header = "GraphicEQ: ";
const string header = "GraphicEQ: ";

// Reads the file and adds the values to the list
try {
Expand All @@ -69,13 +50,13 @@ void Pause() {
}

// Creates a new string with the new values
var newValues = "";
var newValues = header;
foreach (var frequencyGain in frequenciesGains)
newValues += $"{frequencyGain.Frequency} {Math.Round(frequencyGain.Gain + offset, 1)}; ".Replace(",", ".");

// Writes the new file
try {
File.WriteAllText(outputFilePath, header + newValues.TrimEnd("; ".ToCharArray()));
File.WriteAllText(outputFilePath, newValues.TrimEnd("; ".ToCharArray()));
Console.WriteLine("\nFile written successfully: " + outputFilePath);
}
catch (Exception e) {
Expand All @@ -86,6 +67,36 @@ void Pause() {
Pause();
}

return;

// Pauses the program until a key is pressed, probably a stupid way to do it without sending a system pause command
void Pause() {
Console.ReadKey();
}

// I don't know what this does but according to StackOverflow, I need it to create an open file dialog
// Sources: https://stackoverflow.com/questions/68711769/how-to-make-a-openfile-dialog-box-in-console-application-net-core-c
// https://www.pinvoke.net/default.aspx/comdlg32/GetOpenFileName.html
// https://www.pinvoke.net/default.aspx/Structures/OPENFILENAME.html
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName(ref OpenFileName ofn);

// Shows an open file dialog and returns the path of the selected file, from the sources above
static string ShowDialog() {
var ofn = new OpenFileName();
ofn.lStructSize = Marshal.SizeOf(ofn);
// Define Filter for your extensions (Excel, ...)
ofn.lpstrFilter = "Plain Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = new string(new char[256]);
ofn.nMaxFile = ofn.lpstrFile.Length;
ofn.lpstrFileTitle = new string(new char[64]);
ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
ofn.lpstrTitle = "Select input file";
if (GetOpenFileName(ref ofn))
return ofn.lpstrFile;
return string.Empty;
}

// I also have no idea what this does but it's needed for the open file dialog, from the sources above
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct OpenFileName {
Expand Down

0 comments on commit 5c29ea6

Please sign in to comment.