Initial MSVC 2008 projects workspace

This commit is contained in:
alexey.min
2012-02-01 05:25:08 +00:00
commit 03de3bdc95
1446 changed files with 476853 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
rem %1 = l2c_utils_d
rem %2 = output exe dir: m:\dev\l2\servers\l2c\_build\utils_Debug\
rem
rem ..\..\dist
rem echo 1 = %1
rem echo 2 = %2
set out=..\..\out\dist_l2c
rem Copy DLL
copy /y %2\%1.dll %2\%out%
rem Copy PDB
copy /y %2\%1.pdb %2\%out%

View File

@@ -0,0 +1,228 @@
#include "pch.h"
#include "l2c_utils.h"
#include "l2c_configFile.h"
L2C_ConfigFile::L2C_ConfigFile()
{
int i;
for( i=0; i<MAX_SETTINGS; i++ ) m_varName[i] = m_varValue[i] = NULL;
m_nVars = 0;
}
L2C_ConfigFile::~L2C_ConfigFile()
{
clear();
}
void L2C_ConfigFile::clear()
{
int i;
for( i=0; i<MAX_SETTINGS; i++ )
{
if( m_varName[i] ) free( m_varName[i] );
if( m_varValue[i] ) free( m_varValue[i] );
m_varName[i] = m_varValue[i] = NULL;
}
m_nVars = 0;
}
int L2C_ConfigFile::getVarsCount() const
{
return m_nVars;
}
bool L2C_ConfigFile::load( const wchar_t *filename )
{
FILE *f = _wfopen( filename, L"rt" );
if( !f ) return false;
//
wchar_t line[1024] = {0};
int nVarsAdded = 0;
while( !feof( f ) )
{
line[0] = 0;
fgetws( line, 1023, f );
if( line[0] == 0 ) continue;
if( line[0] == L'#' ) continue;
// process line
wchar_t varName[256] = {0};
wchar_t varValue[256] = {0};
wchar_t *pVarName = varName;
wchar_t *pVarValue = varValue;
wchar_t *pLine = line;
// copy var name until ' ' or '='
int nCharsCopied = 0;
while( (nCharsCopied < 255) && ((*pLine) != 0) && ((*pLine) != L' ') && ((*pLine) != L'=') )
{
(*pVarName) = (*pLine);
pVarName++;
pLine++;
nCharsCopied++;
}
(*pVarName) = 0;
// find '=' in line (or end of line)
while( (*pLine) && ((*pLine) != L'=') ) pLine++;
// line end? (no '=' in line)
if( (*pLine) == 0 ) continue;
// if we here, we found '='; move pointer once
pLine++;
// line end? no parameter
if( (*pLine) == 0 ) continue;
// pass all spaces in line
while( (*pLine) == L' ' ) pLine++;
// check for line end? (no var value)
if( (*pLine) )
{
// copy until space or EOL
nCharsCopied = 0;
while( (nCharsCopied < 255) && (*pLine) && ((*pLine) != L' ') && ((*pLine) != L'\r') &&
((*pLine) != L'\n') && ((*pLine) != L'\t') )
{
(*pVarValue) = (*pLine);
pVarValue++;
pLine++;
nCharsCopied++;
}
(*pVarValue) = 0;
}
// add new var
m_varName[nVarsAdded] = _wcsdup( varName );
if( varValue[0] > 0 )
m_varValue[nVarsAdded] = _wcsdup( varValue );
else
m_varValue[nVarsAdded] = NULL;
// inc
nVarsAdded++;
if( nVarsAdded >= L2C_ConfigFile::MAX_SETTINGS ) break; // overflow...
}
//
fclose( f );
m_nVars = nVarsAdded;
return true;
}
const wchar_t *L2C_ConfigFile::getValueStrW( const wchar_t *varName )
{
if( !varName ) return NULL;
int i;
for( i=0; i<m_nVars; i++ )
{
if( m_varName[i] )
if( _wcsicmp( m_varName[i], varName ) == 0 )
return m_varValue[i];
}
return NULL;
}
void L2C_ConfigFile::getValueStrA( const wchar_t *varName, char *out, size_t maxCount )
{
if( !varName || maxCount<1 ) return;
out[0] = 0;
const wchar_t *varValue = getValueStrW( varName );
if( varValue ) l2c_unicode_to_ansi( varValue, out, maxCount );
}
int L2C_ConfigFile::getValueInt( const wchar_t *varName )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return 0;
int i = 0;
swscanf( varValue, L"%d", &i );
return i;
}
long long int L2C_ConfigFile::getValueInt64( const wchar_t *varName )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return 0;
long long int i = 0;
swscanf( varValue, L"%I64d", &i );
return i;
}
bool L2C_ConfigFile::getValueBool( const wchar_t *varName )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return false;
if( _wcsicmp( varValue, L"true" ) == 0 ) return true;
//if( _wcsicmp( varValue, L"false" ) == 0 ) return false;
//if( _wcsicmp( varValue, L"0" ) == 0 ) return false;
return false;
}
double L2C_ConfigFile::getValueDouble( const wchar_t *varName )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return 0.0;
double d = 0.0;
swscanf( varValue, L"%lf", &d );
return d;
}
const wchar_t *L2C_ConfigFile::getValueStrW( const wchar_t *varName, const wchar_t *defVal )
{
if( !varName ) return defVal;
int i;
for( i=0; i<m_nVars; i++ )
{
if( m_varName[i] )
if( _wcsicmp( m_varName[i], varName ) == 0 )
return m_varValue[i];
}
return defVal;
}
void L2C_ConfigFile::getValueStrA( const wchar_t *varName, char *out, size_t maxCount, const char *defVal )
{
if( !varName || maxCount<1 ) return;
out[0] = 0;
const wchar_t *varValue = getValueStrW( varName );
if( varValue )
{
l2c_unicode_to_ansi( varValue, out, maxCount );
}
else
{
if( defVal )
{
strncpy( out, defVal, maxCount );
out[maxCount-1] = 0;
}
}
}
int L2C_ConfigFile::getValueInt( const wchar_t *varName, int defVal )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return defVal;
int i = defVal;
swscanf( varValue, L"%d", &i );
return i;
}
long long int L2C_ConfigFile::getValueInt64( const wchar_t *varName, long long int defVal )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return defVal;
long long int i = defVal;
swscanf( varValue, L"%I64d", &i );
return i;
}
bool L2C_ConfigFile::getValueBool( const wchar_t *varName, bool defVal )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return defVal;
if( _wcsicmp( varValue, L"true" ) == 0 ) return true;
return false;
}
double L2C_ConfigFile::getValueDouble( const wchar_t *varName, double defVal )
{
const wchar_t *varValue = getValueStrW( varName );
if( !varValue ) return defVal;
double d = defVal;
swscanf( varValue, L"%lf", &d );
return d;
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include "l2c_utils.h"
class L2C_UTILS_API L2C_ConfigFile
{
public:
static const int MAX_SETTINGS = 256;
public:
L2C_ConfigFile();
~L2C_ConfigFile();
public:
bool load( const wchar_t *filename );
void clear();
public:
const wchar_t *getValueStrW( const wchar_t *varName );
void getValueStrA( const wchar_t *varName, char *out, size_t maxCount );
int getValueInt( const wchar_t *varName );
long long int getValueInt64( const wchar_t *varName );
bool getValueBool( const wchar_t *varName );
double getValueDouble( const wchar_t *varName );
public:
const wchar_t *getValueStrW( const wchar_t *varName, const wchar_t *defVal );
void getValueStrA( const wchar_t *varName, char *out, size_t maxCount, const char *defVal );
int getValueInt( const wchar_t *varName, int defVal );
long long int getValueInt64( const wchar_t *varName, long long int defVal );
bool getValueBool( const wchar_t *varName, bool defVal );
double getValueDouble( const wchar_t *varName, double defVal );
public:
int getVarsCount() const;
protected:
int m_nVars;
wchar_t *m_varName[MAX_SETTINGS];
wchar_t *m_varValue[MAX_SETTINGS];
};

566
libl2c_utils/l2c_db.cpp Normal file
View File

@@ -0,0 +1,566 @@
#include "pch.h"
#include "l2c_utils.h"
#include "l2c_db.h"
MysqlQuery::MysqlQuery()
{
q = NULL;
result = NULL;
num_rows = num_fields = 0;
strcpy( NULL_RET, "NULL" );
}
MysqlQuery::MysqlQuery( const wchar_t *_Format, ... )
{
q = NULL;
result = NULL;
num_rows = num_fields = 0;
strcpy( NULL_RET, "NULL" );
if( _Format )
{
va_list arglist;
va_start( arglist, _Format );
int n = _vscwprintf( _Format, arglist );
if( n >= 1 )
{
q = (wchar_t *)malloc( 2*n + 16 );
if( q )
{
va_start( arglist, _Format );
vswprintf( q, n+2, _Format, arglist );
}
}
}
}
MysqlQuery::~MysqlQuery()
{
clear();
}
void MysqlQuery::clear()
{
if( q ) free( q );
q = NULL;
if( result ) mysql_free_result( result );
result = NULL;
num_rows = num_fields = 0;
row = NULL;
}
int MysqlQuery::getNumRows() const
{
return num_rows;
}
int MysqlQuery::getNumFields() const
{
return num_fields;
}
void MysqlQuery::create( const wchar_t *_Format, ... )
{
clear();
va_list _ArgList;
va_start( _ArgList, _Format );
int l = _vscwprintf( _Format, _ArgList ); // number of chars will be printed
if( l <= 1 ) return;
q = (wchar_t *)malloc( l*2 + 2 ); // number of bytes needed
if( !q ) return;
va_start( _ArgList, _Format );
vswprintf( q, l+1, _Format, _ArgList );
}
bool MysqlQuery::getNextRow()
{
if( !result ) return false;
row = mysql_fetch_row( result );
if( row ) return true;
return false;
}
char *MysqlQuery::getFieldStr( int idx )
{
if( !row || !result ) return NULL_RET;
char *ret = NULL;
if( idx>=0 && idx<=(num_fields-1) ) ret = row[idx];
if( !ret ) return NULL_RET; // row[idx] can still be NULL if MySQL table specs ALLOW NULL
return ret;
}
char *MysqlQuery::getFieldStr( const wchar_t *field_name )
{
if( !field_name ) return NULL_RET;
size_t flen = wcslen( field_name );
char *aname = (char *)malloc( flen+1 );
if( !aname ) return NULL_RET;
l2c_unicode_to_ansi( field_name, aname, flen );
char *ret = getFieldStr( aname );
free( aname );
aname = NULL;
return ret;
}
char *MysqlQuery::getFieldStr( const char *field_name )
{
if( !field_name ) return NULL_RET;
if( !result || !row ) return NULL_RET;
if( num_fields < 1 ) return NULL_RET;
MYSQL_FIELD *fields = mysql_fetch_fields( result );
if( !fields ) return NULL_RET;
int i;
int idx = -1;
for( i=0; i<num_fields; i++ )
{
if( _stricmp( fields[i].name, field_name ) == 0 )
{
idx = i;
break;
}
}
if( idx == -1 ) return NULL_RET;
if( row[idx] == NULL ) return NULL_RET; // row[idx] can still be NULL if MySQL table specs ALLOW NULL
return row[idx];
}
int MysqlQuery::getFieldInt( int idx )
{
int i = 0;
char *sret = getFieldStr( idx );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%d", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldInt(): cannot convert [%s] to int", sret );
return i;
}
unsigned int MysqlQuery::getFieldUInt( int idx )
{
unsigned int i = 0;
char *sret = getFieldStr( idx );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%u", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldUInt(): cannot convert [%s] to unsigned int", sret );
return i;
}
long long int MysqlQuery::getFieldInt64( int idx )
{
long long int i = 0;
char *sret = getFieldStr( idx );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%I64d", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldInt64(): cannot convert [%s] to int64", sret );
return true;
}
unsigned long long int MysqlQuery::getFieldUInt64( int idx )
{
unsigned long long int i = 0;
char *sret = getFieldStr( idx );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%I64u", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldUInt64(): cannot convert [%s] to unsigned int64", sret );
return i;
}
double MysqlQuery::getFieldDouble( int idx )
{
double i = 0.0;
char *sret = getFieldStr( idx );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%lf", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldDouble(): cannot convert [%s] to double", sret );
return i;
}
bool MysqlQuery::getFieldBool( int idx )
{
bool i = false;
char *sret = getFieldStr( idx );
if( sret == NULL_RET ) return false;
if( _stricmp( sret, "true" ) == 0 ) { i = true; return true; }
if( _stricmp( sret, "false" ) == 0 ) { i = false; return true; }
int ii = 0;
int r = sscanf( sret, "%d", &ii );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldBool(): cannot convert [%s] to bool", sret );
i = (ii != 0);
return i;
}
int MysqlQuery::getFieldInt( const char *field_name )
{
int i = 0;
char *sret = getFieldStr( field_name );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%d", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldInt(): cannot convert [%s] to int", sret );
return i;
}
unsigned int MysqlQuery::getFieldUInt( const char *field_name )
{
unsigned int i = 0;
char *sret = getFieldStr( field_name );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%u", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldUInt(): cannot convert [%s] to unsigned int", sret );
return i;
}
long long int MysqlQuery::getFieldInt64( const char *field_name )
{
long long int i = 0;
char *sret = getFieldStr( field_name );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%I64d", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldInt64(): cannot convert [%s] to int64", sret );
return i;
}
unsigned long long int MysqlQuery::getFieldUInt64( const char *field_name )
{
unsigned long long int i = 0;
char *sret = getFieldStr( field_name );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%I64u", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldUInt64(): cannot convert [%s] to unsigned int64", sret );
return i;
}
double MysqlQuery::getFieldDouble( const char *field_name )
{
double i = 0.0;
char *sret = getFieldStr( field_name );
//if( sret == NULL_RET ) return false;
int r = sscanf( sret, "%lf", &i );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldDouble(): cannot convert [%s] to double", sret );
return i;
}
bool MysqlQuery::getFieldBool( const char *field_name )
{
bool i = false;
char *sret = getFieldStr( field_name );
if( sret == NULL_RET ) return false;
if( _stricmp( sret, "true" ) == 0 ) { i = true; return true; }
if( _stricmp( sret, "false" ) == 0 ) { i = false; return true; }
int ii = 0;
int r = sscanf( sret, "%d", &ii );
if( r != 1 ) throw L2C_Exception( "MysqlQuery::getFieldBool(): cannot convert [%s] to bool", sret );
i = (ii != 0);
return i;
}
// get field content as unicode strings
void MysqlQuery::getFieldStrW( int idx, wchar_t *out, size_t maxCount )
{
char *ret = getFieldStr( idx );
l2c_ansi_to_unicode( ret, out, maxCount );
}
void MysqlQuery::getFieldStrW( const char *field_name, wchar_t *out, size_t maxCount )
{
char *ret = getFieldStr( field_name );
l2c_ansi_to_unicode( ret, out, maxCount );
}
void MysqlQuery::getFieldStrW( const wchar_t *field_name, wchar_t *out, size_t maxCount )
{
char *ret = getFieldStr( field_name );
l2c_ansi_to_unicode( ret, out, maxCount );
}
MysqlConnection::MysqlConnection()
{
mysql_init( &m_mysql );
m_connected = false;
m_errstr[0] = 0;
return;
}
MysqlConnection::~MysqlConnection()
{
close();
}
char *MysqlConnection::escapeStringMalloc( const char *in )
{
if( !in ) return NULL;
size_t ll = strlen( in );
char *out = (char *)malloc( ll*2 + 2 );
if( !out ) return NULL;
if( m_connected )
mysql_real_escape_string( &m_mysql, out, in, ll );
else
mysql_escape_string( out, in, ll );
return out;
}
bool MysqlConnection::connect( const wchar_t *server, const wchar_t *user, const wchar_t *pass, const wchar_t *db )
{
if( m_connected ) return false;
// set option
my_bool bVal = 1;
int oret = mysql_options( &m_mysql, MYSQL_OPT_RECONNECT, (const char *)&bVal );
if( oret != 0 ) return false;
// convert strings
char aserver[256] = {0};
char auser[256] = {0};
char apass[256] = {0};
char adb[256] = {0};
l2c_unicode_to_ansi( server, aserver, 255 );
l2c_unicode_to_ansi( user, auser, 255 );
l2c_unicode_to_ansi( pass, apass, 255 );
l2c_unicode_to_ansi( db, adb, 255 );
// attempt connect
MYSQL *ret = mysql_real_connect( &m_mysql, aserver, auser, apass, adb, 3306, NULL,
/*CLIENT_FOUND_ROWS |*/ CLIENT_INTERACTIVE | CLIENT_MULTI_STATEMENTS );
if( ret == NULL || ret != &m_mysql ) return false;
m_connected = true;
return true;
}
bool MysqlConnection::close()
{
if( !m_connected ) return false;
mysql_close( &m_mysql );
m_connected = false;
return true;
}
const wchar_t *MysqlConnection::getErrorStr()
{
char *aerr = _strdup( mysql_error( &m_mysql ) );
wcscpy( m_errstr, L"<none>" );
if( aerr ) l2c_ansi_to_unicode( aerr, m_errstr, 255 );
return (const wchar_t *)m_errstr;
}
bool MysqlConnection::isConnected()
{
if( m_connected )
{
int ret = mysql_ping( &m_mysql );
if( ret != 0 )
{
close();
return false;
}
return true;
}
return false;
}
bool MysqlConnection::executeQuery( MysqlQuery& q )
{
if( !m_connected ) return false;
if( !q.q ) return false;
size_t qlen = wcslen( q.q );
char *a_query = (char *)malloc( qlen + 1 );
if( !a_query ) return false;
l2c_unicode_to_ansi( q.q, a_query, qlen+1 );
int q_res = mysql_query( &m_mysql, a_query );
free( a_query );
a_query = NULL;
if( q_res == 0 )
{
// no error
q.result = mysql_store_result( &m_mysql );
if( q.result )
{
q.num_fields = (int)mysql_num_fields( q.result );
q.num_rows = (int)mysql_num_rows( q.result );
}
else
{
q.num_fields = mysql_field_count( &m_mysql );
// query was not a SELECT
if( q.num_fields == 0 )
q.num_rows = (int)mysql_affected_rows( &m_mysql );
}
// no error
return true;
}
// error
return false;
}
// memory for static variable
bool MysqlConnectionManager::m_wasLibraryInit = false;
MysqlConnectionManager::MysqlConnectionManager()
{
m_initialized = false;
m_server[0] = m_user[0] = m_pass[0] = m_db[0] = 0;
m_maxConnections = m_numActiveConnections = 0;
m_connections = NULL;
}
MysqlConnectionManager::~MysqlConnectionManager()
{
closeAllConnections();
}
bool MysqlConnectionManager::initialize( int maxConnections, const wchar_t *server, const wchar_t *user,
const wchar_t *pass, const wchar_t *db )
{
if( m_initialized ) return true;
// init lock
InitializeCriticalSectionAndSpinCount( &m_lock, 10 );
Lock();
// init mysql library?
if( !MysqlConnectionManager::m_wasLibraryInit )
{
mysql_server_init( 0, NULL, NULL );
MysqlConnectionManager::m_wasLibraryInit = true;
}
// try allocate
m_connections = (MysqlConnection **)malloc( maxConnections * sizeof(MysqlConnection *) );
if( !m_connections )
{
Unlock();
return false;
}
m_connbusy = (bool *)malloc( maxConnections * sizeof(bool) );
if( !m_connbusy )
{
free( m_connections );
m_connections = NULL;
Unlock();
return false;
}
// init nulls
int i;
for( i=0; i<maxConnections; i++ )
{
m_connections[i] = NULL;
m_connbusy[i] = false;
}
// copy data
wcscpy( m_server, server );
wcscpy( m_user, user );
m_pass[0] = 0;
if( pass ) wcscpy( m_pass, pass );
wcscpy( m_db, db );
m_maxConnections = maxConnections;
//
m_initialized = true;
Unlock();
return true;
}
void MysqlConnectionManager::closeAllConnections()
{
if( !m_initialized ) return;
Lock();
int i;
for( i=0; i<m_maxConnections; i++ )
{
if( m_connections[i] )
{
m_numActiveConnections--;
m_connections[i]->close();
delete m_connections[i];
m_connections[i] = NULL;
m_connbusy[i] = false;
}
}
free( m_connections );
m_connections = NULL;
free( m_connbusy );
m_connbusy = NULL;
m_initialized = false;
Unlock();
DeleteCriticalSection( &m_lock );
}
MysqlConnection *MysqlConnectionManager::getConnection()
{
MysqlConnection *ret = NULL;
int i = 0;
Lock();
for( i=0; i<m_maxConnections; i++ )
{
if( !m_connections[i] )
{
m_connections[i] = new MysqlConnection();
m_connections[i]->connect( m_server, m_user, m_pass, m_db );
m_connbusy[i] = true;
ret = m_connections[i];
m_numActiveConnections++;
break;
}
if( m_connections[i] && !m_connbusy[i] )
{
if( !m_connections[i]->isConnected() )
m_connections[i]->connect( m_server, m_user, m_pass, m_db );
m_connbusy[i] = true;
ret = m_connections[i];
m_numActiveConnections++;
break;
}
}
Unlock();
return ret;
}
bool MysqlConnectionManager::releaseConnection( MysqlConnection *con )
{
if( !con ) return false;
int i = 0;
bool ret = false;
Lock();
for( i=0; i<m_maxConnections; i++ )
{
if( m_connections[i] == con )
{
m_connbusy[i] = false;
// close connections with index > 0
if( i > 0 ) m_connections[i]->close();
// at least 1 conn with idx 0 will be active all the time
m_numActiveConnections--;
ret = true;
break;
}
}
Unlock();
return ret;
}
int MysqlConnectionManager::getCountActiveConnections() const
{
return m_numActiveConnections;
}
int MysqlConnectionManager::getCountMaxConnections() const
{
return m_maxConnections;
}
void MysqlConnectionManager::Lock()
{
EnterCriticalSection( &m_lock );
}
void MysqlConnectionManager::Unlock()
{
LeaveCriticalSection( &m_lock );
}

104
libl2c_utils/l2c_db.h Normal file
View File

@@ -0,0 +1,104 @@
#pragma once
#include "l2c_utils.h"
#include "l2c_exception.h"
class L2C_UTILS_API MysqlQuery
{
friend class MysqlConnection;
public:
MysqlQuery();
MysqlQuery( const wchar_t *_Format, ... );
~MysqlQuery();
public:
void clear();
void create( const wchar_t *_Format, ... );
int getNumRows() const;
int getNumFields() const;
public:
bool getNextRow();
// get field content by field index
char *getFieldStr( int idx );
char *getFieldStr( const wchar_t *field_name ); // really needed?
char *getFieldStr( const char *field_name );
// get field content as unicode strings
void getFieldStrW( int idx, wchar_t *out, size_t maxCount );
void getFieldStrW( const char *field_name, wchar_t *out, size_t maxCount );
void getFieldStrW( const wchar_t *field_name, wchar_t *out, size_t maxCount );
// typecasts
int getFieldInt( int idx );
unsigned int getFieldUInt( int idx );
long long int getFieldInt64( int idx );
unsigned long long int getFieldUInt64( int idx );
double getFieldDouble( int idx );
bool getFieldBool( int idx );
// typecasts - get field by name
int getFieldInt( const char *field_name );
unsigned int getFieldUInt( const char *field_name );
long long int getFieldInt64( const char *field_name );
unsigned long long int getFieldUInt64( const char *field_name );
double getFieldDouble( const char *field_name );
bool getFieldBool( const char *field_name );
protected:
wchar_t *q;
MYSQL_RES *result;
MYSQL_ROW row;
int num_rows;
int num_fields;
char NULL_RET[8];
};
class L2C_UTILS_API MysqlConnection
{
friend class MysqlConnectionManager;
protected:
MysqlConnection();
~MysqlConnection();
protected:
bool connect( const wchar_t *server, const wchar_t *user, const wchar_t *pass, const wchar_t *db );
bool close();
public:
char *escapeStringMalloc( const char *in );
const wchar_t *getErrorStr();
bool isConnected();
bool executeQuery( MysqlQuery& q );
protected:
MYSQL m_mysql;
bool m_connected;
wchar_t m_errstr[256];
};
class L2C_UTILS_API MysqlConnectionManager
{
public:
MysqlConnectionManager();
~MysqlConnectionManager();
bool initialize( int maxConnections, const wchar_t *server, const wchar_t *user,
const wchar_t *pass, const wchar_t *db );
void closeAllConnections();
public:
MysqlConnection *getConnection();
bool releaseConnection( MysqlConnection *con );
int getCountActiveConnections() const;
int getCountMaxConnections() const;
protected:
static bool m_wasLibraryInit;
bool m_initialized;
wchar_t m_server[128];
wchar_t m_user[128];
wchar_t m_pass[128];
wchar_t m_db[128];
int m_maxConnections;
int m_numActiveConnections;
MysqlConnection **m_connections;
bool *m_connbusy;
CRITICAL_SECTION m_lock;
protected:
void Lock();
void Unlock();
};

View File

@@ -0,0 +1,52 @@
#include "pch.h"
#include "l2c_utils.h"
#include "l2c_exception.h"
#include <mysql.h>
L2C_Exception::L2C_Exception()
{
m_what = NULL;
}
L2C_Exception::L2C_Exception( const char *_Format, ... )
{
va_list _ArgList;
va_start( _ArgList, _Format );
int n = _vscprintf( _Format, _ArgList );
if( n >= 1 )
{
m_what = (char *)malloc( n+16 );
if( m_what )
{
va_start( _ArgList, _Format );
vsprintf( m_what, _Format, _ArgList );
}
}
}
L2C_Exception::~L2C_Exception()
{
if( m_what )
{
free( m_what );
m_what = NULL;
}
}
const char *L2C_Exception::what() const
{
return m_what;
}
MysqlException::MysqlException( void *pvMysql )
{
m_what = NULL;
MYSQL *mys = (MYSQL *)pvMysql;
if( mys )
{
const char *myserr = mysql_error( mys );
if( myserr )
m_what = _strdup( myserr );
}
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include "l2c_utils.h"
class L2C_UTILS_API L2C_Exception
{
public:
L2C_Exception();
L2C_Exception( const char *_Format, ... );
~L2C_Exception();
public:
const char *what() const;
protected:
char *m_what;
};
class L2C_UTILS_API MysqlException: public L2C_Exception
{
public:
MysqlException( void *pvMysql );
};

46
libl2c_utils/l2c_lock.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "pch.h"
#include "l2c_lock.h"
CriticalSection::CriticalSection()
{
m_locked = false;
InitializeCriticalSectionAndSpinCount( &m_lock, 10 );
}
CriticalSection::~CriticalSection()
{
if( m_locked ) LeaveCriticalSection( &m_lock );
m_locked = false;
DeleteCriticalSection( &m_lock );
}
void CriticalSection::Lock()
{
if( m_locked ) return;
EnterCriticalSection( &m_lock );
m_locked = true;
}
void CriticalSection::Unlock()
{
if( !m_locked ) return;
LeaveCriticalSection( &m_lock );
m_locked = false;
}
bool CriticalSection::TryLock( unsigned int wait_milliseconds )
{
if( m_locked ) return true;
if( TryEnterCriticalSection( &m_lock ) ) return true;
if( wait_milliseconds > 0 )
{
Sleep( (DWORD)wait_milliseconds );
if( TryEnterCriticalSection( &m_lock ) ) return true;
}
return false;
}
bool CriticalSection::isLocked() const
{
return m_locked;
}

19
libl2c_utils/l2c_lock.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "l2c_utils.h"
class L2C_UTILS_API CriticalSection
{
public:
CriticalSection();
~CriticalSection();
public:
void Lock();
void Unlock();
bool TryLock( unsigned int wait_milliseconds = 0 );
bool isLocked() const;
protected:
CRITICAL_SECTION m_lock;
bool m_locked;
};

238
libl2c_utils/l2c_utils.cpp Normal file
View File

@@ -0,0 +1,238 @@
#include "pch.h"
#include "l2c_utils.h"
// This is an example of an exported variable
//L2C_UTILS_API int nl2c_utils=0;
L2C_UTILS_API void l2c_ansi_to_unicode( const char *ansi, wchar_t *unicode, size_t maxLen )
{
memset( unicode, 0, maxLen*2 );
MultiByteToWideChar( CP_ACP, 0, ansi, -1, unicode, (int)maxLen );
unicode[maxLen-1] = 0;
}
L2C_UTILS_API void l2c_unicode_to_ansi( const wchar_t *unicode, char *ansi, size_t maxLen )
{
memset( ansi, 0, maxLen );
WideCharToMultiByte( CP_ACP, 0, unicode, -1, ansi, (int)maxLen, NULL, NULL );
ansi[maxLen-1] = 0;
}
const int L2C_BASE64_CHARS_PER_LINE = 72;
typedef enum
{
step_A, step_B, step_C
} base64_encodestep;
typedef struct
{
base64_encodestep step;
char result;
int stepcount;
} base64_encodestate;
typedef enum
{
step_a, step_b, step_c, step_d
} base64_decodestep;
typedef struct
{
base64_decodestep step;
char plainchar;
} base64_decodestate;
void base64_init_encodestate(base64_encodestate* state_in)
{
state_in->step = step_A;
state_in->result = 0;
state_in->stepcount = 0;
}
char base64_encode_value(char value_in)
{
static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
if (value_in > 63) return '=';
return encoding[(int)value_in];
}
int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
{
const char* plainchar = plaintext_in;
const char* const plaintextend = plaintext_in + length_in;
char* codechar = code_out;
char result;
char fragment;
result = state_in->result;
switch (state_in->step)
{
while (1)
{
case step_A:
if (plainchar == plaintextend)
{
state_in->result = result;
state_in->step = step_A;
return codechar - code_out;
}
fragment = *plainchar++;
result = (fragment & 0x0fc) >> 2;
*codechar++ = base64_encode_value(result);
result = (fragment & 0x003) << 4;
case step_B:
if (plainchar == plaintextend)
{
state_in->result = result;
state_in->step = step_B;
return codechar - code_out;
}
fragment = *plainchar++;
result |= (fragment & 0x0f0) >> 4;
*codechar++ = base64_encode_value(result);
result = (fragment & 0x00f) << 2;
case step_C:
if (plainchar == plaintextend)
{
state_in->result = result;
state_in->step = step_C;
return codechar - code_out;
}
fragment = *plainchar++;
result |= (fragment & 0x0c0) >> 6;
*codechar++ = base64_encode_value(result);
result = (fragment & 0x03f) >> 0;
*codechar++ = base64_encode_value(result);
++(state_in->stepcount);
if (state_in->stepcount == L2C_BASE64_CHARS_PER_LINE/4)
{
//*codechar++ = '\n';
state_in->stepcount = 0;
}
}
}
/* control should not reach here */
return codechar - code_out;
}
int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
{
char* codechar = code_out;
switch (state_in->step)
{
case step_B:
*codechar++ = base64_encode_value(state_in->result);
*codechar++ = '=';
*codechar++ = '=';
break;
case step_C:
*codechar++ = base64_encode_value(state_in->result);
*codechar++ = '=';
break;
case step_A:
break;
}
//*codechar++ = '\n';
*codechar++ = '\0';
return codechar - code_out;
}
int base64_decode_value(char value_in)
{
static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
static const char decoding_size = sizeof(decoding);
value_in -= 43;
if (value_in < 0 || value_in > decoding_size) return -1;
return decoding[(int)value_in];
}
void base64_init_decodestate(base64_decodestate* state_in)
{
state_in->step = step_a;
state_in->plainchar = 0;
}
int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in)
{
const char* codechar = code_in;
char* plainchar = plaintext_out;
char fragment;
*plainchar = state_in->plainchar;
switch (state_in->step)
{
while (1)
{
case step_a:
do {
if (codechar == code_in+length_in)
{
state_in->step = step_a;
state_in->plainchar = *plainchar;
return plainchar - plaintext_out;
}
fragment = (char)base64_decode_value(*codechar++);
} while (fragment < 0);
*plainchar = (fragment & 0x03f) << 2;
case step_b:
do {
if (codechar == code_in+length_in)
{
state_in->step = step_b;
state_in->plainchar = *plainchar;
return plainchar - plaintext_out;
}
fragment = (char)base64_decode_value(*codechar++);
} while (fragment < 0);
*plainchar++ |= (fragment & 0x030) >> 4;
*plainchar = (fragment & 0x00f) << 4;
case step_c:
do {
if (codechar == code_in+length_in)
{
state_in->step = step_c;
state_in->plainchar = *plainchar;
return plainchar - plaintext_out;
}
fragment = (char)base64_decode_value(*codechar++);
} while (fragment < 0);
*plainchar++ |= (fragment & 0x03c) >> 2;
*plainchar = (fragment & 0x003) << 6;
case step_d:
do {
if (codechar == code_in+length_in)
{
state_in->step = step_d;
state_in->plainchar = *plainchar;
return plainchar - plaintext_out;
}
fragment = (char)base64_decode_value(*codechar++);
} while (fragment < 0);
*plainchar++ |= (fragment & 0x03f);
}
}
/* control should not reach here */
return plainchar - plaintext_out;
}
L2C_UTILS_API void base64_encode_string( const char *str_in, int len_in, char *str_out )
{
base64_encodestate state;
base64_init_encodestate( &state );
int len_coded = base64_encode_block( str_in, len_in, str_out, &state );
base64_encode_blockend( str_out+len_coded, &state );
}
L2C_UTILS_API void base64_decode_string( const char *str_in, int len_in, char *str_out )
{
base64_decodestate state;
base64_init_decodestate( &state );
base64_decode_block( str_in, len_in, str_out, &state );
}

25
libl2c_utils/l2c_utils.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#ifdef L2C_UTILS_EXPORTS
#define L2C_UTILS_API __declspec(dllexport)
#pragma message( "Build utils DLL" )
#else
#define L2C_UTILS_API __declspec(dllimport)
#endif
extern L2C_UTILS_API void l2c_ansi_to_unicode( const char *ansi, wchar_t *unicode, size_t maxLen );
extern L2C_UTILS_API void l2c_unicode_to_ansi( const wchar_t *unicode, char *ansi, size_t maxLen );
extern L2C_UTILS_API void base64_encode_string( const char *str_in, int len_in, char *str_out );
extern L2C_UTILS_API void base64_decode_string( const char *str_in, int len_in, char *str_out );
//extern L2C_UTILS_API int nl2c_utils;
// other library includes
#include "l2c_configFile.h"
#include "l2c_db.h"
#include "l2c_lock.h"
#include "l2c_exception.h"

View File

@@ -0,0 +1,17 @@
#include "pch.h"
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
UNREFERENCED_PARAMETER(hModule);
UNREFERENCED_PARAMETER(lpReserved);
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

1
libl2c_utils/pch.cpp Normal file
View File

@@ -0,0 +1 @@
#include "pch.h"

25
libl2c_utils/pch.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#define WINVER 0x0501
#define _WIN32_WINNT 0x0501
#define _WIN32_IE 0x0600
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <string>
#include <map>
#include <windows.h>
#include <windowsx.h>
#include <wchar.h>
#include <tchar.h>
#include <process.h>
// mysql
#include <mysql.h>