예제 #1
0
파일: completion.c 프로젝트: hiciu/ekg2
static char *rl_strndup(gchar *s, gssize n) {
	static GString *buf = NULL;
	gchar *rec;

		/* XXX: API for stated-size recoding */
	if (G_UNLIKELY(!buf))
		buf = g_string_sized_new(G_LIKELY(n != -1) ? n+1 : 16);
	if (G_LIKELY(n == -1))
		g_string_assign(buf, s);
	else {
		g_string_truncate(buf, 0);
		g_string_append_len(buf, s, n);
	}

	rec = ekg_recode_to_locale(buf->str);
	if (G_LIKELY(g_mem_is_system_malloc()))
		return rec;
	else {
		gsize len = strlen(rec) + 1;
		char *out = malloc(len);

		memcpy(out, rec, len);
		g_free(rec);
		return out;
	}
}
예제 #2
0
파일: gprintf.c 프로젝트: 0xmono/miranda-ng
/**
 * g_vasprintf:
 * @string: the return location for the newly-allocated string.
 * @format: a standard printf() format string, but notice
 *          <link linkend="string-precision">string precision pitfalls</link>.
 * @args: the list of arguments to insert in the output.
 *
 * An implementation of the GNU vasprintf() function which supports 
 * positional parameters, as specified in the Single Unix Specification.
 * This function is similar to g_vsprintf(), except that it allocates a 
 * string to hold the output, instead of putting the output in a buffer 
 * you allocate in advance.
 *
 * Returns: the number of bytes printed.
 *
 * Since: 2.4
 **/
gint 
g_vasprintf (gchar      **string,
	     gchar const *format,
	     va_list      args)
{
  gint len;
  g_return_val_if_fail (string != NULL, -1);

#if !defined(HAVE_GOOD_PRINTF)

  len = _g_gnulib_vasprintf (string, format, args);
  if (len < 0)
    *string = NULL;

#elif defined (HAVE_VASPRINTF)

  len = vasprintf (string, format, args);
  if (len < 0)
    *string = NULL;
  else if (!g_mem_is_system_malloc ()) 
    {
      /* vasprintf returns malloc-allocated memory */
      gchar *string1 = g_strndup (*string, len);
      free (*string);
      *string = string1;
    }

#else

  {
    va_list args2;

    G_VA_COPY (args2, args);

    *string = g_new (gchar, g_printf_string_upper_bound (format, args));

    len = _g_vsprintf (*string, format, args2);
    va_end (args2);
  }
#endif

  return len;
}