Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F126535
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
12 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/funcs.cpp b/util/funcs.cpp
index 50284fb4..03553353 100644
--- a/util/funcs.cpp
+++ b/util/funcs.cpp
@@ -1,300 +1,317 @@
#ifdef USE_ALLEGRO
#include <allegro.h>
#endif
#ifdef USE_SDL
#include <SDL.h>
#endif
#include "funcs.h"
#include "globals.h"
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include "file-system.h"
#include "bitmap.h"
#include "font.h"
#include "init.h"
#include <sstream>
#ifndef USE_ALLEGRO
/* FIXME: move this to the filesystem module */
#include "sfl/sfl.h"
#include "sfl/sflfile.h"
#endif
#ifndef WINDOWS
#include <unistd.h>
#endif
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
using namespace std;
/* remove this once cmake and scons properly set DATA_PATH */
#ifndef DATA_PATH
#define DATA_PATH "data"
#endif
/* the build system should define DATA_PATH */
static string dataPath = DATA_PATH;
const double Util::pi = 3.14159265;
double Util::radians(double degree){
return degree * pi / 180.0;
}
/*
inline int rnd( int q ){
if ( q <= 0 ) return 0;
return (int)( rand() % q );
}
*/
int Util::max(int a, int b){
if (a>b){
return a;
}
return b;
}
/*
int Util::min(int a, int b){
if (a<b){
return a;
}
return b;
}
*/
int Util::clamp(int value, int min, int max){
return Util::min(Util::max(value, min), max);
}
int Util::rnd( int q, int min, int range ){
return q - min + rnd( range );
}
int Util::rnd( int min, int max ){
return rnd( max - min ) + min;
}
/* sleep for `x' milliseconds */
void Util::rest( int x ){
#ifdef USE_ALLEGRO
::rest(x);
#endif
#ifdef USE_SDL
SDL_Delay(x);
#endif
}
void Util::restSeconds(double x){
Util::rest((int)(x * 1000));
}
bool Util::checkVersion(int version){
if (version == Global::getVersion()){
return true;
}
/* when an incompatible version is made, add a check here, like
* version < getVersion(3, 5)
* would mean any client below version 3.5 is incompatible.
*
* assume versions of client's greater than ourself is compatible, but
* this may not be true. There is no way to check this.
*/
if (version < 0){
return false;
}
return true;
}
void Util::setDataPath( const string & str ){
dataPath = str;
}
Filesystem::AbsolutePath Util::getDataPath2(){
return Filesystem::AbsolutePath(dataPath + "/");
}
bool Util::exists( const string & file ){
#ifdef USE_ALLEGRO
return ::exists(file.c_str()) != 0;
#else
return file_exists(file.c_str());
#endif
}
/*
vector<Filesystem::AbsolutePath> Util::getFiles(const Filesystem::AbsolutePath & dataPath, const string & find){
return Filesystem::getFiles(dataPath, find);
}
*/
void Util::blend_palette(int * pal, int mp, int startColor, int endColor ) {
/*
ASSERT(pal);
ASSERT(mp != 0);
*/
int sc_r = Bitmap::getRed(startColor);
int sc_g = Bitmap::getGreen(startColor);
int sc_b = Bitmap::getBlue(startColor);
int ec_r = Bitmap::getRed(endColor);
int ec_g = Bitmap::getGreen(endColor);
int ec_b = Bitmap::getBlue(endColor);
for ( int q = 0; q < mp; q++ ) {
float j = (float)( q + 1 ) / (float)( mp );
int f_r = (int)( 0.5 + (float)( sc_r ) + (float)( ec_r-sc_r ) * j );
int f_g = (int)( 0.5 + (float)( sc_g ) + (float)( ec_g-sc_g ) * j );
int f_b = (int)( 0.5 + (float)( sc_b ) + (float)( ec_b-sc_b ) * j );
pal[q] = Bitmap::makeColor( f_r, f_g, f_b );
}
}
string Util::trim(const std::string & str){
string s;
size_t startpos = str.find_first_not_of(" \t");
size_t endpos = str.find_last_not_of(" \t");
// if all spaces or empty return an empty string
if ((string::npos == startpos) ||
(string::npos == endpos)){
return "";
} else {
return str.substr(startpos, endpos-startpos+1);
}
return str;
}
/* makes the first letter of a string upper case */
string Util::upcase(std::string str){
if ( str.length() > 0 && (str[0] >= 'a' && str[0] <= 'z') ){
str[0] = str[0] - 'a' + 'A';
}
return str;
}
static int lowerCase(int c){
return tolower(c);
}
static int upperCase(int c){
return toupper(c);
}
string Util::upperCaseAll(std::string str){
std::transform(str.begin(), str.end(), str.begin(), upperCase);
return str;
}
string Util::lowerCaseAll(std::string str){
std::transform(str.begin(), str.end(), str.begin(), lowerCase);
/*
for (unsigned int i = 0; i < str.length(); i++){
if (str[0] >= 'A' && str[0] <= 'Z'){
str[0] = str[0] - 'A' + 'a';
}
}
*/
return str;
}
/*Gets the minimum of three values*/
static int minimum(int a,int b,int c){
int min=a;
if(b<min)
min=b;
if(c<min)
min=c;
return min;
}
/* Compute levenshtein distance between s and t
* from: http://www.merriampark.com/ldc.htm
*/
static int levenshtein_distance(const char *s, const char *t){
//Step 1
int k,i,j,n,m,cost,distance;
int * d;
n=strlen(s);
m=strlen(t);
if(n!=0&&m!=0){
d = (int*) malloc((sizeof(int))*(m+1)*(n+1));
m++;
n++;
//Step 2
for(k=0;k<n;k++)
d[k]=k;
for(k=0;k<m;k++)
d[k*n]=k;
//Step 3 and 4
for(i=1;i<n;i++)
for(j=1;j<m;j++)
{
//Step 5
if(s[i-1]==t[j-1])
cost=0;
else
cost=1;
//Step 6
d[j*n+i]=minimum(d[(j-1)*n+i]+1,d[j*n+i-1]+1,d[(j-1)*n+i-1]+cost);
}
distance=d[n*m-1];
free(d);
return distance;
} else {
return -1; //a negative return value means that one or both strings are empty.
}
}
int Util::levenshtein(const std::string & str1, const std::string & str2){
return levenshtein_distance(str1.c_str(), str2.c_str());
}
void Util::limitPrintf(char * buffer, int size, const char * format, va_list args){
#ifdef USE_ALLEGRO
uvszprintf(buffer, size, format, args);
#else
vsnprintf(buffer, size, format, args);
#endif
}
#ifndef WINDOWS
int Util::getPipe(int files[2]){
return pipe(files);
}
#endif
void Util::showError(const Bitmap & screen, const Exception::Base & exception, const string & info){
screen.BlitFromScreen(0, 0);
Bitmap error(screen.getWidth() - 100, screen.getHeight() - 100);
error.fill(Bitmap::darken(Bitmap::makeColor(160, 0, 0), 3));
error.border(1, 2, Bitmap::makeColor(240, 0, 0));
const Font & font = Font::getFont(Global::DEFAULT_FONT, 17, 17);
int y = 10;
std::ostringstream out;
out << info;
out << " " << exception.getTrace();
font.printfWrap(10, 10, Bitmap::makeColor(240, 240, 240), error, error.getWidth() - 20, out.str(), 0);
Global::debug(0) << out.str() << std::endl;
Bitmap::transBlender(0, 0, 0, 220);
error.drawTrans(50, 50, screen);
screen.BlitToScreen();
}
void Util::showError(const Exception::Base & exception, const std::string & info){
Bitmap screen(GFX_X, GFX_Y);
showError(screen, exception, info);
}
+
+string Util::niceSize(unsigned long size){
+ const char sizes[] = {'b', 'k', 'm', 'g', 't'};
+ double real = size;
+ for (unsigned int i = 0; i < sizeof(sizes) / sizeof(const char); i++){
+ if (real > 1024){
+ real /= 1024.0;
+ } else {
+ ostringstream in;
+ in << real << sizes[i];
+ return in.str();
+ }
+ }
+ ostringstream in;
+ in << real << "t";
+ return in.str();
+}
diff --git a/util/funcs.h b/util/funcs.h
index 078c8dfd..399a1291 100644
--- a/util/funcs.h
+++ b/util/funcs.h
@@ -1,96 +1,101 @@
#ifndef _paintown_funcs_h
#define _paintown_funcs_h
#include <stdlib.h>
#include <vector>
#include <string>
#include <stdarg.h>
#include "regex.h"
#include "file-system.h"
namespace Exception{
class Base;
}
class Bitmap;
namespace Util{
extern const double pi;
/* returns a number between 0 and q-1. you will never get `q'. if
* you wanted to get `q' then pass in q+1
*/
inline int rnd( int q ){
if (q <= 0){
return 0;
}
return (int)(rand() % q);
}
// std::vector< std::string > getFiles(const Filesystem::AbsolutePath & dataPath, const std::string & find );
double radians(double degree);
Filesystem::AbsolutePath getDataPath2();
void setDataPath( const std::string & str );
bool exists( const std::string & file );
/* check that `version' is compatible with this program, mostly used
* for network clients.
*/
bool checkVersion(int version);
/* return a random number + some range between min/max */
int rnd( int q, int min, int max );
int max(int a, int b);
inline int min(int a, int b){
if (a<b){
return a;
}
return b;
}
int clamp(int value, int min, int max);
/* return a number between min/max */
int rnd( int min, int max );
void blend_palette( int * pal, int mp, int sc, int ec );
/* rest in milliseconds */
void rest( int x );
/* rest in seconds */
void restSeconds(double x);
std::string trim(const std::string & str);
/* only upper cases the first letter of a string */
std::string upcase(std::string str);
/* lower cases the entire string */
std::string lowerCaseAll(std::string str);
/* upper cases the entire string */
std::string upperCaseAll(std::string str);
+/* convert a size in bytes into human readable form.
+ * 234823592 = 223.94m
+ */
+std::string niceSize(unsigned long size);
+
int levenshtein(const std::string & str1, const std::string & str2);
int getPipe(int files[2]);
void limitPrintf(char * buffer, int size, const char * format, va_list args);
void showError(const Bitmap & screen, const Exception::Base & exception, const std::string & info);
/* will create a screen of size GFX_X, GFX_Y */
void showError(const Exception::Base & exception, const std::string & info);
}
#endif
diff --git a/util/system.cpp b/util/system.cpp
index 81d0799b..937b596a 100644
--- a/util/system.cpp
+++ b/util/system.cpp
@@ -1,75 +1,86 @@
#include <string>
#include "system.h"
#include <stdint.h>
#ifndef WINDOWS
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#endif
#ifndef WINDOWS
static bool isReadable(const std::string & path){
#ifndef WII
if (access(path.c_str(), R_OK) == 0){
return true;
} else {
return false;
}
#else
/* FIXME */
return true;
#endif
}
bool System::isDirectory(const std::string & path){
struct stat info;
if (stat(path.c_str(), &info) == 0){
if (S_ISDIR(info.st_mode) == 1){
return true;
} else {
return false;
}
}
return false;
}
bool System::readableFile(const std::string & path){
return isReadable(path) && ! isDirectory(path);
}
bool System::readable(const std::string & path){
return isReadable(path);
}
void System::makeDirectory(const std::string & path){
mkdir(path.c_str(), 0777);
}
uint64_t System::currentMicroseconds(){
struct timeval hold;
gettimeofday(&hold, NULL);
return hold.tv_sec * 1000 * 1000 + hold.tv_usec;
}
uint64_t System::getModificationTime(const std::string & path){
struct stat data;
if (stat(path.c_str(), &data) == 0){
return data.st_mtime;
}
return 0;
}
+static void * start_memory = 0;
+unsigned long System::memoryUsage(){
+ void * here = sbrk(0);
+ /* hopefully the heap is growing up */
+ return (char*) here - (char*) start_memory;
+}
+
+void System::startMemoryUsage(){
+ start_memory = sbrk(0);
+}
+
#endif
void System::makeAllDirectory(const std::string & path){
unsigned int last = path.find('/');
while (last != std::string::npos){
std::string sofar = path.substr(0, last);
if (sofar != ""){
makeDirectory(sofar);
}
last = path.find('/', last + 1);
}
makeDirectory(path);
}
diff --git a/util/system.h b/util/system.h
index 58913f60..3bce53b1 100644
--- a/util/system.h
+++ b/util/system.h
@@ -1,19 +1,23 @@
#ifndef _paintown_system_h
#define _paintown_system_h
/* system utilities */
#include <string>
#include <stdint.h>
namespace System{
bool isDirectory(const std::string & path);
void makeDirectory(const std::string & path);
void makeAllDirectory(const std::string & path);
bool readableFile(const std::string & path);
bool readable(const std::string & path);
uint64_t getModificationTime(const std::string & path);
uint64_t currentMicroseconds();
+ unsigned long memoryUsage();
+
+ /* call startMemoryUsage once at the very beginning of the program */
+ void startMemoryUsage();
}
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 11:53 AM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68903
Default Alt Text
(12 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline