From 3bd44e5367e6f51b4fb7af854068c67b6f73bb4a Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sat, 5 May 2012 21:07:18 +0400 Subject: [PATCH] Move some things that won't work in dfhack-client to a separate file. --- library/CMakeLists.txt | 1 + library/MiscUtils.cpp | 87 ------------------------ library/PluginManager.cpp | 1 + library/Types.cpp | 129 ++++++++++++++++++++++++++++++++++++ library/include/MiscUtils.h | 3 - library/include/Types.h | 3 + 6 files changed, 134 insertions(+), 90 deletions(-) create mode 100644 library/Types.cpp diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 466175ed4..32b7d9dad 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -64,6 +64,7 @@ DataStatics.cpp DataStaticsCtor.cpp DataStaticsFields.cpp MiscUtils.cpp +Types.cpp PluginManager.cpp TileTypes.cpp VersionInfoFactory.cpp diff --git a/library/MiscUtils.cpp b/library/MiscUtils.cpp index 65fa21e11..9febe6db6 100644 --- a/library/MiscUtils.cpp +++ b/library/MiscUtils.cpp @@ -30,12 +30,9 @@ distribution. #ifndef LINUX_BUILD #include - #include "wdirent.h" #else #include #include - #include - #include #endif #include @@ -128,90 +125,6 @@ std::string toLower(const std::string &str) return rv; } -int getdir(std::string dir, std::vector &files) -{ - DIR *dp; - struct dirent *dirp; - if((dp = opendir(dir.c_str())) == NULL) - { - return errno; - } - while ((dirp = readdir(dp)) != NULL) { - files.push_back(std::string(dirp->d_name)); - } - closedir(dp); - return 0; -} - -bool hasEnding (std::string const &fullString, std::string const &ending) -{ - if (fullString.length() > ending.length()) - { - return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); - } - else - { - return false; - } -} - - -df::general_ref *DFHack::findRef(std::vector &vec, df::general_ref_type type) -{ - for (int i = vec.size()-1; i >= 0; i--) - { - df::general_ref *ref = vec[i]; - if (ref->getType() == type) - return ref; - } - - return NULL; -} - -bool DFHack::removeRef(std::vector &vec, df::general_ref_type type, int id) -{ - for (int i = vec.size()-1; i >= 0; i--) - { - df::general_ref *ref = vec[i]; - if (ref->getType() != type || ref->getID() != id) - continue; - - vector_erase_at(vec, i); - delete ref; - return true; - } - - return false; -} - -df::specific_ref *DFHack::findRef(std::vector &vec, df::specific_ref_type type) -{ - for (int i = vec.size()-1; i >= 0; i--) - { - df::specific_ref *ref = vec[i]; - if (ref->type == type) - return ref; - } - - return NULL; -} - -bool DFHack::removeRef(std::vector &vec, df::specific_ref_type type, void *ptr) -{ - for (int i = vec.size()-1; i >= 0; i--) - { - df::specific_ref *ref = vec[i]; - if (ref->type != type || ref->object != ptr) - continue; - - vector_erase_at(vec, i); - delete ref; - return true; - } - - return false; -} - #ifdef LINUX_BUILD // Linux uint64_t GetTimeMs64() { diff --git a/library/PluginManager.cpp b/library/PluginManager.cpp index b8619d503..320e8dec8 100644 --- a/library/PluginManager.cpp +++ b/library/PluginManager.cpp @@ -28,6 +28,7 @@ distribution. #include "PluginManager.h" #include "RemoteServer.h" #include "Console.h" +#include "Types.h" #include "DataDefs.h" #include "MiscUtils.h" diff --git a/library/Types.cpp b/library/Types.cpp new file mode 100644 index 000000000..72f8bdb8c --- /dev/null +++ b/library/Types.cpp @@ -0,0 +1,129 @@ +/* +https://github.com/peterix/dfhack +Copyright (c) 2009-2011 Petr Mrázek (peterix@gmail.com) + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must +not claim that you wrote the original software. If you use this +software in a product, an acknowledgment in the product documentation +would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and +must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source +distribution. +*/ + +#include "Internal.h" +#include "Export.h" +#include "MiscUtils.h" +#include "Error.h" +#include "Types.h" + +#ifndef LINUX_BUILD + #include + #include "wdirent.h" +#else + #include + #include + #include + #include +#endif + +#include +#include + +#include +#include + + +int DFHack::getdir(std::string dir, std::vector &files) +{ + DIR *dp; + struct dirent *dirp; + if((dp = opendir(dir.c_str())) == NULL) + { + return errno; + } + while ((dirp = readdir(dp)) != NULL) { + files.push_back(std::string(dirp->d_name)); + } + closedir(dp); + return 0; +} + +bool DFHack::hasEnding (std::string const &fullString, std::string const &ending) +{ + if (fullString.length() > ending.length()) + { + return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); + } + else + { + return false; + } +} + +df::general_ref *DFHack::findRef(std::vector &vec, df::general_ref_type type) +{ + for (int i = vec.size()-1; i >= 0; i--) + { + df::general_ref *ref = vec[i]; + if (ref->getType() == type) + return ref; + } + + return NULL; +} + +bool DFHack::removeRef(std::vector &vec, df::general_ref_type type, int id) +{ + for (int i = vec.size()-1; i >= 0; i--) + { + df::general_ref *ref = vec[i]; + if (ref->getType() != type || ref->getID() != id) + continue; + + vector_erase_at(vec, i); + delete ref; + return true; + } + + return false; +} + +df::specific_ref *DFHack::findRef(std::vector &vec, df::specific_ref_type type) +{ + for (int i = vec.size()-1; i >= 0; i--) + { + df::specific_ref *ref = vec[i]; + if (ref->type == type) + return ref; + } + + return NULL; +} + +bool DFHack::removeRef(std::vector &vec, df::specific_ref_type type, void *ptr) +{ + for (int i = vec.size()-1; i >= 0; i--) + { + df::specific_ref *ref = vec[i]; + if (ref->type != type || ref->object != ptr) + continue; + + vector_erase_at(vec, i); + delete ref; + return true; + } + + return false; +} diff --git a/library/include/MiscUtils.h b/library/include/MiscUtils.h index 08df98d3a..0cf34c489 100644 --- a/library/include/MiscUtils.h +++ b/library/include/MiscUtils.h @@ -274,9 +274,6 @@ DFHACK_EXPORT std::string join_strings(const std::string &separator, const std:: DFHACK_EXPORT std::string toUpper(const std::string &str); DFHACK_EXPORT std::string toLower(const std::string &str); -DFHACK_EXPORT int getdir(std::string dir, std::vector &files); -DFHACK_EXPORT bool hasEnding (std::string const &fullString, std::string const &ending); - inline bool bits_match(unsigned required, unsigned ok, unsigned mask) { return (required & mask) == (required & mask & ok); diff --git a/library/include/Types.h b/library/include/Types.h index 018e7ccfd..1f211649d 100644 --- a/library/include/Types.h +++ b/library/include/Types.h @@ -74,6 +74,9 @@ namespace DFHack uint32_t xpNxtLvl; }; + DFHACK_EXPORT int getdir(std::string dir, std::vector &files); + DFHACK_EXPORT bool hasEnding (std::string const &fullString, std::string const &ending); + DFHACK_EXPORT df::general_ref *findRef(std::vector &vec, df::general_ref_type type); DFHACK_EXPORT bool removeRef(std::vector &vec, df::general_ref_type type, int id);