Merge branch 'master' into storageareas

This commit is contained in:
Isaac Connor 2017-05-19 12:28:07 -04:00
commit 1c9c6441d4
1 changed files with 115 additions and 157 deletions

View File

@ -27,16 +27,13 @@
#include "zm_utils.h" #include "zm_utils.h"
void zmLoadConfig() void zmLoadConfig() {
{
FILE *cfg; FILE *cfg;
char line[512]; char line[512];
if ( (cfg = fopen( ZM_CONFIG, "r")) == NULL ) if ( (cfg = fopen( ZM_CONFIG, "r")) == NULL ) {
{
Fatal( "Can't open %s: %s", ZM_CONFIG, strerror(errno) ); Fatal( "Can't open %s: %s", ZM_CONFIG, strerror(errno) );
} }
while ( fgets( line, sizeof(line), cfg ) != NULL ) while ( fgets( line, sizeof(line), cfg ) != NULL ) {
{
char *line_ptr = line; char *line_ptr = line;
// Trim off any cr/lf line endings // Trim off any cr/lf line endings
@ -53,16 +50,14 @@ void zmLoadConfig()
// Remove trailing white space // Remove trailing white space
char *temp_ptr = line_ptr+strlen(line_ptr)-1; char *temp_ptr = line_ptr+strlen(line_ptr)-1;
while ( *temp_ptr == ' ' || *temp_ptr == '\t' ) while ( *temp_ptr == ' ' || *temp_ptr == '\t' ) {
{
*temp_ptr-- = '\0'; *temp_ptr-- = '\0';
temp_ptr--; temp_ptr--;
} }
// Now look for the '=' in the middle of the line // Now look for the '=' in the middle of the line
temp_ptr = strchr( line_ptr, '=' ); temp_ptr = strchr( line_ptr, '=' );
if ( !temp_ptr ) if ( !temp_ptr ) {
{
Warning( "Invalid data in %s: '%s'", ZM_CONFIG, line ); Warning( "Invalid data in %s: '%s'", ZM_CONFIG, line );
continue; continue;
} }
@ -72,12 +67,10 @@ void zmLoadConfig()
char *val_ptr = temp_ptr+1; char *val_ptr = temp_ptr+1;
// Trim trailing space from the name part // Trim trailing space from the name part
do do {
{
*temp_ptr = '\0'; *temp_ptr = '\0';
temp_ptr--; temp_ptr--;
} } while ( *temp_ptr == ' ' || *temp_ptr == '\t' );
while ( *temp_ptr == ' ' || *temp_ptr == '\t' );
// Remove leading white space from the value part // Remove leading white space from the value part
white_len = strspn( val_ptr, " \t" ); white_len = strspn( val_ptr, " \t" );
@ -99,8 +92,7 @@ void zmLoadConfig()
staticConfig.SERVER_NAME = std::string(val_ptr); staticConfig.SERVER_NAME = std::string(val_ptr);
else if ( strcasecmp( name_ptr, "ZM_SERVER_ID" ) == 0 ) else if ( strcasecmp( name_ptr, "ZM_SERVER_ID" ) == 0 )
staticConfig.SERVER_ID = atoi(val_ptr); staticConfig.SERVER_ID = atoi(val_ptr);
else else {
{
// We ignore this now as there may be more parameters than the // We ignore this now as there may be more parameters than the
// c/c++ binaries are bothered about // c/c++ binaries are bothered about
// Warning( "Invalid parameter '%s' in %s", name_ptr, ZM_CONFIG ); // Warning( "Invalid parameter '%s' in %s", name_ptr, ZM_CONFIG );
@ -145,192 +137,158 @@ void zmLoadConfig()
StaticConfig staticConfig; StaticConfig staticConfig;
ConfigItem::ConfigItem( const char *p_name, const char *p_value, const char *const p_type ) ConfigItem::ConfigItem( const char *p_name, const char *p_value, const char *const p_type ) {
{ name = new char[strlen(p_name)+1];
name = new char[strlen(p_name)+1]; strcpy( name, p_name );
strcpy( name, p_name ); value = new char[strlen(p_value)+1];
value = new char[strlen(p_value)+1]; strcpy( value, p_value );
strcpy( value, p_value ); type = new char[strlen(p_type)+1];
type = new char[strlen(p_type)+1]; strcpy( type, p_type );
strcpy( type, p_type );
//Info( "Created new config item %s = %s (%s)\n", name, value, type ); //Info( "Created new config item %s = %s (%s)\n", name, value, type );
accessed = false; accessed = false;
} }
ConfigItem::~ConfigItem() ConfigItem::~ConfigItem() {
{ delete[] name;
delete[] name; delete[] value;
delete[] value; delete[] type;
delete[] type;
} }
void ConfigItem::ConvertValue() const void ConfigItem::ConvertValue() const {
{ if ( !strcmp( type, "boolean" ) ) {
if ( !strcmp( type, "boolean" ) ) cfg_type = CFG_BOOLEAN;
{ cfg_value.boolean_value = (bool)strtol( value, 0, 0 );
cfg_type = CFG_BOOLEAN; } else if ( !strcmp( type, "integer" ) ) {
cfg_value.boolean_value = (bool)strtol( value, 0, 0 ); cfg_type = CFG_INTEGER;
} cfg_value.integer_value = strtol( value, 0, 10 );
else if ( !strcmp( type, "integer" ) ) } else if ( !strcmp( type, "hexadecimal" ) ) {
{ cfg_type = CFG_INTEGER;
cfg_type = CFG_INTEGER; cfg_value.integer_value = strtol( value, 0, 16 );
cfg_value.integer_value = strtol( value, 0, 10 ); } else if ( !strcmp( type, "decimal" ) ) {
} cfg_type = CFG_DECIMAL;
else if ( !strcmp( type, "hexadecimal" ) ) cfg_value.decimal_value = strtod( value, 0 );
{ } else {
cfg_type = CFG_INTEGER; cfg_type = CFG_STRING;
cfg_value.integer_value = strtol( value, 0, 16 ); cfg_value.string_value = value;
} }
else if ( !strcmp( type, "decimal" ) ) accessed = true;
{
cfg_type = CFG_DECIMAL;
cfg_value.decimal_value = strtod( value, 0 );
}
else
{
cfg_type = CFG_STRING;
cfg_value.string_value = value;
}
accessed = true;
} }
bool ConfigItem::BooleanValue() const bool ConfigItem::BooleanValue() const {
{ if ( !accessed )
if ( !accessed ) ConvertValue();
ConvertValue();
if ( cfg_type != CFG_BOOLEAN ) if ( cfg_type != CFG_BOOLEAN ) {
{ Error( "Attempt to fetch boolean value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
Error( "Attempt to fetch boolean value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type ); exit( -1 );
exit( -1 ); }
}
return( cfg_value.boolean_value ); return( cfg_value.boolean_value );
} }
int ConfigItem::IntegerValue() const int ConfigItem::IntegerValue() const {
{ if ( !accessed )
if ( !accessed ) ConvertValue();
ConvertValue();
if ( cfg_type != CFG_INTEGER ) if ( cfg_type != CFG_INTEGER ) {
{ Error( "Attempt to fetch integer value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
Error( "Attempt to fetch integer value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type ); exit( -1 );
exit( -1 ); }
}
return( cfg_value.integer_value ); return( cfg_value.integer_value );
} }
double ConfigItem::DecimalValue() const double ConfigItem::DecimalValue() const {
{ if ( !accessed )
if ( !accessed ) ConvertValue();
ConvertValue();
if ( cfg_type != CFG_DECIMAL ) if ( cfg_type != CFG_DECIMAL ) {
{ Error( "Attempt to fetch decimal value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
Error( "Attempt to fetch decimal value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type ); exit( -1 );
exit( -1 ); }
}
return( cfg_value.decimal_value ); return( cfg_value.decimal_value );
} }
const char *ConfigItem::StringValue() const const char *ConfigItem::StringValue() const {
{ if ( !accessed )
if ( !accessed ) ConvertValue();
ConvertValue();
if ( cfg_type != CFG_STRING ) if ( cfg_type != CFG_STRING ) {
{ Error( "Attempt to fetch string value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type );
Error( "Attempt to fetch string value for %s, actual type is %s. Try running 'zmupdate.pl -f' to reload config.", name, type ); exit( -1 );
exit( -1 ); }
}
return( cfg_value.string_value ); return( cfg_value.string_value );
} }
Config::Config() Config::Config() {
{ n_items = 0;
n_items = 0; items = 0;
items = 0;
} }
Config::~Config() Config::~Config() {
{ if ( items ) {
if ( items ) for ( int i = 0; i < n_items; i++ ) {
{ delete items[i];
for ( int i = 0; i < n_items; i++ ) }
{ delete[] items;
delete items[i]; }
}
delete[] items;
}
} }
void Config::Load() void Config::Load() {
{ static char sql[ZM_SQL_SML_BUFSIZ];
static char sql[ZM_SQL_SML_BUFSIZ];
strncpy( sql, "select Name, Value, Type from Config order by Id", sizeof(sql) ); strncpy( sql, "select Name, Value, Type from Config order by Id", sizeof(sql) );
if ( mysql_query( &dbconn, sql ) ) if ( mysql_query( &dbconn, sql ) ) {
{ Error( "Can't run query: %s", mysql_error( &dbconn ) );
Error( "Can't run query: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) );
exit( mysql_errno( &dbconn ) ); }
}
MYSQL_RES *result = mysql_store_result( &dbconn ); MYSQL_RES *result = mysql_store_result( &dbconn );
if ( !result ) if ( !result ) {
{ Error( "Can't use query result: %s", mysql_error( &dbconn ) );
Error( "Can't use query result: %s", mysql_error( &dbconn ) ); exit( mysql_errno( &dbconn ) );
exit( mysql_errno( &dbconn ) ); }
} n_items = mysql_num_rows( result );
n_items = mysql_num_rows( result );
if ( n_items <= ZM_MAX_CFG_ID ) if ( n_items <= ZM_MAX_CFG_ID ) {
{ Error( "Config mismatch, expected %d items, read %d. Try running 'zmupdate.pl -f' to reload config.", ZM_MAX_CFG_ID+1, n_items );
Error( "Config mismatch, expected %d items, read %d. Try running 'zmupdate.pl -f' to reload config.", ZM_MAX_CFG_ID+1, n_items ); exit( -1 );
exit( -1 ); }
}
items = new ConfigItem *[n_items]; items = new ConfigItem *[n_items];
for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) for( int i = 0; MYSQL_ROW dbrow = mysql_fetch_row( result ); i++ ) {
{ items[i] = new ConfigItem( dbrow[0], dbrow[1], dbrow[2] );
items[i] = new ConfigItem( dbrow[0], dbrow[1], dbrow[2] ); }
} mysql_free_result( result );
mysql_free_result( result );
} }
void Config::Assign() void Config::Assign() {
{
ZM_CFG_ASSIGN_LIST ZM_CFG_ASSIGN_LIST
} }
const ConfigItem &Config::Item( int id ) const ConfigItem &Config::Item( int id ) {
{ if ( !n_items ) {
if ( !n_items ) Load();
{ Assign();
Load(); }
Assign();
}
if ( id < 0 || id > ZM_MAX_CFG_ID ) if ( id < 0 || id > ZM_MAX_CFG_ID ) {
{ Error( "Attempt to access invalid config, id = %d. Try running 'zmupdate.pl -f' to reload config.", id );
Error( "Attempt to access invalid config, id = %d. Try running 'zmupdate.pl -f' to reload config.", id ); exit( -1 );
exit( -1 ); }
}
ConfigItem *item = items[id]; ConfigItem *item = items[id];
if ( !item ) if ( !item ) {
{ Error( "Can't find config item %d", id );
Error( "Can't find config item %d", id ); exit( -1 );
exit( -1 ); }
}
return( *item );
return( *item );
} }
Config config; Config config;