private void Preview(object sender, EventArgs e) { try { foreach (ListViewItem item in this.SelectedItems) { string path = item.SubItems[ClmnPathNumber].Text; JpnEncoding jEnc = JpnEncoding.NameToJpnEncoding(item.SubItems[ClmnCodeNumber].Text); PreviewForm form = new PreviewForm(path, jEnc, AzukiBefore, AzukiAfter, RegexEnabled, RegexOptions); if (OwnerForm != null) { form.Owner = this.OwnerForm; form.Icon = this.OwnerForm.Icon; form.ShowIcon = true; } form.Show(this); } } catch (Exception exception) { MessageBox.Show(exception.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public PreviewForm(string path, JpnEncoding encoding, AzukiTextBox azukiBefore, AzukiTextBox azukiAfter, bool regex = false, RegexOptions regexOptions = RegexOptions.None) { InitializeComponent(); this.Width = DefaultWidth; this.Height = DefaultHeight; this.Text = string.Format("{0} [{1}]", path, encoding.Name); this.azukiBefore.Font = azukiBefore.Font; this.azukiBefore.MarkerColor = azukiBefore.MarkerColor; this.azukiBefore.DrawingOption = azukiBefore.DrawingOption; this.azukiBefore.ColorScheme.SetColor(CharClass.Keyword, azukiBefore.ForeColor, Color.FromArgb(0xff, 0xff, 0x22)); OpenFile(path, encoding); if (!string.IsNullOrEmpty(azukiBefore.Text)) { this.azukiBefore.Document.Highlighter = new WordHighlighter(azukiBefore.Text, CharClass.Keyword, regex, regexOptions); } this.azukiAfter.Font = azukiAfter.Font; this.azukiAfter.MarkerColor = azukiAfter.MarkerColor; this.azukiAfter.DrawingOption = azukiAfter.DrawingOption; if (!string.IsNullOrEmpty(azukiBefore.Text)) { if (regex) { Regex re = new Regex(azukiBefore.Text, regexOptions); this.azukiAfter.Text = re.Replace(this.azukiBefore.Text, azukiAfter.Text); } else { this.azukiAfter.Text = this.azukiBefore.Text.Replace(azukiBefore.Text, azukiAfter.Text); } } else { this.azukiAfter.Text = this.azukiBefore.Text; } }
private void OpenFile(string path, JpnEncoding encoding) { try { JpnEncoding jEnc = encoding; if (jEnc == null) { jEnc = JpnEncoding.DetectEncoding(path, JpnEncoding.Shift_JIS); } using (StreamReader sr = new StreamReader(path, jEnc.Encoding)) { azukiBefore.Text = sr.ReadToEnd(); } azukiBefore.ScrollToCaret(); azukiBefore.Document.ClearHistory(); } catch (Exception) { throw; } }
/// <summary> /// Detect an encoding used in the specified text file. /// </summary> /// <param name="path"></param> /// <param name="defaultEnc"></param> /// <returns></returns> public static JpnEncoding DetectEncoding(string path, JpnEncoding defaultEnc) { StringBuilder strBldr = new StringBuilder(); int nEnc; try { byte[] bytes; using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); } // set the conversion option // see the 'nkf.doc' or http://manpages.ubuntu.com/manpages/natty/ja/man1/nkf.1.html // -g output the result of auto detection // -t do nothing SetNkfOption("-gt"); unsafe { fixed(byte *pbs = bytes) { char *pchar = (char *)pbs; NkfConvert(strBldr, pchar); // 3. convert } } nEnc = NkfGetKanjiCode(); // 7. get the encoding nEnc += 1; if (nEnc == nISO_2022_JP) // // ISO-2022-JP or ASCII { nEnc = -1; // suppose ASCII for (int i = 0; i < bytes.Length; i++) { if (bytes[i] == 0x1b) // ISO-2022-JP uses the character ESC (0x1B) { nEnc = nISO_2022_JP; // ISO-2022-JP break; } } } } catch (Exception) { return(null); } JpnEncoding jEnc; switch (nEnc) { case -1: // ASCII jEnc = JpnEncoding.Shift_JIS; break; case nShift_JIS: // Shift_JIS jEnc = JpnEncoding.Shift_JIS; break; case nEUC_JP: // EUC jEnc = JpnEncoding.EUC_JP; break; case nISO_2022_JP: // ISO_2022_JP jEnc = JpnEncoding.ISO_2022_JP; break; case nUTF_8: // UTF-8 jEnc = JpnEncoding.UTF_8; break; case nUTF_16LE: // UTF-16LE jEnc = JpnEncoding.UTF_16LE; break; case nUTF_16BE: // UTF-16BE jEnc = JpnEncoding.UTF_16BE; break; default: jEnc = defaultEnc; break; } return(jEnc); }