File indexing completed on 2024-05-12 17:16:24

0001 
0002 /***************************************************************************
0003  *   Copyright (C) 2006-2009 by Rajko Albrecht                             *
0004  *   ral@alwins-world.de                                                   *
0005  *                                                                         *
0006  * This program is free software; you can redistribute it and/or           *
0007  * modify it under the terms of the GNU Lesser General Public              *
0008  * License as published by the Free Software Foundation; either            *
0009  * version 2.1 of the License, or (at your option) any later version.      *
0010  *                                                                         *
0011  * This program 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  * Lesser General Public License for more details.                         *
0015  *                                                                         *
0016  * You should have received a copy of the GNU Lesser General Public        *
0017  * License along with this program (in the file LGPL.txt); if not,         *
0018  * write to the Free Software Foundation, Inc., 51 Franklin St,            *
0019  * Fifth Floor, Boston, MA  02110-1301  USA                                *
0020  *                                                                         *
0021  * This software consists of voluntary contributions made by many          *
0022  * individuals.  For exact contribution history, see the revision          *
0023  * history and logs, available at http://kdesvn.alwins-world.de.           *
0024  ***************************************************************************/
0025 #ifndef SVNQT_HELPER_H
0026 #define SVNQT_HELPER_H
0027 
0028 #include "svnqttypes.h"
0029 #include "pool.h"
0030 #include "revision.h"
0031 #include <svn_string.h>
0032 #include <svn_types.h>
0033 #include <svn_version.h>
0034 
0035 #include <iostream>
0036 
0037 #define SVN_VERSION_CHECK(major, minor, patch) ((major<<16)|(minor<<8)|(patch))
0038 #ifdef OVERRIDE_SVN_API_VERSION
0039 #define SVN_API_VERSION OVERRIDE_SVN_API_VERSION
0040 #else
0041 #define SVN_API_VERSION (SVN_VERSION_CHECK(SVN_VER_MAJOR, SVN_VER_MINOR, SVN_VER_PATCH))
0042 #endif
0043 
0044 namespace svn
0045 {
0046 namespace internal
0047 {
0048 class DepthToSvn
0049 {
0050 protected:
0051     svn_depth_t _value;
0052 public:
0053     explicit DepthToSvn(const svn::Depth val): _value(svn_depth_unknown)
0054     {
0055         switch (val) {
0056         case DepthUnknown:
0057             _value = svn_depth_unknown;
0058             break;
0059         case DepthExclude:
0060             _value =  svn_depth_exclude;
0061             break;
0062         case DepthEmpty:
0063             _value =  svn_depth_empty;
0064             break;
0065         case DepthFiles:
0066             _value =  svn_depth_files;
0067             break;
0068         case DepthImmediates:
0069             _value =  svn_depth_immediates;
0070             break;
0071         case DepthInfinity:
0072         default:
0073             _value =  svn_depth_infinity;
0074             break;
0075         }
0076     }
0077 
0078     operator svn_depth_t() const
0079     {
0080         return _value;
0081     }
0082 };
0083 
0084 class RevisionRangesToHash
0085 {
0086 protected:
0087     RevisionRanges m_ranges;
0088 public:
0089     explicit RevisionRangesToHash(const RevisionRanges &_input): m_ranges(_input) {}
0090 
0091     apr_array_header_t *array(const Pool &pool)
0092     {
0093         apr_array_header_t *ranges = apr_array_make(pool, m_ranges.size(), sizeof(svn_opt_revision_range_t *));
0094         svn_opt_revision_range_t *range;
0095 
0096         for (long j = 0; j < m_ranges.count(); ++j) {
0097             range = (svn_opt_revision_range_t *)apr_palloc(pool, sizeof(*range));
0098             range->start = *m_ranges[j].first.revision();
0099             range->end  = *m_ranges[j].second.revision();
0100             APR_ARRAY_PUSH(ranges, svn_opt_revision_range_t *) = range;
0101         }
0102         return ranges;
0103     }
0104 };
0105 
0106 class Map2Hash
0107 {
0108     PropertiesMap _map;
0109 public:
0110     explicit Map2Hash(const PropertiesMap &aMap): _map(aMap) {}
0111 
0112     apr_hash_t *hash(const Pool &pool)const
0113     {
0114         if (_map.count() == 0) {
0115             return nullptr;
0116         }
0117         apr_hash_t *hash = apr_hash_make(pool);
0118         PropertiesMap::ConstIterator it;
0119         const char *propval;
0120         const char *propname;
0121         QByteArray s, n;
0122         for (it = _map.begin(); it != _map.end(); ++it) {
0123             s = it.value().toUtf8();
0124             n = it.key().toUtf8();
0125             propval = apr_pstrndup(pool, s, s.size());
0126             propname = apr_pstrndup(pool, n, n.size());
0127             apr_hash_set(hash, propname, APR_HASH_KEY_STRING, propval);
0128         }
0129         return hash;
0130     }
0131 };
0132 
0133 class Hash2Map
0134 {
0135     PropertiesMap _map;
0136 public:
0137     Hash2Map(apr_hash_t *hash, apr_pool_t *pool)
0138         : _map()
0139     {
0140         if (hash != nullptr) {
0141             apr_hash_index_t *hi;
0142             for (hi = apr_hash_first(pool, hash); hi;
0143                     hi = apr_hash_next(hi)) {
0144                 const void *key;
0145                 void *val;
0146 
0147                 apr_hash_this(hi, &key, nullptr, &val);
0148                 const char *_k = (const char *)key;
0149                 const char *_v = ((const svn_string_t *)val)->data;
0150 
0151                 _map[ QString::fromUtf8(_k)] =
0152                     QString::fromUtf8(_v);
0153             }
0154         }
0155     }
0156 
0157     operator const PropertiesMap &()const
0158     {
0159         return _map;
0160     }
0161 };
0162 }
0163 }
0164 #endif