]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
make COM_StringLengthNoColors and COM_StringDecolorize get an additional length param...
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 27 Apr 2007 22:57:01 +0000 (22:57 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 27 Apr 2007 22:57:01 +0000 (22:57 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7205 d7cf8633-e32d-0410-b094-e92efae38249

cl_screen.c
common.c
common.h
host_cmd.c
prvm_cmds.c

index 4d0d7709f19ba99dc12684399f251e6eff86ee28..4768f4b7d6b46c55c4b9491b14e1395ef50c33d9 100644 (file)
@@ -106,7 +106,6 @@ void SCR_CenterPrint(char *str)
 void SCR_DrawCenterString (void)
 {
        char    *start;
-       int             l;
        int             x, y;
        int             remaining;
        int             color;
@@ -132,17 +131,10 @@ void SCR_DrawCenterString (void)
        do
        {
                // scan the number of characters on the line, not counting color codes
-               int chars = 0;
-               for (l=0 ; l<vid_conwidth.integer/8 ; l++)
-               {
-                       if (start[l] == '\n' || !start[l])
-                               break;
-                       // color codes add no visible characters, so don't count them
-                       if (start[l] == STRING_COLOR_TAG && (start[l+1] >= '0' && start[l+1] <= '9'))
-                               l++;
-                       else
-                               chars++;
-               }
+               char *newline = strchr(start, '\n');
+               int l = newline ? (newline - start) : (int)strlen(start);
+               int chars = COM_StringLengthNoColors(start, l, NULL);
+
                x = (vid_conwidth.integer - chars*8)/2;
                if (l > 0)
                {
@@ -153,15 +145,11 @@ void SCR_DrawCenterString (void)
                        if (remaining <= 0)
                                return;
                }
-
                y += 8;
 
-               while (*start && *start != '\n')
-                       start++;
-
-               if (!*start)
+               if (!newline)
                        break;
-               start++;                // skip the \n
+               start = newline + 1; // skip the \n
        } while (1);
 }
 
index 118e89e715418756d974a6e188b1fb89f62781e3..df73349fc57174a839bb11282749d17362bc2fec 100644 (file)
--- a/common.c
+++ b/common.c
@@ -1293,15 +1293,19 @@ it does not end with an unfinished color code). If it gets filled with FALSE, a
 fix would be adding a STRING_COLOR_TAG at the end of the string.
 
 valid can be set to NULL if the caller doesn't care.
+
+For size_s, specify the maximum number of characters from s to use, or 0 to use
+all characters until the zero terminator.
 ============
 */
 size_t
-COM_StringLengthNoColors(const char *s, qboolean *valid)
+COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid)
 {
+       const char *end = size_s ? (s + size_s) : NULL;
        size_t len = 0;
        for(;;)
        {
-               switch(*s)
+               switch((s == end) ? 0 : *s)
                {
                        case 0:
                                if(valid)
@@ -1309,7 +1313,7 @@ COM_StringLengthNoColors(const char *s, qboolean *valid)
                                return len;
                        case STRING_COLOR_TAG:
                                ++s;
-                               switch(*s)
+                               switch((s == end) ? 0 : *s)
                                {
                                        case 0: // ends with unfinished color code!
                                                ++len;
@@ -1352,24 +1356,28 @@ FALSE. Generally, if escape_carets is false, the output buffer needs
 strlen(str)+1 bytes, and if escape_carets is true, it can need strlen(str)+2
 bytes. In any case, the function makes sure that the resulting string is
 zero terminated.
+
+For size_in, specify the maximum number of characters from in to use, or 0 to use
+all characters until the zero terminator.
 ============
 */
 qboolean
-COM_StringDecolorize(const char *in, char *out, size_t size_out, qboolean escape_carets)
+COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets)
 {
 #define APPEND(ch) do { if(--size_out) { *out++ = (ch); } else { *out++ = 0; return FALSE; } } while(0)
+       const char *end = size_in ? (in + size_in) : NULL;
        if(size_out < 1)
                return FALSE;
        for(;;)
        {
-               switch(*in)
+               switch((in == end) ? 0 : *in)
                {
                        case 0:
                                *out++ = 0;
                                return TRUE;
                        case STRING_COLOR_TAG:
                                ++in;
-                               switch(*in)
+                               switch((in == end) ? 0 : *in)
                                {
                                        case 0: // ends with unfinished color code!
                                                APPEND(STRING_COLOR_TAG);
index 71713c40b9de9551b800dc0bd220005804e7e49c..3d960d136e13308365eb1e6b4e5a9f3aa3309a5a 100644 (file)
--- a/common.h
+++ b/common.h
@@ -287,8 +287,8 @@ int COM_StringBeginsWith(const char *s, const char *match);
 
 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
 
-size_t COM_StringLengthNoColors(const char *s, qboolean *valid);
-qboolean COM_StringDecolorize(const char *in, char *out, size_t size_out, qboolean escape_carets);
+size_t COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
+qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
 
 typedef struct stringlist_s
 {
index de921fdf8fe306503af1724db6970c00470a6393..ad1046c0b71243d9d9c9d59b800991a4a3da637a 100644 (file)
@@ -773,7 +773,7 @@ void Host_Name_f (void)
        // point the string back at updateclient->name to keep it safe
        strlcpy (host_client->name, newName, sizeof (host_client->name));
 
-       COM_StringLengthNoColors(host_client->name, &valid_colors);
+       COM_StringLengthNoColors(host_client->name, 0, &valid_colors);
        if(!valid_colors) // NOTE: this also proves the string is not empty, as "" is a valid colored string
        {
                size_t l;
index 095a313a43bd81db57287ce09b4d0229c108ab8f..73816935ce56b2e4e5123cc36a501ffad76b7670 100644 (file)
@@ -1759,7 +1759,7 @@ void VM_strdecolorize(void)
        VM_SAFEPARMCOUNT(1,VM_strdecolorize);
        szString = PRVM_G_STRING(OFS_PARM0);
 
-       COM_StringDecolorize(szString, szNewString, sizeof(szNewString), TRUE);
+       COM_StringDecolorize(szString, 0, szNewString, sizeof(szNewString), TRUE);
 
        PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(szNewString);
 }
@@ -1783,7 +1783,7 @@ void VM_strlennocol(void)
 
        szString = PRVM_G_STRING(OFS_PARM0);
 
-       nCnt = COM_StringLengthNoColors(szString, NULL);
+       nCnt = COM_StringLengthNoColors(szString, 0, NULL);
 
        PRVM_G_FLOAT(OFS_RETURN) = nCnt;
 }