Here's what i have. Not sure what it is doing exactly.
You pack a account object and then you write it to the buffer right? So shouldn't it print after i write it?
Main
#include <iostream>
#include <iomanip>
#include "account.h"
#include "Lentext.h"
#include "Deltext.h"
using namespace std;
int main()
{
int result;
account a;
LengthTextBuffer b;
strcpy(a.account_Number,"1111111111");
strcpy(a.name, "Name");
strcpy(a.address, "Address");
strcpy(a.city, "City");
strcpy(a.state, "IL");
strcpy(a.zip_Code, "60192");
cout<<"person object's data\n";
a.Print(cout);
result=a.Pack(b);
cout<<"result after pack is"<<result;
b.Print(cout);
result=b.Write(cout);
cout<<"result after write is"<<result;
return 0;
}
[/CODE]
Account.h
[CODE]
#include <iostream>
#include "Lentext.h"
#include "Deltext.h"
#include <string>
using namespace std;
class account
{
public:
char account_Number[11];
char name[1000];
char address[1000];
char city[16];
char state[3];
char zip_Code[10];
char account_Balance[1000];
//default account constructor
account();
//set each field to a empty string
void Clear();
//methods for Lentext
int Pack(LengthTextBuffer&) const;
int UnPack(LengthTextBuffer&);
//methods for Deltext
int Pack(DelimTextBuffer&) const;
int UnPack(DelimTextBuffer&);
//print data
void Print(ostream &);
//intializes a DelimTextBuffer to be used for Persons
int InitBuffer(DelimTextBuffer &);
//intializes a LengthTextBuffer to be used for Persons
int InitBuffer(LengthTextBuffer &);
};
[/CODE]
Account.cpp
[CODE]
#include "Account.h"
#include <iostream>
#include <iomanip>
using namespace std;
account::account()
{
Clear();
}
void account::Clear()
{
account_Number[0]=0;
name[0]=0;
address[0]=0;
city[0]=0;
state[0]=0;
zip_Code[0]=0;
account_Balance[0]=0;
}
//pack the fields into a FixedTextBuffer
//return true if all succeed
int account::Pack(LengthTextBuffer & Buffer) const
{
int result;
Buffer.Clear();
result=Buffer.Pack(account_Number);
result=result&&Buffer.Pack(name);
result=result&&Buffer.Pack(address);
result=result&&Buffer.Pack(city);
result=result&&Buffer.Pack(state);
result=result&&Buffer.Pack(zip_Code);
result=result&&Buffer.Pack(account_Balance);
return result;
}
int account::UnPack(LengthTextBuffer & Buffer)
{
int result;
result=Buffer.Unpack(account_Number);
result=result&&Buffer.Unpack(name);
result=result&&Buffer.Unpack(address);
result=result&&Buffer.Unpack(city);
result=result&&Buffer.Unpack(state);
result=result&&Buffer.Unpack(zip_Code);
result=result&&Buffer.Unpack(account_Balance);
return result;
}
//pack the fields into a FixedTextBuffer
//return TRUE if all succeed, False o/w
int account::Pack(DelimTextBuffer & Buffer) const
{
int result;
Buffer.Clear();
result=Buffer.Pack(account_Number);
result=result&&Buffer.Pack(name);
result=result&&Buffer.Pack(address);
result=result&&Buffer.Pack(city);
result=result&&Buffer.Pack(state);
result=result&&Buffer.Pack(zip_Code);
result=result&&Buffer.Pack(account_Balance);
return result;
}
int account::UnPack(DelimTextBuffer & Buffer)
{
int result;
result=Buffer.Unpack(account_Number);
result=result&&Buffer.Unpack(name);
result=result&&Buffer.Unpack(address);
result=result&&Buffer.Unpack(city);
result=result&&Buffer.Unpack(state);
result=result&&Buffer.Unpack(zip_Code);
result=result&&Buffer.Unpack(account_Balance);
return result;
}
void account::Print(ostream & stream)
{
stream<<"Person:"
<<"\t accountnumber'"<<account_Number<<"\n"
<<"\t name'"<<name<<"\n"
<<"\t address'"<<address<<"\n"
<<"\t city'"<<city<<"\n"
<<"\t state'"<<state<<"\n"
<<"\t zipcode'"<<zip_Code<<"\n"
<<"\t account balance'"<<account_Balance<<"\n";
}
[/CODE]
Lentext.h
[CODE]
#ifndef CLASS_H_LENTEXT
#define CLASS_H_LENTEXT
#include <iostream>
using namespace std;
//a buffer which holds length based text fields
class LengthTextBuffer
{
public:
//construct with a maximum of maxFields
LengthTextBuffer(int maxBytes=1000);
//clear fields from buffer
void Clear();
int Read(istream &);
int Write(ostream &) const;
//set the value of the next field of the buffer
int Pack(const char *, short size=-1);
//extract the value of the next field of the buffer
int Unpack(char *);
void Print(ostream &) const;
void Init(int maxBytes=1000);
private:
//character array to hold field values
char * Buffer;
//size of packed fields
int BufferSize;
//maximum number of characters in the buffer
int MaxBytes;
//packing/unpacking position in buffer
int NextByte;
};
#endif
[/CODE]
Lentext.cpp
[CODE]
#include "Lentext.h"
#include <string.h>
//constructor with a maximum of maxFields
LengthTextBuffer::LengthTextBuffer(int maxBytes)
{
Init(maxBytes);
}
//clear fields from buffer
void LengthTextBuffer::Clear()
{
BufferSize=0;
MaxBytes=0;
NextByte=0;
}
int LengthTextBuffer::Read(istream & stream)
{
Clear();
stream.read((char*)&BufferSize, sizeof(BufferSize));
if(stream.fail())
{
return false;
}
//buffer overflow
if(BufferSize>MaxBytes)
{
return false;
}
stream.read(Buffer,BufferSize);
return stream.good();
}
int LengthTextBuffer::Write(ostream & stream) const
{
stream.write((char *)&BufferSize, sizeof(BufferSize));
stream.write(Buffer, BufferSize);
return stream.good();
}
//set the value of the next field of the buffer
//if size=-1(default) use strlen(str) as length of field
int LengthTextBuffer::Pack(const char * str, short size)
{
//length of string to be packed
size_t len;
if(size>=0)
{
len=size;
}
else
{
len=strlen(str);
}
//str is too short
if(len>strlen(str))
{
return false;
}
//first character to be packed
int start= NextByte;
NextByte += (len+sizeof(len));
if(NextByte>MaxBytes)
{
return false;
}
memcpy(&Buffer[start],&len,sizeof(len));
strncpy(&Buffer[start+sizeof(len)],str, len);
BufferSize=NextByte;
return true;
}
//extract the value of the next field of buffer
int LengthTextBuffer::Unpack(char * str)
{
//length of packed string
short len;
//no more fields
if(NextByte>=BufferSize)
{
return false;
}
//first character to be unpacked;
int start=NextByte;
memcpy(&len, &Buffer[start], sizeof(short));
NextByte += len +sizeof(short);
if(NextByte > BufferSize)
{
return false;
}
strncpy(str, &Buffer[start+sizeof(short)],len);
//zero termination for string
str[len]=0;
return true;
}
void LengthTextBuffer::Print(ostream & stream) const
{
stream<< "Buffer has characters"<<MaxBytes
<<" and Buffer Size " <<BufferSize<<endl;
}
//construct with a maximum of maxFields
void LengthTextBuffer::Init(int maxBytes)
{
if(maxBytes<0)
{
maxBytes=0;
}
MaxBytes=maxBytes;
Buffer=new char[MaxBytes];
Clear();
}
[/CODE]
Deltext.h
[CODE]
#ifndef CLASS_H_DELTEXT
#define CLASS_H_DELTEXT
#include <iostream>
using namespace std;
//a buffer which holds delimited text fields
class DelimTextBuffer
{
public:
//constructor with fields with delimeters
DelimTextBuffer(char Delim='|', int maxBytes=1000);
//clear fields from buffer
void Clear();
int Read(istream &);
int Write(ostream &) const;
int Pack(const char *,int size=-1);
int Unpack(char *);
void Print(ostream &) const;
void Init(char delim, int maxBytes=1000);
private:
char Delim;
//zero terminated string for Delim
char DelimStr[2];
//char array to hold field values
char * Buffer;
//size of packed fields
int BufferSize;
//maximum number of characters in the buffer
int MaxBytes;
//packing/unpacking position in buffer
int NextByte;
};
#endif
[/CODE]
Deltext.cpp
[CODE]
#include "Deltext.h"
#include <string.h>
//construct with a maximum number of maxFields
DelimTextBuffer::DelimTextBuffer(char Delim, int maxBytes)
{
Init(Delim,maxBytes);
}
//clear fields from buffer
void DelimTextBuffer::Clear()
{
BufferSize=0;
MaxBytes=0;
NextByte=0;
}
int DelimTextBuffer::Read(istream & stream)
{
Clear();
stream.read((char*)&BufferSize, sizeof(BufferSize));
if(stream.fail())
{
return false;
}
//buffer overflow
if(BufferSize>MaxBytes)
{
return false;
}
stream.read(Buffer,BufferSize);
return stream.good();
}
int DelimTextBuffer::Write(ostream & stream) const
{
stream.write((char *)&BufferSize, sizeof(BufferSize));
stream.write(Buffer, BufferSize);
return stream.good();
}
//set the value of the next field of the buffer
//if size=-1(default) use strlen(str) as Delim of field
int DelimTextBuffer::Pack(const char * str, int size )
{
//length of string to be packed
size_t len;
if(size>=0)
{
len=size;
}
else
{
len=strlen(str);
}
//str is too short
if(len>strlen(str))
{
return false;
}
//first character to be packed
int start=NextByte;
NextByte +=len + 1;
if(NextByte>MaxBytes)
{
return false;
}
memcpy(&Buffer[start],str, len);
//add delimeter
Buffer[start+len]=Delim;
BufferSize=NextByte;
return true;
}
//extract the value of next field of the buffer
int DelimTextBuffer::Unpack(char * str)
{
//length of packed string
int len=-1;
//first character to be unpacked
int start=NextByte;
for(int i=start; i<BufferSize; i++)
{
if(Buffer[i]==Delim)
{
len=i-start;
break;
}
}
//delimeter not found
if(len==-1)
{
return false;
}
NextByte +=len + 1;
if(NextByte>BufferSize)
{
return false;
}
strncpy(str, &Buffer[start], len);
//zero termination for string
str[len]=0;
return true;
}
void DelimTextBuffer::Print(ostream & stream) const
{
stream<< "Buffer has max characters" <<MaxBytes
<<" and Buffer Size "<<BufferSize<<endl;
}
void DelimTextBuffer::Init(char delim, int maxBytes)
{
Delim=delim;
DelimStr[0]= Delim;
DelimStr[1]=0;
if(maxBytes <0)
{
maxBytes=0;
}
MaxBytes=maxBytes;
Buffer= new char[MaxBytes];
BufferSize=0;
}