diff options
Diffstat (limited to 'project/dclib')
| -rw-r--r-- | project/dclib/dclib-mysql.c | 82 | ||||
| -rw-r--r-- | project/dclib/dclib-mysql.h | 18 | ||||
| -rw-r--r-- | project/dclib/lib-bmg.c | 104 | ||||
| -rw-r--r-- | project/dclib/lib-bmg.h | 32 |
4 files changed, 213 insertions, 23 deletions
diff --git a/project/dclib/dclib-mysql.c b/project/dclib/dclib-mysql.c index f2ad833..74576c3 100644 --- a/project/dclib/dclib-mysql.c +++ b/project/dclib/dclib-mysql.c @@ -203,7 +203,7 @@ void ResetMYSQL ( MySql_t *my ) /////////////////////////////////////////////////////////////////////////////// -enumError OpenMYSQL ( MySql_t *my, uint loglevel ) +enumError OpenMYSQL4 ( MySql_t *my, uint loglevel ) { DASSERT(my); ClearStatusMYSQL(&my->status); @@ -219,7 +219,7 @@ enumError OpenMYSQL ( MySql_t *my, uint loglevel ) } NetworkHost_t host; - ResolveHost(&host,true,my->server,MYSQL_DEFAULT_PORT,false,true); + ResolveHost(&host,true,my->server,my->port?my->port:MYSQL_DEFAULT_PORT,false,true); PRINT("MYSQL CONNECT: %s:%u -> %s\n", host.name, host.port, PrintIP4(0,0,host.ip4,host.port) ); @@ -253,7 +253,7 @@ enumError OpenMYSQL ( MySql_t *my, uint loglevel ) mysql_options(my->mysql,MYSQL_OPT_RECONNECT,&reconnect); } - noPRINT("connect(%p,%s,%s,%s,%s,%u,,)\n", + PRINT0("connect(%p,%s,%s,%s,%s,%u,,)\n", my->mysql, host.name, my->user, my->password, my->database, host.port ); MYSQL *stat = mysql_real_connect ( my->mysql, host.name, my->user, my->password, my->database, @@ -279,6 +279,76 @@ enumError OpenMYSQL ( MySql_t *my, uint loglevel ) /////////////////////////////////////////////////////////////////////////////// +enumError OpenMYSQL ( MySql_t *my, uint loglevel ) +{ + DASSERT(my); + ClearStatusMYSQL(&my->status); + + if (my->mysql) + return ERR_OK; // already connected + + if ( !my || !my->server ) + { + if ( loglevel >= MYLL_ERROR ) + ERROR0(ERR_MISSING_PARAM,"No MySql server defined.\n"); + return ERR_MISSING_PARAM; + } + + const uint port = my->port ? my->port : MYSQL_DEFAULT_PORT; + PRINT("MYSQL CONNECT: %s :%u\n", my->server, port ); + + static bool lib_init_done = false; + if (!lib_init_done) + { + noPRINT("mysql_library_init()\n"); + if (mysql_library_init(0,NULL,NULL)) + { + if ( loglevel >= MYLL_ERROR ) + ERROR0(ERR_DATABASE,"Can't initialize MySql library.\n"); + return ERR_DATABASE; + } + lib_init_done = true; + } + + my->mysql = mysql_init(NULL); + //HEXDUMP16(0,0,my->mysql,sizeof(*my->mysql)); + if (!my->mysql) + { + if ( loglevel >= MYLL_ERROR ) + ERROR0(ERR_DATABASE,"Can't initialize MySql data.\n"); + return ERR_DATABASE; + } + + if (my->auto_reconnect) + { + my_bool reconnect = 1; + mysql_options(my->mysql,MYSQL_OPT_RECONNECT,&reconnect); + } + + PRINT0("connect(%p,%s,%s,%s,%s,%u,,)\n", + my->mysql, my->server, my->user, my->password, my->database, port ); + MYSQL *stat = mysql_real_connect ( my->mysql, my->server, + my->user, my->password, my->database, + port, NULL, 0 ); + + if (!stat) + { + GetStatusMYSQL(&my->status,my->mysql); + if ( loglevel >= MYLL_ERROR ) + ERROR0(ERR_CANT_CONNECT,"Can't connect to %s port %u:\n-> %s\n", + my->server, port, my->status.message ); + mysql_close(my->mysql); + my->mysql = 0; + return ERR_CANT_CONNECT; + } + + mysql_set_character_set(my->mysql,"utf8mb4"); + my->total_connect_count++; + return ERR_OK; +} + +/////////////////////////////////////////////////////////////////////////////// + void CloseMYSQL ( MySql_t *my ) { DASSERT(my); @@ -307,10 +377,11 @@ void CloseMYSQL ( MySql_t *my ) /////////////////////////////////////////////////////////////////////////////// -void DefineDatabase +void DefineDatabase5 ( MySql_t *my, // valid struct ccp server, // NULL or server name + uint port, // 0 or server port ccp user, // NULL or user name ccp password, // NULL or user password ccp database // NULL or name of database @@ -325,6 +396,9 @@ void DefineDatabase my->server = STRDUP(server); } + if (port) + my->port = port; + if (user) { FREE((char*)my->user); diff --git a/project/dclib/dclib-mysql.h b/project/dclib/dclib-mysql.h index 208eac5..f017e11 100644 --- a/project/dclib/dclib-mysql.h +++ b/project/dclib/dclib-mysql.h @@ -54,6 +54,7 @@ typedef struct st_mysql_field st_mysql_field; struct MySql_t; +// [[LogLevelMYSQL]] typedef enum LogLevelMYSQL { MYLL_SILENT, @@ -135,6 +136,7 @@ typedef struct MySql_t //--- database definitions, strings are alloced ccp server; // server + uint port; // port ccp user; // user ccp password; // password ccp database; // database name @@ -184,17 +186,31 @@ extern const SaveRestoreTab_t SRT_Mysql[]; void InitializeMYSQL ( MySql_t *my ); void ResetMYSQL ( MySql_t *my ); enumError OpenMYSQL ( MySql_t *my, uint loglevel ); +enumError OpenMYSQL4 ( MySql_t *my, uint loglevel ); // IPv4 only, deprecated void CloseMYSQL ( MySql_t *my ); -void DefineDatabase +void DefineDatabase5 ( MySql_t *my, // valid struct ccp server, // NULL or server name + uint port, // 0 or server port ccp user, // NULL or user name ccp password, // NULL or user password ccp database // NULL or name of database ); +static inline void DefineDatabase +( + MySql_t *my, // valid struct + ccp server, // NULL or server name + ccp user, // NULL or user name + ccp password, // NULL or user password + ccp database // NULL or name of database +) +{ + DefineDatabase5(my,server,0,user,password,database); +} + void DefineDatabasePrefix ( MySql_t *my, // valid struct diff --git a/project/dclib/lib-bmg.c b/project/dclib/lib-bmg.c index ce1b3ac..6598cef 100644 --- a/project/dclib/lib-bmg.c +++ b/project/dclib/lib-bmg.c @@ -86,6 +86,7 @@ uint opt_bmg_max_recurse = 10; // max recurse depth bool opt_bmg_allow_print = 0; // true: allow '$' to print a log message bool opt_bmg_use_slots = 0; // true: use predifined slots bool opt_bmg_use_raw_sections= 0; // true: use raw sections +bool opt_bmg_use_new_cond = 1; // true: use new @? conditions on output //----------------------------------------------------------------------------- @@ -1503,6 +1504,40 @@ static void copy_param // /////////////////////////////////////////////////////////////////////////////// +/////////////// struct bmg_item_cond_t /////////////// +/////////////////////////////////////////////////////////////////////////////// + +const bmg_t *item_cond_cache_bmg = 0; +u32 item_cond_cache_mid = 0; +bool item_cond_cache_found = false; + +/////////////////////////////////////////////////////////////////////////////// + +void ClearItemCondCacheBMG(void) +{ + item_cond_cache_bmg = 0; +} + +/////////////////////////////////////////////////////////////////////////////// + +bool CheckItemCondBMG ( const bmg_t *bmg, bmg_item_cond_t cond ) +{ + DASSERT(bmg); + if (!cond.active) + return true; + + if ( bmg != item_cond_cache_bmg || cond.mid != item_cond_cache_mid ) + { + item_cond_cache_bmg = bmg; + item_cond_cache_mid = cond.mid; + item_cond_cache_found = FindItemBMG(bmg,cond.mid) != 0; + } + + return cond.not ? !item_cond_cache_found : item_cond_cache_found; +} + +// +/////////////////////////////////////////////////////////////////////////////// /////////////// struct bmg_item_t /////////////// /////////////////////////////////////////////////////////////////////////////// @@ -2562,8 +2597,7 @@ enumError ScanTextBMG ( bmg_t * bmg ) // [[xbuf]] char xbuf[2*BMG_MSG_BUF_SIZE]; enumError err_stat = ERR_OK; - u32 cond = 0; - + bmg_item_cond_t cond; ParamField_t assign; InitializeParamField(&assign); assign.free_data = true; @@ -2600,13 +2634,32 @@ enumError ScanTextBMG ( bmg_t * bmg ) if ( *start == '?' ) { - // SYNTAX/IF: '@?' MID_COND + // SYNTAX/IF: + // '@? -' + // '@?' ['='|'!'] MID_COND + ClearItemCondBMG(&cond); + start = GetNext(start+1,ptr); + if ( *start == '-' ) + continue; + char op = 0; + if ( *start == '=' || *start == '!' ) + op = *start++; u32 mid; - ScanMidBMG(bmg,&mid,0,0,start+1,end,(char**)&start); - if ( ptr >= end ) - break; - cond = mid; + ScanMidBMG(bmg,&mid,0,0,start,end,(char**)&start); + switch(op) + { + case 0: + if (!mid) + continue; + break; + + case '!': + cond.not = true; + break; + } + cond.active = true; + cond.mid = mid; continue; } @@ -3775,10 +3828,25 @@ static void print_message DASSERT(bmg); DASSERT(bi); - if ( bmg->active_cond != bi->cond ) + if (memcmp(&bmg->active_cond,&bi->cond,sizeof(bmg->active_cond))) { bmg->active_cond = bi->cond; - fprintf(f,"@? %x\n",bmg->active_cond); + if (opt_bmg_use_new_cond) + { + if ( bi->cond.active ) + fprintf(f,"@? %c%x\n", bi->cond.not ? '!' : '=', bi->cond.mid ); + else + fprintf(f,"@? -\n"); + } + else + { + if ( bi->cond.active ) + fprintf(f,"@? %s%x\n", + bi->cond.not ? "!" : bi->cond.mid ? "" : "=", + bi->cond.mid ); + else + fprintf(f,"@? 0\n"); + } } ccp slot; @@ -3955,7 +4023,6 @@ enumError SaveTextFileBMG // use DOS/Windows line format -> unix can handle it ;) DASSERT(bmg); - bmg->active_cond = 0; enum { BRIEF_ALL, BRIEF_NO_SYNTAX, BRIEF_NO_COMMENT, BRIEF_NO_MAGIC }; @@ -3963,6 +4030,8 @@ enumError SaveTextFileBMG memset(already_printed,0,sizeof(already_printed)); char mid_name[10], abuf[BMG_ATTRIB_BUF_SIZE]; + ClearItemCondCacheBMG(); + ClearItemCondBMG(&bmg->active_cond); static ccp param_announce = "# All parameters begin with '@'. Unknown parameters are ignored on scanning.\r\n"; @@ -5191,6 +5260,7 @@ bool PatchReplaceBMG DASSERT(dest); DASSERT(src); + ClearItemCondCacheBMG(); copy_param(dest,src); bmg_item_t * dptr = dest->item; @@ -5212,8 +5282,9 @@ bool PatchReplaceBMG break; if ( sptr->mid == mid - && !IsItemEqualBMG(dptr,sptr) - && ( !sptr->cond || FindItemBMG(dest,sptr->cond) ) ) + && !IsItemEqualBMG(dptr,sptr) + && CheckItemCondBMG(dest,sptr->cond) + ) { FreeItemBMG(dptr); DASSERT( dptr->mid == sptr->mid ); @@ -5245,6 +5316,7 @@ bool PatchInsertBMG DASSERT(dest); DASSERT(src); + ClearItemCondCacheBMG(); copy_param(dest,src); const bmg_item_t * sptr = src->item; @@ -5253,7 +5325,7 @@ bool PatchInsertBMG bool dirty = false; for ( ; sptr < send; sptr++ ) { - if ( sptr->cond && !FindItemBMG(dest,sptr->cond) ) + if ( !CheckItemCondBMG(dest,sptr->cond) ) continue; bool old_item; @@ -5291,6 +5363,7 @@ bool PatchOverwriteBMG DASSERT(dest); DASSERT(src); + ClearItemCondCacheBMG(); copy_param(dest,src); const bmg_item_t * sptr = src->item; @@ -5299,7 +5372,7 @@ bool PatchOverwriteBMG bool dirty = false; for ( ; sptr < send; sptr++ ) { - if ( sptr->cond && !FindItemBMG(dest,sptr->cond) ) + if ( !CheckItemCondBMG(dest,sptr->cond) ) continue; bool old_item; @@ -5983,7 +6056,7 @@ bool PatchFillBMG if ( !old_item || !dptr->len ) { const uint len - = snprintf(buf,sizeof(buf),"_A%u_",mid-ctb.arena_name1.beg); + = snprintf(buf,sizeof(buf),"_A%02u_",mid-ctb.arena_name1.beg+1); AssignItemTextBMG(dptr,buf,len); ResetAttribBMG(bmg,dptr); dirty = true; @@ -6514,6 +6587,7 @@ const sizeof_info_t sizeof_info_bmg[] = SIZEOF_INFO_ENTRY(bmg_sect_info_t) SIZEOF_INFO_ENTRY(bmg_sect_list_t) SIZEOF_INFO_ENTRY(bmg_raw_section_t) + SIZEOF_INFO_ENTRY(bmg_item_cond_t) SIZEOF_INFO_ENTRY(bmg_item_t) SIZEOF_INFO_ENTRY(bmg_t) SIZEOF_INFO_ENTRY(bmg_create_t) diff --git a/project/dclib/lib-bmg.h b/project/dclib/lib-bmg.h index 3002618..f982fbf 100644 --- a/project/dclib/lib-bmg.h +++ b/project/dclib/lib-bmg.h @@ -244,6 +244,7 @@ extern uint opt_bmg_max_recurse; // max recurse depth extern bool opt_bmg_allow_print; // true: allow '$' to print a log message extern bool opt_bmg_use_slots; // true: use predifined slots extern bool opt_bmg_use_raw_sections;// true: use raw sections +extern bool opt_bmg_use_new_cond; // true: use new @? conditions on output //----------------------------------------------------------------------------- @@ -667,6 +668,29 @@ bmg_raw_section_t * CreateRawSectionBMG ( bmg_t *bmg, cvp magic ); // /////////////////////////////////////////////////////////////////////////////// +/////////////// struct bmg_item_cond_t /////////////// +/////////////////////////////////////////////////////////////////////////////// +// [[bmg_item_cond_t]] + +typedef struct bmg_item_cond_t // [[2do]] not used yet +{ + bool active; // true: condition is active + bool not; // true: check if 'mid' does not exists + u32 mid; // message id to check +} +bmg_item_cond_t; + +//----------------------------------------------------------------------------- + +bool CheckItemCondBMG ( const bmg_t *bmg, bmg_item_cond_t cond ); + +static inline void ClearItemCondBMG ( bmg_item_cond_t *cond ) + { memset(cond,0,sizeof(*cond)); } + +void ClearItemCondCacheBMG(void); + +// +/////////////////////////////////////////////////////////////////////////////// /////////////// struct bmg_item_t /////////////// /////////////////////////////////////////////////////////////////////////////// // [[bmg_item_t]] @@ -674,7 +698,8 @@ bmg_raw_section_t * CreateRawSectionBMG ( bmg_t *bmg, cvp magic ); typedef struct bmg_item_t { u32 mid; // message ID - u32 cond; // >0: message 'cond' must be defined + bmg_item_cond_t + cond; // condition bmg_slot_t slot; // BMG_NO_PREDEF_SLOT or predifined slot u16 attrib_used; // used size of 'attrib' @@ -701,7 +726,7 @@ static inline bool isSpecialEntryBMG ( const u16 *text ) //----------------------------------------------------------------------------- -void FreeItemBMG( bmg_item_t * bi ); +void FreeItemBMG ( bmg_item_t * bi ); void AssignItemTextBMG ( @@ -811,7 +836,8 @@ typedef struct bmg_t bool have_predef_slots; // temporary, set by HavePredifinedSlots() bool use_slots; // init by opt_bmg_use_slots bool use_raw_sections; // init by opt_bmg_use_raw_sections - u32 active_cond; // active condition (for text output) + + bmg_item_cond_t active_cond; // active condition (for text output) //--- raw data, created by CreateRawBMG() |
