File indexing completed on 2024-04-28 15:23:47

0001 /**
0002  * This file is part of the KHTML renderer for KDE.
0003  *
0004  * Copyright (C) 2006, 2007 Maksim Orlovich (maksim@kde.org)
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 
0022 #ifndef KHTML_MISC_TRANSLATOR_H
0023 #define KHTML_MISC_TRANSLATOR_H
0024 
0025 #include <QMap>
0026 
0027 //---------------------------------------------------------------------------------------------
0028 /* This class is used to do remapping between different encodings reasonably compactly.
0029    It stores things as (L,R) tables, but reads left-side from memory as MemL */
0030 template<typename L, typename R, typename MemL>
0031 class IDTranslator
0032 {
0033 public:
0034     struct Info {
0035         MemL l;
0036         R    r;
0037     };
0038 
0039     IDTranslator(const Info *table)
0040     {
0041         for (const Info *cursor = table; cursor->l; ++cursor) {
0042             m_lToR.insert(cursor->l,  cursor->r);
0043             m_rToL.insert(cursor->r,  cursor->l);
0044         }
0045     }
0046 
0047     bool hasLeft(L l)
0048     {
0049         typename QMap<L, R>::iterator i = m_lToR.find(l);
0050         return i != m_lToR.end();
0051     }
0052 
0053     L toLeft(R r)
0054     {
0055         typename QMap<R, L>::iterator i(m_rToL.find(r));
0056         if (i != m_rToL.end()) {
0057             return *i;
0058         }
0059         return L();
0060     }
0061 
0062     R toRight(L l)
0063     {
0064         typename QMap<L, R>::iterator i = m_lToR.find(l);
0065         if (i != m_lToR.end()) {
0066             return *i;
0067         }
0068         return R();
0069     }
0070 
0071 private:
0072     QMap<L, R> m_lToR;
0073     QMap<R, L> m_rToL;
0074 };
0075 
0076 #define MAKE_TRANSLATOR(name,L,R,MR,table) static IDTranslator<L,R,MR>* s_##name; \
0077     static IDTranslator<L,R,MR>* name() { if (!s_##name) s_##name = new IDTranslator<L,R,MR>(table); \
0078         return s_##name; }
0079 
0080 #endif