protected string CallSafeConvert(DirectableEncConverter aEC, string strInput) { try { return aEC.Convert(strInput); } catch (Exception ex) { MessageBox.Show(String.Format("Conversion failed! Reason: {0}", ex.Message), cstrCaption); } return null; }
protected string CallSafeConvert(DirectableEncConverter aEC, string strInput, bool bAllowAbort) { try { if (!String.IsNullOrEmpty(strInput)) { // if the input side is legacy and the code page of the converter is not the same as the // code page we opened the file with, then we'll be passing the wrong values... if (aEC.IsLhsLegacy) { IEncConverter aIEC = aEC.GetEncConverter; // IsLhsLegacy will throw if GetEncConverter returns null if (aIEC.DirectionForward && (aIEC.CodePageInput != 0) && (aIEC.CodePageInput != m_encOpen.CodePage)) { // we opened the SFM files with Encoding 0 == CP_ACP (or the default code page for this computer) // but if the CodePageInput used by EncConverters is a different code page, then this will fail. // If so, then convert it to a byte array and pass that byte[] abyInput = m_encOpen.GetBytes(strInput); strInput = ECNormalizeData.ByteArrToString(abyInput); aEC.GetEncConverter.EncodingIn = EncodingForm.LegacyBytes; } } string strOutput = aEC.Convert(strInput); aEC.GetEncConverter.EncodingIn = EncodingForm.Unspecified; // in case the user switches directions // similarly, if the output is legacy, then if the code page used was not the same as the // default code page, then we have to convert it so it'll produce the correct answer // (this probably doesn't work for Legacy<>Legacy code pages) if (aEC.IsRhsLegacy) { IEncConverter aIEC = aEC.GetEncConverter; if ( (!aIEC.DirectionForward && (aIEC.CodePageInput != 0) && (Encoding.Default.CodePage != aIEC.CodePageInput)) || (aIEC.DirectionForward && (aIEC.CodePageOutput != 0) && (Encoding.Default.CodePage != aIEC.CodePageOutput))) { int nCP = (!aIEC.DirectionForward) ? aIEC.CodePageInput : aIEC.CodePageOutput; byte[] abyOutput = EncConverters.GetBytesFromEncoding(nCP, strOutput, true); strOutput = new string(Encoding.Default.GetChars(abyOutput)); } } return strOutput; } } catch (Exception ex) { DialogResult res = MessageBox.Show(String.Format("Conversion failed! Reason: {0}", ex.Message), cstrCaption, (bAllowAbort) ? MessageBoxButtons.AbortRetryIgnore : MessageBoxButtons.OK); if (res == DialogResult.Abort) throw; } return strInput; }
// allow these to be overidden by sub-class forms (e.g. to add a round-trip refresh as well) protected virtual void RefreshTextBoxes(DirectableEncConverter aEC) { ForwardString = aEC.Convert(InputString); }
protected string CallSafeConvert(DirectableEncConverter aDEC, string strPreviewData) { string strOutput = null; try { // this might throw up, so don't let it crash the program. strOutput = aDEC.Convert(strPreviewData); } catch (Exception ex) { MessageBox.Show(String.Format("Unable to convert data for preview because: '{0}'", ex.Message), EncConverters.cstrCaption); } return strOutput; }