/********************************************************************** * * maptools.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * PUT COPYRIGHT NOTICE HERE * * $Id: maptools.cpp 194 1999-03-09 20:53:14Z rjmcnab $ * *********************************************************************/ /* $Log$ Revision 1.1 1999/03/09 20:53:13 rjmcnab Moved from src/recpt. Revision 1.1 1999/03/08 19:45:53 rjmcnab Initial revision. */ #include "maptools.h" void stringmap::clear () { fromorder.erase (fromorder.begin(), fromorder.end()); mapfrom.erase (mapfrom.begin(), mapfrom.end()); } void stringmap::importmap (const text_tarray &maparray) { clear (); text_tarray::const_iterator here = maparray.begin(); text_tarray::const_iterator end = maparray.end(); text_t from; text_t to; while (here != end) { splitmapentry (*here, from, to); fromorder.push_back (from); mapfrom[from] = to; here++; } } void stringmap::exportmap (text_tarray &maparray) const { maparray.erase (maparray.begin(), maparray.end()); text_tarray::const_iterator here = fromorder.begin(); text_tarray::const_iterator end = fromorder.end(); text_tmap::const_iterator toptr; text_t tempmap; while (here != end) { toptr = mapfrom.find (*here); if (toptr != mapfrom.end()) { joinmapentry ((*toptr).first, (*toptr).second, tempmap); maparray.push_back (tempmap); } here++; } } void stringmap::getfromarray (text_tarray &fromarray) const { fromarray = fromorder; } void stringmap::gettoarray (text_tarray &toarray) const { toarray.erase(toarray.begin(), toarray.end()); text_tarray::const_iterator here = fromorder.begin(); text_tarray::const_iterator end = fromorder.end(); text_tmap::const_iterator toptr; while (here != end) { toptr = mapfrom.find (*here); if (toptr != mapfrom.end()) { toarray.push_back((*toptr).second); } here++; } } bool stringmap::fromexists (const text_t &from) const { return (mapfrom.find (from) != mapfrom.end ()); } bool stringmap::toexists (const text_t &to) const { text_tarray::const_iterator here = fromorder.begin(); text_tarray::const_iterator end = fromorder.end(); text_tmap::const_iterator toptr; while (here != end) { toptr = mapfrom.find (*here); if (toptr != mapfrom.end() && (*toptr).second == to) return true; here++; } // it was not found return false; } bool stringmap::from2to (const text_t &from, text_t &to) const { text_tmap::const_iterator toptr = mapfrom.find (from); if (toptr == mapfrom.end()) { to.clear(); return false; } to = (*toptr).second; return true; } bool stringmap::to2from (const text_t &to, text_t &from) const { text_tarray::const_iterator here = fromorder.begin(); text_tarray::const_iterator end = fromorder.end(); text_tmap::const_iterator toptr; while (here != end) { toptr = mapfrom.find (*here); if (toptr != mapfrom.end() && (*toptr).second == to) { from = (*toptr).first; return true; } here++; } // it was not found from.clear(); return false; } void splitmapentry (const text_t &map, text_t &from, text_t &to) { from.clear (); to.clear(); text_t::const_iterator here = map.begin(); text_t::const_iterator end = map.end(); // get the "from" part of the map while (here != end) { if (*here == '-') { here++; if (here != end && *here == '>') { here++; break; // found "->" } // didn't find "->" from.push_back('-'); } else { from.push_back(*here); here++; } } // get the "to" part of the map while (here != end) { to.push_back(*here); here++; } } void joinmapentry (const text_t &from, const text_t &to, text_t &map) { map = from; map += "->"; map += to; }