static public bool ExportPacketToCSV(PacketList PL, string TargetFileName)
        {
            if (!PL.IsPreParsed)
            {
                return(false);
            }

            List <string> FieldNames = new List <string>();

            void AddFieldName(string fName)
            {
                if (fName.StartsWith("?") || fName.StartsWith("_"))
                {
                    return;
                }
                if (!FieldNames.Contains(fName))
                {
                    FieldNames.Add(fName);
                }
            }

            int FieldIndex(string fName)
            {
                for (int i = 0; i < FieldNames.Count; i++)
                {
                    if (FieldNames[i] == fName)
                    {
                        return(i);
                    }
                }
                return(-1);
            }

            string FilteredString(string s)
            {
                string res = string.Empty;

                foreach (char c in s)
                {
                    if (((byte)c < 16) || ((byte)c > 192))
                    {
                        res += ".";
                    }
                    else
                    {
                        res += c;
                    }
                }
                return(res);
            }

            var packetCount = PL.Count();

            // Generate Table-Headers
            FieldNames.Clear();
            AddFieldName("Header-Time");
            for (int x = 0; x < packetCount; x++)
            {
                foreach (var pv in PL.GetPacket(x).PP.ParsedView)
                {
                    AddFieldName(pv.Var);
                }
            }

            if (FieldNames.Count > 100)
            {
                if (MessageBox.Show("The exported file will contain a large number of field columns (" + FieldNames.Count.ToString() + ")\r\n" +
                                    "Are you sure you want to continue ?", "Export CSV", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return(false);
                }
            }

            List <List <string> > csvString = new List <List <string> >();

            csvString.Add(FieldNames); // add header

            // Generate CSV data
            for (int x = 0; x < packetCount; x++)
            {
                var           thisPacket = PL.GetPacket(x);
                var           pvl        = thisPacket.PP.ParsedView;
                List <string> line       = new List <string>();
                foreach (var _ in FieldNames)
                {
                    line.Add("");
                }
                line[0] = thisPacket.TimeStamp.ToString();
                foreach (var pv in pvl)
                {
                    var fIndex = FieldIndex(pv.Var);
                    if (fIndex > 0)
                    {
                        line[fIndex] = FilteredString(pv.Data);
                    }
                }
                csvString.Add(line);
            }

            var lines = new List <string>();

            foreach (var lData in csvString)
            {
                string l = string.Empty;
                foreach (var f in lData)
                {
                    string FieldPart;
                    if (f.Contains("\"") || f.Contains(";") || f.Contains("'"))
                    {
                        FieldPart = "\"" + f.Replace("\"", "\\\"") + "\"";
                    }
                    else
                    {
                        FieldPart = f;
                    }
                    l += FieldPart + ";";
                }
                lines.Add(l);
            }
            File.WriteAllLines(TargetFileName, lines);

            return(true);
        }
예제 #2
0
        private void FillListBox(ListBox lb, PacketList pList, UInt16 GotTolastSync = 0)
        {
            int GotoIndex = -1;

            Application.UseWaitCursor = true;
            using (LoadingForm loadform = new LoadingForm(this))
            {
                try
                {
                    Random rand = new Random();
                    if (rand.Next(100) >= 95)
                    {
                        switch (rand.Next(5))
                        {
                        case 0:
                            loadform.Text = "That's a lot of data, please wait ...";
                            break;

                        case 1:
                            loadform.Text = "Burning circles, please wait ...";
                            break;

                        case 2:
                            loadform.Text = "I'm bored, please wait ...";
                            break;

                        case 3:
                            loadform.Text = "Camping NM, please wait ...";
                            break;

                        default:
                            loadform.Text = "Sacrificing Taru-Taru's, please wait ...";
                            break;
                        }
                    }
                    else
                    {
                        loadform.Text = "Populating Listbox, please wait ...";
                    }
                    loadform.Show();
                    loadform.pb.Minimum = 0;
                    loadform.pb.Maximum = pList.Count();
                    lb.Items.Clear();
                    for (int i = 0; i < pList.Count(); i++)
                    {
                        PacketData pd = pList.GetPacket(i);
                        switch (pd.PacketLogType)
                        {
                        case PacketLogTypes.Outgoing:
                            lb.Items.Add("=> " + pd.HeaderText);
                            break;

                        case PacketLogTypes.Incoming:
                            lb.Items.Add("<= " + pd.HeaderText);
                            break;

                        default:
                            lb.Items.Add("?? " + pd.HeaderText);
                            break;
                        }
                        if ((GotoIndex < 0) && (GotTolastSync > 0) && (pd.PacketSync == GotTolastSync))
                        {
                            GotoIndex = lb.Items.Count - 1;
                        }
                        loadform.pb.Value = i;
                        if ((i % 50) == 0)
                        {
                            loadform.pb.Refresh();
                        }
                    }
                    if (GotoIndex >= 0)
                    {
                        lb.SelectedIndex = GotoIndex;
                    }
                    loadform.Hide();
                }
                catch
                {
                }
            }
            Application.UseWaitCursor = false;
        }