97 lines
1.8 KiB
C++
97 lines
1.8 KiB
C++
#include "libJelling/message.h"
|
|
|
|
Message::Message(){
|
|
this->isHeader = 0;
|
|
this->isType = 0;
|
|
this->isID = 0;
|
|
this->isSender = 0;
|
|
this->msg = (char*)NULL;
|
|
}
|
|
|
|
int Message::init(const unsigned char type, int ID, const char* sender,const char* message){
|
|
if(setType(type)) return INVALID_TYPE;
|
|
if(setMessage(message)) return NOT_ENOUGH_RAM;
|
|
setID((unsigned int)ID);
|
|
if(setSender(sender)) return TOO_LONG;
|
|
return 0;
|
|
}
|
|
|
|
int Message::setType(const unsigned char typeH){
|
|
this->type = typeH;
|
|
this->isType = 1;
|
|
return 0;
|
|
}
|
|
|
|
int Message::setID(unsigned int ID){
|
|
memcpy(&this->ID, &ID, sizeof(int));
|
|
this->isID = 1;
|
|
return 0;
|
|
}
|
|
|
|
int Message::setSender(const char *sender){
|
|
memcpy(this->sender, sender, 10);
|
|
this->isSender = 1;
|
|
return 0;
|
|
}
|
|
|
|
int Message::setMessage(const char *message){
|
|
int length;
|
|
length = strlen(message) + SIZE_HEADER + 1;
|
|
this->msg = (char*)realloc(this->msg, length);
|
|
if(this->msg == NULL) return NOT_ENOUGH_RAM;
|
|
memcpy(this->msg+SIZE_HEADER, message, length-SIZE_HEADER);
|
|
return 0;
|
|
}
|
|
|
|
int Message::setTotalMessage(char *message){
|
|
this->msg = message;
|
|
this->isHeader = 1;
|
|
return 0;
|
|
}
|
|
|
|
char* Message::getTime(){
|
|
if(this->isHeader){
|
|
return this->msg;
|
|
}else{
|
|
return (char*)NULL;
|
|
}
|
|
}
|
|
|
|
unsigned char Message::getType(){
|
|
if(this->isHeader){
|
|
return *(this->msg+TYPE_POS);
|
|
}else if(this->isType){
|
|
return this->type;
|
|
}else{
|
|
return RQT;
|
|
}
|
|
}
|
|
|
|
int Message::getID(){
|
|
if(this->isHeader){
|
|
return (unsigned int)this->msg+ID_POS;
|
|
}else if(this->isID){
|
|
return this->ID;
|
|
}else{
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
char* Message::getSender(){
|
|
if(this->isHeader){
|
|
return this->msg+SENDER_POS;
|
|
}else if(this->isSender){
|
|
return this->sender;
|
|
}else{
|
|
return (char*)NULL;
|
|
}
|
|
}
|
|
|
|
int Message::getLength(){
|
|
if(this->msg == NULL) return -1;
|
|
return strlen(this->msg+SIZE_HEADER)+1;
|
|
}
|
|
|
|
unsigned char* Message::getMessage(){
|
|
return (unsigned char*)this->msg+SIZE_HEADER;
|
|
} |