[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
src/klftools/klfutil.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *   file klfutil.h
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist at bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id$ */
00023 
00024 
00025 #ifndef KLFUTIL_H
00026 #define KLFUTIL_H
00027 
00028 #include <QString>
00029 #include <QStringList>
00030 #include <QUrl>
00031 #include <QMap>
00032 #include <QVariant>
00033 //#include <QProgressDialog>
00034 #include <QLabel>
00035 //#include <QDomElement>
00036 #include <QTextFormat>
00037 
00038 #include <klfdefs.h>
00039 
00040 
00042 KLF_EXPORT bool klfEnsureDir(const QString& dir);
00043 
00044 
00046 
00050 template<class Value>
00051 class klfEqualFunc
00052 {
00053 public:
00054   bool operator()(const Value& a, const Value& b) { return a == b; }
00055 };
00056 
00058 
00064 class klfStrCaseEqualFunc
00065 {
00066   Qt::CaseSensitivity cs;
00067 public:
00068   klfStrCaseEqualFunc(Qt::CaseSensitivity caseSensitive) : cs(caseSensitive) { }
00069   bool operator()(const QString& a, const QString& b) { return QString::compare(a, b, cs) == 0; }
00070 };
00071 
00072 
00081 template<class Key, class Value, class ValCompareFunc>
00082 inline bool klfMapIsIncludedIn(const QMap<Key,Value>& a, const QMap<Key,Value>& b,
00083                                ValCompareFunc cfunc = klfEqualFunc<Value>())
00084 {
00085   typename QMap<Key,Value>::const_iterator iter;
00086   for (iter = a.begin(); iter != a.end(); ++iter) {
00087     if (!b.contains(iter.key()) || ! cfunc(b[iter.key()], iter.value())) {
00088       return false;
00089     }
00090   }
00091   // the map a is included in b
00092   return true;
00093 }
00094 
00096 
00100 template<class Key>
00101 inline bool klfMapIsIncludedIn(const QMap<Key,QString>& a, const QMap<Key,QString>& b, Qt::CaseSensitivity cs)
00102 {
00103   return klfMapIsIncludedIn(a, b, klfStrCaseEqualFunc(cs));
00104 }
00105 
00106 
00108 
00120 enum KlfUrlCompareFlag {
00123   KlfUrlCompareEqual = 0x01,
00127   KlfUrlCompareLessSpecific = 0x02,
00131   KlfUrlCompareMoreSpecific = 0x04,
00133   KlfUrlCompareBaseEqual = 0x08,
00134 
00139   klfUrlCompareFlagIgnoreQueryItemValueCase = 0x1000000
00140 };
00142 
00151 KLF_EXPORT uint klfUrlCompare(const QUrl& url1, const QUrl& url2, uint interestFlags = 0xffffffff,
00152                               const QStringList& interestQueryItems = QStringList());
00153 
00154 
00166 KLF_EXPORT bool klfMatch(const QVariant& testForHitCandidateValue, const QVariant& queryValue,
00167                          Qt::MatchFlags flags, const QString& queryStringCache = QString());
00168 
00169 
00170 
00171 template<class T>
00172 inline QVariantList klfListToVariantList(const QList<T>& list)
00173 {
00174   QVariantList l;
00175   int k;
00176   for (k = 0; k < list.size(); ++k)
00177     l << QVariant::fromValue<T>(list[k]);
00178 
00179   return l;
00180 }
00181 
00182 
00183 template<class T>
00184 inline QList<T> klfVariantListToList(const QVariantList& vlist)
00185 {
00186   QList<T> list;
00187   int k;
00188   for (k = 0; k < vlist.size(); ++k) {
00189     list << vlist[k].value<T>();
00190   }
00191   return list;
00192 }
00193 
00194 template<class T> inline QVariantMap klfMapToVariantMap(const QMap<QString, T>& map)
00195 {
00196   QVariantMap vmap;
00197   for (typename QMap<QString,T>::const_iterator it = map.begin(); it != map.end(); ++it) {
00198     vmap[it.key()] = QVariant::fromValue<T>(it.value());
00199   }
00200   return vmap;
00201 }
00202 
00203 
00204 template<class T>
00205   inline QMap<QString, T> klfVariantMapToMap(const QVariantMap& vmap)
00206 {
00207   QMap<QString, T> map;
00208   for (QVariantMap::const_iterator it = vmap.begin(); it != vmap.end(); ++it) {
00209     map[it.key()] = it.value().value<T>();
00210   }
00211   return map;
00212 }
00213 
00214 
00215 
00235 KLF_EXPORT QStringList klfSearchFind(const QString& wildcard_expression, int limit = -1);
00236 
00250 KLF_EXPORT QString klfSearchPath(const QString& prog, const QString& extra_path = "");
00251 
00260 KLF_EXPORT QString klfSearchPath(const QString& fname, const QStringList& path);
00261 
00262 
00263 
00271 KLF_EXPORT QString klfGetEnvironmentVariable(const QStringList& env, const QString& var);
00272 
00280 KLF_EXPORT void klfSetEnvironmentVariable(QStringList * env, const QString& var,
00281                                           const QString& value);
00282 
00290 KLF_EXPORT QStringList klfSetEnvironmentVariable(const QStringList& env, const QString& var,
00291                                                  const QString& value);
00292 
00295 KLF_EXPORT QStringList klfMapToEnvironmentList(const QMap<QString,QString>& map);
00296 
00299 KLF_EXPORT QMap<QString,QString> klfEnvironmentListToMap(const QStringList& env);
00300 
00301 
00303 enum KlfEnvAction {
00304   KlfEnvPathPrepend      = 0x0001, 
00305   KlfEnvPathReplace      = 0x0002, 
00306   KlfEnvPathAppend       = 0x0003, 
00307   KlfEnvPathNoAction     = 0x0000, 
00308   KlfEnvPathActionMask   = 0x00ff, 
00309   KlfEnvPathNoDuplicates = 0x0100, 
00310   KlfEnvPathFlagsMask    = 0xff00, 
00311 
00313   KlfEnvMergeExpandVars          = 0x00010000,
00315   KlfEnvMergeExpandNotRecursive  = 0x00020000,
00316   KlfEnvMergeFlagsMask           = 0x00ff0000 
00317 };
00318 
00319 
00328 KLF_EXPORT void klfMergeEnvironment(QStringList * env, const QStringList& addvars,
00329                                     const QStringList& pathvars = QStringList(),
00330                                     uint mergeaction = KlfEnvPathPrepend);
00331 
00342 KLF_EXPORT QStringList klfMergeEnvironment(const QStringList& env, const QStringList& addvars,
00343                                            const QStringList& pathvars = QStringList(),
00344                                            uint mergeaction = KlfEnvPathPrepend);
00345 
00351 KLF_EXPORT QStringList klfGetEnvironmentPath(const QStringList& env, const QString& var = QLatin1String("PATH"));
00352 
00357 KLF_EXPORT QStringList klfSplitEnvironmentPath(const QString& value);
00358 
00364 KLF_EXPORT QString klfJoinEnvironmentPath(const QStringList& paths);
00365 
00366 
00377 KLF_EXPORT QString klfSetEnvironmentPath(const QString& oldpaths, const QString& newpaths,
00378                                          uint action = KlfEnvPathAppend|KlfEnvPathNoDuplicates);
00379 
00390 KLF_EXPORT QStringList klfSetEnvironmentPath(const QStringList& oldpaths, const QStringList& newpaths,
00391                                              uint action = KlfEnvPathAppend|KlfEnvPathNoDuplicates);
00392 
00399 KLF_EXPORT QStringList klfSetEnvironmentPath(const QStringList& env, const QStringList& newitems,
00400                                              const QString& var = QLatin1String("PATH"),
00401                                              uint action = KlfEnvPathAppend|KlfEnvPathNoDuplicates);
00402 
00409 KLF_EXPORT void klfSetEnvironmentPath(QStringList * env, const QStringList& newitems,
00410                                       const QString& var = QLatin1String("PATH"),
00411                                       uint action = KlfEnvPathAppend|KlfEnvPathNoDuplicates);
00412 
00419 KLF_EXPORT QString klfExpandEnvironmentVariables(const QString& expression, const QStringList& env = QStringList(),
00420                                                  bool recursive = true);
00421 
00426 KLF_EXPORT QStringList klfCurrentEnvironment();
00427 
00428 
00437 KLF_EXPORT QString klfPrefixedPath(const QString& path, const QString& reference = QString());
00438 
00439 
00446 KLF_EXPORT QString klfUrlLocalFilePath(const QUrl& url);
00447 
00448 
00451 template<class T, class MapOp>
00452 inline QList<T> klfListMap(const QList<T>& list, MapOp op)
00453 {
00454   QList<T> l;
00455   for(typename QList<T>::const_iterator it = list.begin(); it != list.end(); ++it) {
00456     l << op(*it);
00457   }
00458   return l;
00459 }
00460 
00461 struct __klf_StrArg_MapOp
00462 {
00463   __klf_StrArg_MapOp(const QString& t) : templ(t) { }
00464   QString templ;
00465   inline QString operator()(const QString& str)
00466   {
00467     return templ.arg(str);
00468   }
00469 };
00470 
00483 inline QStringList klfMapStringList(const QStringList& list, const QString& mapstr)
00484 {
00485   return klfListMap(list, __klf_StrArg_MapOp(mapstr));
00486 }
00487 
00488 template<class T> inline QList<T> klfMkList(const T& a) { return QList<T>()<<a; }
00489 template<class T> inline QList<T> klfMkList(const T& a, const T& b) { return QList<T>()<<a<<b; }
00490 template<class T> inline QList<T> klfMkList(const T& a, const T& b, const T& c) { return QList<T>()<<a<<b<<c; }
00491 template<class T>
00492 inline QList<T> klfMkList(const T& a, const T& b, const T& c, const T& d) { return QList<T>()<<a<<b<<c<<d; }
00493 
00494 
00495 template<class Value>
00496 struct KLFMapInitData { const char * key; Value value; };
00497 
00498 template<class Value>
00499 inline QMap<QString, Value> klfMakeMap(KLFMapInitData<Value> x[])
00500 {
00501   QMap<QString,Value> map;
00502   int k;
00503   for (k = 0; x[k].key != NULL; ++k) {
00504     map[QString::fromUtf8(x[k].key)] = x[k].value;
00505   }
00506   return map;
00507 }
00508 
00509 
00510 class KLFTargeter;
00511 
00512 class KLF_EXPORT KLFTarget {
00513 public:
00514   KLFTarget() : pTargetOf() { }
00515   virtual ~KLFTarget();
00516 
00517 protected:
00518   QList<KLFTargeter*> pTargetOf;
00519   friend class KLFTargeter;
00520 };
00521 
00522 class KLF_EXPORT KLFTargeter {
00523 public:
00524   KLFTargeter() : pTarget(NULL) { }
00525   virtual ~KLFTargeter();
00526 
00527   virtual void setTarget(KLFTarget *target);
00528 
00529 protected:
00530   KLFTarget *pTarget;
00531   friend class KLFTarget;
00532 };
00533 
00534 
00535 
00536 
00537 
00538 
00539 
00613 template<class T> class KLFRefPtr
00614 {
00615 public:
00617   typedef T* Pointer;
00618 
00619   KLFRefPtr() : p(NULL), autodelete(true)
00620   {
00621     KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00622   }
00623 
00624   KLFRefPtr(const KLFRefPtr& copy) : p(copy.p), autodelete(copy.autodelete)
00625   {
00626     KLF_DEBUG_BLOCK(KLF_FUNC_NAME+" [copy]") ;
00627     set();
00628   }
00629 
00630   ~KLFRefPtr()
00631   {
00632     KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00633     unset();
00634   }
00635 
00636   T * ptr() { return p; }
00637   const T * ptr() const { return p; }
00638 
00639   bool autoDelete() const { return autodelete; }
00640   void setAutoDelete(bool on) { autodelete = on; }
00641 
00642   void setPointer(Pointer newptr)
00643   {
00644     klfDbg("new pointer: "<<newptr<<"; old pointer was "<<p) ;
00645     if (newptr == p) // no change
00646       return;
00647     unset();
00648     p = newptr;
00649     set();
00650   }
00651   void setNull()
00652   {
00653     setPointer(NULL);
00654   }
00655 
00656   KLFRefPtr<T>& operator=(const KLFRefPtr<T>& other)
00657   {
00658     KLF_DEBUG_BLOCK(KLF_FUNC_NAME+"(KLFRefPtr<T*>)") ;
00659     setPointer(other.p);
00660     autodelete = other.autodelete;
00661     return *this;
00662   }
00663   template<class OtherPtr> KLFRefPtr<T>& operator=(OtherPtr aptr)
00664   {
00665     KLF_DEBUG_BLOCK(KLF_FUNC_NAME+"(OtherPtr)") ;
00666     setPointer((T*)(aptr));
00667     return *this;
00668   }
00669 
00670   KLFRefPtr<T>& operator=(Pointer newptr)
00671   {
00672     KLF_DEBUG_BLOCK(KLF_FUNC_NAME+"(Pointer)") ;
00673     setPointer(newptr);
00674     return *this;
00675   }
00676 
00677   /*
00678    KLFRefPtr<T>& operator=(long int value)
00679    {
00680    if ((void*)value != (void*)NULL) {
00681    klfWarning("ERROR: *** Cannot set non-NULL long int value "<<value) ;
00682    }
00683    setPointer(NULL);
00684    return *this;
00685    }
00686    KLFRefPtr<T>& operator=(int value)
00687    {
00688    if ((void*)value != (void*)NULL) {
00689    klfWarning("ERROR: *** Cannot set non-NULL long int value "<<value) ;
00690    }
00691    setPointer(NULL);
00692    return *this;
00693    }
00694   */
00695 
00696   inline operator T *()
00697   {  return p;  }
00698   inline operator const T *() const
00699   {  return p;  }
00700 
00701   inline T * operator()()
00702   { return p; }
00703   inline const T * operator()() const
00704   { return p; }
00705 
00706   /*  inline T & operator*()
00707       {  return *p; }
00708       inline const T & operator*() const
00709       {  return *p; } */
00710 
00711   inline Pointer operator->()
00712   {  return p;  }
00713   inline Pointer operator->() const
00714   {  return p;  }
00715 
00716   template<class OtherPtr>
00717   inline OtherPtr dyn_cast() { return dynamic_cast<OtherPtr>(p); }
00718 
00719   template<class OtherPtr>
00720   inline const OtherPtr dyn_cast() const { return dynamic_cast<const OtherPtr>(p); }
00721 
00722   inline bool operator==(void * otherptr) const
00723   { return (void *)p == otherptr; }
00724 
00725   inline bool operator==(const KLFRefPtr<T>& otherptr) const
00726   { return p == otherptr.p; }
00727 
00728   inline bool operator!=(void * otherptr) const
00729   { return ! (this->operator==(otherptr)); }
00730 
00731   inline bool operator!=(const KLFRefPtr<T>& otherptr) const
00732   { return ! (this->operator==(otherptr)); }
00733 
00734 
00735 private:
00736   void operator+=(int ) { }
00737   void operator-=(int ) { }
00738 
00740   Pointer p;
00743   bool autodelete;
00744 
00745   void unset() {
00746     KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00747     if (p != NULL) {
00748       int n = p->deref();
00749       klfDbg(p<<": deref()! n="<<n<<";  autodelete="<<autodelete) ;
00750       if (autodelete && n <= 0) {
00751         klfDbg("Deleting at refcount="<<n<<".") ;
00752         delete p;
00753       }
00754       p = NULL;
00755     }
00756   }
00757   void set() {
00758     KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00759     if (p != NULL) {
00760       int n = p->ref();
00761       Q_UNUSED(n) ;
00762       klfDbg(p<<": ref()! n="<<n) ;
00763     }
00764   }
00765 
00766 };
00767 
00768 
00769 
00770 
00771 
00772 
00773 
00774 
00775 
00776 
00778 struct KLFPointerGuard
00779 {
00780   KLFPointerGuard() : ptr(NULL), refcount(0) {}
00781 
00782   void * ptr;
00783 
00784   int ref() { return ++refcount; }
00785   int deref() { return --refcount; }
00786 private:
00787   int refcount;
00788 };
00789 
00835 template<class T>
00836 class KLFSyncGuardPtr
00837 {
00838 public:
00839   typedef T* Pointer;
00840 
00841   KLFSyncGuardPtr()
00842   {
00843     ptrguard = NULL;
00844   }
00845   ~KLFSyncGuardPtr()
00846   {
00847   }
00848 
00849   KLFSyncGuardPtr(const KLFSyncGuardPtr& copy)
00850   {
00851     ptrguard = copy.ptrguard;
00852   }
00853   KLFSyncGuardPtr(T * obj)
00854   {
00855     *this = obj;
00856   }
00857 
00858   inline Pointer ptr()
00859   {
00860     if (ptrguard == NULL)
00861       return NULL;
00862     return (Pointer) ptrguard->ptr;
00863   }
00864 
00865   inline operator T*()
00866   { return ptr(); }
00867   inline operator const T*()
00868   { return ptr(); }
00869 
00870   inline T* operator()()
00871   { return ptr(); }
00872   inline const T* operator()() const
00873   { return ptr(); }
00874 
00875   inline T& operator*()
00876   { return *ptr(); }
00877   inline const T& operator*() const
00878   { return *ptr(); }
00879 
00880   inline T* operator->()
00881   { return ptr(); }
00882   inline const T* operator->() const
00883   { return ptr(); }
00884   
00885 
00886   Pointer operator=(Pointer p)
00887   {
00888     if (p == NULL) {
00889       invalidate();
00890       return NULL;
00891     }
00892     if (ptrguard != NULL) {
00893       klfWarning("Leaving tracked pointer "<<ptrguard->ptr<<" for another pointer!") ;
00894     } else {
00895       ptrguard = new KLFPointerGuard;
00896     }
00897     ptrguard->ptr = (void*)p;
00898     return p;
00899   };
00900 
00901   const KLFSyncGuardPtr& operator=(const KLFSyncGuardPtr& copy)
00902   {
00903     KLF_ASSERT_CONDITION(ptrguard == NULL, "Leaving tracked pointer "<<ptrguard->ptr<<" for a copy pointer!",
00904                          ;) ;
00905     ptrguard = copy.ptrguard;
00906     return *this;
00907   }
00908   
00909   void invalidate()
00910   {
00911     if (ptrguard != NULL)
00912       ptrguard->ptr = NULL;
00913   }
00914 
00915   void reset()
00916   {
00917     ptrguard.setNull();
00918   }
00919 
00920 private:
00921   KLFRefPtr<KLFPointerGuard> ptrguard;
00922 };
00923 
00924 
00925 
00926 
00927 
00928 
00929 
00930 
00931 
00932 
00933 
00934 
00935 
00936 
00937 
00938 
00939 
00940 
00941 // -----
00942 
00943 
00944 
00945 template<class T>
00946 struct KLFVariantConverter {
00947   static QVariant convert(const T& value)
00948   {
00949     return QVariant::fromValue<T>(value);
00950   }
00951   static T recover(const QVariant& variant)
00952   {
00953     return variant.value<T>();
00954   }
00955 };
00956 template<class T>
00957 struct KLFVariantConverter<QList<T> > {
00958   static QVariant convert(const QList<T>& list)
00959   {
00960     return QVariant::fromValue<QVariantList>(klfListToVariantList(list));
00961   }
00962   static QList<T> recover(const QVariant& variant)
00963   {
00964     return klfVariantListToList<T>(variant.toList());
00965   }
00966 };
00967 template<>
00968 struct KLFVariantConverter<QTextCharFormat> {
00969   static QVariant convert(const QTextCharFormat& value)
00970   {
00971     return QVariant::fromValue<QTextFormat>(value);
00972   }
00973   static QTextCharFormat recover(const QVariant& variant)
00974   {
00975     return variant.value<QTextFormat>().toCharFormat();
00976   }
00977 };
00978 
00979 
00980 
00981 
00985 #define KLFTOOLS_INIT                           \
00986   Q_INIT_RESOURCE(klftoolsres)
00987 
00988 
00989 
00990 #endif

Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net