/*
 * $Id: ToString.cpp 82 2008-05-01 16:48:45Z rosiere $
 *
 * [ Description ]
 * 
 */

#include "../include/ToString.h"

namespace morpheo {

  std::string toString (const char *fmt, ...)
  {
    int size = 128; // initial size
    char *tmp;
    va_list ap;
    while (1) 
      {
	// Allocation
	tmp = new char [size];
	
	va_start(ap, fmt);
	int n = vsnprintf (tmp, size, fmt, ap);
	va_end(ap);
	
	// n : (<= GlibC 2.0.6) number of print caracter, if n == -1, tmp is truncate 
	//     ( > GlibC 2.1  ) number of caracter

	// Test if ok
	if ((n > -1) and (n < size))
	  break;
	
	if (n > -1)
	  size = n+1; // ( > GlibC 2.1  )
	else
	  size *= 2;  // (<= GlibC 2.0.6)
	
	delete [] tmp;
      }
    
    std::string str = toString(tmp);
    delete [] tmp;

    return str;
  }


}; // end namespace morpheo
