Ejemplo n.º 1
0
        private void ImportData(object sender, EventArgs e)
        {
#if DEBUG
            Console.WriteLine("Importing into file with {0} entries", EditedFile.Entries.Count);
#endif
            OpenFileDialog openDBFileDialog = new OpenFileDialog {
                InitialDirectory = Settings.Default.ImportExportDirectory,
                FileName         = Settings.Default.TsvFile(EditedFile.CurrentType.Name)
            };

            bool tryAgain = false;
            if (openDBFileDialog.ShowDialog() == DialogResult.OK)
            {
                Settings.Default.ImportExportDirectory = Path.GetDirectoryName(openDBFileDialog.FileName);
                do
                {
                    try {
                        DialogResult question = MessageBox.Show("Replace the current data?", "Replace data?",
                                                                MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                        if (question == DialogResult.Cancel)
                        {
                            return;
                        }
                        DBFile imported;
                        try {
                            using (var stream = new MemoryStream(File.ReadAllBytes(openDBFileDialog.FileName))) {
                                imported = new TextDbCodec().Decode(stream);
                            }
                            if (question == DialogResult.Yes)
                            {
                                EditedFile = imported;
                            }
                            else
                            {
                                EditedFile.Import(imported);
                            }
                        } catch (DBFileNotSupportedException exception) {
                            ShowDBFileNotSupportedMessage(exception.Message);
                        }

                        CurrentPackedFile.Data = (Codec.Encode(EditedFile));
                        Open();
                    } catch (Exception ex) {
                        tryAgain = (MessageBox.Show(string.Format("Import failed: {0}", ex.Message),
                                                    "Import failed",
                                                    MessageBoxButtons.RetryCancel)
                                    == DialogResult.Retry);
                    }
                } while (tryAgain);
            }
        }
Ejemplo n.º 2
0
        // test the tsv codec
        public void TestTsvExport(DBFile originalFile)
        {
            Tuple <string, int> tuple = new Tuple <string, int>(originalFile.CurrentType.Name, originalFile.Header.Version);
            DBFile reimport;

            try {
                // export to tsv
                TextDbCodec codec      = new TextDbCodec();
                string      exportPath = Path.Combine(Path.GetTempPath(), "exportTest.tsv");
#if DEBUG
                if (originalFile.CurrentType.Name.Equals(debug_at))
                {
                    Console.WriteLine("stop right here");
                }
#endif
                using (Stream filestream = File.Open(exportPath, FileMode.Create)) {
                    codec.Encode(filestream, originalFile);
                }
                // re-import
                using (Stream filestream = File.OpenRead(exportPath)) {
                    reimport = codec.Decode(filestream);
                    // check all read values against original ones
                    for (int row = 0; row < originalFile.Entries.Count; row++)
                    {
                        for (int column = 0; column < originalFile.CurrentType.Fields.Count; column++)
                        {
                            FieldInstance originalValue = originalFile[row, column];
                            FieldInstance reimportValue = reimport[row, column];
                            if (!originalValue.Equals(reimportValue))
                            {
                                tsvFails.Add(tuple);
                            }
                        }
                    }
                }
            } catch (Exception x) {
                Console.WriteLine(x);
                tsvFails.Add(tuple);
            }
        }