From: <bo...@us...> - 2003-11-22 02:50:21
|
Update of /cvsroot/sharedaemon/core/src In directory sc8-pr-cvs1:/tmp/cvs-serv7741 Modified Files: ParseClass.h Log Message: Added support for reading values to variables of different types as in the ed2k packet format string. I.e. you ca now use e.g. u32 a; Get(a,"msglen") where the format string is '<WORD>msglen' Index: ParseClass.h =================================================================== RCS file: /cvsroot/sharedaemon/core/src/ParseClass.h,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- ParseClass.h 20 Nov 2003 16:27:25 -0000 1.8 +++ ParseClass.h 22 Nov 2003 02:50:17 -0000 1.9 @@ -18,6 +18,9 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#ifndef PARSECLASS_H +#define PARSECLASS_H + /* * *** ATTENTION *** ATTENTION *** ATTENTION *** * @@ -34,6 +37,7 @@ #include "osdep.h" // For PRINTF_FORMAT #include "util.h" #include "mprintf.h" +#include "parse-ed2k.h" #include <cassert> #include <cstdarg> @@ -41,6 +45,36 @@ #include <cstdlib> #include <new> +/* +#define STRUCT 0 +#define BITMAP 1 +#define TAG 9 +#define EMPTY 11 +#define NUM_TYPES 12 +*/ + +/* + * Run Time Type Information. + * + * Special care must be taken on BYTE and CHAR as one cannot distinquished + * between them. Thus GetTypeID(BYTE) == GetTypeID(CHAR)! + */ +template <class T> int GetTypeID( T &t ) { generate=error+on+invalid+type; } +inline int GetTypeID( char &t ) { return CHAR; } +inline int GetTypeID( unsigned char &t ) { return BYTE; } +inline int GetTypeID( signed char &t ) { return BYTE; } +inline int GetTypeID( short &t ) { return WORD; } +inline int GetTypeID( unsigned short &t ) { return WORD; } +inline int GetTypeID( long &t ) { return DWORD; } +inline int GetTypeID( unsigned long &t ) { return DWORD; } +inline int GetTypeID( int &t ) { return DWORD; } +inline int GetTypeID( unsigned int &t ) { return DWORD; } +inline int GetTypeID( range &t ) { return RANGE; } +inline int GetTypeID( ipport &t ) { return IPPORT; } +inline int GetTypeID( hash &t ) { return HASH; } +inline int GetTypeID( tag &t ) { return TAG; } +inline int GetTypeID( bitmap &t ) { return BITMAP; } + #define ISSTRUCT(x) true #define ISARRAY(x) true @@ -190,6 +224,38 @@ size_t IncrementalReadNonArray(const u8 * buffer,size_t buffsize); size_t IncrementalReadArray(const u8 * buffer,size_t buffsize); + template <class D,class S> void Cast(D & dst,const S & src) const { + fprintf(stderr,"%s:%s:%i: FATAL: Cannot cast types.\n" + ,__FILE__,__FUNCTION__,__LINE__ + ); + abort(); + } + + void Cast(unsigned long & dst,const u16 & src) const { dst=src; } + void Cast(unsigned int & dst,const u16 & src) const { dst=src; } + void Cast(unsigned short & dst,const u16 & src) const { dst=src; } + + void Cast(unsigned long & dst,const u8 & src) const { dst=src; } + void Cast(unsigned int & dst,const u8 & src) const { dst=src; } + void Cast(unsigned short & dst,const u8 & src) const { dst=src; } + +/* +inline int GetTypeID( char &t ) { return CHAR; } +inline int GetTypeID( unsigned char &t ) { return BYTE; } +inline int GetTypeID( signed char &t ) { return BYTE; } +inline int GetTypeID( short &t ) { return WORD; } +inline int GetTypeID( unsigned short &t ) { return WORD; } +inline int GetTypeID( long &t ) { return DWORD; } +inline int GetTypeID( unsigned long &t ) { return DWORD; } +inline int GetTypeID( int &t ) { return DWORD; } +inline int GetTypeID( unsigned int &t ) { return DWORD; } +inline int GetTypeID( range &t ) { return RANGE; } +inline int GetTypeID( ipport &t ) { return IPPORT; } +inline int GetTypeID( hash &t ) { return HASH; } +inline int GetTypeID( tag &t ) { return TAG; } +inline int GetTypeID( bitmap &t ) { return BITMAP; } +*/ + /* * Get * @@ -211,63 +277,59 @@ fprintf( stderr, "%s:%s:%i: expr=\"%s\" is invalid or index=%i " - "is out of range\n", - __FILE__,__FUNCTION__,__LINE__,expr,index + "is out of range\n" + ,__FILE__,__FUNCTION__,__LINE__,expr,index ); abort(); } -/* - printf("*** MARK *** "); fflush(stdout); -*/ - if (!struc && !array) { -/* - printf(" !struc && !array "); fflush(stdout); -*/ - var=*(T*)(data+size*index+ps->offset); + void * ptr=data+size*index+ps->offset; + + if (GetTypeID(var) == ps->type_id) { + var=*(T*)ptr; + } else { + int tid=GetTypeID(var); + + switch (ps->type_id) { + case WORD: + if (tid==DWORD) { + Cast(var,*(u16*)ptr); + return; + } + break; + case BYTE: + if (tid==DWORD || tid==WORD) { + Cast(var,*(u8 *)ptr); + return; + } + break; + default: + /* nothing to do here */ + break; + } + + fprintf(stderr, + "%s:%s:%i: FATAL: Type doesn " + "not match. %s was expected " + "but %s was given\n" + ,__FILE__,__FUNCTION__,__LINE__ + ,::type[ps->type_id].type_name + ,tid>=0 + ?::type[tid].type_name + :"<special>" + ); + abort(); + } } else { -/* - printf("!(!struc && !array) "); fflush(stdout); -*/ ParseClass * PC=*(ParseClass**) (data+size*(index+1)-sizeof(ParseClass*)); -/* - printf("PC=%p, ",reinterpret_cast<void*>(PC)); - fflush(stdout); -*/ if (struc) { -/* - printf("struct\n"); - printf( - ">>PC[ps->referer_id]." - "Get(var,next_index,subexpr)\n" - ); -*/ PC[ps->referer_id].Get(var,next_index,subexpr); -/* - printf( - "<<PC[ps->referer_id]." - "Get(var,next_index,subexpr)\n" - ); -*/ } else { -/* - printf("!struct\n"); - printf( - "PC->data=%p, PC->size=%i, " - "next_index=%i, PC->nume=%i\n" - ,PC->data,PC->size, - next_index,PC->nume - ); -*/ var=*(T*)(PC->data+PC->size*next_index); } } - -/* - printf("*** MARK 2 *** "); fflush(stdout); -*/ } /* @@ -433,3 +495,5 @@ Set(var,0,expr); } }; + +#endif // #ifndef PARSECLASS_H |