stdutil: Formatting
This commit is contained in:
+141
-72
@@ -1,8 +1,8 @@
|
||||
|
||||
#include "stdutil.hpp"
|
||||
|
||||
#include <set>
|
||||
#include <math.h>
|
||||
#include <set>
|
||||
#include <stdlib.h>
|
||||
#include <windows.h>
|
||||
|
||||
@@ -11,8 +11,8 @@ using namespace std;
|
||||
#ifdef MEMDEBUG
|
||||
|
||||
struct Mem {
|
||||
Mem *next, *prev;
|
||||
const char *file;
|
||||
Mem * next, *prev;
|
||||
const char* file;
|
||||
int line, size, tag;
|
||||
};
|
||||
|
||||
@@ -21,27 +21,36 @@ static bool track;
|
||||
static Mem head, tail;
|
||||
static Mem x_head, x_tail;
|
||||
|
||||
static void remove(Mem *m) {
|
||||
static void remove(Mem* m)
|
||||
{
|
||||
m->next->prev = m->prev;
|
||||
m->prev->next = m->next;
|
||||
}
|
||||
|
||||
static void insert(Mem *m, Mem *next) {
|
||||
static void insert(Mem* m, Mem* next)
|
||||
{
|
||||
m->next = next;
|
||||
m->prev = next->prev;
|
||||
next->prev->next = m;
|
||||
next->prev = m;
|
||||
}
|
||||
|
||||
static void init() {
|
||||
if (head.next) return;
|
||||
head.next = head.prev = &tail; head.tag = 'HEAD';
|
||||
tail.next = tail.prev = &head; tail.tag = 'TAIL';
|
||||
x_head.next = x_head.prev = &x_tail; x_head.tag = 'HEAD';
|
||||
x_tail.next = x_tail.prev = &x_head; x_tail.tag = 'TAIL';
|
||||
static void init()
|
||||
{
|
||||
if (head.next)
|
||||
return;
|
||||
head.next = head.prev = &tail;
|
||||
head.tag = 'HEAD';
|
||||
tail.next = tail.prev = &head;
|
||||
tail.tag = 'TAIL';
|
||||
x_head.next = x_head.prev = &x_tail;
|
||||
x_head.tag = 'HEAD';
|
||||
x_tail.next = x_tail.prev = &x_head;
|
||||
x_tail.tag = 'TAIL';
|
||||
}
|
||||
|
||||
static void check(Mem *m) {
|
||||
static void check(Mem* m)
|
||||
{
|
||||
if (m->tag != 'DNEW') {
|
||||
MessageBox(GetDesktopWindow(), "mem_check: pre_tag!='DNEW'", "Memory error", MB_OK | MB_ICONWARNING);
|
||||
if (m->tag == 'NDWE') {
|
||||
@@ -51,7 +60,7 @@ static void check(Mem *m) {
|
||||
}
|
||||
ExitProcess(0);
|
||||
}
|
||||
int *t = (int*)((char*)(m + 1) + m->size);
|
||||
int* t = (int*)((char*)(m + 1) + m->size);
|
||||
if (*t != 'dnew') {
|
||||
MessageBox(GetDesktopWindow(), "mem_check: post_tag!='dnew'", "Memory error", MB_OK | MB_ICONWARNING);
|
||||
string t = "Probable memory overwrite - new file: " + string(m->file) + " line:" + itoa(m->line);
|
||||
@@ -60,21 +69,30 @@ static void check(Mem *m) {
|
||||
}
|
||||
}
|
||||
|
||||
static void *op_new(size_t size, const char *file = "<unknown>", int line = 0) {
|
||||
static void* op_new(size_t size, const char* file = "<unknown>", int line = 0)
|
||||
{
|
||||
init();
|
||||
Mem *m = (Mem*)malloc(sizeof(Mem) + size + sizeof(int));
|
||||
Mem* m = (Mem*)malloc(sizeof(Mem) + size + sizeof(int));
|
||||
memset(m + 1, 0xcc, size);
|
||||
m->file = file; m->line = line; m->size = size; m->tag = 'DNEW';
|
||||
int *t = (int*)((char*)(m + 1) + size); *t = 'dnew';
|
||||
if (track) insert(m, head.next);
|
||||
else insert(m, x_head.next);
|
||||
m->file = file;
|
||||
m->line = line;
|
||||
m->size = size;
|
||||
m->tag = 'DNEW';
|
||||
int* t = (int*)((char*)(m + 1) + size);
|
||||
*t = 'dnew';
|
||||
if (track)
|
||||
insert(m, head.next);
|
||||
else
|
||||
insert(m, x_head.next);
|
||||
return m + 1;
|
||||
}
|
||||
|
||||
static void op_delete(void *q) {
|
||||
static void op_delete(void* q)
|
||||
{
|
||||
init();
|
||||
if (!q) return;
|
||||
Mem *m = (Mem*)q - 1;
|
||||
if (!q)
|
||||
return;
|
||||
Mem* m = (Mem*)q - 1;
|
||||
check(m);
|
||||
remove(m);
|
||||
m->tag = 'NDWE';
|
||||
@@ -82,17 +100,21 @@ static void op_delete(void *q) {
|
||||
free(m);
|
||||
}
|
||||
|
||||
void trackmem(bool enable) {
|
||||
void trackmem(bool enable)
|
||||
{
|
||||
init();
|
||||
if (track == enable) return;
|
||||
if (track == enable)
|
||||
return;
|
||||
track = enable;
|
||||
Mem *m;
|
||||
Mem* m;
|
||||
while ((m = head.next) != &tail) {
|
||||
remove(m); insert(m, x_head.next);
|
||||
remove(m);
|
||||
insert(m, x_head.next);
|
||||
}
|
||||
}
|
||||
|
||||
void checkmem(ostream &out) {
|
||||
void checkmem(ostream& out)
|
||||
{
|
||||
init();
|
||||
Mem *m, *next;
|
||||
int sum = 0, usum = 0, xsum = 0;
|
||||
@@ -116,14 +138,38 @@ void checkmem(ostream &out) {
|
||||
out << "Total mem in use:" << (sum + usum + xsum) << endl;
|
||||
}
|
||||
|
||||
void * _cdecl operator new(size_t size) { return op_new(size); }
|
||||
void * _cdecl operator new[](size_t size) { return op_new(size); }
|
||||
void * _cdecl operator new(size_t size, const char *file, int line) { return op_new(size, file, line); }
|
||||
void * _cdecl operator new[](size_t size, const char *file, int line) { return op_new(size, file, line); }
|
||||
void _cdecl operator delete(void *q) { op_delete(q); }
|
||||
void _cdecl operator delete[](void *q) { op_delete(q); }
|
||||
void _cdecl operator delete(void *q, const char *file, int line) { op_delete(q); }
|
||||
void _cdecl operator delete[](void *q, const char *file, int line) { op_delete(q); }
|
||||
void* _cdecl operator new(size_t size)
|
||||
{
|
||||
return op_new(size);
|
||||
}
|
||||
void* _cdecl operator new[](size_t size)
|
||||
{
|
||||
return op_new(size);
|
||||
}
|
||||
void* _cdecl operator new(size_t size, const char* file, int line)
|
||||
{
|
||||
return op_new(size, file, line);
|
||||
}
|
||||
void* _cdecl operator new[](size_t size, const char* file, int line)
|
||||
{
|
||||
return op_new(size, file, line);
|
||||
}
|
||||
void _cdecl operator delete(void* q)
|
||||
{
|
||||
op_delete(q);
|
||||
}
|
||||
void _cdecl operator delete[](void* q)
|
||||
{
|
||||
op_delete(q);
|
||||
}
|
||||
void _cdecl operator delete(void* q, const char* file, int line)
|
||||
{
|
||||
op_delete(q);
|
||||
}
|
||||
void _cdecl operator delete[](void* q, const char* file, int line)
|
||||
{
|
||||
op_delete(q);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
@@ -132,15 +178,18 @@ void _cdecl operator delete[](void *q, const char *file, int line) { op_delete(q
|
||||
|
||||
#endif
|
||||
|
||||
int atoi(const string &s) {
|
||||
int atoi(const string& s)
|
||||
{
|
||||
return atoi(s.c_str());
|
||||
}
|
||||
|
||||
double atof(const string &s) {
|
||||
double atof(const string& s)
|
||||
{
|
||||
return atof(s.c_str());
|
||||
}
|
||||
|
||||
string itoa(int n) {
|
||||
string itoa(int n)
|
||||
{
|
||||
char buff[32];
|
||||
_itoa(n, buff, 10);
|
||||
return string(buff);
|
||||
@@ -182,8 +231,8 @@ string itoa(int n) {
|
||||
/////////////
|
||||
//By FLOYD!//
|
||||
/////////////
|
||||
string ftoa(float n) {
|
||||
|
||||
string ftoa(float n)
|
||||
{
|
||||
static const int digits = 6;
|
||||
|
||||
int eNeg = -4, ePos = 8; // limits for e notation.
|
||||
@@ -193,14 +242,12 @@ string ftoa(float n) {
|
||||
int dec, sign;
|
||||
|
||||
if (_finite(n)) {
|
||||
|
||||
// if ( digits < 1 ) digits = 1; // less than one digit is nonsense
|
||||
// if ( digits > 8 ) digits = 8; // practical maximum for float
|
||||
|
||||
t = _ecvt(n, digits, &dec, &sign);
|
||||
|
||||
if (dec <= eNeg + 1 || dec > ePos) {
|
||||
|
||||
_gcvt(n, digits, buffer);
|
||||
t = buffer;
|
||||
return t;
|
||||
@@ -210,34 +257,34 @@ string ftoa(float n) {
|
||||
// number with no e-notation or multiple trailing zeroes.
|
||||
|
||||
if (dec <= 0) {
|
||||
|
||||
t = "0." + string(-dec, '0') + t;
|
||||
dec = 1; // new location for decimal point
|
||||
|
||||
} else if (dec < digits) {
|
||||
|
||||
t = t.substr(0, dec) + "." + t.substr(dec);
|
||||
|
||||
} else {
|
||||
|
||||
t = t + string(dec - digits, '0') + ".0";
|
||||
dec += dec - digits;
|
||||
|
||||
}
|
||||
|
||||
// Finally, trim off excess zeroes.
|
||||
|
||||
int dp1 = dec + 1, p = t.length();
|
||||
while (--p > dp1 && t[p] == '0');
|
||||
while (--p > dp1 && t[p] == '0')
|
||||
;
|
||||
t = string(t, 0, ++p);
|
||||
|
||||
return sign ? "-" + t : t;
|
||||
|
||||
} // end of finite case
|
||||
|
||||
if (_isnan(n)) return "NaN";
|
||||
if (n > 0.0) return "Infinity";
|
||||
if (n < 0.0) return "-Infinity";
|
||||
if (_isnan(n))
|
||||
return "NaN";
|
||||
if (n > 0.0)
|
||||
return "Infinity";
|
||||
if (n < 0.0)
|
||||
return "-Infinity";
|
||||
|
||||
abort();
|
||||
}
|
||||
@@ -279,80 +326,102 @@ string ftoa( float n ){
|
||||
}
|
||||
*/
|
||||
|
||||
string tolower(const string &s) {
|
||||
string tolower(const string& s)
|
||||
{
|
||||
string t = s;
|
||||
for (unsigned int k = 0; k < t.size(); ++k) t[k] = tolower(t[k]);
|
||||
for (unsigned int k = 0; k < t.size(); ++k)
|
||||
t[k] = tolower(t[k]);
|
||||
return t;
|
||||
}
|
||||
|
||||
string toupper(const string &s) {
|
||||
string toupper(const string& s)
|
||||
{
|
||||
string t = s;
|
||||
for (unsigned int k = 0; k < t.size(); ++k) t[k] = toupper(t[k]);
|
||||
for (unsigned int k = 0; k < t.size(); ++k)
|
||||
t[k] = toupper(t[k]);
|
||||
return t;
|
||||
}
|
||||
|
||||
string fullfilename(const string &t) {
|
||||
string fullfilename(const string& t)
|
||||
{
|
||||
char buff[MAX_PATH + 1], *p;
|
||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||
return string(buff);
|
||||
}
|
||||
|
||||
string filenamepath(const string &t) {
|
||||
string filenamepath(const string& t)
|
||||
{
|
||||
char buff[MAX_PATH + 1], *p;
|
||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||
if (!p) return "";
|
||||
*p = 0; return string(buff);
|
||||
if (!p)
|
||||
return "";
|
||||
*p = 0;
|
||||
return string(buff);
|
||||
}
|
||||
|
||||
string filenamefile(const string &t) {
|
||||
string filenamefile(const string& t)
|
||||
{
|
||||
char buff[MAX_PATH + 1], *p;
|
||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||
if (!p) return "";
|
||||
if (!p)
|
||||
return "";
|
||||
return string(p);
|
||||
}
|
||||
|
||||
const int MIN_SIZE = 256;
|
||||
|
||||
qstreambuf::qstreambuf() {
|
||||
qstreambuf::qstreambuf()
|
||||
{
|
||||
buf = new char[MIN_SIZE];
|
||||
setg(buf, buf, buf);
|
||||
setp(buf, buf, buf + MIN_SIZE);
|
||||
}
|
||||
|
||||
qstreambuf::~qstreambuf() {
|
||||
qstreambuf::~qstreambuf()
|
||||
{
|
||||
delete buf;
|
||||
}
|
||||
|
||||
int qstreambuf::size() {
|
||||
int qstreambuf::size()
|
||||
{
|
||||
return pptr() - gptr();
|
||||
}
|
||||
|
||||
char *qstreambuf::data() {
|
||||
char* qstreambuf::data()
|
||||
{
|
||||
return gptr();
|
||||
}
|
||||
|
||||
qstreambuf::int_type qstreambuf::underflow() {
|
||||
qstreambuf::int_type qstreambuf::underflow()
|
||||
{
|
||||
if (gptr() == egptr()) {
|
||||
if (gptr() == pptr()) return traits_type::eof();
|
||||
if (gptr() == pptr())
|
||||
return traits_type::eof();
|
||||
setg(gptr(), gptr(), pptr());
|
||||
}
|
||||
|
||||
return traits_type::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
qstreambuf::int_type qstreambuf::overflow(qstreambuf::int_type c) {
|
||||
if (c == traits_type::eof()) return c;
|
||||
qstreambuf::int_type qstreambuf::overflow(qstreambuf::int_type c)
|
||||
{
|
||||
if (c == traits_type::eof())
|
||||
return c;
|
||||
|
||||
if (pptr() == epptr()) {
|
||||
int sz = size();
|
||||
int n_sz = sz * 2; if (n_sz < MIN_SIZE) n_sz = MIN_SIZE;
|
||||
char *n_buf = new char[n_sz];
|
||||
int n_sz = sz * 2;
|
||||
if (n_sz < MIN_SIZE)
|
||||
n_sz = MIN_SIZE;
|
||||
char* n_buf = new char[n_sz];
|
||||
memcpy(n_buf, gptr(), sz);
|
||||
delete buf; buf = n_buf;
|
||||
delete buf;
|
||||
buf = n_buf;
|
||||
setg(buf, buf, buf + sz);
|
||||
setp(buf + sz, buf + sz, buf + n_sz);
|
||||
}
|
||||
|
||||
*pptr() = traits_type::to_char_type(c);
|
||||
pbump(1); return traits_type::not_eof(c);
|
||||
pbump(1);
|
||||
return traits_type::not_eof(c);
|
||||
}
|
||||
|
||||
+100
-58
@@ -2,93 +2,135 @@
|
||||
#ifndef STDUTIL_H
|
||||
#define STDUTIL_H
|
||||
|
||||
#pragma warning(disable:4786)
|
||||
#pragma warning(disable : 4786)
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
void trackmem( bool enable );
|
||||
void checkmem( std::ostream &out );
|
||||
void trackmem(bool enable);
|
||||
void checkmem(std::ostream& out);
|
||||
|
||||
//some stuff that should be in std libs
|
||||
int atoi( const std::string &s );
|
||||
double atof( const std::string &s );
|
||||
std::string itoa( int n );
|
||||
std::string ftoa( float n );
|
||||
std::string tolower( const std::string &s );
|
||||
std::string toupper( const std::string &s );
|
||||
std::string fullfilename( const std::string &t );
|
||||
std::string filenamepath( const std::string &t );
|
||||
std::string filenamefile( const std::string &t );
|
||||
int atoi(const std::string& s);
|
||||
double atof(const std::string& s);
|
||||
std::string itoa(int n);
|
||||
std::string ftoa(float n);
|
||||
std::string tolower(const std::string& s);
|
||||
std::string toupper(const std::string& s);
|
||||
std::string fullfilename(const std::string& t);
|
||||
std::string filenamepath(const std::string& t);
|
||||
std::string filenamefile(const std::string& t);
|
||||
|
||||
//lazy version of auto_ptr
|
||||
template<class T>
|
||||
class a_ptr{
|
||||
public:
|
||||
a_ptr(T *t=0):t(t){}
|
||||
~a_ptr(){delete t;}
|
||||
a_ptr &operator=(T *t){this->t=t;return *this;}
|
||||
T &operator*()const{return *t;}
|
||||
T *operator->()const{return t;}
|
||||
operator T&()const{return *t;}
|
||||
operator T*()const{return t;}
|
||||
T *release(){ T *tt=t;t=0;return tt; }
|
||||
private:
|
||||
T *t;
|
||||
class a_ptr {
|
||||
public:
|
||||
a_ptr(T* t = 0) : t(t) {}
|
||||
~a_ptr()
|
||||
{
|
||||
delete t;
|
||||
}
|
||||
a_ptr& operator=(T* t)
|
||||
{
|
||||
this->t = t;
|
||||
return *this;
|
||||
}
|
||||
T& operator*() const
|
||||
{
|
||||
return *t;
|
||||
}
|
||||
T* operator->() const
|
||||
{
|
||||
return t;
|
||||
}
|
||||
operator T&() const
|
||||
{
|
||||
return *t;
|
||||
}
|
||||
operator T*() const
|
||||
{
|
||||
return t;
|
||||
}
|
||||
T* release()
|
||||
{
|
||||
T* tt = t;
|
||||
t = 0;
|
||||
return tt;
|
||||
}
|
||||
|
||||
private:
|
||||
T* t;
|
||||
};
|
||||
|
||||
//Speed-up for SLOW sstream
|
||||
class qstreambuf : public std::streambuf{
|
||||
public:
|
||||
class qstreambuf : public std::streambuf {
|
||||
public:
|
||||
qstreambuf();
|
||||
~qstreambuf();
|
||||
int size(); //bytes unread
|
||||
char *data(); //start of bytes unread
|
||||
private:
|
||||
char *buf;
|
||||
char* data(); //start of bytes unread
|
||||
private:
|
||||
char* buf;
|
||||
int_type underflow();
|
||||
int_type overflow( int_type c );
|
||||
int_type overflow(int_type c);
|
||||
};
|
||||
|
||||
template<class T>
|
||||
class pool{
|
||||
T *free;
|
||||
enum{ N=512 };
|
||||
public:
|
||||
class pool {
|
||||
T* free;
|
||||
enum { N = 512 };
|
||||
|
||||
public:
|
||||
typedef size_t size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef T *pointer;
|
||||
typedef const T *const_pointer;
|
||||
typedef T &reference;
|
||||
typedef const T &const_reference;
|
||||
typedef T* pointer;
|
||||
typedef const T* const_pointer;
|
||||
typedef T& reference;
|
||||
typedef const T& const_reference;
|
||||
typedef T value_type;
|
||||
pointer address( reference q )const{ return &q; }
|
||||
const_pointer address( const_reference q )const{ return &q; }
|
||||
pool():free(0){}
|
||||
pointer allocate( size_type n,const void *){
|
||||
clog<<"Allocating "<<n<<endl;
|
||||
if( n>1 ) return new T[n];
|
||||
if( !free ){
|
||||
free=(T*)new char[sizeof(T)*N];
|
||||
for( int k=0;k<N-1;++k ) *(T**)(free+k)=free+k+1;
|
||||
*(T**)(free+N-1)=0;
|
||||
pointer address(reference q) const
|
||||
{
|
||||
return &q;
|
||||
}
|
||||
T *t=free;
|
||||
free=*(T**)t;
|
||||
const_pointer address(const_reference q) const
|
||||
{
|
||||
return &q;
|
||||
}
|
||||
pool() : free(0) {}
|
||||
pointer allocate(size_type n, const void*)
|
||||
{
|
||||
clog << "Allocating " << n << endl;
|
||||
if (n > 1)
|
||||
return new T[n];
|
||||
if (!free) {
|
||||
free = (T*)new char[sizeof(T) * N];
|
||||
for (int k = 0; k < N - 1; ++k)
|
||||
*(T**)(free + k) = free + k + 1;
|
||||
*(T**)(free + N - 1) = 0;
|
||||
}
|
||||
T* t = free;
|
||||
free = *(T**)t;
|
||||
return t;
|
||||
}
|
||||
void deallocate( pointer q,size_type n ){
|
||||
clog<<"Deallocating "<<n<<endl;
|
||||
while( n-->0 ){
|
||||
*(T**)q=free;
|
||||
*(T**)free=q;
|
||||
void deallocate(pointer q, size_type n)
|
||||
{
|
||||
clog << "Deallocating " << n << endl;
|
||||
while (n-- > 0) {
|
||||
*(T**)q = free;
|
||||
*(T**)free = q;
|
||||
++q;
|
||||
}
|
||||
}
|
||||
void construct( pointer p,const T &q ){ new(p)T(q); }
|
||||
void destroy( pointer p ){ p->~T(); }
|
||||
void construct(pointer p, const T& q)
|
||||
{
|
||||
new (p) T(q);
|
||||
}
|
||||
void destroy(pointer p)
|
||||
{
|
||||
p->~T();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user