00001
00002
00003 #include "xmlUtil/id/NamedId.h"
00004
00005 namespace xmlUtil {
00006
00007
00008
00009
00010 NamedId::NamedId(const int len) {
00011
00012
00013
00014
00015 m_fields = new Fields(0);
00016 }
00017
00018 NamedId::~NamedId() {
00019 delete m_fields;
00020 }
00021
00022 NamedId::NamedId(const NamedId& toCopy) {
00023 m_fields = new Fields(*(toCopy.m_fields));
00024 }
00025
00027 NamedId::NamedId(const NamedId& toCopy, unsigned int n) {
00028 unsigned int ix;
00029 m_fields = new Fields(0);
00030
00031 for (ix = 0; ix < n; ix++) {
00032
00033 m_fields->push_back(new IdField(*(*(toCopy.m_fields))[ix]));
00034 }
00035
00036 }
00037
00038 void NamedId::addField(std::string name, unsigned value) {
00039 IdField* field = new IdField;
00040 field->name = name;
00041 field->value = value;
00042 m_fields->push_back(field);
00043 }
00044
00045 void NamedId::addField(const IdField& newField) {
00046 IdField* field = new IdField(newField);
00047 m_fields->push_back(field);
00048 }
00049
00050 void NamedId::popField(unsigned int n) {
00051 while (n > 0) {
00052 m_fields->pop_back();
00053 --n;
00054 }
00055 }
00056
00057 bool NamedId::hasSubpath(const NameSeq& subpath) const {
00058 unsigned int pathLen = subpath.size();
00059 if (pathLen > m_fields->size()) return false;
00060
00061
00062
00063
00064
00065 for (unsigned int ix = 0; ix < pathLen; ix++) {
00066 if (subpath[ix]->compare(((*m_fields)[ix])->name) ) return false;
00067 }
00068 return true;
00069 }
00070
00071 int NamedId::hasField(std::string fname) const {
00072 for (unsigned int ix = 0; ix < m_fields->size(); ix++) {
00073 if (!(fname.compare(((*m_fields)[ix])->name) ) ) return ix;
00074 }
00075 return -1;
00076 }
00077
00078 Identifier *NamedId::stripNames() {
00079 Identifier *stripped = new Identifier();
00080
00081 for (unsigned int ix = 0; ix < m_fields->size(); ix++) {
00082 stripped->push_back((*m_fields)[ix]->value);
00083 }
00084 return stripped;
00085 }
00086
00088 std::ostream& operator<<(std::ostream& s, const NamedId& nId) {
00089 if (nId.size() == 0) {
00090 s << "()" << std::endl;
00091 return s;
00092 }
00093 NamedId::Fields::const_iterator it = nId.m_fields->begin();
00094 s << "(" << (*it)->name << "=" << (*it)->value;
00095 ++it;
00096 while (it != nId.m_fields->end()) {
00097 s << ", " << std::endl << (*it)->name << "=" << (*it)->value;
00098 ++it;
00099 }
00100 s << ")" << std::endl;
00101 return s;
00102 }
00103
00104 std::ostream& operator<<(std::ostream& s, const NameSeq& seq) {
00105 if (seq.size() == 0) {
00106 s << "()" << std::endl;
00107 return s;
00108 }
00109 std::vector<std::string *>::const_iterator it = seq.begin();
00110 s << "(" << (**it);
00111 ++it;
00112 while (it != seq.end()) {
00113 s << ", " << (**it);
00114 ++it;
00115 }
00116 s << ")" << std::endl;
00117 return s;
00118 }
00119
00120 std::ostream& operator<<(std::ostream& s, const Identifier& id) {
00121 if (id.size() == 0) {
00122 s << "()" << std::endl;
00123 return s;
00124 }
00125 std::vector<unsigned>::const_iterator it = id.begin();
00126 s << "(" << (*it);
00127 ++it;
00128 while (it != id.end()) {
00129 s << ", " << (*it);
00130 ++it;
00131 }
00132 s << ")" << std::endl;
00133 return s;
00134 }
00135
00136 }
00137