Replace PesFile.readyStatus with exceptions and exception handling

Branch_2.1.x
Nathan Crawford 2016-03-22 19:48:13 -04:00
rodzic d2f64f0205
commit 3fda9e60b1
2 zmienionych plików z 28 dodań i 38 usunięć

Wyświetl plik

@ -29,20 +29,21 @@ using System.IO;
namespace PesFile
{
public enum statusEnum { NotOpen, IOError, ParseError, Ready };
public class PECFormatException : System.Exception
{
public PECFormatException(string message) : base(message) { }
}
public class PesFile
{
int imageWidth;
int imageHeight;
string _filename;
private int imageWidth;
private int imageHeight;
private string _filename;
public List<StitchBlock> blocks = new List<StitchBlock>();
public List<Tuple<int, int>> colorTable = new List<Tuple<int, int>>();
private statusEnum readyStatus = statusEnum.NotOpen;
Int64 startStitches = 0;
string lastError = "";
Point translateStart;
UInt16 pesVersion;
private Int64 startStitches = 0;
private Point translateStart;
private UInt16 pesVersion;
// If set to true, this variable means we couldn't figure out some or
@ -132,9 +133,7 @@ namespace PesFile
if (startFileSig != "#PES")
{
// This is not a file that we can read
readyStatus = statusEnum.ParseError;
lastError = "Missing #PES at beginning of file";
return;
throw new PECFormatException("Missing #PES at beginning of file");
}
// PES version
@ -146,8 +145,7 @@ namespace PesFile
if (!UInt16.TryParse(versionString, out pesVersion))
{
// This is not a file that we can read
readyStatus = statusEnum.ParseError;
lastError = "PES version is not the correct format";
throw new PECFormatException("PES version is not the correct format");
return;
}
@ -156,8 +154,7 @@ namespace PesFile
if (fileIn.BaseStream.Length < (pecstart + 532))
{
// This file is probably truncated
readyStatus = statusEnum.ParseError;
lastError = "File appears to be truncated (PEC section is beyond end of file)";
throw new PECFormatException("File appears to be truncated (PEC section is beyond end of file)");
return;
}
@ -325,7 +322,6 @@ namespace PesFile
imageHeight = maxY - minY;
translateStart.X = -minX;
translateStart.Y = -minY;
readyStatus = statusEnum.Ready;
}
}
}
@ -407,16 +403,6 @@ namespace PesFile
return outfile.ToString();
}
public statusEnum getStatus()
{
return readyStatus;
}
public string getLastError()
{
return lastError;
}
public bool getColorWarning()
{
return colorWarning;

Wyświetl plik

@ -289,19 +289,30 @@ namespace embroideryReader
{
if (!System.IO.File.Exists(filename))
{
MessageBox.Show("File \"" + filename + "\" does not exist", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// "An error occured while reading the file:"
MessageBox.Show(Translation.StringID.ERROR_FILE + Environment.NewLine + "File \"" + filename + "\" does not exist", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
try
{
design = new PesFile.PesFile(filename);
}
catch(System.IO.IOException ioex)
{
MessageBox.Show("IOException while reading file \"" + filename + "\":" + Environment.NewLine + ioex.Message);
// "An error occured while reading the file:"
MessageBox.Show(Translation.StringID.ERROR_FILE + Environment.NewLine + filename + ":" + Environment.NewLine + ioex.Message, "IOException", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
design = null;
}
catch(PesFile.PECFormatException pecex)
{
// "This file is either corrupt or not a valid PES file."
MessageBox.Show(Translation.StringID.ERROR_FILE + Environment.NewLine + filename + ":" + Environment.NewLine + pecex.Message, "PECFormatException", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
design = null;
}
loadedFileName = filename;
if (design.getStatus() == PesFile.statusEnum.Ready)
if (design != null)
{
updateDesignImage();
@ -317,6 +328,7 @@ namespace embroideryReader
{
toolStripStatusLabel1.Text = "";
}
copyToolStripMenuItem.Enabled = true;
saveDebugInfoToolStripMenuItem.Enabled = true;
printPreviewToolStripMenuItem.Enabled = true;
@ -331,14 +343,6 @@ namespace embroideryReader
}
else
{
string message = translation.GetTranslatedString(Translation.StringID.ERROR_FILE) + // "An error occured while reading the file:"
Environment.NewLine + design.GetFileName() +
Environment.NewLine + design.getLastError();
if (design.getStatus() == PesFile.statusEnum.ParseError)
{
message += Environment.NewLine + translation.GetTranslatedString(Translation.StringID.CORRUPT_FILE); // "This file is either corrupt or not a valid PES file."
}
MessageBox.Show(message);
copyToolStripMenuItem.Enabled = false;
saveDebugInfoToolStripMenuItem.Enabled = false;
printPreviewToolStripMenuItem.Enabled = false;