Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version
Go to top

CooksMate

, 21 Jan 2008
A simple program to help get the timing of a roast dinner
cooksmate-bin-mono.zip
cooksmate-bin-mono
CooksMate
mono
cooksmate.exe
cooksmate.exe.mdb
cooksmate.sh
log4net.dll
uk.org.aspellclark.common.dll
uk.org.aspellclark.common.gui.dll
cooksmate-bin-net.zip
cooksmate-bin-net
CooksMate
bin
Debug
cooksmate.exe
cooksmate.log
log4net.dll
uk.org.aspellclark.common.dll
uk.org.aspellclark.common.gui.dll
cooksmate-src.zip
cooksmate-src
Common
common.build
Properties
src
main
csharp
NotImplementedDlg.resources
CommonFile
common.file.build
CommonGui
common.gui.build
CooksMate
cooksmate.build
cooksmate.sh
cooksmate_norm.png
DlgAction.resources
DlgEditRecipe.resources
MainForm.resources
libs
mono
log4net.dll
Mono.GetOptions.dll
sqlite-3.5.4.so
net
log4net.dll
Mono.GetOptions.dll
sqlite3.def
sqlite3.dll
/*
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 * User: andy
 * Date: 30/07/2006
 * Time: 08:37
 */

using System;
using System.IO;
using System.Collections;


namespace uk.org.aspellclark.common.file
{
    /// <summary>
    ///   <name>DelimitedFile</name>
    ///   <namespace>uk.org.aspellclark.common</namespace>
    ///   <version>1.0</version>
    ///   <author>Andy Aspell-Clark</author>
    ///   <description><p>This is a base class that handles all interaction with a delimited text file.
    ///     It can be sub classed to provide handling for more specific files.</p><p>
    ///     It contains member functions to open and close a file, to read in the first and subsequent
    ///     lines of the file as field lists as well as simple strings.</p>
    ///   </description>
    ///   <history>
    ///     <historyitem> 6 Aug 2004  1.0 ARA  Initial Version.</historyitem>
    ///   </history>
    /// </summary>
    public class DelimitedFile
    {
        /// <summary>
        /// the character used to delimit the fields in the file
        /// </summary>
        protected string m_sDelimiter;

        /// <summary>
        ///
        /// </summary>
        protected string m_sFilepath;

        /// <summary>
        /// the name of the file to read
        /// </summary>
        protected string m_sFilename;

        /// <summary>
        ///
        /// </summary>
        protected Int64 m_lFileSize;

        /// <summary>
        ///
        /// </summary>
        protected Int64 m_lPosition;

        /// <summary>
        ///
        /// </summary>
        protected FileInfo m_fiFileInfo;

        /// <summary>
        ///
        /// </summary>
        protected FileStream m_fsFileStream;

        /// <summary>
        ///
        /// </summary>
        protected StreamReader m_srTextStream;


        /// <summary>
        ///
        /// </summary>
        /// <param name="psDelimiter"></param>
        public DelimitedFile(string psDelimiter)
        {
            m_sDelimiter = psDelimiter;
        }

        /// <summary>
        /// opens the file ready for reading
        /// </summary>
        public void openFile()
        {
            m_srTextStream = new StreamReader(m_sFilepath + m_sFilename);
        }


        /// <summary>
        ///
        /// </summary>
        public void closeFile()
        {
            m_srTextStream.Close();
        }

        #region Properties

        /// <summary>
        /// returns the character used to delimit the fields in the file
        /// </summary>
        /// <returns></returns>
        public string Delimiter
        {
            get { return m_sDelimiter;  }
            set { m_sDelimiter = value; }
        }


        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string Filename
        {
            get { return m_sFilename;  }
            set { m_sFilename = value; }
        }


        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Int64 FileSize
        {
            get { return m_lFileSize;  }
            set { m_lFileSize = value; }
        }


        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Int64 CurrentPosition
        {
            get { return m_lPosition;  }
            set { m_lPosition = value; }
        }


        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public int NumberOfLines
        {
            get { return 0;  }
        }
        #endregion

        /// <summary>
        /// reads the first line of the file
        /// </summary>
        /// <returns>a list of the fields in the first line of the file</returns>
        public string readFirstTextLineFromFile()
        {
            string sFileLine = null;

            closeFile();
            openFile();
            sFileLine = m_srTextStream.ReadLine();
            if (sFileLine != null)
            {
                m_lPosition += sFileLine.Length + 2;
            }

            return sFileLine;
        }

