Click here to Skip to main content
15,896,912 members
Articles / Programming Languages / C#

Persistent String Parser

Rate me:
Please Sign up or sign in to vote.
4.33/5 (18 votes)
21 Dec 2008Public Domain4 min read 77.6K   384   38  
Parse a string with quoted elements, insert/add/delete elements, and is CLS compliant
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QStringParser;

namespace ParserTest
{
    public partial class Form1 : Form
    {
        CQStringParser parser;

        public Form1()
        {
            InitializeComponent();
            parser = new CQStringParser();
            IDC_EDIT_PARSE_STRING.Text = "1,2,3,4";
            IDC_EDIT_DELIMITER.Text = ",";
            IDC_RADIO_DELFIRST.Checked = true;
            Parse();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        //--------------------------------------------------------------------------------
        // Just a little function to reduce typing. Shows the specified message.          
        //--------------------------------------------------------------------------------
        private void ShowMessage(string sMsg)
        {
            System.Windows.Forms.MessageBox.Show(sMsg);
        }

        private void UpdateParsingString()
        {
            string sTemp = parser.OriginalString;
            IDC_EDIT_PARSE_STRING.Text = sTemp;
        }

        //--------------------------------------------------------------------------------
        // call the parser's parsing routie and reset form fields.                        
        //--------------------------------------------------------------------------------
        private void Parse()
        {
            if (parser != null)
            {
                string sVal = IDC_EDIT_PARSE_STRING.Text;
                string sDelimiter = IDC_EDIT_DELIMITER.Text;
                string sQuote = IDEC_EDIT_QUOTE_CHAR.Text;
                char cQuote = (sQuote.Length == 0 || !IDC_CHECK_USE_QUOTES.Checked) ? '\0' : sQuote[0];
                parser.ResetOriginalString(sVal, sDelimiter, cQuote);
                IDC_LISTBOX.Items.Clear();
                int nCount = parser.Count;
                if (nCount > 0)
                {
                    string sTemp = "";
                    for (int i = 0; i < nCount; i++)
                    {
                        sTemp = parser.GetField(i);
                        if (sTemp == "")
                        {
                            sTemp = "<NO DATA>";
                        }
                        sTemp = "Index " + i.ToString() + "\t\t" + sTemp;
                        IDC_LISTBOX.Items.Add(sTemp);
                    }
                }
            }
            else
            {
                ShowMessage("ERROR - parser object is null");
            }
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_DONE_Click(object sender, EventArgs e)
        {
            DialogResult dResult = DialogResult.OK;
            Close();
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_PARSE_Click(object sender, EventArgs e)
        {
            Parse();
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_INSERTGO_Click(object sender, EventArgs e)
        {
            string sInsert = IDC_EDIT_INSERT.Text;
            int    nIndex  = Convert.ToInt32(IDC_EDIT_POS.Text);
            parser.InsertField(nIndex, sInsert);
            UpdateParsingString();
            Parse();
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_ADDGO_Click(object sender, EventArgs e)
        {
            string sAdd = IDC_EDIT_ADD.Text;
            parser.AddField(sAdd);
            UpdateParsingString();
            Parse();
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_DELINDEX_GO_Click(object sender, EventArgs e)
        {
            int nIndex = Convert.ToInt32(IDC_EDIT_DELINDEX.Text);
            bool bSuccess = parser.DeleteField(nIndex);
            if (!bSuccess)
            {
                ShowMessage("ERROR - index not deleted (value out of range?)");
            }
            else
            {
                UpdateParsingString();
                Parse();
            }
        }

        //--------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------
        private void IDC_BTN_DELSTRING_GO_Click(object sender, EventArgs e)
        {
            string sVal = IDC_EDIT_DELSTRING.Text;
            bool bExact = IDC_CHECK_EXACTMATCH.Checked;
            CQStringParser.eDeleteAction nAction = CQStringParser.eDeleteAction.SP_DELETE_FIRST;
            if (IDC_RADIO_DELALL.Checked) nAction = CQStringParser.eDeleteAction.SP_DELETE_ALL;
            if (IDC_RADIO_DELFIRST.Checked) nAction = CQStringParser.eDeleteAction.SP_DELETE_FIRST;
            if (IDC_RADIO_DELLAST.Checked) nAction = CQStringParser.eDeleteAction.SP_DELETE_LAST;
            bool bSuccess = parser.DeleteField(sVal, bExact, nAction);
            if (!bSuccess)
            {
                ShowMessage("ERROR - string not found");
            }
            else
            {
                UpdateParsingString();
                Parse();
            }
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions