EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EVEUtils.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-common.h"
27 
29 #include "utils/EVEUtils.h"
30 
31 bool IsPrintable( const PyString* str )
32 {
33  return IsPrintable( str->content() );
34 }
35 
36 bool IsPrintable( const PyWString* str )
37 {
38  // how to do it correctly?
39  return IsPrintable( str->content() );
40 }
41 
42 bool DBTYPE_IsCompatible( DBTYPE type, const PyRep* rep )
43 {
44 // Helper macro, checks type and range
45 #define CheckTypeRangeUnsigned( type, lower_bound, upper_bound ) \
46  ( rep->Is##type() && (int64)rep->As##type()->value() >= lower_bound && (int64)rep->As##type()->value() <= upper_bound )
47 #define CheckTypeRange( type, lower_bound, upper_bound ) \
48  ( rep->Is##type() && rep->As##type()->value() >= lower_bound && rep->As##type()->value() <= upper_bound )
49 
50  if( rep->IsNone() )
51  // represents NULL
52  return true;
53 
54  return true;
55 
56  switch( type )
57  {
58  case DBTYPE_UI8:
59  case DBTYPE_CY:
60  case DBTYPE_FILETIME:
61  return ( CheckTypeRangeUnsigned( Int, 0LL, 0xFFFFFFFFFFFFFFFFLL )
62  || CheckTypeRangeUnsigned( Long, 0LL, 0xFFFFFFFFFFFFFFFFLL )
63  || CheckTypeRangeUnsigned( Float, 0LL, 0xFFFFFFFFFFFFFFFFLL ) );
64  case DBTYPE_UI4:
65  return ( CheckTypeRangeUnsigned( Int, 0L, 0xFFFFFFFFL )
66  || CheckTypeRangeUnsigned( Long, 0L, 0xFFFFFFFFL )
67  || CheckTypeRangeUnsigned( Float, 0L, 0xFFFFFFFFL ) );
68  case DBTYPE_UI2:
69  return ( CheckTypeRangeUnsigned( Int, 0, 0xFFFF )
70  || CheckTypeRangeUnsigned( Long, 0, 0xFFFF )
71  || CheckTypeRangeUnsigned( Float, 0, 0xFFFF ) );
72  case DBTYPE_UI1:
73  return ( CheckTypeRangeUnsigned( Int, 0, 0xFF )
74  || CheckTypeRangeUnsigned( Long, 0, 0xFF )
75  || CheckTypeRangeUnsigned( Float, 0, 0xFF ) );
76 
77  case DBTYPE_I8:
78  return ( CheckTypeRange( Int, -0x7FFFFFFFFFFFFFFFLL, 0x7FFFFFFFFFFFFFFFLL )
79  || CheckTypeRange( Long, -0x7FFFFFFFFFFFFFFFLL, 0x7FFFFFFFFFFFFFFFLL )
80  || CheckTypeRange( Float, -0x7FFFFFFFFFFFFFFFLL, 0x7FFFFFFFFFFFFFFFLL ) );
81  case DBTYPE_I4:
82  return ( CheckTypeRange( Int, -0x7FFFFFFFL, 0x7FFFFFFFL )
83  || CheckTypeRange( Long, -0x7FFFFFFFL, 0x7FFFFFFFL )
84  || CheckTypeRange( Float, -0x7FFFFFFFL, 0x7FFFFFFFL ) );
85  case DBTYPE_I2:
86  return ( CheckTypeRange( Int, -0x7FFF, 0x7FFF )
87  || CheckTypeRange( Long, -0x7FFF, 0x7FFF )
88  || CheckTypeRange( Float, -0x7FFF, 0x7FFF ) );
89  case DBTYPE_I1:
90  return ( CheckTypeRange( Int, -0x7F, 0x7F )
91  || CheckTypeRange( Long, -0x7F, 0x7F )
92  || CheckTypeRange( Float, -0x7F, 0x7F ) );
93 
94  case DBTYPE_R8:
95  return ( CheckTypeRange( Int, -DBL_MAX, DBL_MAX )
96  || CheckTypeRange( Long, -DBL_MAX, DBL_MAX )
97  || CheckTypeRange( Float, -DBL_MAX, DBL_MAX ) );
98  case DBTYPE_R4:
99  return ( CheckTypeRange( Int, -FLT_MAX, FLT_MAX )
100  || CheckTypeRange( Long, -FLT_MAX, FLT_MAX )
101  || CheckTypeRange( Float, -FLT_MAX, FLT_MAX ) );
102 
103  case DBTYPE_BOOL:
104  return rep->IsBool();
105 
106  case DBTYPE_BYTES:
107  return rep->IsBuffer();
108 
109  // this looks like a horrible hack, and it is - but one that is used on live!
110  // this works because STR type stuff is just tacked on to the marshal object
111  case DBTYPE_STR:
112  return true;
113  //return rep->IsString();
114 
115  case DBTYPE_WSTR:
116  return rep->IsWString();
117 
118  case DBTYPE_EMPTY:
119  case DBTYPE_ERROR:
120  return false;
121  }
122 
123  return false;
124 
125 #undef CheckTypeRange
126 #undef CheckTypeRangeUnsigned
127 }
Base Python wire object.
Definition: PyRep.h:66
Python string.
Definition: PyRep.h:430
bool IsBuffer() const
Definition: PyRep.h:104
const std::string & content() const
Get the PyWString content.
Definition: PyRep.h:499
Python wide string.
Definition: PyRep.h:475
#define CheckTypeRange(type, lower_bound, upper_bound)
bool IsPrintable(const PyString *str)
Checks whether string is printable.
Definition: EVEUtils.cpp:31
bool DBTYPE_IsCompatible(DBTYPE type, const PyRep *rep)
Definition: EVEUtils.cpp:42
DBTYPE
Definition: dbtype.h:67
bool IsNone() const
Definition: PyRep.h:111
bool IsBool() const
Definition: PyRep.h:103
const std::string & content() const
Get the PyString content.
Definition: PyRep.h:458
#define CheckTypeRangeUnsigned(type, lower_bound, upper_bound)
bool IsWString() const
Definition: PyRep.h:106