#include <cassert>
#include "am.h"
#include "labellist.h"
#include "wwwi/string.h"
using WWWI::StringConcat;
using std::strchr;
using std::strlen;
LabelList::LabelList() {
AMCTOR(LabelList);
}
LabelList::LabelList(const char *i_strName) {
const char *pcPos = i_strName;
const char *pcDot;
AMCTOR(LabelList);
while(NULL!=(pcDot = strchr(pcPos,'.'))) {
this->push_back(Label(pcDot-pcPos,pcPos));
pcPos = pcDot + 1;
}
if (*pcPos!=0) {
this->push_back(Label(strlen(pcPos),pcPos));
}
}
LabelList::LabelList(const unsigned char *ci_ucpLabels) {
unsigned short us;
AMCTOR(LabelList);
while(*ci_ucpLabels!=0) {
us = *ci_ucpLabels++;
this->push_back(Label(us,(const char*)ci_ucpLabels));
ci_ucpLabels += us;
}
}
LabelList::LabelList(const LabelList &ci_llr) {
AMCTOR(LabelList);
(*this) = ci_llr;
}
unsigned short LabelList::GetRDLength() const {
LabelList::const_iterator it;
unsigned usLen = 1;
for(it=this->begin();it!=this->end();++it) {
usLen += ((*it).GetLength() + 1);
}
return usLen;
}
unsigned short LabelList::ToRaw(unsigned char *i_ucpData) const {
LabelList::const_iterator lbi;
unsigned short usLength = 1;
for(lbi=this->begin();lbi!=this->end();++lbi) {
*i_ucpData++ = (unsigned char)((*lbi).GetLength());
memcpy(i_ucpData,(*lbi).GetString(),(*lbi).GetLength());
i_ucpData += (*lbi).GetLength();
usLength += ((*lbi).GetLength() + 1);
}
*i_ucpData = 0;
assert(usLength<256);
return usLength;
}
void LabelList::ToString(char *o_strOut, size_t i_szOut) const {
assert(o_strOut!=NULL);
assert(i_szOut>0);
LabelList::const_iterator lbi;
o_strOut[0] = 0;
for(lbi=this->begin();lbi!=this->end();++lbi) {
if (lbi!=this->begin()) StringConcat(o_strOut,i_szOut,".");
StringConcat(o_strOut,i_szOut,(*lbi).GetString());
}
}
char *LabelList::ToString() const {
unsigned short us = this->GetRDLength();
char *strOut = new char[us];
this->ToString(strOut,us);
return strOut;
}
LabelList::~LabelList() {
AMDTOR(LabelList);
}
ostream& operator<<(ostream& io_smr, const LabelList &ci_llr) {
io_smr << &ci_llr;
return io_smr;
}
ostream& operator<<(ostream& io_smr, LabelListConstPtr ci_llp) {
LabelList::const_iterator lbi;
if (ci_llp->size()==0) {
io_smr << ".";
return io_smr;
}
for(lbi=ci_llp->begin();lbi!=ci_llp->end();++lbi) {
io_smr << (*lbi) << ".";
}
return io_smr;
}