Boost logo

Boost :

From: John Nagle (nagle_at_[hidden])
Date: 2004-05-17 13:06:07


   With no base class for all fixed_string types, though,
there's no way to pass fixed_string objects around, short
of templating every function that uses them.

   A base class seems to be needed. Otherwise, there's
no direct replacement for "char *" arguments.

   Here's a simple test program. It should be possible
to do the common operations done here. That doesn't
seem possible with the latest version in the Boost
sandbox.

                        John Nagle
                        Animats

====

static void printstring(const char_string_base& str)
{
        printf("Printing from a function: %s\n",str.c_str());
}

static void editstring(char_string_base& str,float val)
{ sprintf(str,"F = %f\n",val); }

int maindunn()
{
        // String test
        char_string<80> s1;
        char_string<10> s2;
        s1.copy("This is a test");
        printf("Hello: %s\n",s1.c_str());
        sprintf(s1,"N=%d\n",100);
        s2.copy(s1);
        printf("Printing with printf directly: %s",s2.c_str());
        editstring(s2,100); // This overflows the string.
        printstring(s2);
}

                        John Nagle

Reece Dunn wrote:

> John Nagle wrote:
>
>> That's an older version, though; the strings have
>> no length field, and you said you were putting one in.
>> Should I do that?
>
>
> I have been busy with other things. I have finally gotten around to
> updating the code, which now includes:
> * internal length tracking
> * null-termination as a given (i.e. it will no longer work in data
> structure headers for signature checking, etc.)
> * safe_strings.hpp uses your strategy of putting the C-string functions
> in the global namespace (I would have liked a better approach to this).
>
> #include <boost/fixed_string/safe_strings.hpp>
> // ...
> boost::char_string< 15 > cstr;
> strcpy( cstr, "Meine Grosse Welt!" );
> std::cout << cstr << '\n';
>
> sprintf( cstr, "Hello %d Worlds!", 10 );
> std::cout << cstr << '\n';
>
> Results in:
> Meine Grosse W
> Hello 10 World
>
> Regards,
> Reece
>
> _________________________________________________________________
> Use MSN Messenger to send music and pics to your friends
> http://www.msn.co.uk/messenger
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk