View Single Post
  #11 (permalink)  
Old August 21st, 2002
Moak
Guest
 
Posts: n/a
Default

Good luck with your new home, if you're becoming my neighbour call me for a beer. :)

Thx for the answers, I still need lots of time. Since my network
class grows with my server (no Gnutella thingie) and the server
takes all my time. I'll plan to use STL std::string with some tiny
addition yet like sstring::format() and sstring::operator const char*().

Btw when using std::string here's a port of format() member:

Code:
void vformat(const char *fmt, va_list ap)
{
    if(strstr(fmt, "%n")) {             //check against %n, which can be exploited by accident
        assert(0 && "sstring::format: Parachute for unsafe type %n");
        assign(fmt);                    //note: really nobody needs unsafe %n
        return;
    }
    
    std::vector<char> buffer;           //buffer for creating string
    size_t size = strlen(fmt)+16;       //some extra characters, string will grow

    while (1) 
    {
        buffer.reserve(size);           //ensure we have that much space in the buffer
                                        //try to print into allocated space
        int n = vsnprintf(&buffer[0], buffer.capacity(), fmt, ap);
        if (n>-1 && n<buffer.capacity())
        {                               //allocated space was fine
            assign(&buffer[0]);         //store the formated string (unfortunately a copy)
            return;
        }
                                        //else try again with more space
        if (n>-1)                       //glibc 2.1
            size = n+1;                 //precisely what is needed
        else                            //glibc 2.0 or MS-crt
            size = buffer.capacity()*2; //twice the old size
    }
}
Reply With Quote