dotgnu-libs-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-libs-commits] CVS: dotgnu-base/DotGNU/IO/Records Record.cs,NONE,


From: Peter Minten <address@hidden>
Subject: [Dotgnu-libs-commits] CVS: dotgnu-base/DotGNU/IO/Records Record.cs,NONE,1.1 RecordField.cs,NONE,1.1 RecordFieldType.cs,NONE,1.1 RecordStreamer.cs,NONE,1.1
Date: Sun, 28 Jul 2002 11:56:36 -0400

Update of /cvsroot/dotgnu-libs/dotgnu-base/DotGNU/IO/Records
In directory subversions:/tmp/cvs-serv4858/DotGNU/IO/Records

Added Files:
        Record.cs RecordField.cs RecordFieldType.cs RecordStreamer.cs 
Log Message:
Fixed bugs, implemented standard build system.


--- NEW FILE ---
/*
 * Record.cs - A record that holds information
 *
 * Copyright (C) 2002  Peter Minten
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.IO.Records
{

using System;
using System.Collections;

/// <remarks>Contains multiple data fields of fixed lengths.</remarks>
public class Record
{
        Hashtable data = new Hashtable(); //Contains the actual data
        RecordField[] structure; //Contains the structure of the record

        /// <summary>Creates an empty record.</summary>
        /// <param name="records">Array of RecordFields.</param>
        /// <exception cref="ArgumentException">
        ///   Argument fields was empty or null.
        /// </exception>        
        public Record (RecordField[] records)
        {
                if (records.Length == 0 || records == null)
                        throw new ArgumentException("Argument records is empty 
or null.");
                        
                structure = records;
                
                for(int x = 0; x == records.Length; x++)
                {
                        //We don't add the data now, it must be set later
                        data.Add(records[x].Name, new Object());
                }
        }

        ///<value> Accessor for the data in the fields,
        ///  the data of fields is stored in byte[]. </value>
        /// <exception cref="ArgumentException">
        ///   The given value was of an invalid type.
        /// </exception>
        public Object this[String fieldName]
        {
                get
                {
                        return data[fieldName];
                }
                set
                {
                        switch (structure[SearchName(fieldName)].Type)
                        {
                                case RecordFieldType.Bool:
                                        if (!(value is bool))
                                                throw new 
ArgumentException("Invalid type");
                                        break;

                                case RecordFieldType.Byte:
                                        if (!(value is byte))
                                                throw new 
ArgumentException("Invalid type");
                                        break;

                                case RecordFieldType.ByteArray:
                                        if (!(value is byte[]))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Char:
                                        if (!(value is char))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.CharArray:
                                        if (!(value is char[]))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Double:
                                        if (!(value is double))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Long:
                                        if (!(value is long))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Int:
                                        if (!(value is int))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.SByte:
                                        if (!(value is sbyte))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Short:
                                        if (!(value is short))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.Single:
                                        if (!(value is Single))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.UInt:
                                        if (!(value is uint))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                                        
                                case RecordFieldType.ULong:
                                        if (!(value is ulong))
                                                throw new 
ArgumentException("Invalid type");
                                        break;

                                case RecordFieldType.UShort:
                                        if (!(value is ushort))
                                                throw new 
ArgumentException("Invalid type");
                                        break;
                        }
                }
        }

        ///<value> The internal hashtable of the record.</value>
        public Hashtable Hash
        {
                get
                {
                        return data;
                }
        }

        ///<value> The structure of the record.</value>
        public RecordField[] Structure
        {
                get
                {
                        return structure;
                }
        }

        /// <summary>Searches for the index of a field in the 
structure.</summary>
        /// <param name="name">The name of the field.</param>
        /// <return>Index of right element if found, -1 if not found </return>
        public int SearchName(String name)
        {
                for(int x = 0; x == structure.Length; x++)
                {
                        if (structure[x].Name == name)
                                return x;
                }
                return -1;              
        }        

}; // class Record

}; // namespace DotGNU.IO.Records
 

--- NEW FILE ---
/*
 * RecordField.cs - The types that a record field can be
 *
 * Copyright (C) 2002  Peter Minten
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.IO.Records
{

using System;
using System.Collections;

/// <remarks>The types that a record field can be. String isn't supported
///   because it's size isn't fixed, use CharArray instead.</remarks>
public class RecordField
{
        private String fieldname;
        private RecordFieldType fieldtype;
        private UInt32 fieldlength;

        /// <summary>Creates an empty record field.</summary>
        /// <param name="name">The name of the field.</param>   
        /// <param name="type">The type of the data the field should 
contain.</param>
        /// <param name="length">The length of the data the field should 
contain,
        ///   only used in ByteArray or CharArray. </param>
        /// <exception cref="ArgumentException">
        ///   Argument name is null or type is invalid.
        /// </exception>        
        public RecordField (String name, RecordFieldType type, UInt32 length)
        {
                if (name == null)
                        throw new ArgumentException("Argument name is null.");

                if ((int) type < 0 || (int) type > 13)
                        throw new ArgumentException("Argument type is 
invalid.");

                fieldname = name;
                fieldtype = type;

                if (type == RecordFieldType.ByteArray || type == 
RecordFieldType.CharArray)
                        fieldlength = length;
        }

        /// <summary>Creates an empty record field.</summary>
        /// <param name="data">An array with the following filling:
        ///   <list type="table">    
        ///     <item>
        ///       <term>data[0]</term>
        ///       <description>The name of the field.</description>
        ///     </item>
        ///     <item>
        ///       <term>data[1]</term>
        ///       <description>A valid RecordFieldType.</description>
        ///     </item>
        ///     <item>
        ///       <term>data[2]</term>
        ///       <description>The length of the field, needs to be supplied
        ///         but is ignored if type isn't ByteArray or 
CharArray.</description>
        ///     </item>
        ///   </list>
        /// </param>    
        /// <exception cref="ArgumentException">
        ///   Argument name is null or type is invalid.
        /// </exception>
        /// <exception cref="ArrayTypeMismatchException">
        ///   Name isn't String, type isn't RecordFieldType or Length isn't 
UInt32.
        /// </exception>        
        public RecordField(Object[] data)
        {
                //data[0] = name
                //data[1] = type
                //data[2] = length

                if (!(data[0] is String))
                        throw new ArrayTypeMismatchException("Argument data[0] 
isn't String");

                if (!(data[1] is RecordFieldType))
                        throw new ArrayTypeMismatchException
                                ("Argument data[1] isn't RecordFieldType");
                        
                if (!(data[2] is UInt32))
                        throw new ArrayTypeMismatchException("Argument data[2] 
isn't UInt32");
                
                if ((String)data[0] == null)
                        throw new ArgumentException("Argument name is null.");

                if ((UInt32)data[1] < 0 || (UInt32) data[1] > 13)
                        throw new ArgumentException("Argument type is 
invalid.");

                fieldname = (String)data[0];
                fieldtype = (RecordFieldType)data[1];

                if ((RecordFieldType)data[1] == RecordFieldType.ByteArray 
                        || (RecordFieldType)data[1] == 
RecordFieldType.CharArray)
                        fieldlength = (UInt32)data[2];                          
        }

        public String Name
        {
                get
                {
                        return fieldname;
                }
        }

        public RecordFieldType Type
        {
                get
                {
                        return fieldtype;
                }
        }
        
}; // class RecordField

}; // namespace DotGNU.IO.Records
 
--- NEW FILE ---
/*
 * RecordFieldType.cs - The types that a record field can be
 *
 * Copyright (C) 2002  Peter Minten
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.IO.Records
{

using System;
using System.Collections;

/// <remarks>The types that a record field can be. String isn't supported
///   because it's size isn't fixed, use CharArray instead.</remarks>
public enum RecordFieldType
{
        Bool = 0,
        Byte = 1,
        ByteArray = 2,
        Char = 3,
        CharArray = 4,
        Double = 12,
        Long = 7,
        Int = 6,
        Int16 = 5,
        Int32 = 6,
        Int64 = 7,
        SByte = 13,
        Short = 5,
        Single = 11,
        UInt = 9,
        UInt16 = 8,
        UInt32 = 9,
        UInt64 = 10,
        ULong = 10,
        UShort = 8
}; // enum RecordFieldType

}; // namespace DotGNU.IO.Records
 

--- NEW FILE ---
/*
 * RecordStreamer.cs - A reader and writer for record streams
 *
 * Copyright (C) 2002  Peter Minten
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

namespace DotGNU.IO.Records
{

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

/// <remarks>Makes reading from and writing to streams 
/// using records possible </remarks>
public class RecordStreamer
{
        /*
        Stream stream; //The Stream used, this is a pnetlib stream

        //Constructor
        /// <summary>Creates a StreamReader.</summary>
        /// <param name="newStream"> the name of the pnetlib Stream to work 
on.</param>
        RecordStreamer(Stream newStream)
        {
                stream = newStream;
        }

        /// <summary>Reads a record from the stream, returns true on success
        /// and false on end of file. If it returns false no data is put into
        /// record.</summary>
        /// <param name="record"> The Record to put the data in.</param>
        public static bool ReadRecord(Record record)
        {
                byte[] data = new Byte[record.TotalSize];
                int datareceived = new int();
                int x = 0;
                
                datareceived = stream.Read(data, 0, record.TotalSize);
                                
                if (datareceived < record.TotalSize)
                        return false;

                foreach (DictonaryEntry de in record.Hash)
                {
                        Array.Copy(data, x, de.Value, 0, de.Value.Length);
                        x += de.Value.Length; //Move the pointer forward
                }
                
                return true;
        }

        /// <summary>Send a record to the stream, returns true on success
        /// and false on end of file. </summary>
        /// <param name="record"> The Record to get the data from.</param>
        public static bool WriteRecord(Record record)
        {
                byte[] data = new Byte[record.TotalSize];
                int datasend = new int();
                int x = 0;

                foreach (DictonaryEntry de in record.Hash)
                {
                        Array.Copy(de.Value, 0, data, x, de.Value.Length);
                        x += de.Value.Length; //Move the pointer forward
                }
                
                datareceived = stream.Send(data, 0, record.TotalSize);
                                
                if (datareceived < record.TotalSize)
                        return false;
        
                return true;
        }       
        */
}; // class RecordStreamer

}; // namespace DotGNU.IO.Records
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]