[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[140] | 4 | |
---|
| 5 | #ifndef _STL_UTIL_H_ |
---|
| 6 | #define _STL_UTIL_H_ |
---|
| 7 | |
---|
| 8 | #include "nonstd_stl.h" |
---|
[180] | 9 | #include <stdarg.h> |
---|
[140] | 10 | |
---|
| 11 | template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N]) |
---|
| 12 | { |
---|
[257] | 13 | for (unsigned int i = 0; i < N; i++) |
---|
| 14 | v.push_back(d[i]); |
---|
[140] | 15 | } |
---|
| 16 | |
---|
| 17 | template<typename T> void erase(vector<T>& v, const T& e) |
---|
| 18 | { |
---|
[257] | 19 | typename vector<T>::iterator it = std::find(v.begin(), v.end(), e); |
---|
| 20 | if (it != v.end()) |
---|
| 21 | v.erase(it); |
---|
[140] | 22 | } |
---|
| 23 | |
---|
| 24 | template<typename T> void deleteVectorElements(vector<T*>& v) |
---|
| 25 | { |
---|
[257] | 26 | for (typename vector<T*>::iterator it = v.begin(); it != v.end(); it++) |
---|
| 27 | delete *it; |
---|
| 28 | v.clear(); |
---|
[140] | 29 | } |
---|
| 30 | |
---|
| 31 | template<typename T> int findIndex(vector<T>& v, const T& e) |
---|
| 32 | { |
---|
[257] | 33 | typename vector<T>::iterator it = find(v.begin(), v.end(), e); |
---|
| 34 | if (it != v.end()) |
---|
| 35 | return &*it - &v.front(); |
---|
| 36 | return -1; |
---|
[140] | 37 | } |
---|
| 38 | |
---|
[257] | 39 | |
---|
| 40 | char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap") |
---|
| 41 | |
---|
[140] | 42 | string ssprintf(const char* format, ...); |
---|
[180] | 43 | string ssprintf_va(const char* format, va_list ap); |
---|
[140] | 44 | |
---|
| 45 | string stripExt(const string& filename); // strip extension from filename |
---|
[180] | 46 | string getFileExt(const string& filename); // get extension (starting with ".") from filename |
---|
[246] | 47 | string getFileDir(const string& filename); // get path component excluding filename ("" if no dir in file) |
---|
[140] | 48 | |
---|
[246] | 49 | |
---|
[257] | 50 | bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true); |
---|
| 51 | bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true); |
---|
| 52 | bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true); |
---|
| 53 | bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true); |
---|
[140] | 54 | |
---|
| 55 | template<class T> class DeletingVector // deletes the elements (pointers) in destructor |
---|
| 56 | { |
---|
[257] | 57 | public: |
---|
| 58 | std::vector<T*> vector; |
---|
| 59 | ~DeletingVector() |
---|
[140] | 60 | { |
---|
[257] | 61 | for (int i = vector.size() - 1; i >= 0; i--) |
---|
| 62 | delete vector[i]; |
---|
[140] | 63 | } |
---|
[257] | 64 | T* operator[](int i) { return vector[i]; } |
---|
| 65 | int size() { return vector.size(); } |
---|
| 66 | void push_back(T* x) { vector.push_back(x); } |
---|
[140] | 67 | }; |
---|
| 68 | |
---|
| 69 | #endif |
---|