コード例 #1
0
ファイル: CsvImportForm.cs プロジェクト: Stoom/KeePass
        private bool GuessFieldTypes()
        {
            CsvOptions opt = GetCsvOptions();
            if(opt == null) { Debug.Assert(false); return false; }

            string strData = GetDecodedText();
            CsvStreamReaderEx csv = new CsvStreamReaderEx(strData, opt);

            string[] v;
            while(true)
            {
                v = csv.ReadLine();
                if(v == null) return false;
                if(v.Length == 0) continue;
                if((v.Length == 1) && (v[0].Length == 0)) continue;
                break;
            }
            if(v.Length <= 3) return false;

            CsvFieldInfo[] vFields = new CsvFieldInfo[v.Length];
            int nDetermined = 0;
            for(int i = 0; i < v.Length; ++i)
            {
                CsvFieldInfo fi = GuessFieldType(v[i]);
                if(fi != null) ++nDetermined;
                else fi = new CsvFieldInfo(CsvFieldType.Ignore, null, null);

                vFields[i] = fi;
            }

            // Accept the guesses only if at least half of them are
            // probably correct
            if(nDetermined < (v.Length + 1) / 2) return false;

            m_lvFields.Items.Clear();
            foreach(CsvFieldInfo fi in vFields)
                AddCsvField(fi.Type, fi.Name, fi.Format);

            return true;
        }
コード例 #2
0
ファイル: CsvImportForm.cs プロジェクト: Stoom/KeePass
        private void PerformImport(PwGroup pgStorage, bool bCreatePreview)
        {
            List<CsvFieldInfo> lFields = GetCsvFieldInfos();

            if(bCreatePreview)
            {
                int dx = m_lvImportPreview.ClientRectangle.Width; // Before clearing

                m_lvImportPreview.Items.Clear();
                m_lvImportPreview.Columns.Clear();

                foreach(CsvFieldInfo cfi in lFields)
                {
                    string strCol = CsvFieldToString(cfi.Type);
                    if(cfi.Type == CsvFieldType.CustomString)
                        strCol = (cfi.Name ?? string.Empty);
                    m_lvImportPreview.Columns.Add(strCol, dx / lFields.Count);
                }
            }

            CsvOptions opt = GetCsvOptions();
            if(opt == null) { Debug.Assert(bCreatePreview); return; }

            string strData = GetDecodedText();
            CsvStreamReaderEx csr = new CsvStreamReaderEx(strData, opt);

            Dictionary<string, PwGroup> dGroups = new Dictionary<string, PwGroup>();
            dGroups[string.Empty] = pgStorage;

            if(bCreatePreview) m_lvImportPreview.BeginUpdate();

            DateTime dtNow = DateTime.Now;
            DateTime dtNoExpire = KdbTime.NeverExpireTime.ToDateTime();
            bool bIgnoreFirstRow = m_cbIgnoreFirst.Checked;
            bool bIsFirstRow = true;
            bool bMergeGroups = m_cbMergeGroups.Checked;

            while(true)
            {
                string[] v = csr.ReadLine();
                if(v == null) break;
                if(v.Length == 0) continue;
                if((v.Length == 1) && (v[0].Length == 0)) continue;

                if(bIsFirstRow && bIgnoreFirstRow)
                {
                    bIsFirstRow = false;
                    continue;
                }
                bIsFirstRow = false;

                PwGroup pg = pgStorage;
                PwEntry pe = new PwEntry(true, true);

                ListViewItem lvi = null;
                for(int i = 0; i < Math.Min(v.Length, lFields.Count); ++i)
                {
                    string strField = v[i];
                    CsvFieldInfo cfi = lFields[i];

                    if(cfi.Type == CsvFieldType.Ignore) { }
                    else if(cfi.Type == CsvFieldType.Group)
                        pg = FindCreateGroup(strField, pgStorage, dGroups,
                            cfi.Format, opt, bMergeGroups);
                    else if(cfi.Type == CsvFieldType.Title)
                        ImportUtil.AppendToField(pe, PwDefs.TitleField,
                            strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.UserName)
                        ImportUtil.AppendToField(pe, PwDefs.UserNameField,
                            strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.Password)
                        ImportUtil.AppendToField(pe, PwDefs.PasswordField,
                            strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.Url)
                        ImportUtil.AppendToField(pe, PwDefs.UrlField,
                            strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.Notes)
                        ImportUtil.AppendToField(pe, PwDefs.NotesField,
                            strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.CustomString)
                        ImportUtil.AppendToField(pe, (string.IsNullOrEmpty(cfi.Name) ?
                            PwDefs.NotesField : cfi.Name), strField, m_pwDatabase);
                    else if(cfi.Type == CsvFieldType.CreationTime)
                        pe.CreationTime = ParseDateTime(ref strField, cfi, dtNow);
                    // else if(cfi.Type == CsvFieldType.LastAccessTime)
                    //	pe.LastAccessTime = ParseDateTime(ref strField, cfi, dtNow);
                    else if(cfi.Type == CsvFieldType.LastModTime)
                        pe.LastModificationTime = ParseDateTime(ref strField, cfi, dtNow);
                    else if(cfi.Type == CsvFieldType.ExpiryTime)
                    {
                        bool bParseSuccess;
                        pe.ExpiryTime = ParseDateTime(ref strField, cfi, dtNow,
                            out bParseSuccess);
                        pe.Expires = (bParseSuccess && (pe.ExpiryTime != dtNoExpire));
                    }
                    else if(cfi.Type == CsvFieldType.Tags)
                    {
                        List<string> lTags = StrUtil.StringToTags(strField);
                        foreach(string strTag in lTags)
                            pe.AddTag(strTag);
                    }
                    else { Debug.Assert(false); }

                    if(bCreatePreview)
                    {
                        strField = StrUtil.MultiToSingleLine(strField);

                        if(lvi != null) lvi.SubItems.Add(strField);
                        else lvi = m_lvImportPreview.Items.Add(strField);
                    }
                }

                if(bCreatePreview)
                {
                    // Create remaining subitems
                    for(int r = v.Length; r < lFields.Count; ++r)
                    {
                        if(lvi != null) lvi.SubItems.Add(string.Empty);
                        else lvi = m_lvImportPreview.Items.Add(string.Empty);
                    }
                }

                pg.AddEntry(pe, true);
            }

            if(bCreatePreview)
            {
                m_lvImportPreview.EndUpdate();
                ProcessResize();
            }
        }
