Skip to content

Commit

Permalink
Add error handling for external operations
Browse files Browse the repository at this point in the history
 -  Make last char dependent upon newline style
 -  Version 1.1
  • Loading branch information
A9G-Data-Droid committed May 4, 2021
1 parent c5ac463 commit 0d111b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
41 changes: 28 additions & 13 deletions Split-FANUC-Program-Backup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ private static string BuildDate

private const string cncProgramFileExtension = ".CNC";
private const string defaultCNCprogramName = "Uknown.CNC";
private const char percent = '%';
private const char programDelimiter = '%';
private const int minimumProgramSize = 7;

/// <summary>
/// The left side of the OR (|) describes an "O Number", the original CNC program name structure, consisting of an "O" followed by 4 to 8 numbers
Expand Down Expand Up @@ -60,9 +61,17 @@ static int Main(string[] args)

// Make a subfolder named like the filename to hold all the programs we split out of it
string outputFolder = Path.Combine(backupFile.DirectoryName, Path.GetFileNameWithoutExtension(backupFile.Name));
Directory.CreateDirectory(outputFolder);
try
{
Directory.CreateDirectory(outputFolder);
} catch
{ // Fail gracefully
outputFolder = backupFile.DirectoryName;
}

SplitALLPROGtxt(backupFile, outputFolder);

// Success
return 0;
}

Expand All @@ -74,14 +83,18 @@ private static void SplitALLPROGtxt(FileInfo backupFile, string outputFolder)
{
foreach (string cncProgramText in GetCNCProgams(backupFile.FullName))
{
if (cncProgramText.Length > 7)
{
string programFileName = GetProgramNameFromHeader(cncProgramText);
if (programFileName.Length < 1) { programFileName = defaultCNCprogramName; }
string programFileName = GetProgramNameFromHeader(cncProgramText);
if (programFileName.Length < 1) { programFileName = defaultCNCprogramName; }

string outputFilename = Path.Combine(outputFolder, programFileName + cncProgramFileExtension);
string outputFilename = Path.Combine(outputFolder, programFileName + cncProgramFileExtension);
try
{
File.WriteAllTextAsync(outputFilename, cncProgramText);
Console.WriteLine("CREATED FILE: " + outputFilename);
} catch (Exception err)
{
Console.WriteLine("ERROR " + err.HResult + ": " + err.Message);
Console.WriteLine("FAILED TO CREATE FILE: " + outputFilename);
}
}
}
Expand All @@ -92,8 +105,7 @@ private static void SplitALLPROGtxt(FileInfo backupFile, string outputFolder)
/// <param name="cncProgramText">The full text of a CNC program</param>
/// <returns>The program name from the header</returns>
private static string GetProgramNameFromHeader(string cncProgramText)
{
/// Searches for O#### formatted program names
{
return Regex.Match(cncProgramText, oNumberPattern, RegexOptions.Multiline).Value;
}

Expand All @@ -106,12 +118,12 @@ static IEnumerable<string> GetCNCProgams(string fileName)
{
StringBuilder content = new();

/// Searches for CNC programs between O numbers symbols
/// Searches for CNC programs between program name symbols
foreach(string line in File.ReadLines(fileName))
{
if (Regex.IsMatch(line, oNumberPattern))
{
if (content.Length > 4)
if (content.Length > minimumProgramSize)
{ // Return the file we have in the buffer
yield return CncProgramText(content);
}
Expand All @@ -131,10 +143,13 @@ static string CncProgramText(StringBuilder content)
{
// Add % to the top
content.Insert(0, Environment.NewLine);
content.Insert(0, percent);
content.Insert(0, programDelimiter);

// Add % to the bottom when missing
if (content[^3] != percent) { content.AppendLine(percent.ToString()); }
#pragma warning disable S1854 // Unused assignments should be removed
int lastCharIndex = Environment.NewLine.Length + 1;
#pragma warning restore S1854 // Unused assignments should be removed
if (content[^lastCharIndex] != programDelimiter) { content.AppendLine(programDelimiter.ToString()); }

return content.ToString();
}
Expand Down
5 changes: 4 additions & 1 deletion Split-FANUC-Program-Backup/Split-FANUC-Program-Backup.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<RootNamespace>Split_FANUC_Program_Backup</RootNamespace>
<StartupObject>Split_FANUC_Program_Backup.Program</StartupObject>
<Deterministic>False</Deterministic>
<AssemblyVersion>1.0.*</AssemblyVersion>
<AssemblyVersion>1.1.*</AssemblyVersion>
<Version>1.1.0</Version>
<NeutralLanguage>en</NeutralLanguage>
<PackageProjectUrl>https://github.com/A9G-Data-Droid/Split-FANUC-Program-Backup/</PackageProjectUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down

0 comments on commit 0d111b8

Please sign in to comment.