Skip to content

Commit 3bd44e5

Browse files
committed
Move some things that won't work in dfhack-client to a separate file.
1 parent 81f2240 commit 3bd44e5

6 files changed

Lines changed: 134 additions & 90 deletions

File tree

library/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ DataStatics.cpp
6464
DataStaticsCtor.cpp
6565
DataStaticsFields.cpp
6666
MiscUtils.cpp
67+
Types.cpp
6768
PluginManager.cpp
6869
TileTypes.cpp
6970
VersionInfoFactory.cpp

library/MiscUtils.cpp

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ distribution.
3030

3131
#ifndef LINUX_BUILD
3232
#include <Windows.h>
33-
#include "wdirent.h"
3433
#else
3534
#include <sys/time.h>
3635
#include <ctime>
37-
#include <dirent.h>
38-
#include <errno.h>
3936
#endif
4037

4138
#include <ctype.h>
@@ -128,90 +125,6 @@ std::string toLower(const std::string &str)
128125
return rv;
129126
}
130127

131-
int getdir(std::string dir, std::vector<std::string> &files)
132-
{
133-
DIR *dp;
134-
struct dirent *dirp;
135-
if((dp = opendir(dir.c_str())) == NULL)
136-
{
137-
return errno;
138-
}
139-
while ((dirp = readdir(dp)) != NULL) {
140-
files.push_back(std::string(dirp->d_name));
141-
}
142-
closedir(dp);
143-
return 0;
144-
}
145-
146-
bool hasEnding (std::string const &fullString, std::string const &ending)
147-
{
148-
if (fullString.length() > ending.length())
149-
{
150-
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
151-
}
152-
else
153-
{
154-
return false;
155-
}
156-
}
157-
158-
159-
df::general_ref *DFHack::findRef(std::vector<df::general_ref*> &vec, df::general_ref_type type)
160-
{
161-
for (int i = vec.size()-1; i >= 0; i--)
162-
{
163-
df::general_ref *ref = vec[i];
164-
if (ref->getType() == type)
165-
return ref;
166-
}
167-
168-
return NULL;
169-
}
170-
171-
bool DFHack::removeRef(std::vector<df::general_ref*> &vec, df::general_ref_type type, int id)
172-
{
173-
for (int i = vec.size()-1; i >= 0; i--)
174-
{
175-
df::general_ref *ref = vec[i];
176-
if (ref->getType() != type || ref->getID() != id)
177-
continue;
178-
179-
vector_erase_at(vec, i);
180-
delete ref;
181-
return true;
182-
}
183-
184-
return false;
185-
}
186-
187-
df::specific_ref *DFHack::findRef(std::vector<df::specific_ref*> &vec, df::specific_ref_type type)
188-
{
189-
for (int i = vec.size()-1; i >= 0; i--)
190-
{
191-
df::specific_ref *ref = vec[i];
192-
if (ref->type == type)
193-
return ref;
194-
}
195-
196-
return NULL;
197-
}
198-
199-
bool DFHack::removeRef(std::vector<df::specific_ref*> &vec, df::specific_ref_type type, void *ptr)
200-
{
201-
for (int i = vec.size()-1; i >= 0; i--)
202-
{
203-
df::specific_ref *ref = vec[i];
204-
if (ref->type != type || ref->object != ptr)
205-
continue;
206-
207-
vector_erase_at(vec, i);
208-
delete ref;
209-
return true;
210-
}
211-
212-
return false;
213-
}
214-
215128
#ifdef LINUX_BUILD // Linux
216129
uint64_t GetTimeMs64()
217130
{

library/PluginManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ distribution.
2828
#include "PluginManager.h"
2929
#include "RemoteServer.h"
3030
#include "Console.h"
31+
#include "Types.h"
3132

3233
#include "DataDefs.h"
3334
#include "MiscUtils.h"

library/Types.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
https://github.com/peterix/dfhack
3+
Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com)
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any
7+
damages arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any
10+
purpose, including commercial applications, and to alter it and
11+
redistribute it freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must
14+
not claim that you wrote the original software. If you use this
15+
software in a product, an acknowledgment in the product documentation
16+
would be appreciated but is not required.
17+
18+
2. Altered source versions must be plainly marked as such, and
19+
must not be misrepresented as being the original software.
20+
21+
3. This notice may not be removed or altered from any source
22+
distribution.
23+
*/
24+
25+
#include "Internal.h"
26+
#include "Export.h"
27+
#include "MiscUtils.h"
28+
#include "Error.h"
29+
#include "Types.h"
30+
31+
#ifndef LINUX_BUILD
32+
#include <Windows.h>
33+
#include "wdirent.h"
34+
#else
35+
#include <sys/time.h>
36+
#include <ctime>
37+
#include <dirent.h>
38+
#include <errno.h>
39+
#endif
40+
41+
#include <ctype.h>
42+
#include <stdarg.h>
43+
44+
#include <sstream>
45+
#include <map>
46+
47+
48+
int DFHack::getdir(std::string dir, std::vector<std::string> &files)
49+
{
50+
DIR *dp;
51+
struct dirent *dirp;
52+
if((dp = opendir(dir.c_str())) == NULL)
53+
{
54+
return errno;
55+
}
56+
while ((dirp = readdir(dp)) != NULL) {
57+
files.push_back(std::string(dirp->d_name));
58+
}
59+
closedir(dp);
60+
return 0;
61+
}
62+
63+
bool DFHack::hasEnding (std::string const &fullString, std::string const &ending)
64+
{
65+
if (fullString.length() > ending.length())
66+
{
67+
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
68+
}
69+
else
70+
{
71+
return false;
72+
}
73+
}
74+
75+
df::general_ref *DFHack::findRef(std::vector<df::general_ref*> &vec, df::general_ref_type type)
76+
{
77+
for (int i = vec.size()-1; i >= 0; i--)
78+
{
79+
df::general_ref *ref = vec[i];
80+
if (ref->getType() == type)
81+
return ref;
82+
}
83+
84+
return NULL;
85+
}
86+
87+
bool DFHack::removeRef(std::vector<df::general_ref*> &vec, df::general_ref_type type, int id)
88+
{
89+
for (int i = vec.size()-1; i >= 0; i--)
90+
{
91+
df::general_ref *ref = vec[i];
92+
if (ref->getType() != type || ref->getID() != id)
93+
continue;
94+
95+
vector_erase_at(vec, i);
96+
delete ref;
97+
return true;
98+
}
99+
100+
return false;
101+
}
102+
103+
df::specific_ref *DFHack::findRef(std::vector<df::specific_ref*> &vec, df::specific_ref_type type)
104+
{
105+
for (int i = vec.size()-1; i >= 0; i--)
106+
{
107+
df::specific_ref *ref = vec[i];
108+
if (ref->type == type)
109+
return ref;
110+
}
111+
112+
return NULL;
113+
}
114+
115+
bool DFHack::removeRef(std::vector<df::specific_ref*> &vec, df::specific_ref_type type, void *ptr)
116+
{
117+
for (int i = vec.size()-1; i >= 0; i--)
118+
{
119+
df::specific_ref *ref = vec[i];
120+
if (ref->type != type || ref->object != ptr)
121+
continue;
122+
123+
vector_erase_at(vec, i);
124+
delete ref;
125+
return true;
126+
}
127+
128+
return false;
129+
}

library/include/MiscUtils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,6 @@ DFHACK_EXPORT std::string join_strings(const std::string &separator, const std::
274274
DFHACK_EXPORT std::string toUpper(const std::string &str);
275275
DFHACK_EXPORT std::string toLower(const std::string &str);
276276

277-
DFHACK_EXPORT int getdir(std::string dir, std::vector<std::string> &files);
278-
DFHACK_EXPORT bool hasEnding (std::string const &fullString, std::string const &ending);
279-
280277
inline bool bits_match(unsigned required, unsigned ok, unsigned mask)
281278
{
282279
return (required & mask) == (required & mask & ok);

library/include/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ namespace DFHack
7474
uint32_t xpNxtLvl;
7575
};
7676

77+
DFHACK_EXPORT int getdir(std::string dir, std::vector<std::string> &files);
78+
DFHACK_EXPORT bool hasEnding (std::string const &fullString, std::string const &ending);
79+
7780
DFHACK_EXPORT df::general_ref *findRef(std::vector<df::general_ref*> &vec, df::general_ref_type type);
7881
DFHACK_EXPORT bool removeRef(std::vector<df::general_ref*> &vec, df::general_ref_type type, int id);
7982

0 commit comments

Comments
 (0)