Exemple #1
0
bool 
Converter::can_open(Speakers new_spk) const
{
  if ((FORMAT_MASK(new_spk.format) & converter_formats) == 0)
    return false;

  if (new_spk.format == format)
    return true; 

  if (new_spk.nch() == 0)
    return false;

  if (find_conversion(format, new_spk) == 0)
    return false;

  return true;
}
Exemple #2
0
char *recode_out(const SERVER_REC *server, const char *str, const char *target)
{
	char *recoded = NULL;
	const char *from = NULL;
	const char *to = NULL;
	char *translit_to = NULL;
	gboolean translit, term_is_utf8, recode;
	int len;

	if (!str)
		return NULL;

	recode = settings_get_bool("recode");
	if (!recode)
		return g_strdup(str);

	len = strlen(str);

	translit = settings_get_bool("recode_transliterate");

	to = find_conversion(server, target);
	if (to == NULL)
		/* default outgoing charset if set */
		to = settings_get_str("recode_out_default_charset");

	if (to && *to != '\0') {
		if (translit && !is_translit(to))
			to = translit_to = g_strconcat(to ,"//TRANSLIT", NULL);

		term_is_utf8 = recode_get_charset(&from);
		recoded = g_convert(str, len, to, from, NULL, NULL, NULL);
	}
	g_free(translit_to);
	if (!recoded)
		recoded = g_strdup(str);

	return recoded;
}
Exemple #3
0
bool 
Converter::init()
{
  /////////////////////////////////////////////////////////
  // Initialize convertor:
  // * find conversion function
  // * allocate buffer
  // * reset filter state

  if (spk.format == format)
    // no conversion required; no buffer required
    return true; 
  
  convert = find_conversion(format, spk);
  if (convert == 0)
    return false;

  buf.allocate(spk.nch() * nsamples * sample_size(format));

  if (format == FORMAT_LINEAR)
  {
    // set channel pointers
    out_samples[0] = (sample_t *)buf.begin();
    for (int ch = 1; ch < spk.nch(); ch++)
      out_samples[ch] = out_samples[ch-1] + nsamples;
    out_rawdata = 0;
  }
  else
  {
    // set rawdata pointer
    out_rawdata = buf.begin();
    out_samples.zero();
  }

  reset();
  return true;
}
Exemple #4
0
char *recode_in(const SERVER_REC *server, const char *str, const char *target)
{
	const char *from = NULL;
	const char *to = NULL;
	char *translit_to = NULL;
	char *recoded = NULL;
	gboolean term_is_utf8, str_is_utf8, translit, recode, autodetect;
	int len;
	int i;

	if (!str)
		return NULL;

	recode = settings_get_bool("recode");
	if (!recode)
		return g_strdup(str);

	len = strlen(str);

	/* Only validate for UTF-8 if an 8-bit encoding. */
	str_is_utf8 = 0;
	for (i = 0; i < len; ++i) {
		if (str[i] & 0x80) {
			str_is_utf8 = g_utf8_validate(str, len, NULL);
			break;
		}
	}
	translit = settings_get_bool("recode_transliterate");
	autodetect = settings_get_bool("recode_autodetect_utf8");
	term_is_utf8 = recode_get_charset(&to);

	if (autodetect && str_is_utf8)
		if (term_is_utf8)
			return g_strdup(str);
		else
			from = "UTF-8";
			
	else {
		from = find_conversion(server, target);
	}

	if (translit && !is_translit(to))
		to = translit_to = g_strconcat(to, "//TRANSLIT", NULL);
		
	if (from)
		recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);

	if (!recoded) {
		if (term_is_utf8) {
			if (!str_is_utf8)
				from = settings_get_str("recode_fallback");

		} else if (str_is_utf8)
			from = "UTF-8";

		if (from)
			recoded = g_convert_with_fallback(str, len, to, from, NULL, NULL, NULL, NULL);

		if (!recoded)
			recoded = g_strdup(str);
	}
	g_free(translit_to);
	return recoded;
}
Exemple #5
0
/**
 * tracker_sparql_escape_uri_vprintf:
 * @format: a standard printf() format string, but notice
 *     <link linkend="string-precision">string precision pitfalls</link> documented in g_strdup_printf()
 * @args: the list of parameters to insert into the format string
 *
 * Similar to the standard C vsprintf() function but safer, since it
 * calculates the maximum space required and allocates memory to hold
 * the result.
 *
 * The result is escaped using g_uri_escape_string().
 *
 * Returns: a newly-allocated string holding the result. The returned string
 * should be freed with g_free() when no longer needed.
 *
 * Since: 0.10
 */
gchar *
tracker_sparql_escape_uri_vprintf (const gchar *format,
                                   va_list      args)
{
	GString *format1;
	GString *format2;
	GString *result = NULL;
	gchar *output1 = NULL;
	gchar *output2 = NULL;
	const char *p;
	gchar *op1, *op2;
	va_list args2;

	format1 = g_string_new (NULL);
	format2 = g_string_new (NULL);
	p = format;
	while (TRUE) {
		const char *after;
		const char *conv = find_conversion (p, &after);
		if (!conv)
			break;

		g_string_append_len (format1, conv, after - conv);
		g_string_append_c (format1, 'X');
		g_string_append_len (format2, conv, after - conv);
		g_string_append_c (format2, 'Y');

		p = after;
	}

	/* Use them to format the arguments
	 */
	G_VA_COPY (args2, args);

	output1 = g_strdup_vprintf (format1->str, args);
	va_end (args);
	if (!output1) {
		va_end (args2);
		goto cleanup;
	}

	output2 = g_strdup_vprintf (format2->str, args2);
	va_end (args2);
	if (!output2)
		goto cleanup;

	result = g_string_new (NULL);

	op1 = output1;
	op2 = output2;
	p = format;
	while (TRUE) {
		const char *after;
		const char *output_start;
		const char *conv = find_conversion (p, &after);
		char *escaped;

		if (!conv) {
			g_string_append_len (result, p, after - p);
			break;
		}

		g_string_append_len (result, p, conv - p);
		output_start = op1;
		while (*op1 == *op2) {
			op1++;
			op2++;
		}

		*op1 = '\0';
		escaped = g_uri_escape_string (output_start, G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT, FALSE);
		g_string_append (result, escaped);
		g_free (escaped);

		p = after;
		op1++;
		op2++;
	}

cleanup:
	g_string_free (format1, TRUE);
	g_string_free (format2, TRUE);
	g_free (output1);
	g_free (output2);

	if (result)
		return g_string_free (result, FALSE);
	else
		return NULL;
}