コード例 #3
0
        public void Read(CsvStreamReaderEx csr)
        {
            if (csr == null)
            {
                Debug.Assert(false); return;
            }

            string[] vHeader;
            while (true)
            {
                vHeader = csr.ReadLine();
                if (vHeader == null)
                {
                    return;
                }
                if (vHeader.Length == 0)
                {
                    continue;
                }
                if ((vHeader.Length == 1) && (vHeader[0].Length == 0))
                {
                    continue;
                }
                break;
            }

            m_dColumns.Clear();

            CsvTableDataHandler <T>[] vHandlers = new CsvTableDataHandler <T> [vHeader.Length];
            for (int i = vHeader.Length - 1; i >= 0; --i)
            {
                string str = vHeader[i];

                m_dColumns[str] = i;

                CsvTableDataHandler <T> f;
                m_dHandlers.TryGetValue(str, out f);
                vHandlers[i] = f;
            }

            while (true)
            {
                string[] vRow = csr.ReadLine();
                if (vRow == null)
                {
                    break;
                }
                if (vRow.Length == 0)
                {
                    continue;
                }
                if ((vRow.Length == 1) && (vRow[0].Length == 0))
                {
                    continue;
                }

                T t = ((m_fObjectNew != null) ? m_fObjectNew(vRow) : default(T));

                Debug.Assert(vRow.Length == vHandlers.Length);
                int m = Math.Min(vRow.Length, vHandlers.Length);
                for (int i = 0; i < m; ++i)
                {
                    CsvTableDataHandler <T> f = vHandlers[i];
                    if (f != null)
                    {
                        f(vRow[i], t, vRow);
                    }
                }

                if (m_fObjectCommit != null)
                {
                    m_fObjectCommit(t, vRow);
                }
            }
        }