src/misc.h

changeset 183
f1b8cb53d2a2
child 185
6fea53f1ffc2
equal deleted inserted replaced
182:9374fea8f77f 183:f1b8cb53d2a2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef MISC_H
20 #define MISC_H
21
22 #include "common.h"
23 #include "str.h"
24 #include "config.h"
25
26 #define NUM_PRIMES 500
27
28 class QColor;
29 class QAction;
30
31 // Prime numbers
32 extern const ushort g_uaPrimes[NUM_PRIMES];
33
34 // Returns whether a given string represents a floating point number.
35 bool isNumber (str& zToken);
36
37 // Converts a float value to a string value.
38 str ftoa (double fCoord);
39
40 // Simplifies the given fraction.
41 void simplify (short& dNum, short& dDenom);
42
43 // Grid stuff
44 typedef struct {
45 const char* const name;
46 floatconfig* const confs[4];
47 QAction** const actionptr;
48 } gridinfo;
49
50 extern_cfg (int, grid);
51 static const short g_NumGrids = 3;
52 extern const gridinfo g_GridInfo[3];
53
54 inline const gridinfo& currentGrid () {
55 return g_GridInfo[grid];
56 }
57
58 namespace Grid {
59 enum Type {
60 Coarse,
61 Medium,
62 Fine
63 };
64
65 enum Config {
66 X,
67 Y,
68 Z,
69 Angle
70 };
71
72 double snap (double value, const Grid::Config axis);
73 };
74
75 // =============================================================================
76 template<class T> void dataswap (T& a, T& b) {
77 T c = a;
78 a = b;
79 b = c;
80 }
81
82 // =============================================================================
83 // StringParser
84 //
85 // String parsing utility
86 // =============================================================================
87 class StringParser {
88 public:
89 std::vector<str> zaTokens;
90 short dPos;
91
92 StringParser (str zInText, char cSeparator);
93
94 bool atEnd ();
95 bool atBeginning ();
96 bool next (str& zVal);
97 bool peekNext (str& zVal);
98 bool getToken (str& zVal, const ushort uInPos);
99 bool findToken (short& dResult, char const* sNeedle, short dArgs);
100 size_t size ();
101 void rewind ();
102 void seek (short dAmount, bool bRelative);
103 bool tokenCompare (short int dInPos, const char* sOther);
104
105 str operator[] (const size_t uIndex) {
106 return zaTokens[uIndex];
107 }
108 };
109
110 #endif // MISC_H

mercurial