Пример #1
0
gchar *mrim_package_read_LPSA(MrimPackage *pack) {
	gsize str_len = mrim_package_read_UL(pack);
	if (str_len) {
		gchar *str = g_new(gchar, str_len + 1);
		mrim_package_read_raw(pack, str, str_len);
		str[str_len] = '\0';
		gchar *string = g_convert(str, -1, "UTF8" , "CP1251", NULL, NULL, NULL);
		g_free(str);
		return string;
	} else {
		return NULL;
	}
}
Пример #2
0
gchar* TextCodecGtk::gconvert_till_error(const char* str,
                                         gssize length,
                                         const char* to_codeset,
                                         const char* from_codeset,
                                         gsize *bytes_read,
                                         gsize *bytes_written,
                                         GError **error)
{
    //first we try to convert the string 
    GError* internal_error = NULL;
    gchar* result = g_convert (str,
                               length,
                               to_codeset,
                               from_codeset,
                               bytes_read,
                               bytes_written,
                               &internal_error);

    //if there are unsupported characters
    //bytes_read would store the number of bytes that could be successfully converted
    //so we try to convert only that
    if(!result && (*bytes_read < length)  && internal_error )
    {
        result = g_convert ( str,
                             *bytes_read,
                             to_codeset,
                             from_codeset,
                             bytes_read,
                             bytes_written,
                             NULL);
    }

    if(error)
        *error = internal_error;
    else if(internal_error)
        g_error_free(internal_error);

    return result;
}
Пример #3
0
gboolean gnomegadu_protocol_send_message (gchar * uin_str, gchar *msg)
{
    gchar *msg_windows1250 = g_convert (msg, strlen (msg), "WINDOWS-1250", "UTF-8", NULL, NULL, NULL);

    if (!gnomegadu_protocol_check_connected())
	return FALSE;

    if (gg_send_message(gnomegadu_gadugadu_session, GG_CLASS_CHAT, g_ascii_strtod (uin_str, NULL), msg_windows1250) == -1)
	return FALSE;

    g_free(msg_windows1250);
    return TRUE;
}
Пример #4
0
static void
store_meta(gpointer key,
           gpointer value,
           gpointer user_data)
{
    GwyContainer *meta = (GwyContainer*)user_data;
    gchar *cval;

    if (!(cval = g_convert(value, strlen(value), "UTF-8", "ISO-8859-1",
                           NULL, NULL, NULL)))
        return;
    gwy_container_set_string_by_name(meta, key, cval);
}
Пример #5
0
static void utf32to8(char *t, char *s)
{
  gsize rn,wn=0;
  GError *err = NULL;
  char *utf8 = g_convert(s, 4, "UTF-8", "UTF-32", &rn, &wn, &err);

  if (utf8) {
    memcpy(t, utf8, wn);
    g_free(utf8);
  }

  t[wn]=0;
}
Пример #6
0
/* Length must be passed, as the string might be Unicode, in which case we can't
 * count zeroes (see strlen call below). */
