code-experiments/examples/bbob2009-c-cmaes/coco.c
/**
 * @brief Formatted string duplication, with va_list arguments.
 */
@brief Formatted string duplication, with va_list arguments.
@brief Formatted string duplication with va_list arguments .
static char *coco_vstrdupf(const char *str, va_list args) {
  static char buf[COCO_VSTRDUPF_BUFLEN];
  long written;
#if 0
  written = vsnprintf(buf, COCO_VSTRDUPF_BUFLEN - 2, str, args);
  if (written < 0)
  coco_error("coco_vstrdupf(): vsnprintf failed on '%s'", str);
#else 
  assert(strlen(str) < COCO_VSTRDUPF_BUFLEN / 2 - 2);
  if (strlen(str) >= COCO_VSTRDUPF_BUFLEN / 2 - 2)
    coco_error("coco_vstrdupf(): string is too long");
  written = vsprintf(buf, str, args);
  if (written < 0)
    coco_error("coco_vstrdupf(): vsprintf failed on '%s'", str);
#endif
  if (written > COCO_VSTRDUPF_BUFLEN - 3)
    coco_error("coco_vstrdupf(): A suspiciously long string is tried to being duplicated '%s'", buf);
  return coco_strdup(buf);
}
static char * coco_vstrdupf ( const char * str , va_list args ) { static char buf [ COCO_VSTRDUPF_BUFLEN ] ; long written ; #if 0 written = vsnprintf ( buf , COCO_VSTRDUPF_BUFLEN - 2 , str , args ) ; if ( written < 0 ) coco_error ( " " , str ) ; #else assert ( strlen ( str ) < COCO_VSTRDUPF_BUFLEN / 2 - 2 ) ; if ( strlen ( str ) >= COCO_VSTRDUPF_BUFLEN / 2 - 2 ) coco_error ( " " ) ; written = vsprintf ( buf , str , args ) ; if ( written < 0 ) coco_error ( " " , str ) ; #endif if ( written > COCO_VSTRDUPF_BUFLEN - 3 ) coco_error ( " " , buf ) ; return coco_strdup ( buf ) ; }
@brief Formatted string duplication, with va_list arguments.
@brief Formatted string duplication with va_list arguments .
/* apparently args can only be used once, therefore
   * len = vsnprintf(NULL, 0, str, args) to find out the
   * length does not work. Therefore we use a buffer
   * which limits the max length. Longer strings should
   * never appear anyway, so this is rather a non-issue. */ /* less safe alternative, if vsnprintf is not available */