/************************ template_nullterminate *************************
 * Copyright RISC User May 1992                                        *
 * Replaces the <Ret> string terminations of a template window by a Null *
 * character so that C library functions can manipulate them.            *
 * All the strings associated with a window are reterminated:- window    *
 * title, and all icon text/spritenames (including validation strings)   *
 * The function will normally be called after template_init() during the *
 * initialisation of a program.                                          *
 * Example:                                                              *
 *   template_init();                                                    *
 *   mywind = template_syshandle("mywind");                              *
 *   template_nullterminate(mywind);                                     *
 * The function calls two subfunctions;                                  *
 *   icon_nullterminate   finds the strings associated with an icon      *
 *   string_nullterminate reterminates the string                        *
 * icon_nullterminate returns immediately if there is no icon data, else *
 * passes the found strings to string_nullterminate.                     *
 * string_nullterminate actually replaces the first control character    *
 * found, rather than only <Ret>, just to be on the safe side.           *
 *************************************************************************/

#include "wimp.h"

static void string_nullterminate(char * str)
{
        while (*str++ >= ' ')   /* this search method compiles nicely */
                ;
        *(str - 1) = '\0';
}


static void icon_nullterminate(wimp_icondata * icondata, wimp_iconflags iconflags)
{
        if (iconflags & (wimp_ITEXT | wimp_ISPRITE) == 0)
                return;

        if (iconflags & wimp_INDIRECT) {
                string_nullterminate(icondata->indirecttext.buffer);
                if ((int) icondata->indirecttext.validstring > 0) {
                        string_nullterminate(icondata->indirecttext.validstring);
                }
        } else {
                string_nullterminate(icondata->text);
        }
}


void template_nullterminate(wimp_wind * wind)
{
        wimp_icon * icon;
        int i;

        icon_nullterminate(&wind->title, wind->titleflags);
        for (i = wind->nicons, icon = (wimp_icon *) (wind + 1); i > 0; i--, icon++) {
                icon_nullterminate(&icon->data, icon->flags);
        }
}
