Click here to Skip to main content
15,896,726 members
Articles / Mobile Apps

Generate PDB files in C#

Rate me:
Please Sign up or sign in to vote.
4.20/5 (13 votes)
10 Jan 2006GPL32 min read 154.7K   1.6K   30  
This article shows how to create a .pdb file to use as database on Palm devices.
/* 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)


Written By
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......

Comments and Discussions