Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version

Generate PDB files in C#

, 10 Jan 2006 GPL3
This article shows how to create a .pdb file to use as database on Palm devices.
csv_and_xml_sample.zip
Test.csv
writepdbs_demo.zip
WritePDBs.exe
writepdbs_source.zip
WritePDBs
App.ico
bin
obj
WritePDBs.csproj.user
/* Pilot Database Creator Classes
 * Developed By: Muhammad Rizwan Sumra
 * Date: December 20,2005
 * Feel free to contact for any Bug or any problem at rsumra@hotmail.com
 */
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
namespace WritePDBs
{
	#region Main Class
	class PDBCreators
	{
		PDBHeader header=new PDBHeader();
		ArrayList pdbRecord=new ArrayList();
		public PDBCreators()
		{
		}
		public PDBHeader PDBHeaders
		{
			set
			{
				header=value;
			}
			get
			{
				return header;
			}
		}
		public Object[] AddRecord
		{
			set
			{
				string row="";
				for (int i=0; i<value.Length; i++)
				{
					if (value[i].GetType().ToString()=="System.Int32")
						continue;
					row+=value[i].ToString().Trim()+'\0';
				}
				pdbRecord.Add(new PDBRecordList(row));
			}
		}

		public void GeneratePDB(string fileName)
		{
			try
			{
				FileStream fs=new FileStream(fileName,FileMode.Create);
				BinaryWriter bw=new BinaryWriter(fs);
				int discard=0;
				UInt32 index=78;	 // Header length is 78 Bytes
				header.NumRecords=(UInt16)pdbRecord.Count;

				bw.Write(HexEncoding.GetBytes(header.ToString(),out discard));

				index+=(UInt32)(pdbRecord.Count*8)+2; // start index of Data List Area
				string result="";
				for (int i=0; i<pdbRecord.Count; i++)
				{
					PDBRecordList rec=(PDBRecordList)pdbRecord[i];
					rec.RecordOffset=index;
					bw.Write(HexEncoding.GetBytes(rec.ToString(),out discard));
					index+=(UInt32)rec.DataRecord.Length;
					result+=rec.DataRecord;
				}
				char[] padding={'\0','\0'};
				bw.Write(HexEncoding.GetBytes(HexEncoding.GetStringToChar(padding,2),out discard));
				result=HexEncoding.GetStringToChar(result.ToCharArray(),result.Length);
				bw.Write(HexEncoding.GetBytes(result,out discard));
				bw.Close();
				fs.Close();
			}
			catch (Exception exc)
			{
				throw new PDBException("Error in writing pdb file "+exc.Message);
			}

		}	
	}
	#endregion 

	#region Other Classes
	#region Eception Class
	class PDBException :Exception
	{
		string strValue;
		public PDBException(string str)
		{
			strValue=str;
		}
		public override string ToString()
		{
			return strValue;
		}
	}
	#endregion 

	class PDBRecordList
	{
		#region Declarations
		UInt32 offset=0;					   // 4 Bytes
		byte recordAttribute=0X40;
		char[] uniqueID={'\0','\0','\0'};  // 3 Bytes
		string dataRecord="";
		#endregion 

		#region Properties
		public string DataRecord
		{
			set
			{
				dataRecord=value;
			}
			get
			{
				return dataRecord;
			}
		}
		public UInt32 RecordOffset
		{
			set
			{
				offset=value;
			}
			get
			{
				return offset;
			}
		}
		#endregion 

		#region Functions
		public PDBRecordList(string str)
		{
			dataRecord=str;
		}
		public override string ToString()
		{

			string getAll="";
			getAll+=offset.ToString("X8");
			getAll+=recordAttribute.ToString("X2");		
			getAll+=HexEncoding.GetStringToChar(uniqueID,3);
			return getAll;
		}

		#endregion 
	}
	class PDBHeader
	{
		#region Declarations
		char[] databaseName={'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
							'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
							'\0','\0'};					// 32 Bytes

		UInt16 fileAttributes = 0X0008;			// 2 Bytes
		UInt16 version = 0X0000;					// 2 Bytes
		UInt32 creationDate;						// 4 Bytes
		UInt32 modificationDate;					// 4 Bytes
		UInt32 lastBackupDate;					// 4 Bytes
		UInt32 modification=0X0000;				// 4 Bytes
		UInt32 appInfoArea=0X0000;				// 4 Bytes
		UInt32 sortInfoArea=0X0000;				// 4 Bytes
		char[]  type={'d','a','t','a'};					// 4 Bytes
		char[]  creatorID;						// 4 Bytes
		UInt32 uniqueID=0X0000;					// 4 Bytes
		UInt32 nextRecord=0X0000;				// 4 Bytes
		UInt16 numRecords;						// 2 Bytes
		#endregion 

		#region Properties
		public UInt16 NumRecords
		{
			set
			{
				numRecords=value;
			}
			get
			{
				return numRecords;
			}
		}
		public DateTime CreationDate
		{
			set
			{
				TimeSpan ts=DateTime.Now-value;
				creationDate=(UInt32)ts.TotalSeconds;
			}
		}
		public DateTime ModificationDate
		{
			set
			{
				TimeSpan ts=DateTime.Now-value;
				modificationDate=(UInt32)ts.TotalSeconds;
			}
		}
		public DateTime LastBackupDate
		{
			set
			{
				TimeSpan ts=DateTime.Now-value;
				lastBackupDate=(UInt32)ts.TotalSeconds;
			}
		}

		public string CreatorID
		{
			set
			{
				if (value.Length==4)
				{
					creatorID=value.ToCharArray();
				}
				else
				{
					throw new PDBException("Creator ID is invalid (must be 4 charactors)");;
				}
			}
			get
			{
				return new String(creatorID);
			}
		}

		public string DataBaseName
		{
			set
			{
				if (value.Length<=32)
				{
					databaseName=value.ToCharArray();
				}
				else
				{
					
					throw new PDBException("Database Name Exceeds 32 Charactors");;
				}
			}
			get
			{
				return new String(databaseName);
			}
		}

		#endregion 

		#region Functions

		public PDBHeader()
		{
			TimeSpan ts=DateTime.Now-DateTime.Parse("01-01-1904");
			creationDate=(UInt32)ts.TotalSeconds;
			modificationDate=(UInt32)ts.TotalSeconds;
			lastBackupDate=(UInt32)ts.TotalSeconds;
			numRecords=0;
		}

		public override string ToString()
		{
			string getAll="";
			getAll+=HexEncoding.GetStringToChar(databaseName,32);
			getAll+=fileAttributes.ToString("X4");
			getAll+=version.ToString("X4");
			getAll+=creationDate.ToString("X8");
			getAll+=modificationDate.ToString("X8");
			
			
			getAll+=lastBackupDate.ToString("X8");		
			getAll+=modification.ToString("X8");
			getAll+=appInfoArea.ToString("X8");	
			getAll+=sortInfoArea.ToString("X8");
			getAll+=HexEncoding.GetStringToChar(type,4);
			getAll+=HexEncoding.GetStringToChar(creatorID,4);
			getAll+=uniqueID.ToString("X8");
			getAll+=nextRecord.ToString("X8");
			getAll+=numRecords.ToString("X4");

			return getAll;
		}

		#endregion 
	}
	#endregion 
}

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

MRSumra
Web Developer
Pakistan Pakistan
I'm working as Software Engineer at ISF (Islamabad Software Factory) in Pakistan and involves in multiple products having different languages in different phases. My favourite areas are PDA's, Palm's, .Net and webservices......

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.1 | Last Updated 10 Jan 2006
Article Copyright 2006 by MRSumra
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid