2012-12-31 17:36:52 -08:00
|
|
|
// Copyright © 2008-2013 Pioneer Developers. See AUTHORS.txt for details
|
2012-09-15 17:59:15 -07:00
|
|
|
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
|
2012-09-12 04:38:30 -07:00
|
|
|
|
2008-11-27 14:48:15 -08:00
|
|
|
#ifndef _UTILS_H
|
|
|
|
#define _UTILS_H
|
2008-06-24 03:17:31 -07:00
|
|
|
|
|
|
|
#include <string>
|
2008-12-27 17:02:45 -08:00
|
|
|
#include <vector>
|
2009-03-02 09:57:00 -08:00
|
|
|
#include <stdio.h>
|
2011-02-07 18:07:09 -08:00
|
|
|
#include <stdarg.h>
|
2011-02-09 05:11:11 -08:00
|
|
|
#include "libs.h"
|
2008-06-24 03:17:31 -07:00
|
|
|
|
2010-03-30 05:44:20 -07:00
|
|
|
#ifndef __GNUC__
|
|
|
|
#define __attribute(x)
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
2011-08-21 06:56:30 -07:00
|
|
|
// GCC warns when a function marked __attribute((noreturn)) actually returns a value
|
|
|
|
// but other compilers which don't see the noreturn attribute of course require that
|
|
|
|
// a function with a non-void return type should return something.
|
|
|
|
#ifndef __GNUC__
|
2011-08-21 14:45:09 -07:00
|
|
|
#define RETURN_ZERO_NONGNU_ONLY return 0;
|
2011-08-21 06:56:30 -07:00
|
|
|
#else
|
2011-08-21 14:45:09 -07:00
|
|
|
#define RETURN_ZERO_NONGNU_ONLY
|
2011-08-21 06:56:30 -07:00
|
|
|
#endif
|
|
|
|
|
2011-05-12 13:30:30 -07:00
|
|
|
void Error(const char *format, ...) __attribute((format(printf,1,2))) __attribute((noreturn));
|
2010-03-30 05:44:20 -07:00
|
|
|
|
2008-12-27 17:02:45 -08:00
|
|
|
std::string string_join(std::vector<std::string> &v, std::string sep);
|
2009-01-06 15:19:07 -08:00
|
|
|
std::string format_date(double time);
|
2009-07-01 07:43:00 -07:00
|
|
|
std::string format_date_only(double time);
|
2012-06-25 18:28:31 -07:00
|
|
|
std::string format_distance(double dist, int precision = 2);
|
2011-02-17 21:08:27 -08:00
|
|
|
std::string format_money(Sint64 money);
|
2008-06-24 03:17:31 -07:00
|
|
|
|
2011-04-14 20:02:06 -07:00
|
|
|
static inline Sint64 isqrt(Sint64 a)
|
|
|
|
{
|
|
|
|
Sint64 ret=0;
|
|
|
|
Sint64 s;
|
|
|
|
Sint64 ret_sq=-a-1;
|
|
|
|
for(s=62; s>=0; s-=2){
|
|
|
|
Sint64 b;
|
|
|
|
ret+= ret;
|
|
|
|
b=ret_sq + ((2*ret+1)<<s);
|
|
|
|
if(b<0){
|
|
|
|
ret_sq=b;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-22 02:06:54 -07:00
|
|
|
void Screendump(const char* destFile, const int w, const int h);
|
|
|
|
|
2011-08-19 01:58:35 -07:00
|
|
|
// find string in bigger string, ignoring case
|
|
|
|
const char *pi_strcasestr(const char *haystack, const char *needle);
|
|
|
|
|
2012-06-17 11:17:06 -07:00
|
|
|
inline bool starts_with(const char *s, const char *t) {
|
|
|
|
assert(s && t);
|
|
|
|
while ((*s == *t) && *t) { ++s; ++t; }
|
|
|
|
return (*t == '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool starts_with(const std::string &s, const char *t) {
|
|
|
|
assert(t);
|
|
|
|
return starts_with(s.c_str(), t);
|
|
|
|
}
|
|
|
|
|
2013-02-16 07:46:53 -08:00
|
|
|
inline bool starts_with(const std::string &s, const std::string &t) {
|
|
|
|
return starts_with(s.c_str(), t.c_str());
|
|
|
|
}
|
|
|
|
|
2012-06-17 11:17:06 -07:00
|
|
|
inline bool ends_with(const char *s, size_t ns, const char *t, size_t nt) {
|
2013-12-02 07:49:47 -08:00
|
|
|
if (ns<nt) return false;
|
|
|
|
s += (ns-nt);
|
|
|
|
for (size_t i=0; i<nt; i++)
|
|
|
|
if(tolower(*s++)!=tolower(*t++)) return false;
|
|
|
|
return true;
|
2012-06-17 11:17:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ends_with(const char *s, const char *t) {
|
|
|
|
return ends_with(s, strlen(s), t, strlen(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ends_with(const std::string &s, const char *t) {
|
|
|
|
return ends_with(s.c_str(), s.size(), t, strlen(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ends_with(const std::string &s, const std::string &t) {
|
|
|
|
return ends_with(s.c_str(), s.size(), t.c_str(), t.size());
|
|
|
|
}
|
|
|
|
|
2011-05-22 16:50:32 -07:00
|
|
|
// add a few things that MSVC is missing
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
|
|
// round & roundf. taken from http://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/auxiliary/util/u_math.h
|
2012-03-30 05:07:32 -07:00
|
|
|
static inline double round(double x)
|
2011-05-22 16:50:32 -07:00
|
|
|
{
|
|
|
|
return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float roundf(float x)
|
|
|
|
{
|
|
|
|
return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
|
|
|
|
}
|
|
|
|
#endif /* _MSC_VER */
|
|
|
|
|
2013-01-03 14:25:38 -08:00
|
|
|
static inline Uint32 ceil_pow2(Uint32 v)
|
|
|
|
{
|
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
v++;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2011-11-22 23:19:31 -08:00
|
|
|
void hexdump(const unsigned char *buf, int bufsz);
|
|
|
|
|
2008-11-27 14:48:15 -08:00
|
|
|
#endif /* _UTILS_H */
|