2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file util.h
|
|
|
|
/// @brief Public interface for general utilities.
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2012-09-25 02:09:42 +02:00
|
|
|
#include <cstdint>
|
2010-06-24 03:51:18 +02:00
|
|
|
|
2011-07-16 08:42:55 +02:00
|
|
|
#include <string>
|
2010-06-24 03:51:18 +02:00
|
|
|
#include <algorithm>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2010-06-01 10:03:17 +02:00
|
|
|
#include <libaegisub/types.h>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
namespace util {
|
|
|
|
|
2011-02-11 00:55:59 +01:00
|
|
|
|
|
|
|
/// Whether the path is a file or directory.
|
|
|
|
enum PathType {
|
|
|
|
TypeFile, ///< File
|
|
|
|
TypeDir ///< Directory
|
|
|
|
};
|
|
|
|
|
2011-02-10 03:47:41 +01:00
|
|
|
// Calculate midpoint from a list of Integers
|
2011-02-03 16:49:13 +01:00
|
|
|
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
|
|
|
|
|
2011-02-10 03:47:41 +01:00
|
|
|
// Get the parent directory of a path.
|
|
|
|
// @param path Path to process.
|
2010-05-21 03:13:36 +02:00
|
|
|
const std::string DirName(const std::string& path);
|
2011-02-10 03:47:41 +01:00
|
|
|
|
|
|
|
// Rename a file.
|
|
|
|
// @param from Source.
|
|
|
|
// @param to Destination.
|
2010-05-21 03:13:36 +02:00
|
|
|
void Rename(const std::string& from, const std::string& to);
|
2011-02-10 03:47:41 +01:00
|
|
|
|
|
|
|
// Get time sutable for logging mechinisms.
|
|
|
|
// @param tv timeval
|
2010-06-09 04:07:43 +02:00
|
|
|
void time_log(agi_timeval &tv);
|
2011-02-10 03:47:41 +01:00
|
|
|
|
|
|
|
// Make all alphabetic characters lowercase.
|
|
|
|
// @param str Input string
|
2011-02-09 23:31:44 +01:00
|
|
|
void str_lower(std::string &str);
|
2011-02-10 03:47:41 +01:00
|
|
|
|
|
|
|
// Convert a string to Integer.
|
|
|
|
// @param str Input string
|
2011-02-10 01:41:15 +01:00
|
|
|
int strtoi(std::string &str);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-02-11 00:55:59 +01:00
|
|
|
/// Check for amount of free space on a Path.
|
|
|
|
// @param path[in] Path to check
|
|
|
|
// @param type PathType (default is TypeDir)
|
2011-07-26 21:51:07 +02:00
|
|
|
uint64_t freespace(std::string const& path, PathType type=TypeDir);
|
2011-02-11 00:55:59 +01:00
|
|
|
|
2010-06-24 03:24:09 +02:00
|
|
|
struct delete_ptr {
|
|
|
|
template<class T>
|
|
|
|
void operator()(T* ptr) const {
|
|
|
|
delete ptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<class T>
|
|
|
|
void delete_clear(T& container) {
|
2012-01-31 01:44:55 +01:00
|
|
|
if (!container.empty()) {
|
|
|
|
std::for_each(container.begin(), container.end(), delete_ptr());
|
|
|
|
container.clear();
|
|
|
|
}
|
2010-06-24 03:24:09 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
} // namespace util
|
|
|
|
} // namespace agi
|