From: havoc Date: Fri, 24 Nov 2006 22:55:08 +0000 (+0000) Subject: patch from Dresk for DP_QC_STRINGCOLORFUNCTIONS extension (strlennocol measures lengt... X-Git-Tag: xonotic-v0.1.0preview~3770 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=a0e87ae24ea6074187ef09c55701558f446df894;p=xonotic%2Fdarkplaces.git patch from Dresk for DP_QC_STRINGCOLORFUNCTIONS extension (strlennocol measures length of string with color chars removed (useful for centering text), strdecolorize returns a string with color codes stripped) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@6626 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/clvm_cmds.c b/clvm_cmds.c index 4e284d21..cc7130b8 100644 --- a/clvm_cmds.c +++ b/clvm_cmds.c @@ -68,6 +68,7 @@ char *vm_cl_extensions = "DP_QC_RANDOMVEC " "DP_QC_SINCOSSQRTPOW " //"DP_QC_STRINGBUFFERS " //[515]: not needed ? +"DP_QC_STRINGCOLORFUNCTIONS " "DP_QC_TRACEBOX " //"DP_QC_TRACETOSS " "DP_QC_TRACE_MOVETYPE_HITMODEL " @@ -2729,8 +2730,8 @@ VM_acos, // #472 float(float c) VM_acos (DP_QC_ASINACOSATANATAN2TAN) VM_atan, // #473 float(float t) VM_atan (DP_QC_ASINACOSATANATAN2TAN) VM_atan2, // #474 float(float c, float s) VM_atan2 (DP_QC_ASINACOSATANATAN2TAN) VM_tan, // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN) -NULL, // #476 -NULL, // #477 +VM_strlennocol, // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS) +VM_strdecolorize, // #477 string(string s) : DRESK - Decolorized String (DP_QC_STRINGCOLORFUNCTIONS) NULL, // #478 NULL, // #479 e10, e10 // #480-499 (LordHavoc) diff --git a/mvm_cmds.c b/mvm_cmds.c index 43919655..19f3ced7 100644 --- a/mvm_cmds.c +++ b/mvm_cmds.c @@ -5,7 +5,8 @@ char *vm_m_extensions = "DP_CINEMATIC_DPV " -"DP_QC_ASINACOSATANATAN2TAN"; +"DP_QC_ASINACOSATANATAN2TAN " +"DP_QC_STRINGCOLORFUNCTIONS"; /* ========= @@ -916,7 +917,9 @@ prvm_builtin_t vm_m_builtins[] = { VM_atan, // #473 float(float t) VM_atan (DP_QC_ASINACOSATANATAN2TAN) VM_atan2, // #474 float(float c, float s) VM_atan2 (DP_QC_ASINACOSATANATAN2TAN) VM_tan, // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN) - 0,0,0,0,0, // 480 + VM_strlennocol, // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS) + VM_strdecolorize, // #477 string(string s) : DRESK - Decolorized String (DP_QC_STRINGCOLORFUNCTIONS) + 0,0,0, // 480 e10, // 490 e10, // 500 e100, // 600 diff --git a/prvm_cmds.c b/prvm_cmds.c index bca25737..b78d2e75 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -1787,6 +1787,138 @@ void VM_strlen(void) PRVM_G_FLOAT(OFS_RETURN) = 0; } +// DRESK - Decolorized String +/* +========= +VM_strdecolorize + +string strdecolorize(string s) +========= +*/ +// string (string s) strdecolorize = #472; // returns the passed in string with color codes stripped +void VM_strdecolorize(void) +{ + char *szNewString; + const char *szString; + size_t nCnt; + int nPos; + int nFillPos; + int bFinished; + nPos = 0; + nFillPos = 0; + nCnt = 0; + bFinished = 0; + + // Prepare Strings + VM_SAFEPARMCOUNT(1,VM_strdecolorize); + szString = PRVM_G_STRING(OFS_PARM0); + szNewString = VM_GetTempString(); + + while(!bFinished) + { // Traverse through String + if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0) + { // String End Found + szNewString[nFillPos++] = szString[nPos]; + bFinished = 1; + } + else + if( szString[nPos] == STRING_COLOR_TAG) + { // Color Code Located + if( szString[nPos + 1] == STRING_COLOR_TAG) + { // Valid Characters to Include + szNewString[nFillPos++] = szString[nPos]; + nPos = nPos + 1; + szNewString[nFillPos++] = szString[nPos]; + } + else + if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' ) + { // Color Code Found; Increment Position + nPos = nPos + 1; + } + else + { // Unknown Color Code; Include + szNewString[nFillPos++] = szString[nPos]; + nPos = nPos + 1; + } + } + else + // Include Character + szNewString[nFillPos++] = szString[nPos]; + + // Increment Position + nPos = nPos + 1; + } + + PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(szNewString); +} + +// DRESK - String Length (not counting color codes) +/* +========= +VM_strlennocol + +float strlennocol(string s) +========= +*/ +// float(string s) strlennocol = #471; // returns how many characters are in a string not including color codes +// For example, ^2Dresk returns a length of 5 +void VM_strlennocol(void) +{ + const char *szString; + size_t nCnt; + int nPos; + int bFinished; + nPos = 0; + nCnt = 0; + bFinished = 0; + + VM_SAFEPARMCOUNT(1,VM_strlennocol); + + szString = PRVM_G_STRING(OFS_PARM0); + if(szString) + { // Valid String + while(!bFinished) + { // Count Characters + // SV_BroadcastPrintf("Position '%d'; Character '%c'; Length '%d'\n", nPos, szString[nPos], nCnt); + + if( szString[nPos] == '\n' || szString[nPos] == '\r' || szString[nPos] <= 0) + { // String End Found + // SV_BroadcastPrintf("Found End of String at '%d'\n", nPos); + bFinished = 1; + } + else + if( szString[nPos] == STRING_COLOR_TAG) + { // Color Code Located + if( szString[nPos + 1] == STRING_COLOR_TAG) + { // Increment Length; Skip Color Code + nCnt = nCnt + 1; + nPos = nPos + 1; + } + else + if( szString[nPos + 1] >= '0' && szString[nPos + 1] <= '9' ) + { // Color Code Found; Increment Position + // SV_BroadcastPrintf("Found Color Codes at '%d'\n", nPos); + nPos = nPos + 1; + } + else + { // Unknown Color Code; Increment Length! + nPos = nPos + 1; + nCnt = nCnt + 1; + } + } + else + // Increment String Length + nCnt = nCnt + 1; + + // Increment Position + nPos = nPos + 1; + } + PRVM_G_FLOAT(OFS_RETURN) = nCnt; + } + else + PRVM_G_FLOAT(OFS_RETURN) = 0; +} + /* ========= VM_strcat diff --git a/prvm_cmds.h b/prvm_cmds.h index 2cc2a890..562fe586 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -294,6 +294,11 @@ void VM_stov(void); void VM_strzone(void); void VM_strunzone(void); +// DRESK - String Length (not counting color codes) +void VM_strlennocol(void); +// DRESK - Decolorized String +void VM_strdecolorize(void); + void VM_clcommand (void); void VM_tokenize (void); diff --git a/svvm_cmds.c b/svvm_cmds.c index 1bf84cb5..89760f55 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -67,6 +67,7 @@ char *vm_sv_extensions = "DP_QC_RANDOMVEC " "DP_QC_SINCOSSQRTPOW " "DP_QC_STRINGBUFFERS " +"DP_QC_STRINGCOLORFUNCTIONS " "DP_QC_TRACEBOX " "DP_QC_TRACETOSS " "DP_QC_TRACE_MOVETYPE_HITMODEL " @@ -2856,8 +2857,8 @@ VM_acos, // #472 float(float c) VM_acos (DP_QC_ASINACOSATANATAN2TAN) VM_atan, // #473 float(float t) VM_atan (DP_QC_ASINACOSATANATAN2TAN) VM_atan2, // #474 float(float c, float s) VM_atan2 (DP_QC_ASINACOSATANATAN2TAN) VM_tan, // #475 float(float a) VM_tan (DP_QC_ASINACOSATANATAN2TAN) -NULL, // #476 -NULL, // #477 +VM_strlennocol, // #476 float(string s) : DRESK - String Length (not counting color codes) (DP_QC_STRINGCOLORFUNCTIONS) +VM_strdecolorize, // #477 string(string s) : DRESK - Decolorized String (DP_QC_STRINGCOLORFUNCTIONS) NULL, // #478 NULL, // #479 e10, e10 // #480-499 (LordHavoc)