/// <summary>Performs an export from the command line</summary> public static void ExportToFile(StartupOptions startup_options) { string tmp_settings_path = Path.Combine(Path.GetTempPath(), "rylog_settings_" + Guid.NewGuid() + ".xml"); try { // Copy the settings to a tmp file so that we don't trash the normal settings if (Path_.FileExists(startup_options.SettingsPath)) { File.Copy(startup_options.SettingsPath, tmp_settings_path); } else { new Settings().Save(tmp_settings_path); } startup_options.SettingsPath = tmp_settings_path; // Load an instance of the app and the options. var m = new Main(startup_options); // Override settings passed on the command line if (startup_options.RowDelim != null) { m.Settings.RowDelimiter = startup_options.RowDelim; } if (startup_options.ColDelim != null) { m.Settings.ColDelimiter = startup_options.ColDelim; } if (startup_options.PatternSetFilepath != null) { // Specifying a pattern set implies the filters and transforms should be enabled m.Settings.Patterns = PatternSet.Load(startup_options.PatternSetFilepath); m.Settings.FiltersEnabled = true; m.Settings.TransformsEnabled = true; } // Do the export using (var outp = new StreamWriter(new FileStream(startup_options.ExportPath, FileMode.Create, FileAccess.Write, FileShare.Read))) { try { var d = new BLIData(m, new SingleFile(startup_options.FileToLoad)); using (d.file) { var rng = new[] { new RangeI(0, long.MaxValue) }; var row_delimiter = Misc.Robitise(m.Settings.RowDelimiter); var col_delimiter = Misc.Robitise(m.Settings.ColDelimiter); if (startup_options.NoGUI) { using (var done = new ManualResetEvent(false)) { ThreadPool.QueueUserWorkItem(x => { d.progress = (c, l) => true; DoExport(d, rng, row_delimiter, col_delimiter, outp); done.Set(); }); done.WaitOne(); if (!startup_options.Silent) { Console.WriteLine("Export completed successfully."); } } } else { if (m.DoExportWithProgress(d, rng, row_delimiter, col_delimiter, outp)) { if (!startup_options.Silent) { Console.WriteLine("Export completed successfully."); } } } } } catch (Exception ex) { Environment.ExitCode = 1; if (!startup_options.Silent) { Console.WriteLine($"Export failed.\r\n{ex.Message}"); } } } } finally { if (Path_.FileExists(tmp_settings_path)) { File.Delete(tmp_settings_path); } } }
/// <summary>Show the export dialog</summary> private void ShowExportDialog() { if (Src == null) { return; } // Determine the export file path var filepath = Settings.ExportFilepath; if (!filepath.HasValue()) { filepath = Path.ChangeExtension(Src.PsuedoFilepath, ".exported" + Path.GetExtension(Src.PsuedoFilepath)); } // Prompt for export settings var dlg = new ExportUI(filepath, Misc.Humanise(m_encoding.GetString(m_row_delim)), Misc.Humanise(m_encoding.GetString(m_col_delim)), FileByteRange); using (dlg) { if (dlg.ShowDialog(this) != DialogResult.OK) { return; } // Save the export filepath to the settings Settings.ExportFilepath = dlg.OutputFilepath; // Find the range to export IEnumerable <RangeI> rng; switch (dlg.RangeToExport) { default: throw new ArgumentOutOfRangeException(); case ExportUI.ERangeToExport.WholeFile: rng = new[] { FileByteRange }; break; case ExportUI.ERangeToExport.Selection: rng = SelectedRowRanges; break; case ExportUI.ERangeToExport.ByteRange: rng = new[] { dlg.ByteRange }; break; } // Delimiters var row_delimiter = Misc.Robitise(dlg.RowDelim); var col_delimiter = Misc.Robitise(dlg.ColDelim); // Do the export using (var outp = new StreamWriter(new FileStream(dlg.OutputFilepath, FileMode.Create, FileAccess.Write, FileShare.Read))) { try { var d = new BLIData(this, Src); if (DoExportWithProgress(d, rng, row_delimiter, col_delimiter, outp)) { MsgBox.Show(this, "Export completed successfully.", Application.ProductName, MessageBoxButtons.OK); } } catch (Exception ex) { Log.Write(ELogLevel.Error, ex, "Export failed"); MsgBox.Show(this, string.Format("Export failed.\r\n{0}", ex.Message), Application.ProductName, MessageBoxButtons.OK); } } } }