1 |
/* -*- c++ -*- |
2 |
* Copyright (C) 2007-2017 Andreas Persson |
3 |
* |
4 |
* This program is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU General Public License as |
6 |
* published by the Free Software Foundation; either version 2, or (at |
7 |
* your option) any later version. |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, but |
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with program; see the file COPYING. If not, write to the Free |
16 |
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
17 |
* 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#ifndef GIGEDIT_GLOBAL_H |
21 |
#define GIGEDIT_GLOBAL_H |
22 |
|
23 |
#if HAVE_CONFIG_H |
24 |
# include <config.h> |
25 |
#endif |
26 |
|
27 |
#if !defined(WIN32) |
28 |
# include <unistd.h> |
29 |
# include <errno.h> |
30 |
#endif |
31 |
|
32 |
#include <sstream> |
33 |
#include <map> |
34 |
|
35 |
#ifdef LIBGIG_HEADER_FILE |
36 |
# include LIBGIG_HEADER_FILE(gig.h) |
37 |
#else |
38 |
# include <gig.h> |
39 |
#endif |
40 |
|
41 |
//FIXME: for some reason AC GETTEXT check fails on the Mac cross compiler? |
42 |
#if (HAVE_GETTEXT || defined(__APPLE__)) |
43 |
# include <libintl.h> |
44 |
# define _(String) gettext(String) |
45 |
#else |
46 |
# define _(String) String |
47 |
#endif |
48 |
|
49 |
#if defined(WIN32) && !HAVE_CONFIG_H |
50 |
# include "../../win32/libgigedit_private.h" // like config.h, automatically generated by Dev-C++ |
51 |
# define PACKAGE "gigedit" |
52 |
# define VERSION VER_STRING // VER_STRING defined in libgig_private.h |
53 |
#endif // WIN32 |
54 |
|
55 |
#define UNICODE_RIGHT_ARROW Glib::ustring(1, gunichar(0x2192)) |
56 |
#define UNICODE_LEFT_ARROW Glib::ustring(1, gunichar(0x2190)) |
57 |
|
58 |
template<class T> inline std::string ToString(T o) { |
59 |
std::stringstream ss; |
60 |
ss << o; |
61 |
return ss.str(); |
62 |
} |
63 |
|
64 |
inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) { |
65 |
for (uint i = 0; i < rgn->Dimensions; ++i) |
66 |
if (rgn->pDimensionDefinitions[i].dimension == type) |
67 |
return i; |
68 |
return -1; |
69 |
} |
70 |
|
71 |
inline int getDimensionRegionIndex(gig::DimensionRegion* dr) { |
72 |
if (!dr) return -1; |
73 |
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
74 |
for (uint i = 0; i < 256; ++i) |
75 |
if (rgn->pDimensionRegions[i] == dr) |
76 |
return i; |
77 |
return -1; |
78 |
} |
79 |
|
80 |
/// Find the number of bits required to hold the specified amount of zones. |
81 |
inline int zoneCountToBits(int nZones) { |
82 |
if (!nZones) return 0; |
83 |
int iFinalBits = 0; |
84 |
int zoneBits = nZones - 1; |
85 |
for (; zoneBits > 1; iFinalBits += 2, zoneBits >>= 2); |
86 |
iFinalBits += zoneBits; |
87 |
return iFinalBits; |
88 |
} |
89 |
|
90 |
/** |
91 |
* Returns the sum of all bits of all dimensions defined before the given |
92 |
* dimensions (@a type). This allows to access cases of that particular |
93 |
* dimension directly. If the supplied dimension @a type does not exist in the |
94 |
* the supplied @a region, then this function returns -1 instead! |
95 |
* |
96 |
* @param type - dimension that shall be used |
97 |
* @param rgn - parent region of that dimension |
98 |
*/ |
99 |
inline int baseBits(gig::dimension_t type, gig::Region* rgn) { |
100 |
int previousBits = 0; |
101 |
for (uint i = 0; i < rgn->Dimensions; ++i) { |
102 |
if (rgn->pDimensionDefinitions[i].dimension == type) return previousBits; |
103 |
previousBits += rgn->pDimensionDefinitions[i].bits; |
104 |
} |
105 |
return -1; |
106 |
} |
107 |
|
108 |
// key: dimension type, value: dimension's zone index |
109 |
class DimensionCase : public std::map<gig::dimension_t,int> { |
110 |
public: |
111 |
bool isViolating(const DimensionCase& c) const { |
112 |
for (DimensionCase::const_iterator it = begin(); it != end(); ++it) { |
113 |
if (c.find(it->first) == c.end()) continue; |
114 |
if (c.find(it->first)->second != it->second) return true; |
115 |
} |
116 |
return false; |
117 |
} |
118 |
}; |
119 |
|
120 |
inline DimensionCase dimensionCaseOf(gig::DimensionRegion* dr) { |
121 |
DimensionCase dimCase; |
122 |
int idr = getDimensionRegionIndex(dr); |
123 |
if (idr < 0) return dimCase; |
124 |
gig::Region* rgn = (gig::Region*)dr->GetParent(); |
125 |
int bitpos = 0; |
126 |
for (int d = 0; d < rgn->Dimensions; ++d) { |
127 |
const gig::dimension_def_t& dimdef = rgn->pDimensionDefinitions[d]; |
128 |
const int zone = (idr >> bitpos) & ((1 << dimdef.bits) - 1); |
129 |
dimCase[dimdef.dimension] = zone; |
130 |
bitpos += rgn->pDimensionDefinitions[d].bits; |
131 |
} |
132 |
return dimCase; |
133 |
} |
134 |
|
135 |
inline std::vector<gig::DimensionRegion*> dimensionRegionsMatching(const DimensionCase& dimCase, gig::Region* rgn) { |
136 |
std::vector<gig::DimensionRegion*> v; |
137 |
for (int idr = 0; idr < 256; ++idr) { |
138 |
if (!rgn->pDimensionRegions[idr]) continue; |
139 |
DimensionCase c = dimensionCaseOf(rgn->pDimensionRegions[idr]); |
140 |
if (!dimCase.isViolating(c)) v.push_back(rgn->pDimensionRegions[idr]); |
141 |
} |
142 |
return v; |
143 |
} |
144 |
|
145 |
#endif // GIGEDIT_GLOBAL_H |