Пример #1
0
gchar*
mu_str_convert_to_utf8 (const char* buffer, const char *charset)
{
	GError *err;
	gchar * utf8;

	g_return_val_if_fail (buffer, NULL);
	g_return_val_if_fail (charset, NULL );

	err = NULL;
	utf8 = g_convert_with_fallback (buffer, -1, "UTF-8",
					charset, NULL,
					NULL, NULL, &err);
	if (!utf8) /* maybe the charset lied; try 8859-15 */
		utf8 = g_convert_with_fallback (buffer, -1, "UTF-8",
						"ISO8859-15", NULL,
						NULL, NULL, &err);
	/* final attempt, maybe it was utf-8 already */
	if (!utf8 && g_utf8_validate (buffer, -1, NULL))
		utf8 = g_strdup (buffer);

	if (!utf8) {
		g_warning ("%s: conversion failed from %s: %s",
			 __FUNCTION__, charset, err ? err->message : "");
	}

	g_clear_error (&err);

	return utf8;
}
Пример #2
0
gboolean
get_name_info_from_component (Component componentID,
    ComponentDescription * desc, gchar ** name, gchar ** info)
{
  Handle nameHandle = NewHandle (200);
  Handle infoHandle = NewHandle (200);
  gchar *tmpname;
  gchar *tmpinfo;
  OSErr result;
  gboolean ret = TRUE;

  result = GetComponentInfo (componentID, desc, nameHandle, infoHandle, NULL);
  if (result != noErr) {
    ret = FALSE;
    goto done;
  }
#if DEBUG_DUMP
  GST_LOG ("ComponentDescription dump");
  gst_util_dump_mem ((const guchar *) desc, sizeof (ComponentDescription));
  gst_util_dump_mem ((gpointer) * nameHandle, 200);
  gst_util_dump_mem ((gpointer) * infoHandle, 200);
  GST_LOG ("0x%x 0x%x", **((guint8 **) nameHandle), **((guint8 **) infoHandle));
#endif

  if (*nameHandle && name) {
    gsize read, written;

    tmpname = g_strndup ((*(char **) nameHandle) + 1,
        **((guint8 **) nameHandle));
    *name = g_convert_with_fallback (tmpname, -1, "ASCII", "MAC",
        (gchar *) " ", &read, &written, NULL);
    if (!*name)
      GST_WARNING ("read:%" G_GSIZE_FORMAT ", written:%" G_GSIZE_FORMAT, read,
          written);
    g_free (tmpname);
  }

  if (*infoHandle && info) {
    tmpinfo =
        g_strndup ((*(char **) infoHandle) + 1, **((guint8 **) infoHandle));
    *info =
        g_convert_with_fallback (tmpinfo, -1, "ASCII", "MAC", (gchar *) " ",
        NULL, NULL, NULL);
    g_free (tmpinfo);
  }

done:
  DisposeHandle (nameHandle);
  DisposeHandle (infoHandle);

  return ret;
}
Пример #3
0
Файл: log.c Проект: azuwis/mpd
static void
file_log_func(const gchar *log_domain,
	      G_GNUC_UNUSED GLogLevelFlags log_level,
	      const gchar *message, G_GNUC_UNUSED gpointer user_data)
{
	char *converted;

	if (log_level > log_threshold)
		return;

	if (log_charset != NULL) {
		converted = g_convert_with_fallback(message, -1,
						    log_charset, "utf-8",
						    NULL, NULL, NULL, NULL);
		if (converted != NULL)
			message = converted;
	} else
		converted = NULL;

	if (log_domain == NULL)
		log_domain = "";

	fprintf(stderr, "%s%s%s%.*s\n",
		stdout_mode ? "" : log_date(),
		log_domain, *log_domain == 0 ? "" : ": ",
		chomp_length(message), message);

	g_free(converted);
}
Пример #4
0
/* -------------------------------------------------------------------------- */
static void mpd_song_set_artist(struct song *song, const char *artist)
{
    song->artist = g_convert_with_fallback(artist, -1,
            "iso-8859-1", "utf-8", "?", NULL, NULL, NULL);
    if (!song->artist)
        song->artist = g_strdup("(Unknown)");
}
Пример #5
0
/* -------------------------------------------------------------------------- */
static void mpd_song_set_title(struct song *song, const char *title)
{
    song->title = g_convert_with_fallback(title, -1,
            "iso-8859-1", "utf-8", "?", NULL, NULL, NULL);
    if (!song->title)
        song->title = g_strdup("(Unknown)");
}
Пример #6
0
static void
soup_logger_print_basic_auth (SoupLogger *logger, const char *value)
{
	char *decoded, *decoded_utf8, *p;
	gsize len;

	decoded = (char *)g_base64_decode (value + 6, &len);
	if (decoded && !g_utf8_validate (decoded, -1, NULL)) {
		decoded_utf8 = g_convert_with_fallback (decoded, -1,
							"UTF-8", "ISO-8859-1",
							NULL, NULL, &len,
							NULL);
		if (decoded_utf8) {
			g_free (decoded);
			decoded = decoded_utf8;
		}
	}

	if (!decoded)
		decoded = g_strdup (value);
	p = strchr (decoded, ':');
	if (p) {
		while (++p < decoded + len)
			*p = '*';
	}
	soup_logger_print (logger, SOUP_LOGGER_LOG_HEADERS, '>',
			   "Authorization: Basic [%.*s]", len, decoded);
	g_free (decoded);
}
Пример #7
0
int
get_stamp_str (char *fmt, time_t tim, char **ret)
{
	char *loc = NULL;
	char dest[128];
	gsize len;

	/* strftime wants the format string in LOCALE! */
	if (!prefs.utf8_locale)
	{
		const gchar *charset;

		g_get_charset (&charset);
		loc = g_convert_with_fallback (fmt, -1, charset, "UTF-8", "?", 0, 0, 0);
		if (loc)
			fmt = loc;
	}

	len = strftime_validated (dest, sizeof (dest), fmt, localtime (&tim));
	if (len)
	{
		if (prefs.utf8_locale)
			*ret = g_strdup (dest);
		else
			*ret = g_locale_to_utf8 (dest, len, 0, &len, 0);
	}

	if (loc)
		g_free (loc);

	return len;
}
Пример #8
0
/* schreibt eine UTF-8 Zeichenkette auf die Standardausgabe */
gint printf_utf8(const gchar *format, ...)
{
    gchar *str_utf8, *str_locale;
    const char *locale_charset;
    va_list ap;
    gsize len_utf8, len_locale;
    gint retval;

    va_start(ap, format);
    str_utf8 = g_strdup_vprintf(format, ap);
    va_end(ap);

    len_utf8 = (gsize) strlen(str_utf8);

    if (!g_get_charset(&locale_charset)) {
        str_locale = g_convert_with_fallback(str_utf8, (gssize) len_utf8,
                                             locale_charset, "UTF-8",
                                             "_",
                                             NULL, &len_locale, NULL);
        g_free(str_utf8);
    } else {
        str_locale = str_utf8;
        str_utf8 = NULL;
        len_locale = len_utf8;
    }

    if (str_locale == NULL)
        return -1;

    retval = (gint) fwrite(str_locale, sizeof(gchar), len_locale, stdout);

    g_free(str_locale);

    return retval;
}
Пример #9
0
char *ua_fs (const TCHAR *s, int defchar) {
#ifdef NO_TRANSLATION
	if (s == NULL) return NULL;
	return strdup(s);
#else
    // we convert from fs-uae's internal encoding (UTF-8) to latin-1 here,
    // so file names can be read properly in the amiga

    char def[] = "?";
    if (defchar < 128) {
        def[0] = defchar;
    }

    gsize read, written;
    gchar *result = g_convert_with_fallback(s, -1, "ISO-8859-1",
            "UTF-8", def, &read, &written, NULL);
    if (result == NULL) {
        write_log("WARNING: ua_fs failed to convert string %s", s);
        return strdup("");
    }

    // duplicate with libc malloc
    char *result_malloced = strdup(result);
    g_free(result);
    return result_malloced;
#endif
}
Пример #10
0
char *ua_fs_copy (char *dst, int maxlen, const TCHAR *src, int defchar) {
#ifdef NO_TRANSLATION
    dst[0] = 0;
    strncpy(dst, src, maxlen);
    return dst;
#else
    char def[] = "?";
    if (defchar < 128) {
        def[0] = defchar;
    }

    gsize read, written;
    gchar *result = g_convert_with_fallback(src, -1, "ISO-8859-1",
            "UTF-8", def, &read, &written, NULL);
    if (result == NULL) {
        write_log("WARNING: ua_fs_copy failed to convert string %s", src);
        dst[0] = '\0';
        return dst;
    }

    strncpy(dst, result, maxlen);
    g_free(result);
    return dst;
#endif
}
Пример #11
0
static gchar *
convert_encoding (GstSubParse * self, const gchar * str, gsize len)
{
  const gchar *encoding;
  GError *err = NULL;
  gchar *ret;

  if (self->valid_utf8) {
    if (g_utf8_validate (str, len, NULL)) {
      GST_LOG_OBJECT (self, "valid UTF-8, no conversion needed");
      return g_strndup (str, len);
    }
    GST_INFO_OBJECT (self, "invalid UTF-8!");
    self->valid_utf8 = FALSE;
  }

  encoding = self->encoding;
  if (encoding == NULL || *encoding == '\0') {
    encoding = g_getenv ("GST_SUBTITLE_ENCODING");
  }
  if (encoding == NULL || *encoding == '\0') {
    /* if local encoding is UTF-8 and no encoding specified
     * via the environment variable, assume ISO-8859-15 */
    if (g_get_charset (&encoding)) {
      encoding = "ISO-8859-15";
    }
  }

  ret = g_convert_with_fallback (str, len, "UTF-8", encoding, "*", NULL,
      NULL, &err);

  if (err) {
    GST_WARNING_OBJECT (self, "could not convert string from '%s' to UTF-8: %s",
        encoding, err->message);
    g_error_free (err);

    /* invalid input encoding, fall back to ISO-8859-15 (always succeeds) */
    ret = g_convert_with_fallback (str, len, "UTF-8", "ISO-8859-15", "*",
        NULL, NULL, NULL);
  }

  GST_LOG_OBJECT (self,
      "successfully converted %" G_GSIZE_FORMAT " characters from %s to UTF-8"
      "%s", len, encoding, (err) ? " , using ISO-8859-15 as fallback" : "");

  return ret;
}
Пример #12
0
/*
 * Function: refresh_textview_from_fd
 *
 * This function is called when data are readable from our pipe.
 * It reads the data and adds it to the GTK textview.
 */
gboolean refresh_textview_from_fd(GIOChannel * channel,
				  GIOCondition condition, gpointer data)
{
    GtkTextView *view = GTK_TEXT_VIEW(data);
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

    gchar buf[1024];
    gsize len;
    GError *error = NULL;
    gint status;


    while ((status =
	    g_io_channel_read_chars(channel, buf, 1024, &len, &error))
	   == G_IO_STATUS_AGAIN) {

	while (gtk_events_pending())
	    gtk_main_iteration();
    }

    if (status != G_IO_STATUS_NORMAL) {
	/* status = G_IO_STATUS_ERROR or G_IO_STATUS_EOF */
	if (error) {
	    fprintf(stderr, _("Error while reading sub-child output : %s"),
		    error->message);
	    g_error_free(error);
	    error = NULL;
	}
	return FALSE;
    }

    if (len > 0) {
	GtkTextIter end;
	GtkTextMark *mark;
	gchar *utftext;
	gsize localelen;
	gsize utflen;

	gtk_text_buffer_get_end_iter(buffer, &end);

	if (!g_utf8_validate(buf, len, NULL)) {
	    utftext =
		g_convert_with_fallback(buf, len, "UTF-8",
					"ISO-8859-1", NULL, &localelen,
					&utflen, NULL);
	    gtk_text_buffer_insert(buffer, &end, utftext, utflen);
	    g_free(utftext);
	} else {
	    gtk_text_buffer_insert(buffer, &end, buf, len);
	}

	/* Scroll down TextView */
	mark = gtk_text_buffer_get_insert(buffer);
	gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(view), mark, 0.0, FALSE,
				     0.0, 1.0);
    }
    return TRUE;
}
Пример #13
0
static void GtkErrorLog (const char *format, ...)
{
	GtkWidget *dialog, *label;
	va_list args;
	char buf[256];

	if (dialog_connecting != NULL) {
		gtk_widget_destroy (dialog_connecting);
		dialog_connecting = NULL;
	}

	va_start (args, format);
	vsnprintf (buf, 255, format, args);
	va_end (args);

	if (g_utf8_validate (buf, strlen (buf), NULL)) {
		label = gtk_label_new (buf);
	} else {
		const gchar *charset;
		gchar       *utf8;

		(void) g_get_charset (&charset);
		utf8 = g_convert_with_fallback (buf, strlen (buf), "UTF-8",
		                                charset, NULL, NULL, NULL, NULL);

		if (utf8) {
			label = gtk_label_new (utf8);
			g_free (utf8);
		} else {
			label = gtk_label_new (buf);
			g_warning ("Message Output is not in UTF-8"
			           "nor in locale charset.\n");
		}
	}

	dialog = gtk_dialog_new_with_buttons ("Error",
	                                       NULL,
	                                       GTK_DIALOG_DESTROY_WITH_PARENT,
	                                       GTK_STOCK_OK,
	                                       GTK_RESPONSE_ACCEPT,
	                                       NULL);
	label = gtk_label_new (buf);
	gtk_widget_show (label);

	gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox),
	                   label);
	gtk_widget_show (dialog);

	switch (gtk_dialog_run (GTK_DIALOG (dialog))) {
	case GTK_RESPONSE_ACCEPT:
		break;
	default:
		break;
	}
	gtk_widget_destroy (dialog);
}
Пример #14
0
static gboolean
matedialog_text_handle_stdin (GIOChannel  *channel,
                          GIOCondition condition,
                          gpointer     data)
{
  static GtkTextBuffer *buffer;
  gchar buf[1024];

  gsize len;

  buffer = GTK_TEXT_BUFFER (data);

  if ((condition & G_IO_IN) || (condition & (G_IO_IN | G_IO_HUP))) {
    GError *error = NULL;
    gint status;

    while (channel->is_readable != TRUE)
      ;

    do {
      status = g_io_channel_read_chars (channel, buf, 1024, &len, &error);

      while (gtk_events_pending ())
        gtk_main_iteration ();

    } while (status == G_IO_STATUS_AGAIN);

    if (status != G_IO_STATUS_NORMAL) {
      if (error) {
        g_warning ("matedialog_text_handle_stdin () : %s", error->message);
        g_error_free (error);
        error = NULL;
      }
      return FALSE;
    }

    if (len > 0) {
      GtkTextIter end;
      gchar *utftext; 
      gsize localelen; 
      gsize utflen;

      gtk_text_buffer_get_end_iter (buffer, &end);

      if (!g_utf8_validate (buf, len, NULL)) {
        utftext = g_convert_with_fallback (buf, len, "UTF-8", "ISO-8859-1", NULL, &localelen, &utflen, NULL);
        gtk_text_buffer_insert (buffer, &end, utftext, utflen);
        g_free (utftext);
      } else {
        gtk_text_buffer_insert (buffer, &end, buf, len);
      }
    }
  }

  return TRUE;
}
Пример #15
0
void mrim_package_add_LPSA(MrimPackage *pack, gchar *string) {
	gsize str_len;
	gchar *str = g_convert_with_fallback(string, -1, "CP1251" , "UTF8", NULL, NULL, &str_len, NULL);
	if (str) {
		mrim_package_add_UL(pack, str_len);
		mrim_package_add_raw(pack, str, str_len);
		g_free(str);
	} else {
		mrim_package_add_UL(pack, 0);
	}
}
Пример #16
0
/**
 * Search all Joystick devices
 */
gboolean search_devices(GList **list_controllers) {
  int i;
  Controller_info *ctrl_info;
  struct udev *udev;
  struct udev_device *dev;

  for(i = 0; i < 32; ++i) {
    gchar *str = NULL;
    str = g_strdup_printf("/dev/input/js%d", i);
    int fd = open(str, O_RDONLY);
    if (fd < 0) {
      //printf("Could not found joystick: %s\n", str->str);
      break;
    } else {
      ctrl_info = g_malloc(sizeof(Controller_info));
      uint8_t num_axis   = 0;
      uint8_t num_button = 0;
      ioctl(fd, JSIOCGAXES,    &num_axis);
      ioctl(fd, JSIOCGBUTTONS, &num_button);
      ctrl_info->filename    = g_strdup(str);
      ctrl_info->num_axis    = num_axis;
      ctrl_info->num_buttons = num_button;
      // Get Name 
      char name_c_str[1024];
      if (ioctl(fd, JSIOCGNAME(sizeof(name_c_str)), name_c_str) < 0) {
          printf("%s : %s", str, strerror(errno));
          break;
      } else {
         ctrl_info->name = g_convert_with_fallback(name_c_str, sizeof(name_c_str), "UTF-8", "ISO-8859-1", NULL, NULL, NULL, NULL);
      }

      /* Create the udev object */
      udev = udev_new();
      if (!udev) {
        printf("Can't create udev\n");
        exit(0);
      }

      dev = udev_device_new_from_subsystem_sysname(udev, "input", g_strdup_printf("js%d", i));
      if (dev == NULL)
            break;

      ctrl_info->serial = uint32_atoi(g_strdup_printf("%s%s", udev_list_entry_get_value(udev_list_entry_get_by_name(udev_device_get_properties_list_entry(dev), "ID_VENDOR_ID")), udev_list_entry_get_value(udev_list_entry_get_by_name(udev_device_get_properties_list_entry(dev), "ID_MODEL_ID"))));

      udev_device_unref(dev);
      udev_unref(udev);
      printf("%s : %d, %d, 0x%08x\n", ctrl_info->name, ctrl_info->num_axis, ctrl_info->num_buttons, ctrl_info->serial);
    }
    *list_controllers = g_list_append(*list_controllers, ctrl_info);
    close(fd);
  }

  return TRUE;
}
Пример #17
0
/* Internal function: convert a null-terminated UTF-8 string to a 
null-terminated Latin-1 string, replacing characters that cannot be represented 
in Latin-1 by a placeholder. If bytes_written is not NULL it will be filled with
the number of bytes returned, not counting the NULL terminator. The returned
string must be freed afterwards. Returns NULL on error. */
gchar *
convert_utf8_to_latin1(const gchar *s, gsize *bytes_written)
{
	GError *error = NULL;
	gchar *retval = g_convert_with_fallback(s, -1, "ISO-8859-1", "UTF-8", PLACEHOLDER_STRING, NULL, bytes_written, &error);
	
	if(retval == NULL)
		IO_WARNING("Error during utf8->latin1 conversion of string", s, error->message);

	return retval;
}
Пример #18
0
GstTagList *
gst_rm_utils_read_tags (const guint8 * data, guint datalen,
    GstRmUtilsStringReadFunc read_string_func)
{
  const gchar *gst_tags[] = { GST_TAG_TITLE, GST_TAG_ARTIST,
    GST_TAG_COPYRIGHT, GST_TAG_COMMENT
  };
  GstTagList *tags;
  guint i;

  g_assert (read_string_func != NULL);

  GST_DEBUG ("File Content : (CONT) len = %d", datalen);

  tags = gst_tag_list_new ();

  for (i = 0; i < G_N_ELEMENTS (gst_tags); ++i) {
    gchar *str = NULL;
    guint total_length = 0;

    str = read_string_func (data, datalen, &total_length);
    data += total_length;
    datalen -= total_length;

    if (str != NULL && !g_utf8_validate (str, -1, NULL)) {
      const gchar *encoding;
      gchar *tmp;

      encoding = g_getenv ("GST_TAG_ENCODING");
      if (encoding == NULL || *encoding == '\0') {
        if (g_get_charset (&encoding))
          encoding = "ISO-8859-15";
      }
      GST_DEBUG ("converting tag from %s to UTF-8", encoding);
      tmp = g_convert_with_fallback (str, -1, "UTF-8", encoding, "*",
          NULL, NULL, NULL);
      g_free (str);
      str = tmp;
    }

    GST_DEBUG ("%s = %s", gst_tags[i], GST_STR_NULL (str));
    if (str != NULL && *str != '\0') {
      gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, gst_tags[i], str, NULL);
    }
    g_free (str);
  }

  if (gst_structure_n_fields ((GstStructure *) tags) > 0)
    return tags;

  gst_tag_list_free (tags);
  return NULL;
}
Пример #19
0
static gchar *vcard_strdup_decode(gchar *text, OsmoVCardEncoding enc)
{
	gchar *s, *pi = text, *po, shex[3] = { 'X', 'X', '\0' };
	guint32 n_eq = 0;

	switch(enc) {
		/* all non-ASCII characters should be encoded QP */
		case VCARD_QUOTED_PRINTABLE:
			/* count "=" chars */
			while(*pi != '\0') {
				if((*pi == '=') && (*(pi + 1) != '\n'))
					n_eq ++;
				pi ++;
			}
			/* allocate memory */
			po = s = g_new0(gchar, strlen(text) - (n_eq * 2) + 1);
			pi = text;
			/* copy and decode text */
			while(*pi != '\0') {
				if(*pi == '=') {
					if(*(pi + 1) == '\n')
						/* '=' at end of line => soft break */
						*po = ' ';
					else {
						shex[0] = *(++ pi);
						shex[1] = *(++ pi);
						*po = strtol(shex, NULL, 16);
					}
				}
				else
					*po = *pi;
				po ++;
				pi ++;
			}
			break;
		case VCARD_BASE64:
			/* base64 is used for photos and such stuff... */
			s = g_strdup("(BASE64 encoding currently unsupported)");
		default:
			s = g_strdup(text);
	}

	/* convert to UTF-8 if necessary */
	if(g_utf8_validate(s, -1, NULL) == FALSE) {
		po = g_convert_with_fallback(s, -1, "utf-8", "iso-8859-1",
			"?", NULL, NULL, NULL);
		g_free(s);
		s = po;
	}
	return s;
}
Пример #20
0
void mrim_package_add_base64(MrimPackage *pack, gchar *fmt, ...) {
	gchar *buffer = NULL;
	gsize buffer_size = 0;
	va_list ap;
	va_start(ap, fmt);
	while (*fmt) {
		switch (*fmt) {
			case 'u':
				{
					guint32 value = va_arg(ap, guint32);
					buffer = g_realloc(buffer, buffer_size + sizeof(guint32));
					g_memmove(buffer + buffer_size, &value, sizeof(guint32));
					buffer_size += sizeof(guint32);
				}			
				break;
			case 's': //CP1251
				{
					gchar *string = va_arg(ap, gchar*);
					gsize str_len = g_utf8_strlen(string, -1);
					buffer = g_realloc(buffer, buffer_size + sizeof(guint32) + str_len);
					g_memmove(buffer + buffer_size, &str_len, sizeof(guint32));
					gchar *str = g_convert_with_fallback(string, -1, "CP1251" , "UTF8", NULL, NULL, NULL, NULL);
					g_memmove(buffer + buffer_size + sizeof(guint32), str, str_len);
					g_free(str);
					buffer_size += sizeof(guint32) + str_len;
				}
				break;
			case 'w': //UTF16
				{
					gchar *string = va_arg(ap, gchar*);
					gsize str_len = g_utf8_strlen(string, -1) * sizeof(gunichar2);
					buffer = g_realloc(buffer, buffer_size + sizeof(guint32) + str_len);
					g_memmove(buffer + buffer_size, &str_len, sizeof(guint32));
					gunichar2 *str = g_utf8_to_utf16(string, -1, NULL, NULL, NULL);
					g_memmove(buffer + buffer_size + sizeof(guint32), str, str_len);
					g_free(str);
					buffer_size += sizeof(guint32) + str_len;
				}
				break;
		}
		fmt++;
	}
	va_end(ap);
	gchar *encoded = purple_base64_encode((gchar*)buffer, buffer_size);
	guint32 encoded_len = strlen(encoded);
	mrim_package_add_UL(pack, encoded_len);
	mrim_package_add_raw(pack, encoded, encoded_len);
	g_free(encoded);
	g_free(buffer);
}
Пример #21
0
static id3_utf8_t *
import_8bit_string(id3_latin1_t* isostr, const char* encoding)
{
    id3_utf8_t* utf8 = (id3_utf8_t *)
        g_convert_with_fallback((const char*)isostr,
                        strlen((const char *) isostr),
                        "utf-8",
                        encoding,
                        NULL, NULL, NULL, NULL);
    if (utf8 == NULL) {
        g_debug("Unable to convert %s string to UTF-8: '%s'", encoding, isostr);
    }
    return utf8;
}
Пример #22
0
static gboolean exec_channel_callback (GIOChannel *channel, GIOCondition condition, gpointer data)
{
	ExecCmd *cmd = (ExecCmd*) data;
	gboolean cont = TRUE;

	/* there's data to be read */
	if ((condition & G_IO_IN) || (condition & G_IO_PRI)) {
		static const gint BUFF_SIZE = 1024;
		gchar buffer[BUFF_SIZE];
		OBJZERO(buffer);
		gsize bytes = 0;
		const GIOStatus status =
				g_io_channel_read_chars(channel, buffer, (BUFF_SIZE - 1) * sizeof(gchar), &bytes, NULL);
		/* need to check what to do for again */
		if ((status == G_IO_STATUS_ERROR) || (status == G_IO_STATUS_AGAIN)) {
			g_warning("exec_channel_callback - read error [%d]\n", status);
			cont = FALSE;
		} else if (cmd->read_proc) {
			GError *error = NULL;
			const gchar* to_codeset = "UTF-8";
			const gchar* from_codeset = "ISO-8859-1";
			gchar* fallback = NULL;
			gchar *converted = g_convert_with_fallback(buffer, bytes, to_codeset, from_codeset, fallback, NULL, NULL,
					&error);
			if (converted != NULL) {
				cmd->read_proc(cmd, converted);
				g_free(converted);
			} else {
				if (error != NULL) {
					g_warning("exec_channel_callback - conversion error [%s]", error->message);
					g_error_free(error);
				} else
					g_warning("exec_channel_callback - unknown conversion error");
				cmd->read_proc(cmd, buffer);
			}
		}
	}

	if ((cont == FALSE) || (condition & G_IO_HUP) || (condition & G_IO_ERR) || (condition & G_IO_NVAL)) {
		/* We assume a failure here (even on G_IO_HUP) as exec_spawn_process will
		 * check the return code of the child to determine if it actually worked
		 * and set the correct state accordingly.*/
		exec_cmd_set_state(cmd, FAILED);
		g_debug("exec_channel_callback - condition [%d]\n", condition);
		cont = FALSE;
	}

	return cont;
}
Пример #23
0
static gchar*
strdup_convert (const gchar *string,
		const gchar *charset)
{
  if (!g_utf8_validate (string, -1, NULL))
    {
      GString *gstring = g_string_new ("[Invalid UTF-8] ");
      guchar *p;

      for (p = (guchar *)string; *p; p++)
	{
	  if (CHAR_IS_SAFE(*p) &&
	      !(*p == '\r' && *(p + 1) != '\n') &&
	      *p < 0x80)
	    g_string_append_c (gstring, *p);
	  else
	    g_string_append_printf (gstring, "\\x%02x", (guint)(guchar)*p);
	}
      
      return g_string_free (gstring, FALSE);
    }
  else
    {
      GError *err = NULL;
      
      gchar *result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, &err);
      if (result)
	return result;
      else
	{
#ifdef USE_LIBICONV_GNU	
      /* No libiconv in Android, so we only duplicate message if no 
	   * USE_LIBICONV_GNU */
           
	  /* Not thread-safe, but doesn't matter if we print the warning twice
	   */
	  static gboolean warned = FALSE; 
	  if (!warned)
	    {
	      warned = TRUE;
	      _g_fprintf (stderr, "GLib: Cannot convert message: %s\n", err->message);
	    }
	  g_error_free (err);
#endif	  
	  return g_strdup (string);
	}
    }
}
Пример #24
0
static guchar *
selection_get_text_plain (const GtkSelectionData *selection_data)
{
  const gchar *charset = NULL;
  gchar *str, *result;
  gsize len;
  GError *error = NULL;

  str = g_strdup ((const gchar *) selection_data->data);
  len = selection_data->length;
  
  if (selection_data->type == text_plain_atom)
    charset = "ISO-8859-1";
  else if (selection_data->type == text_plain_locale_atom)
    g_get_charset (&charset);

  if (charset)
    {
      gchar *tmp = str;
      str = g_convert_with_fallback (tmp, len, 
				     "UTF-8", charset,
				     NULL, NULL, &len, &error);
      g_free (tmp);

      if (!str)
	{
	  g_warning ("Error converting from %s to %s: %s",
		     charset, "UTF-8", error->message);
	  g_error_free (error);

	  return NULL;
	}
    }
  else if (!g_utf8_validate (str, -1, NULL))
    {
      g_warning ("Error converting from %s to %s: %s",
		 "text/plain;charset=utf-8", "UTF-8", "invalid UTF-8");
      g_free (str);

      return NULL;
    }

  result = normalize_to_lf (str, len);
  g_free (str);

  return (guchar *) result;
}
Пример #25
0
static gboolean
decode_rfc5987 (char *encoded_string)
{
	char *q, *decoded;
	gboolean iso_8859_1 = FALSE;

	q = strchr (encoded_string, '\'');
	if (!q)
		return FALSE;
	if (g_ascii_strncasecmp (encoded_string, "UTF-8",
				 q - encoded_string) == 0)
		;
	else if (g_ascii_strncasecmp (encoded_string, "iso-8859-1",
				      q - encoded_string) == 0)
		iso_8859_1 = TRUE;
	else
		return FALSE;

	q = strchr (q + 1, '\'');
	if (!q)
		return FALSE;

	decoded = soup_uri_decode (q + 1);
	if (iso_8859_1) {
		char *utf8 =  g_convert_with_fallback (decoded, -1, "UTF-8",
						       "iso-8859-1", "_",
						       NULL, NULL, NULL);
		g_free (decoded);
		if (!utf8)
			return FALSE;
		decoded = utf8;
	}

	/* If encoded_string was UTF-8, then each 3-character %-escape
	 * will be converted to a single byte, and so decoded is
	 * shorter than encoded_string. If encoded_string was
	 * iso-8859-1, then each 3-character %-escape will be
	 * converted into at most 2 bytes in UTF-8, and so it's still
	 * shorter.
	 */
	strcpy (encoded_string, decoded);
	g_free (decoded);
	return TRUE;
}
Пример #26
0
void openFile(const std::string& filename)
{
	std::ifstream file;
	file.open(filename.c_str(), std::ios::in | std::ios::binary);

	if (!file)
	{
		std::cerr << "Can't open file '" << filename << "'." << std::endl;
		return;
	}

	vmime::string data;
	char buffer[16384];

	do
	{
		file.read(buffer, sizeof(buffer));
		data += vmime::string(buffer, file.gcount());
	}
	while (file.gcount());

	std::shared_ptr<vmime::message> msg = std::make_shared<vmime::message>();
	msg->parse(data);

	currentMessage = msg;

	char* convData = g_convert_with_fallback(data.c_str(), data.length(),
		"UTF-8", "ISO-8859-1", "?", NULL, NULL, NULL);

	if (convData == NULL)
	{
		gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textArea)),
			"GLib UTF-8 conversion error.", -1);
	}
	else
	{
		gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textArea)),
			convData, strlen(convData));

		g_free(convData);
	}

	updateTreeView();
}
Пример #27
0
/* This will try to convert a string to utf-8,
 */
static id3_utf8_t * processID3FieldString (int is_id3v1, const id3_ucs4_t *ucs4, int type, enum id3_field_textencoding id3_encoding)
{
	id3_utf8_t *utf8, *utf8_stripped;
	id3_utf8_t *isostr;
	const char *encoding;

	if (type == TAG_ITEM_GENRE)
		ucs4 = id3_genre_name(ucs4);
	/* use encoding field here? */
	if ((encoding = config_get_string(CONF_ID3V1_ENCODING, NULL)) != NULL) {
		if(!is_id3v1 && (id3_encoding == ID3_FIELD_TEXTENCODING_UTF_16 || id3_encoding == ID3_FIELD_TEXTENCODING_UTF_8))
			isostr = id3_ucs4_utf8duplicate(ucs4);
		else
			isostr = (id3_utf8_t*)id3_ucs4_latin1duplicate(ucs4);
		if (G_UNLIKELY(!isostr)) {
			return NULL;
		}

                if(id3_encoding == 0xff || id3_encoding == ID3_FIELD_TEXTENCODING_ISO_8859_1) {
                utf8 = (id3_utf8_t *)
			g_convert_with_fallback((const char*)isostr, -1,
						"utf-8", encoding,
						NULL, NULL, NULL, NULL);
                } else
                        utf8 = (id3_utf8_t *) g_strdup((const char*)isostr);
		if (utf8 == NULL) {
			g_debug("Unable to convert %s string to UTF-8: '%s'",
				encoding, isostr);
			g_free(isostr);
			return NULL;
		}
		g_free(isostr);
	} else {
		utf8 = id3_ucs4_utf8duplicate(ucs4);
		if (G_UNLIKELY(!utf8)) {
			return NULL;
		}
	}

	utf8_stripped = (id3_utf8_t *)g_strdup(g_strstrip((gchar *)utf8));
	g_free(utf8);

	return utf8_stripped;
}
Пример #28
0
CLIP_DLLEXPORT char *
_clip_locale_from_utf8(char *text)
{
#ifdef OS_CYGWIN
	char *buf;
	char *locale_text;
	gsize br, bw;
	GError *ge;
	int len;

	if (!WinCharset)
	{
		WinCharset = malloc(10);
		snprintf(WinCharset,10,"cp%d",GetACP());
	}
	if (!ClipHostCharset)
		ClipHostCharset = _clip_host_charset();

	if (!WinCharset || !ClipHostCharset || !text)
		return text;

	locale_text = g_locale_from_utf8(text, strlen(text), &br, &bw, &ge);
	len = strlen(locale_text);
	
	buf = (char *) malloc(len+1); buf[len] = 0;
	_clip_translate_charset(WinCharset,ClipHostCharset,locale_text,buf,len);
	g_free(locale_text);
	return buf;
#else
	gsize br, bw;
	GError *ge;
	gchar *t_utf;

	t_utf = g_convert_with_fallback(text, 
									strlen(text), 
									_clip_host_charset(),
									"utf-8",
									"?",
									&br,
									&bw,
									&ge);
	return t_utf;
#endif
}
Пример #29
0
// The returned string should be freed when no longer needed.
gchar *convert_str_to_utf8(gchar *string, gchar *encoding_str)
{
#ifdef DETAIL
	g_debug("! Launch convert_str_to_utf8() with string = %s, encoding_str = %s", string, encoding_str);
#endif
	if (string==NULL) return NULL;

	gchar *local_string = NULL;
	if (encoding_str)
	{
		//g_debug("string = %s", string);
		//g_debug("encoding_str = %s", encoding_str);
		local_string = g_convert_with_fallback (string,
							-1,
							"UTF-8",
							encoding_str,
							"_",
							NULL,
							NULL,
							NULL);
		// g_debug("Get local_string = %s", local_string);

		if ((local_string == NULL ) || (local_string[0]=='\0'))
		{
			g_free(local_string);
			local_string = g_strdup(string);
			gint i = 0;
			while (local_string[i])
			{
				if (local_string[i] < 32 || local_string[i]>126)
					local_string[i] = '?';
				i++;
			}
		}

		// g_debug("FINAL: local_string = %s", local_string);
		return local_string;
	}
	else
		return g_strdup(string);
}
Пример #30
0
static gboolean
selection_set_text_plain (GtkSelectionData *selection_data,
			  const gchar      *str,
			  gint              len)
{
  const gchar *charset = NULL;
  gchar *result;
  GError *error = NULL;

  result = normalize_to_crlf (str, len);
  if (selection_data->target == text_plain_atom)
    charset = "ASCII";
  else if (selection_data->target == text_plain_locale_atom)
    g_get_charset (&charset);

  if (charset)
    {
      gchar *tmp = result;
      result = g_convert_with_fallback (tmp, -1, 
					charset, "UTF-8", 
					NULL, NULL, NULL, &error);
      g_free (tmp);
    }

  if (!result)
    {
      g_warning ("Error converting from %s to %s: %s",
		 "UTF-8", charset, error->message);
      g_error_free (error);
      
      return FALSE;
    }
  
  gtk_selection_data_set (selection_data,
			  selection_data->target, 
			  8, (guchar *) result, strlen (result));
  g_free (result);
  
  return TRUE;
}