//********************************************************************* // Handles fixed size messages // Returns strings through reference parameters because of C++'s // policy on array parameters // I would prefer to return as value of function for GetTopic and GetText // but then strings would have to be a first class object // we will deal with this in later implementations // AUTHOR: Chris Wild // DATE: July 25, 1997 //********************************************************************* #include "message.h" #include #include CMessage::CMessage() { ZeroMemory(topic,sizeof(topic)); // I like clean memoery ZeroMemory(text, sizeof(text)); } // Initializes message from already open file CMessage::CMessage(HANDLE fileHandle) { DWORD nBytes; ReadFile(fileHandle,topic,SIZE_TOPIC+1,&nBytes,0); ReadFile(fileHandle,text,SIZE_TEXT+1,&nBytes,0); } CMessage::CMessage(const CMessage& msg) { strncpy(topic,msg.topic,SIZE_TOPIC+1); strncpy(text,msg.text,SIZE_TEXT+1); } CMessage::~CMessage() { // nothing to do } // Sets the message topic //POST: Topic of this message is set to Cstring // I don't assume that the input string "thisTopic" is the // correct length. void CMessage::SetTopic(const char* thisTopic) { int size = (strlen(thisTopic)>SIZE_TOPIC?SIZE_TOPIC:strlen(thisTopic)); strncpy(topic,thisTopic,size); topic[size] = '\0'; // force null terminated string } // Gets the message topic // PRE: thisTopic is big enough (maximum SIZE_TOPIC+1 characters) void CMessage::GetTopic(char* thisTopic) const { strncpy(thisTopic,topic,strlen(topic)+1); } // Sets text of message // I don't assume that the input string "thisMessage" is the // correct length. void CMessage::SetText(const char* thisMsg) { int size = (strlen(thisMsg)>SIZE_TEXT?SIZE_TEXT:strlen(thisMsg)); strncpy(text,thisMsg,size); text[size] = '\0'; } // returns text of message // PRE: thisMsg is big enough (maximum SIZE_TEXT+1 characters) void CMessage::GetText(char* thisMsg) const { strncpy(thisMsg,text,strlen(text)+1); } bool CMessage::ReadMsg(HANDLE fileHandle) { DWORD nBytes; if(!ReadFile(fileHandle,topic,SIZE_TOPIC+1,&nBytes,0)) return FALSE; if(!ReadFile(fileHandle,text,SIZE_TEXT+1,&nBytes,0)) return FALSE; return TRUE; } bool CMessage::WriteMsg(HANDLE fileHandle) { DWORD nBytes; if(!WriteFile(fileHandle, topic, SIZE_TOPIC+1, &nBytes,NULL)) { cerr << "Error: " << GetLastError() << " when writing file:" << endl; return FALSE; } if(!WriteFile(fileHandle, text, SIZE_TEXT+1, &nBytes,NULL)) { cerr << "Error: " << GetLastError() << " when writing file:" << endl; return FALSE; } return TRUE; }