gchar *
convert_string_1 (const gchar *string, gssize length, const gchar *from_codeset,
                         const gchar *to_codeset, const gboolean display_error)
{
    gchar *output;
    GError *error = NULL;
    gsize bytes_written;

    g_return_val_if_fail (string != NULL, NULL);

    output = g_convert(string, length, to_codeset, from_codeset, NULL, &bytes_written, &error);
    //output = g_convert_with_fallback(string, length, to_codeset, from_codeset, "?", NULL, &bytes_written, &error);

    if (output == NULL)
    {
        gchar *escaped_str = g_strescape(string, NULL);
        if (display_error)
        {
            Log_Print(LOG_ERROR,"convert_string(): Failed conversion from charset '%s' to '%s'. "
                      "String '%s'. Errcode %d (%s).",
                      from_codeset, to_codeset, escaped_str, error->code, error->message);
        }
        g_free(escaped_str);
        g_error_free(error);
        // Return the input string without converting it. If the string is
        // displayed in the UI, it must be in UTF-8!
        if ( (g_ascii_strcasecmp(to_codeset, "UTF-8"))
        ||   (g_utf8_validate(string, -1, NULL)) )
        {
            return g_strdup(string);
        }
    }else
    {
        // Patch from Alexey Illarionov:
        //    g_convert returns null-terminated string only with one \0 at the
        // end. It can cause some garbage at the end of a string for UTF-16.
        // The second \0 should be set manually.
        gchar *new_output;
        new_output = g_realloc (output, bytes_written + 2);
        if (new_output != NULL)
        {
            output = new_output;
            output[bytes_written] = output[bytes_written + 1] = 0;
        }
    }

    //g_print("from %s => len: %d, string: '%s'\n     (%x %x %x %x %x %x %x %x)\n",from_codeset,length,string,string[0],string[1],string[2],string[3],string[4],string[5],string[6],string[7]);
    //g_print("to   %s => len: %d, output: '%s'\n     (%x %x %x %x %x %x %x %x)\n\n",to_codeset,bytes_written+2,output,output[0],output[1],output[2],output[3],output[4],output[5],output[6],output[7]);

    return output;
}
Пример #7
0
void WAttGtk::activate_cmd_scrolled_ok(GtkWidget* w, gpointer data)
{
  WAttGtk* watt = (WAttGtk*)data;
  gchar *text, *textutf8;
  unsigned char* s;
  int sts;

  if (watt->input_open) {
    GtkTextIter start_iter, end_iter;
    gtk_text_buffer_get_start_iter(watt->cmd_scrolled_buffer, &start_iter);
    gtk_text_buffer_get_end_iter(watt->cmd_scrolled_buffer, &end_iter);

    textutf8 = gtk_text_buffer_get_text(
        watt->cmd_scrolled_buffer, &start_iter, &end_iter, FALSE);
    text = g_convert(textutf8, -1, "ISO8859-1", "UTF-8", NULL, NULL, NULL);
    g_free(textutf8);

    if (text) {
      // Replace ctrl characters with space
      for (s = (unsigned char*)text; *s; s++) {
        if (*s < ' ' && *s != 10 && *s != 13)
          *s = ' ';
      }

      sts = ((WAttNav*)watt->wattnav)
                ->set_attr_value(watt->input_node, watt->input_name, text);
    }
    g_object_set(watt->cmd_scrolledinput, "visible", FALSE, NULL);
    watt->set_prompt("");
    watt->input_open = 0;

    int w, h;
    gdk_drawable_get_size(watt->pane->window, &w, &h);
    gtk_paned_set_position(GTK_PANED(watt->pane), h - 50);

    ((WAttNav*)watt->wattnav)->redraw();
    ((WAttNav*)watt->wattnav)->set_inputfocus();

    if (text)
      g_free(text);
    else
      watt->message('E', "Input error, invalid character");

    if (watt->pending_close) {
      if (watt->close_cb)
        (watt->close_cb)(watt);
      else
        delete watt;
    }
  }
}
Пример #8
0
static TEspeakSuccess espeak_set_punctuation_list_from_utf8(const gchar * punct)
{
	TEspeakSuccess result = ERROR;
	wchar_t *wc_punct =
	    (wchar_t *) g_convert(punct, -1, "wchar_t", "utf-8", NULL, NULL,
				  NULL);
	if (wc_punct != NULL) {
		espeak_ERROR ret = espeak_SetPunctuationList(wc_punct);
		if (ret == EE_OK)
			result = OK;
		g_free(wc_punct);
	}
	return result;
}
Пример #9
0
void XttTblGtk::set_prompt(const char* prompt)
{
  if (streq(prompt, "")) {
    g_object_set(cmd_prompt, "visible", FALSE, NULL);
    g_object_set(msg_label, "visible", TRUE, NULL);
  } else {
    char* promptutf8
        = g_convert(prompt, -1, "UTF-8", "ISO8859-1", NULL, NULL, NULL);

    g_object_set(msg_label, "visible", FALSE, NULL);
    g_object_set(cmd_prompt, "visible", TRUE, "label", promptutf8, NULL);
    g_free(promptutf8);
  }
}
Пример #10
0
gchar *Lyric_Str2UTF8(const gchar * lpszStr)
{
	gchar * lpszUTF8 = aud_str_to_utf8(lpszStr);
	if (lpszUTF8)
	{
		if(g_strstr_len(lpszUTF8, strlen(lpszUTF8), "??????"))
		{
			g_free(lpszUTF8);
			return MyStr2UTF8(lpszStr);
		}
		return lpszUTF8;
	}
	return g_convert(lpszStr, strlen(lpszStr), "UTF-8", "GBK", NULL, NULL, NULL);
}
Пример #11
0
char *
utf8_to_fs_charset(const char *path_utf8)
{
	gchar *p;

	p = g_convert(path_utf8, -1,
		      fs_charset, "utf-8",
		      NULL, NULL, NULL);
	if (p == NULL)
		/* fall back to UTF-8 */
		p = g_strdup(path_utf8);

	return p;
}
Пример #12
0
char *recode_out(const SERVER_REC *server, const char *str, const char *target)
{
#ifdef HAVE_GLIB2
	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");

	if (server != NULL && server->tag != NULL && target != NULL) {
		char *tagtarget = g_strdup_printf("%s/%s", server->tag, target);
		to = iconfig_get_str("conversions", tagtarget, NULL);
		g_free(tagtarget);
	}
	if (to == NULL || *to == '\0')
		to = iconfig_get_str("conversions", target, NULL);
	if ((to == NULL || *to == '\0') && server != NULL)
		to = iconfig_get_str("conversions", server->tag, NULL);
	if (to == NULL || *to == '\0')
		/* 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;
#else
	return g_strdup(str);
#endif
}
Пример #13
0
gchar *slurpfile(const gchar *fname, GError **err)
{
  gchar *inbuf = NULL, *outbuf = NULL;
  gsize inlen, outlen;
  if (g_file_get_contents(fname, &inbuf, &inlen, err)) {
    if (g_ascii_strcasecmp(inputEncoding, "UTF-8") != 0) {
      outbuf = g_convert(inbuf, inlen, "UTF-8", inputEncoding, NULL, &outlen, err);
      g_free(inbuf);
    }
    else
      outbuf = inbuf;
  }
  return outbuf;
}
Пример #14
0
static char*
get_new_name(const char* name, const char* encoding)
{
    char* new_name = NULL;

    if (encoding == NULL)
	return NULL;

    if (g_utf8_validate(name, -1, NULL)) {
	char* unescaped = g_uri_unescape_string(name, NULL);
	if (g_utf8_validate(unescaped, -1, NULL)) {
	    // A filename from MacOSX is usually in NFD.
	    // So, if the filename is not in NFC, try to make it NFC.
	    char* normalized = g_utf8_normalize(unescaped, -1, G_NORMALIZE_NFC);
	    if (normalized != NULL) {
		// Som filenames are valid UTF-8 form,
		// but are misconverted with wrong encoding.
		// In that case, the names are illegible.
		// So, we try to reconvert it with the user selected encoding
		new_name = get_reconverted_name(normalized, encoding);
		if (new_name != NULL) {
		    g_free(normalized);
		} else {
		    new_name = normalized;
		}	
	    }
	    g_free(unescaped);
	} else {
	    new_name = g_convert(unescaped, -1, "UTF-8", encoding, NULL, NULL, NULL);
	    g_free(unescaped);
	}
    } else {
	new_name = g_convert(name, -1, "UTF-8", encoding, NULL, NULL, NULL);
    }

    return new_name;
}
Пример #15
0
/**
 * Converts the passed-in string from UTF-8 to ASCII for http transmisison.
 *
 * @param pczInString String to convert
 *
 * @return The converted string which MUST BE FREED BY THE CALLER.
 */
static gchar *
convertToASCII(const gchar *pczInString)
{
  // for UTF-8 to ASCII conversions
  setlocale(LC_CTYPE, "en_US");

  GError * pError = NULL;

  gsize szBytesRead = 0;
  gsize szBytesWritten = 0;

  gchar * pcConvertedString = g_convert(pczInString,
                                        strlen(pczInString),
                                        "ASCII//TRANSLIT",
                                        "UTF-8",
                                        &szBytesRead,
                                        &szBytesWritten,
                                        &pError);

  if (pError)
    {
      LXW_LOG(LXW_ERROR, "yahooutil::convertToASCII(%s): Error: %s", 
              pczInString, pError->message);

      g_error_free(pError);

      pcConvertedString = g_strndup(pczInString, strlen(pczInString));
    }

  // now escape space, if any
  xmlChar * pxEscapedString = xmlURIEscapeStr((const xmlChar *)pcConvertedString, NULL);

  if (pxEscapedString)
    {
      // release ConvertedString, reset it, then release EscapedString.
      // I know it's confusing, but keeps everything as a gchar and g_free
      g_free(pcConvertedString);

      pcConvertedString = g_strndup((const gchar *)pxEscapedString, 
                                    strlen((const gchar *)pxEscapedString));

      xmlFree(pxEscapedString);
    }

  // restore locale to default
  setlocale(LC_CTYPE, "");

  return pcConvertedString;
}
Пример #16
0
TCHAR *au_fs (const char *src) {
#ifdef NO_TRANSLATION
	if (src == NULL) return NULL;
	return strdup(src);
#else
    gsize read, written;
    gchar *result = g_convert(src, -1, "UTF-8",
            "ISO-8859-1", &read, &written, NULL);
    if (result == NULL) {
        write_log("WARNING: au_fs_copy failed to convert string %s", src);
        return strdup("");
    }
    return result;
#endif
}
Пример #17
0
static char *
hf_probe_volume_get_label (const struct volume_id *vid)
{
  char *label = NULL;

  if (vid && *vid->label)
    {
      if (g_utf8_validate(vid->label, -1, NULL))
	label = g_strdup(vid->label);
      else				/* assume ISO8859-1 */
	label = g_convert(vid->label, -1, "UTF-8", "ISO8859-1", NULL, NULL, NULL);
    }

  return label;
}
Пример #18
0
gchar *
encode_to_originally(const gchar *str)
{
    GError* error = NULL;
    if(!str)
            return NULL;
    gchar* pgb = g_convert(str, -1, "iso-8859-1", "utf-8", NULL, NULL, &error);
    if(error)
    {
        fprintf(stderr,"%s to originally %s\n",str,error->message);
        g_error_free(error);
        pgb = NULL;
    }
    return pgb;
}
Пример #19
0
gchar *
g_locale_from_utf8 (const gchar *utf8string,
		    gssize       len,            
		    gsize       *bytes_read,    
		    gsize       *bytes_written,
		    GError     **error)
{
  const gchar *charset;

  if (g_get_charset (&charset))
    return g_strdup (utf8string);
  else
    return g_convert (utf8string, len,
		      charset, "UTF-8", bytes_read, bytes_written, error);
}
static GValue *
str_to_gvalue (const char *str, gboolean try_convert)
{
	GValue *val;

	/* Empty */
	if (!str || strlen (str) < 1)
		return NULL;

	if (!g_utf8_validate (str, -1, NULL)) {
		if (try_convert && !(str = g_convert (str, -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL)))
			str = g_convert (str, -1, "C", "UTF-8", NULL, NULL, NULL);

		if (!str)
			/* Invalid */
			return NULL;
	}

	val = g_slice_new0 (GValue);
	g_value_init (val, G_TYPE_STRING);
	g_value_set_string (val, str);

	return val;
}
Пример #21
0
gchar *
convert_fallback(gchar const *text, gssize size, gchar const *fallback)
{
    gchar *res;
    gsize read, written;
    GString *str = g_string_new("");

    while ((res = g_convert(text, size, "UTF-8", "ASCII", &read, &written, NULL))
            == NULL) {
        res = g_convert(text, read, "UTF-8", "ASCII", NULL, NULL, NULL);
        str = g_string_append(str, res);

        str = g_string_append(str, fallback);
        text = text + read + 1;
        size = size - read;
    }

    str = g_string_append(str, res);
    g_free(res);

    res = str->str;
    g_string_free(str, FALSE);
    return res;
}
Пример #22
0
void packet_add_string(packet_constructor_t *pc, unsigned char *v)
{
	packet_add_field(pc);
	GError *error = NULL;
	size_t conv_len;
	char *conv = g_convert((char *) v, -1, "UTF16BE", "UTF8", NULL, &conv_len, &error);
	if (!conv)
		dief("g_convert UTF8->UTF16BE failed (error: %s, string: '%s')", error->message, (char *) v);
	unsigned char lenb[2];
	jshort_write(lenb, conv_len/2);
	g_byte_array_append(pc->data, lenb, 2);
	g_byte_array_append(pc->data, (unsigned char*)conv, conv_len);
	pc->offset += 2 + conv_len;
	g_free(conv);
}
Пример #23
0
void XttTblGtk::valchanged_cmd_input(GtkWidget* w, gpointer data)
{
  XttTbl* xtt = (XttTbl*)data;
  int sts;
  char *text, *textutf8;

  textutf8 = gtk_editable_get_chars(GTK_EDITABLE(w), 0, -1);
  text = g_convert(textutf8, -1, "ISO8859-1", "UTF-8", NULL, NULL, NULL);
  g_free(textutf8);

  sts = xtt->command(text);
  g_object_set(w, "visible", FALSE, NULL);
  xtt->set_prompt("");
  xtt->command_open = 0;
  xtt->tblnav->set_inputfocus();
}
Пример #24
0
gchar*
gsl_convert_to_utf8 (const gchar *codeset,
		     const gchar *string)
{
  gchar *result;

  g_return_val_if_fail (codeset != NULL, NULL);
  if (!string)
    return NULL;

  result = g_convert (string, strlen (string), GSL_CODESET_UTF8, codeset, NULL, NULL, NULL);
  if (!result)
    result = g_strconcat ("??unknown-codeset:", codeset, "??", NULL);

  return result;
}
Пример #25
0
static void
store_meta(gpointer key,
           gpointer value,
           gpointer user_data)
{
    GwyContainer *meta = (GwyContainer*)user_data;

    if (g_utf8_validate(value, -1, NULL)) {
        gwy_container_set_string_by_name(meta, key, g_strdup(value));
    }
    else {
        gchar *s = g_convert(value, -1, "UTF-8", "CP1252", NULL, NULL, NULL);
        if (s)
            gwy_container_set_string_by_name(meta, key, s);
    }
}
Пример #26
0
const gchar *
cut_take_convert_helper (const gchar *string,
                         const gchar *to_code_set,
                         const gchar *from_code_set,
                         CutCallbackFunction callback)
{
    gchar *converted;
    GError *error = NULL;

    converted = g_convert(string, -1, to_code_set, from_code_set,
                          NULL, NULL, &error);
    gcut_assert_error_helper(error, "error");
    if (callback)
        callback();
    return cut_take_string(converted);
}
Пример #27
0
static void
store_meta(gpointer key,
           gpointer value,
           gpointer user_data)
{
    GwyContainer *meta = (GwyContainer*)user_data;

    if (g_utf8_validate(value, -1, NULL)) {
        gwy_container_set_string_by_name(meta, key, g_strdup(value));
    }
    else {
        // FIXME: Is this Windows-locale dependent?
        gchar *s = g_convert(value, -1, "UTF-8", "GB2312", NULL, NULL, NULL);
        if (s)
            gwy_container_set_string_by_name(meta, key, s);
    }
}
Пример #28
0
gboolean mape_edit_view_save(MapeEditView* view,
                             const gchar* filename,
                             GError** error)
{
	GtkTextBuffer* buffer;
	GtkTextIter begin;
	GtkTextIter end;
	gchar* source;
	gchar* conv;
	gboolean result;

	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view) );
	gtk_text_buffer_get_start_iter(buffer, &begin);
	gtk_text_buffer_get_end_iter(buffer, &end);
	source = gtk_text_buffer_get_text(buffer, &begin, &end, TRUE);

	conv = g_convert(
		source,
		-1,
		view->encoding,
		"UTF-8",
		NULL,
		NULL,
		error
	);

	g_free(source);
	if(conv == NULL) return FALSE;

	result = g_file_set_contents(filename, conv, -1, error);
	g_free(conv);
	if(result == FALSE) return FALSE;
	
	gtk_text_buffer_set_modified(
		gtk_text_view_get_buffer(GTK_TEXT_VIEW(view->view)),
		FALSE
	);

	mape_edit_view_set_filename(view, filename);

	/* Rerender with new file path --
	 * different Script.c lookup for algo=script overlays */
	mape_edit_view_reload(view);

	return TRUE;
}
Пример #29
0
static void
chmfile_file_info(ChmFile *chmfile)
{
  struct chmFile *cfd;

  cfd = chm_open(chmfile->filename);

  if (cfd == NULL) {
    g_error(_("Can not open chm file %s."), chmfile->filename);
    return;
  }

  chmfile_system_info(cfd, chmfile);
  chmfile_windows_info(cfd, chmfile);

  /* Convert book title to UTF-8 */
  if (chmfile->title != NULL && chmfile->encoding != NULL) {
    gchar *title_utf8;

    title_utf8 = g_convert(chmfile->title, -1, "UTF-8",
                           chmfile->encoding,
                           NULL, NULL, NULL);
    g_free(chmfile->title);
    chmfile->title = title_utf8;
  }

  /* Convert filename to UTF-8 */
  if (chmfile->hhc != NULL && chmfile->encoding != NULL) {
    gchar *filename_utf8;

    filename_utf8 = convert_filename_to_utf8(chmfile->hhc, chmfile->encoding);
    g_free(chmfile->hhc);
    chmfile->hhc = filename_utf8;
  }

  if (chmfile->hhk != NULL && chmfile->encoding != NULL) {
    gchar *filename_utf8;

    filename_utf8 = convert_filename_to_utf8(chmfile->hhk, chmfile->encoding);
    g_free(chmfile->hhk);
    chmfile->hhk = filename_utf8;
  }

  chm_close(cfd);
}
Пример #30
0
/**
 * mono_utf8_from_external:
 * @in: pointer to the string buffer.
 *
 * Tries to turn a NULL-terminated string into UTF8.
 *
 * First, see if it's valid UTF8, in which case there's nothing more
 * to be done.  Next, run through the colon-separated encodings in
 * MONO_EXTERNAL_ENCODINGS and do an iconv conversion on each,
 * returning the first successful conversion to utf8.  If no
 * conversion succeeds, return NULL.
 *
 * Callers must free the returned string if not NULL.
 *
 * This function is identical to mono_unicode_from_external, apart
 * from returning utf8 not utf16; it's handy in a few places to work
 * in utf8.
 */
gchar *mono_utf8_from_external (const gchar *in)
{
	gchar *res=NULL;
	gchar **encodings;
	const gchar *encoding_list;
	int i;
	
	if(in==NULL) {
		return(NULL);
	}
	
	encoding_list=g_getenv ("MONO_EXTERNAL_ENCODINGS");
	if(encoding_list==NULL) {
		encoding_list = "";
	}
	
	encodings=g_strsplit (encoding_list, ":", 0);
	for(i=0;encodings[i]!=NULL; i++) {
		
		/* "default_locale" is a special case encoding */
		if(!strcmp (encodings[i], "default_locale")) {
			res=g_locale_to_utf8 (in, -1, NULL, NULL, NULL);
			if(res!=NULL && !g_utf8_validate (res, -1, NULL)) {
				g_free (res);
				res=NULL;
			}
		} else {
			res=g_convert (in, -1, "UTF8", encodings[i], NULL,
				       NULL, NULL);
		}

		if(res!=NULL) {
			g_strfreev (encodings);
			return(res);
		}
	}
	
	g_strfreev (encodings);
	
	if(g_utf8_validate (in, -1, NULL)) {
		return(g_strdup (in));
	}

	return(NULL);
}