/********************************************************************** * * maptools.cpp -- * Copyright (C) 1999 The New Zealand Digital Library Project * * A component of the Greenstone digital library software * from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: maptools.cpp 534 1999-09-07 04:57:43Z sjboddie $ * *********************************************************************/ /* $Log$ Revision 1.3 1999/09/07 04:57:22 sjboddie added gpl notice Revision 1.2 1999/08/31 22:39:28 rjmcnab Changes for AIX. 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 &mapentry, text_t &from, text_t &to) { from.clear (); to.clear(); text_t::const_iterator here = mapentry.begin(); text_t::const_iterator end = mapentry.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 &mapentry) { mapentry = from; mapentry += "->"; mapentry += to; }