|
| 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 | +} |
0 commit comments