        /// <summary>
        /// reads the first line of the file
        /// </summary>
        /// <returns>a list of the fields in the first line of the file</returns>
        public virtual ArrayList readFirstLineFromFile()
        {
            ArrayList alFields = null;

            String line = readFirstTextLineFromFile();
            alFields = convertLineToFields(line);

            return alFields;
        }


        /// <summary>
        /// reads the first line of the file
        /// </summary>
        /// <returns>a list of the fields in the first line of the file</returns>
        public string readNextTextLineFromFile()
        {
            string sFileLine = null;
            sFileLine = m_srTextStream.ReadLine();
            if (sFileLine != null)
            {
                m_lPosition += sFileLine.Length + 2;
                //OperationProgress_D.SetCurrentValue(m_lPosition);
            }
            return sFileLine;
        }


        /// <summary>
        ///
        /// </summary>
        /// <returns>a list of the fields in the next line of the file</returns>
        public virtual ArrayList readNextLineFromFile()
        {
            ArrayList alFields = null;

            String line = readNextTextLineFromFile();
            alFields = convertLineToFields(line);

            return alFields;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public ArrayList convertLineToFields(string line)
        {
            ArrayList alFields = null;
            bool bInsideQuote = false;
            if (line != null)
            {
                alFields = new ArrayList();
                while (line != "")
                {
                    if ((line[0] == '\"') && (bInsideQuote == false))
                    {
                        bInsideQuote = true;
                    }
                    int indexof = line.IndexOf(m_sDelimiter);
                    if (bInsideQuote == true)
                    {
                        int indexof2 = line.IndexOf('"',1);
                        if (indexof < indexof2)
                        {
                            indexof = line.IndexOf(m_sDelimiter, indexof2);
                        }
                        line = line.Substring(1, line.Length-1);
                    }
                    if (indexof == -1)
                    {
                        if (bInsideQuote)
                        {
                        	alFields.Add(line.Trim().Substring(0,line.Length-1));
                        }
                        else
                        {
                        	alFields.Add(line.Trim());
                        }
                        line="";
                    }
                    else
                    {
                        if (bInsideQuote == true)
                        {
                            alFields.Add(line.Substring(0, indexof-2).Trim());
                        }
                        else
                        {
                            alFields.Add(line.Substring(0, indexof).Trim());
                        }
                        if ( indexof == 0 )
                        {
                        	line = line.Substring(1);
                        }
                        else
                        {
                        	line = line.Substring(indexof);
                        }
                    }
                    bInsideQuote = false;
                }
            }
//            ArrayList alFields2 = new ArrayList();
//            foreach (string str in alFields)
//            {
//                if (str != "")
//                {
//                    if (str[str.Length-1] == '\"')
//                    {
//                        alFields2.Add(str.Substring(0, str.Length - 1));
//                    }
//                    else
//                    {
//                        alFields2.Add(str);
//                    }
//                }
//                else
//                {
//                    alFields2.Add(str);
//                }
//            }
            return alFields;
        }
        /// <summary>
        /// sets the character used to delimit the fields in the file
        /// </summary>
        /// <param name="aDelim"></param>
        public void setDelimiter(string aDelim)
        {
            m_sDelimiter = aDelim;
        }
        /// <summary>
        /// sets the name of the file to read
        /// </summary>
        /// <param name="aFilename"></param>
        public void setFilename(string aFilename)
        {
            int indexOf = aFilename.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
            if (indexOf == -1)
            {
                indexOf = aFilename.LastIndexOf(System.IO.Path.DirectorySeparatorChar);
            }
            m_sFilename = aFilename.Substring(indexOf+1);
            m_sFilepath = aFilename.Substring(0 , indexOf+1);
            m_fiFileInfo = new FileInfo(m_sFilepath + m_sFilename);
            m_lFileSize = m_fiFileInfo.Length;
            //OperationProgress_D.SetMaxValue(m_lFileSize);
        }

    }//class()
}//namespace()

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 The GNU General Public License (GPLv3)

Share

About the Author

Andy Aspell-Clark
Software Developer (Senior) Airbus Defense and Space
United Kingdom United Kingdom
No Biography provided
Follow on   Twitter   LinkedIn

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 21 Jan 2008
Article Copyright 2007 by Andy Aspell-Clark
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid