Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Import ninjam_server_0.06.tar.gz WDL/ directory

The server has a newer version of the WDL library.  Import those
changes.

Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
  • Loading branch information...
commit e74f010ce5dd5d3a08c78cbe4de37b93d8423fae 1 parent 339f912
@stefanha stefanha authored
View
83 WDL/heapbuf.h
@@ -49,10 +49,12 @@
#ifndef _WDL_HEAPBUF_H_
#define _WDL_HEAPBUF_H_
+#ifndef WDL_HEAPBUF_IMPL_ONLY
+
class WDL_HeapBuf
{
public:
- WDL_HeapBuf(int granul=64) : m_granul(granul), m_buf(NULL), m_alloc(0), m_size(0)
+ WDL_HeapBuf(int granul=4096) : m_granul(granul), m_buf(NULL), m_alloc(0), m_size(0), m_mas(0)
{
}
~WDL_HeapBuf()
@@ -63,36 +65,107 @@ class WDL_HeapBuf
void *Get() { return m_buf; }
int GetSize() { return m_size; }
- void *Resize(int newsize)
+ void SetGranul(int granul)
+ {
+ m_granul=granul;
+ }
+
+ void SetMinAllocSize(int mas)
+ {
+ m_mas=mas;
+ }
+#define WDL_HEAPBUF_PREFIX
+#else
+#define WDL_HEAPBUF_PREFIX WDL_HeapBuf::
+#endif
+
+ void * WDL_HEAPBUF_PREFIX Resize(int newsize, bool resizedown
+#ifdef WDL_HEAPBUF_INTF_ONLY
+ =true);
+#else
+#ifdef WDL_HEAPBUF_IMPL_ONLY
+ )
+#else
+ =true)
+#endif
{
- if (!newsize) // special case, free all when resized to 0
+ if (newsize < m_mas && newsize < m_alloc)
+ {
+ m_size=newsize;
+ return Get();
+ }
+ if (!newsize && !m_mas) // special case, free all when resized to 0
+ {
+ if (resizedown)
{
free(m_buf);
m_buf=NULL;
m_alloc=0;
}
- else if (newsize > m_alloc || newsize < m_alloc - (m_granul<<2)) // if we grew over our allocation, or shrunk too far down we should resize down
+ }
+ else if (newsize > m_alloc || (resizedown && newsize < m_alloc - (m_granul<<2))) // if we grew over our allocation, or shrunk too far down we should resize down
{
int newalloc = (newsize > m_alloc) ? (newsize + m_granul) : newsize;
+ if (newalloc < m_mas) newalloc=m_mas;
+
+ if (newalloc != m_alloc || !m_buf)
+ {
void *nbuf=realloc(m_buf,newalloc);
- if (!nbuf) return 0; // ack, error!
+ if (!nbuf)
+ {
+ if (!newalloc) return m_buf;
+ nbuf=malloc(newalloc);
+ if (!nbuf)
+ {
+ // todo: throw some error here, because we couldnt allocate our block!!
+ nbuf=m_buf;
+ }
+ else
+ {
+ if (m_buf) memcpy(nbuf,m_buf,newsize<m_size?newsize:m_size);
+ free(m_buf);
+ m_buf=0;
+ }
+ }
m_buf=nbuf;
m_alloc=newalloc;
}
+ }
m_size=newsize;
return m_buf;
}
+#endif
+#ifndef WDL_HEAPBUF_IMPL_ONLY
private:
int m_granul;
void *m_buf;
int m_alloc;
int m_size;
+ int m_mas;
};
+template<class PTRTYPE> class WDL_TypedBuf
+{
+ public:
+ WDL_TypedBuf(int granul=64) : m_hb(granul)
+ {
+ }
+ ~WDL_TypedBuf()
+ {
+ }
+ PTRTYPE *Get() { return (PTRTYPE *) m_hb.Get(); }
+ int GetSize() { return m_hb.GetSize()/sizeof(PTRTYPE); }
+
+ PTRTYPE *Resize(int newsize, bool resizedown=true) { return (PTRTYPE *)m_hb.Resize(newsize*sizeof(PTRTYPE),resizedown); }
+
+ private:
+ WDL_HeapBuf m_hb;
+};
#endif
+#endif
View
83 WDL/lineparse.h
@@ -51,9 +51,11 @@
*/
-#ifndef _LINEPARSE_H_
-#define _LINEPARSE_H_
+#ifndef WDL_LINEPARSE_H_
+#define WDL_LINEPARSE_H_
+
+#ifndef WDL_LINEPARSE_IMPL_ONLY
class LineParser {
public:
@@ -73,8 +75,24 @@ class LineParser {
{
return m_bCommentBlock;
}
+ int getnumtokens() { return m_nt-m_eat; }
+
+ void eattoken() { m_eat++; }
- int parse(char *line, int ignore_escaping=0) // returns -1 on error
+#define WDL_LINEPARSE_PREFIX
+#else
+#define WDL_LINEPARSE_PREFIX LineParser::
+#endif
+
+ int WDL_LINEPARSE_PREFIX parse(const char *line, int ignore_escaping
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ =1); // returns -1 on error
+#else
+#ifdef WDL_LINEPARSE_IMPL_ONLY
+ )
+#else
+ =1)
+#endif
{
freetokens();
bool bPrevCB=m_bCommentBlock;
@@ -93,12 +111,19 @@ class LineParser {
}
return 0;
}
+#endif
- int getnumtokens() { return m_nt-m_eat; }
- void eattoken() { m_eat++; }
+ double WDL_LINEPARSE_PREFIX gettoken_float(int token, int *success
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ =0);
+#else
- double gettoken_float(int token, int *success=0)
+#ifdef WDL_LINEPARSE_IMPL_ONLY
+ )
+#else
+ =0)
+#endif
{
token+=m_eat;
if (token < 0 || token >= m_nt)
@@ -118,7 +143,17 @@ class LineParser {
}
return atof(m_tokens[token]);
}
- int gettoken_int(int token, int *success=0)
+#endif
+
+ int WDL_LINEPARSE_PREFIX gettoken_int(int token, int *success
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ =0) ;
+#else
+#ifdef WDL_LINEPARSE_IMPL_ONLY
+ )
+#else
+ =0)
+#endif
{
token+=m_eat;
if (token < 0 || token >= m_nt || !m_tokens[token][0])
@@ -133,13 +168,21 @@ class LineParser {
if (success) *success=! (int)(*tmp);
return l;
}
- char *gettoken_str(int token)
+#endif
+ char * WDL_LINEPARSE_PREFIX gettoken_str(int token)
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ ;
+#else
{
token+=m_eat;
if (token < 0 || token >= m_nt) return "";
return m_tokens[token];
}
- int gettoken_enum(int token, const char *strlist) // null seperated list
+#endif
+ int WDL_LINEPARSE_PREFIX gettoken_enum(int token, const char *strlist) // null seperated list
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ ;
+#else
{
int x=0;
char *tt=gettoken_str(token);
@@ -155,8 +198,15 @@ class LineParser {
}
return -1;
}
+#endif
+
+#ifndef WDL_LINEPARSE_IMPL_ONLY
private:
- void freetokens()
+#endif//!WDL_LINEPARSE_IMPL_ONLY
+ void WDL_LINEPARSE_PREFIX freetokens()
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ ;
+#else
{
if (m_tokens)
{
@@ -168,8 +218,12 @@ class LineParser {
m_tokens=0;
m_nt=0;
}
+#endif
- int doline(char *line, int ignore_escaping=0)
+ int WDL_LINEPARSE_PREFIX doline(const char *line, int ignore_escaping)
+#ifdef WDL_LINEPARSE_INTF_ONLY
+ ;
+#else
{
m_nt=0;
if ( m_bCommentBlock )
@@ -201,7 +255,7 @@ class LineParser {
else if (*line == '`') lstate=4;
if (lstate) line++;
int nc=0;
- char *p = line;
+ const char *p = line;
while (*line)
{
if (line[0] == '$' && line[1] == '\\') {
@@ -248,11 +302,14 @@ class LineParser {
}
return 0;
}
+#endif
+#ifndef WDL_LINEPARSE_IMPL_ONLY
int m_eat;
int m_nt;
bool m_bCommentBlock;
char **m_tokens;
};
-#endif//_LINEPARSE_H_
+#endif//!WDL_LINEPARSE_IMPL_ONLY
+#endif//WDL_LINEPARSE_H_
View
26 WDL/ptrlist.h
@@ -65,6 +65,13 @@ template<class PTRTYPE> class WDL_PtrList
PTRTYPE *Get(int index) { if (GetList() && index >= 0 && index < GetSize()) return GetList()[index]; return NULL; }
int GetSize(void) { return m_hb.GetSize()/sizeof(PTRTYPE *); }
+ int Find(PTRTYPE *p)
+ {
+ int x;
+ if (p) for (x = 0; x < GetSize(); x ++) if (Get(x) == p) return x;
+ return -1;
+ }
+
PTRTYPE *Add(PTRTYPE *item)
{
int s=GetSize();
@@ -94,18 +101,33 @@ template<class PTRTYPE> class WDL_PtrList
return (GetList()[x] = item);
}
- void Delete(int index)
+ void Delete(int index, bool wantDelete=false, void (*delfunc)(void *)=NULL)
{
PTRTYPE **list=GetList();
int size=GetSize();
if (list && index >= 0 && index < size)
{
+ if (wantDelete)
+ {
+ if (delfunc) delfunc(Get(index));
+ else delete Get(index);
+ }
if (index < --size) memcpy(list+index,list+index+1,sizeof(PTRTYPE *)*(size-index));
m_hb.Resize(size * sizeof(PTRTYPE*));
}
}
- void Empty()
+ void Empty(bool wantDelete=false, void (*delfunc)(void *)=NULL)
{
+ if (wantDelete)
+ {
+ int x;
+ for (x = GetSize()-1; x >= 0; x --)
+ {
+ if (delfunc) delfunc(Get(x));
+ else delete Get(x);
+ m_hb.Resize(x*sizeof(PTRTYPE *),false);
+ }
+ }
m_hb.Resize(0);
}
View
26 WDL/queue.h
@@ -63,11 +63,13 @@ class WDL_Queue
{
}
- void Add(const void *buf, int len)
+ void *Add(const void *buf, int len)
{
int olen=m_hb.GetSize();
- void *obuf=m_hb.Resize(olen+len);
- if (obuf) memcpy((char*)obuf+olen,buf,len);
+ void *obuf=m_hb.Resize(olen+len,false);
+ if (!obuf) return 0;
+ if (buf) memcpy((char*)obuf+olen,buf,len);
+ return (char*)obuf+olen;
}
int GetSize()
@@ -87,12 +89,18 @@ class WDL_Queue
return m_hb.GetSize() - m_pos;
}
+ void Clear()
+ {
+ m_pos=0;
+ m_hb.Resize(0,false);
+ }
+
void Advance(int bytecnt)
{
m_pos+=bytecnt;
}
- void Compact()
+ void Compact(bool allocdown=false)
{
if (m_pos > 0)
{
@@ -100,14 +108,18 @@ class WDL_Queue
if (m_pos < olen)
{
void *a=m_hb.Get();
- if (a) memcpy(a,(char*)a+m_pos,olen-m_pos);
- m_hb.Resize(olen-m_pos);
+ if (a) memmove(a,(char*)a+m_pos,olen-m_pos);
+ m_hb.Resize(olen-m_pos,allocdown);
}
- else m_hb.Resize(0);
+ else m_hb.Resize(0,allocdown);
m_pos=0;
}
}
+ void SetGranul(int granul)
+ {
+ m_hb.SetGranul(granul);
+ }
private:
View
10 WDL/sha.cpp
@@ -46,13 +46,6 @@
*/
-#ifdef _WIN32
-#include <windows.h>
-#else
-#include <stdlib.h>
-#include <string.h>
-#endif
-
#include "sha.h"
/// sha
@@ -71,7 +64,8 @@ void WDL_SHA1::reset()
H[2] = 0x98badcfeL;
H[3] = 0x10325476L;
H[4] = 0xc3d2e1f0L;
- memset(W,0,sizeof(W));
+ int x;
+ for (x = 0; x < sizeof(W)/sizeof(W[0]); x ++) W[x]=0;
}
View
52 WDL/string.h
@@ -52,6 +52,7 @@
#include "heapbuf.h"
+#ifndef WDL_STRING_IMPL_ONLY
class WDL_String
{
public:
@@ -70,8 +71,20 @@ class WDL_String
~WDL_String()
{
}
+#define WDL_STRING_PREFIX
+#else
+#define WDL_STRING_PREFIX WDL_String::
+#endif
- void Set(const char *str, int maxlen=0)
+ void WDL_STRING_PREFIX Set(const char *str, int maxlen
+#ifdef WDL_STRING_INTF_ONLY
+ =0);
+#else
+#ifdef WDL_STRING_IMPL_ONLY
+ )
+#else
+ =0)
+#endif
{
int s=strlen(str);
if (maxlen && s > maxlen) s=maxlen;
@@ -83,8 +96,17 @@ class WDL_String
newbuf[s]=0;
}
}
+#endif
- void Append(const char *str, int maxlen=0)
+ void WDL_STRING_PREFIX Append(const char *str, int maxlen
+#ifdef WDL_STRING_INTF_ONLY
+ =0);
+#else
+#ifdef WDL_STRING_IMPL_ONLY
+ )
+#else
+ =0)
+#endif
{
int s=strlen(str);
if (maxlen && s > maxlen) s=maxlen;
@@ -98,8 +120,12 @@ class WDL_String
newbuf[olds+s]=0;
}
}
+#endif
- void DeleteSub(int position, int len)
+ void WDL_STRING_PREFIX DeleteSub(int position, int len)
+#ifdef WDL_STRING_INTF_ONLY
+ ;
+#else
{
char *p=Get();
if (!p || !*p) return;
@@ -109,8 +135,17 @@ class WDL_String
if (position+len > l) len=l-position;
memmove(p+position,p+position+len,l-position-len+1); // +1 for null
}
+#endif
- void Insert(const char *str, int position, int maxlen=0)
+ void WDL_STRING_PREFIX Insert(const char *str, int position, int maxlen
+#ifdef WDL_STRING_INTF_ONLY
+ =0);
+#else
+#ifdef WDL_STRING_IMPL_ONLY
+ )
+#else
+ =0)
+#endif
{
int sl=strlen(Get());
if (position > sl) position=sl;
@@ -125,13 +160,19 @@ class WDL_String
memcpy(cur+position,str,ilen);
cur[sl+ilen]=0;
}
+#endif
- void SetLen(int length) // sets number of chars allocated for string, not including null terminator
+ void WDL_STRING_PREFIX SetLen(int length) // sets number of chars allocated for string, not including null terminator
+#ifdef WDL_STRING_INTF_ONLY
+ ;
+#else
{ // can use to resize down, too, or resize up for a sprintf() etc
char *b=(char*)m_hb.Resize(length+1);
if (b) b[length]=0;
}
+#endif
+#ifndef WDL_STRING_IMPL_ONLY
char *Get()
{
if (m_hb.Get()) return (char *)m_hb.Get();
@@ -141,6 +182,7 @@ class WDL_String
private:
WDL_HeapBuf m_hb;
};
+#endif
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.