Converter converter = null;// 実行中でなければnull private void convertWorker_DoWork(object sender, DoWorkEventArgs e) { Properties.Settings.SetCurrentLanguage(); try { Directory.CreateDirectory(Path.GetTempPath()); } catch(Exception) { MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFOLDER, Path.GetTempPath())); return; } if(FirstFiles != null) { for(int i = 0 ; i < FirstFiles.Count / 2 ; ++i) { string file = FirstFiles[2 * i]; string workdir; if (Properties.Settings.Default.workingDirectory == "file") workdir = Path.GetDirectoryName(file); else if (Properties.Settings.Default.workingDirectory == "current") workdir = Directory.GetCurrentDirectory(); else workdir = Path.GetTempPath(); string tmppath; try { string inputextension = Path.GetExtension(file); tmppath = Path.Combine(workdir,TempFilesDeleter.GetTempFileName(inputextension)); File.Delete(tmppath); File.Copy(file, tmppath, true); } catch(Exception) { MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, workdir)); continue; } var output = Path.GetFullPath(FirstFiles[2 * i + 1]); converter = new Converter(this, tmppath, output); converter.Convert(); } FirstFiles = null; converter = null; return; } try { string outputFilePath = outputFileNameTextBox.Text; if(InputFromFileRadioButton.Checked) { string inputTeXFilePath = inputFileNameTextBox.Text; if(!File.Exists(inputTeXFilePath)) { MessageBox.Show(String.Format(Properties.Resources.NOTEXIST, inputTeXFilePath), Properties.Resources.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } string extension = Path.GetExtension(outputFilePath).ToLower(); string tmpTeXFileName; string tmpDir; if (InputFromFileRadioButton.Checked) { if (Properties.Settings.Default.workingDirectory == "file") tmpDir = Path.GetDirectoryName(inputFileNameTextBox.Text); else if (Properties.Settings.Default.workingDirectory == "current") tmpDir = Directory.GetCurrentDirectory(); else tmpDir = Path.GetTempPath(); tmpTeXFileName = TempFilesDeleter.GetTempFileName(Path.GetExtension(inputFileNameTextBox.Text), tmpDir); }else{ if (Properties.Settings.Default.workingDirectory == "current") tmpDir = Directory.GetCurrentDirectory(); else tmpDir = Path.GetTempPath(); tmpTeXFileName = TempFilesDeleter.GetTempFileName(); } if(tmpTeXFileName == null) { MessageBox.Show(String.Format(Properties.Resources.FAIL_TMPFILE, tmpDir)); return; } string tmpFileBaseName = Path.GetFileNameWithoutExtension(tmpTeXFileName); using(converter = new Converter(this, Path.Combine(tmpDir, tmpTeXFileName), outputFileNameTextBox.Text)) { if(!converter.CheckFormat()) return; if(!converter.CheckInputFormat()) return; #region TeX ソースファイルの準備 // 外部ファイルから入力する場合はテンポラリディレクトリにコピー if(InputFromFileRadioButton.Checked) { string inputTeXFilePath = inputFileNameTextBox.Text; string tmpfile = Path.Combine(tmpDir, tmpTeXFileName); File.Copy(inputTeXFilePath, tmpfile, true); // 読み取り専用の場合解除しておく(後でFile.Deleteに失敗するため)。 (new FileInfo(tmpfile)).Attributes = FileAttributes.Normal; converter.AddInputPath(Path.GetDirectoryName(inputTeXFilePath)); } // 直接入力の場合 tex ソースを出力 if(InputFromTextboxRadioButton.Checked) { using(StreamWriter sw = new StreamWriter(Path.Combine(tmpDir, tmpTeXFileName), false, Converter.GetInputEncoding())) { try { TeXSource.WriteTeXSourceFile(sw, myPreambleForm.PreambleTextBox.Text, sourceTextBox.Text); } catch { } } } #endregion if(converter.Convert()) { if(Properties.Settings.Default.setFileToClipBoard) { if(converter.OutputFileNames.Count > 0) { Invoke(new Action(() => { var flist = new System.Collections.Specialized.StringCollection(); foreach(var f in converter.OutputFileNames) flist.Add(f); Clipboard.SetFileDropList(flist); })); } } } } } finally { converter = null; } }
static int TeX2imgMain(List<string> cmds) { Properties.Settings.Default.SaveSettings = !nogui; // オプション解析 List<string> files; var options = GetOptiontSet(); try { files = options.Parse(cmds); } catch (Mono.Options.OptionException e) { if (e.OptionName != null) { var msg = String.Format(Properties.Resources.INVALID_INPUT_TO_OPTION, e.OptionName); if (e.Message != "") msg += " : " + e.Message; msg += "\n" + String.Format(Properties.Resources.SEEHELPMSG, "TeX2img" + (nogui ? "c" : "") + ".exe /help"); if (nogui) Console.WriteLine(msg); else MessageBox.Show(msg, "TeX2img"); } return -1; } // 各種バイナリのパスが設定されていなかったら推測する。 // "/exit"が指定されている場合はメッセージ表示をしない。 setPath(exit); if (help) { ShowHelp(); return 0; } if (version) { ShowVersion(); return 0; } // CUIモードの引数なしはエラー if (nogui && files.Count == 0) { Console.WriteLine(Properties.Resources.NOARGUMENT + "\n"); ShowHelp(); return -1; } if (preview != null) Properties.Settings.Default.previewFlag = (bool)preview; //Console.WriteLine(preview == null ? "null" : preview.ToString()); // すぐに終了 if (exit) { Properties.Settings.Default.Save(); return 0; } // filesのチェック string err = ""; for (int i = 0; i < files.Count / 2; ++i) { var chkconv = new Converter(null, files[2 * i], files[2 * i + 1]); if (!chkconv.CheckInputFormat()) { err += String.Format(Properties.Resources.INVALID_EXTENSION, files[2 * i]) + "\n"; } if (!File.Exists(files[2 * i])) { err += String.Format(Properties.Resources.NOTEXIST, files[2 * i]); if (files[2 * i].StartsWith("-") || files[2 * i].StartsWith("/")) err += Properties.Resources.MAY_BE_OPTION; err += "\n"; } if (!chkconv.CheckFormat()) { err += String.Format(Properties.Resources.INVALID_EXTENSION, files[2 * i + 1]) + "\n"; } } if (files.Count % 2 != 0) { err += String.Format(Properties.Resources.NOOUTPUT_FILE, files[files.Count - 1]); if (files[files.Count - 1].StartsWith("-") || files[files.Count - 1].StartsWith("/")) err += Properties.Resources.MAY_BE_OPTION; err += "\n"; } if (err != "") { err = err.Remove(err.Length - 1);// 最後の改行を削除 if (nogui) Console.WriteLine(err); else MessageBox.Show(err, "TeX2img"); return -2; } if (nogui) { // CUIでオプション指定がないときは,設定によらずプレビューをしないようにする。 if (preview == null) { preview = Properties.Settings.Default.previewFlag; Properties.Settings.Default.previewFlag = false; } int r = CUIExec(quiet, files); Properties.Settings.Default.previewFlag = (bool)preview; return r; } else { Application.Run(new MainForm(files)); return 0; } }