Skip to content

Commit 0d111b8

Browse files
author
A9G-Data-Droid
committed
Add error handling for external operations
- Make last char dependent upon newline style - Version 1.1
1 parent c5ac463 commit 0d111b8

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

Split-FANUC-Program-Backup/Program.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ private static string BuildDate
2323

2424
private const string cncProgramFileExtension = ".CNC";
2525
private const string defaultCNCprogramName = "Uknown.CNC";
26-
private const char percent = '%';
26+
private const char programDelimiter = '%';
27+
private const int minimumProgramSize = 7;
2728

2829
/// <summary>
2930
/// 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
@@ -60,9 +61,17 @@ static int Main(string[] args)
6061

6162
// Make a subfolder named like the filename to hold all the programs we split out of it
6263
string outputFolder = Path.Combine(backupFile.DirectoryName, Path.GetFileNameWithoutExtension(backupFile.Name));
63-
Directory.CreateDirectory(outputFolder);
64+
try
65+
{
66+
Directory.CreateDirectory(outputFolder);
67+
} catch
68+
{ // Fail gracefully
69+
outputFolder = backupFile.DirectoryName;
70+
}
6471

6572
SplitALLPROGtxt(backupFile, outputFolder);
73+
74+
// Success
6675
return 0;
6776
}
6877

@@ -74,14 +83,18 @@ private static void SplitALLPROGtxt(FileInfo backupFile, string outputFolder)
7483
{
7584
foreach (string cncProgramText in GetCNCProgams(backupFile.FullName))
7685
{
77-
if (cncProgramText.Length > 7)
78-
{
79-
string programFileName = GetProgramNameFromHeader(cncProgramText);
80-
if (programFileName.Length < 1) { programFileName = defaultCNCprogramName; }
86+
string programFileName = GetProgramNameFromHeader(cncProgramText);
87+
if (programFileName.Length < 1) { programFileName = defaultCNCprogramName; }
8188

82-
string outputFilename = Path.Combine(outputFolder, programFileName + cncProgramFileExtension);
89+
string outputFilename = Path.Combine(outputFolder, programFileName + cncProgramFileExtension);
90+
try
91+
{
8392
File.WriteAllTextAsync(outputFilename, cncProgramText);
8493
Console.WriteLine("CREATED FILE: " + outputFilename);
94+
} catch (Exception err)
95+
{
96+
Console.WriteLine("ERROR " + err.HResult + ": " + err.Message);
97+
Console.WriteLine("FAILED TO CREATE FILE: " + outputFilename);
8598
}
8699
}
87100
}
@@ -92,8 +105,7 @@ private static void SplitALLPROGtxt(FileInfo backupFile, string outputFolder)
92105
/// <param name="cncProgramText">The full text of a CNC program</param>
93106
/// <returns>The program name from the header</returns>
94107
private static string GetProgramNameFromHeader(string cncProgramText)
95-
{
96-
/// Searches for O#### formatted program names
108+
{
97109
return Regex.Match(cncProgramText, oNumberPattern, RegexOptions.Multiline).Value;
98110
}
99111

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

109-
/// Searches for CNC programs between O numbers symbols
121+
/// Searches for CNC programs between program name symbols
110122
foreach(string line in File.ReadLines(fileName))
111123
{
112124
if (Regex.IsMatch(line, oNumberPattern))
113125
{
114-
if (content.Length > 4)
126+
if (content.Length > minimumProgramSize)
115127
{ // Return the file we have in the buffer
116128
yield return CncProgramText(content);
117129
}
@@ -131,10 +143,13 @@ static string CncProgramText(StringBuilder content)
131143
{
132144
// Add % to the top
133145
content.Insert(0, Environment.NewLine);
134-
content.Insert(0, percent);
146+
content.Insert(0, programDelimiter);
135147

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

139154
return content.ToString();
140155
}

Split-FANUC-Program-Backup/Split-FANUC-Program-Backup.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<RootNamespace>Split_FANUC_Program_Backup</RootNamespace>
77
<StartupObject>Split_FANUC_Program_Backup.Program</StartupObject>
88
<Deterministic>False</Deterministic>
9-
<AssemblyVersion>1.0.*</AssemblyVersion>
9+
<AssemblyVersion>1.1.*</AssemblyVersion>
10+
<Version>1.1.0</Version>
11+
<NeutralLanguage>en</NeutralLanguage>
12+
<PackageProjectUrl>https://github.com/A9G-Data-Droid/Split-FANUC-Program-Backup/</PackageProjectUrl>
1013
</PropertyGroup>
1114

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

0 commit comments

Comments
 (0)