Ejemplo n.º 1
0
static gint cgraphics_textbox_keypress_handler( GtkWidget *widget, GdkEventKey *e, widget_t *w )
{
	int ucs4 = gdk_keyval_to_unicode(e->keyval);
	gchar * utf8 = g_ucs4_to_utf8(&ucs4, 1, NULL, NULL, NULL);
	int r = event_send( w, "key_down", "i", "key", (int)utf8[0] );
	g_free(utf8);
	return r;
}
Ejemplo n.º 2
0
static void
gimp_number_pair_entry_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  GimpNumberPairEntry        *entry = GIMP_NUMBER_PAIR_ENTRY (object);
  GimpNumberPairEntryPrivate *priv;

  priv = GIMP_NUMBER_PAIR_ENTRY_GET_PRIVATE (entry);

  switch (property_id)
    {
    case PROP_LEFT_NUMBER:
      g_value_set_double (value, priv->left_number);
      break;
    case PROP_RIGHT_NUMBER:
      g_value_set_double (value, priv->right_number);
      break;
    case PROP_DEFAULT_LEFT_NUMBER:
      g_value_set_double (value, priv->default_left_number);
      break;
    case PROP_DEFAULT_RIGHT_NUMBER:
      g_value_set_double (value, priv->default_right_number);
      break;
    case PROP_USER_OVERRIDE:
      g_value_set_boolean (value, priv->user_override);
      break;
    case PROP_SEPARATORS:
      g_value_take_string (value,
                           g_ucs4_to_utf8 (priv->separators,
                                           priv->num_separators,
                                           NULL, NULL, NULL));
      break;
    case PROP_ALLOW_SIMPLIFICATION:
      g_value_set_boolean (value, priv->allow_simplification);
      break;
    case PROP_DEFAULT_TEXT:
      g_value_set_string (value, priv->default_text);
      break;
    case PROP_MIN_VALID_VALUE:
      g_value_set_double (value, priv->min_valid_value);
      break;
    case PROP_MAX_VALID_VALUE:
      g_value_set_double (value, priv->max_valid_value);
      break;
    case PROP_RATIO:
      g_value_set_double (value, gimp_number_pair_entry_get_ratio (entry));
      break;
    case PROP_ASPECT:
      g_value_set_enum (value, gimp_number_pair_entry_get_aspect (entry));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}
Ejemplo n.º 3
0
bool convert_to_utf8(FacadePhraseIndex * phrase_index,
                     MatchResults match_results,
                     /* in */ const char * delimiter,
                     /* in */ bool show_tokens,
                     /* out */ char * & result_string){
    //init variables
    if ( NULL == delimiter )
        delimiter = "";
    result_string = NULL;

    PhraseItem item;

    for ( size_t i = 0; i < match_results->len; ++i ){
        phrase_token_t token = g_array_index
            (match_results, phrase_token_t, i);
        if ( null_token == token )
            continue;

        phrase_index->get_phrase_item(token, item);
        ucs4_t buffer[MAX_PHRASE_LENGTH];
        item.get_phrase_string(buffer);

        guint8 length = item.get_phrase_length();
        gchar * phrase = NULL;
        char * tmp = NULL;

        if (show_tokens) {
            tmp = g_ucs4_to_utf8(buffer, length, NULL, NULL, NULL);
            phrase = g_strdup_printf("%d %s", token, tmp);
            g_free(tmp);
        } else {
            phrase = g_ucs4_to_utf8(buffer, length, NULL, NULL, NULL);
        }

        tmp = result_string;
        if ( NULL == result_string )
            result_string = g_strdup(phrase);
        else
            result_string = g_strconcat(result_string, delimiter, phrase, NULL);
        g_free(phrase);
        g_free(tmp);
    }
    return true;
}
Ejemplo n.º 4
0
/*
 * Callback for search_char
 */
gboolean find_tag_callback(gunichar ch, gpointer data)
{
	gchar *gch;
	gch = g_ucs4_to_utf8(&ch, 1, NULL, NULL, NULL);

	if (gch && *gch == *((gchar *)data)) {
		return TRUE;
	} else
		return FALSE;
}
Ejemplo n.º 5
0
/* Internal function: Convert a Unicode buffer to a null-terminated UTF-8 
string. The returned string must be freed afterwards. Returns NULL on error. */
gchar *
convert_ucs4_to_utf8(const gunichar *buf, const glong len)
{
	GError *error = NULL;
	gchar *retval = g_ucs4_to_utf8(buf, len, NULL, NULL, &error);
		
	if(retval == NULL)
		WARNING_S("Error during unicode->utf8 conversion", error->message);
		
	return retval;
}
Ejemplo n.º 6
0
Archivo: nfkc.c Proyecto: AlekSi/Jabbin
/**
 * g_utf8_normalize:
 * @str: a UTF-8 encoded string.
 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
 * @mode: the type of normalization to perform.
 *
 * Converts a string into canonical form, standardizing
 * such issues as whether a character with an accent
 * is represented as a base character and combining
 * accent or as a single precomposed character. You
 * should generally call g_utf8_normalize() before
 * comparing two Unicode strings.
 *
 * The normalization mode %G_NORMALIZE_DEFAULT only
 * standardizes differences that do not affect the
 * text content, such as the above-mentioned accent
 * representation. %G_NORMALIZE_ALL also standardizes
 * the "compatibility" characters in Unicode, such
 * as SUPERSCRIPT THREE to the standard forms
 * (in this case DIGIT THREE). Formatting information
 * may be lost but for most text operations such
 * characters should be considered the same.
 * For example, g_utf8_collate() normalizes
 * with %G_NORMALIZE_ALL as its first step.
 *
 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
 * but returned a result with composed forms rather
 * than a maximally decomposed form. This is often
 * useful if you intend to convert the string to
 * a legacy encoding or pass it to a system with
 * less capable Unicode handling.
 *
 * Return value: a newly allocated string, that is the
 *   normalized form of @str.
 **/
static gchar *
g_utf8_normalize (const gchar * str, gssize len, GNormalizeMode mode)
{
  gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
  gchar *result;

  result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
  g_free (result_wc);

  return result;
}
Ejemplo n.º 7
0
/* Set length to a positive number to enable line input; set to 0 to disable */
void gglk_text_line_input_set(GglkText *tb, int length,
			      const gunichar *text)
{
    GtkTextIter b, e;
    gchar *line_utf8;

    if(tb->line_maxlen != 0) {
	gglk_text_line_input_end(tb);

	/* Clear out old text there */
	gtk_text_buffer_get_iter_at_mark(tb->buffer, &b, tb->startedit);
	gtk_text_buffer_get_iter_at_mark(tb->buffer, &e, tb->endedit);
	gtk_text_buffer_delete(tb->buffer, &b, &e);

	gtk_text_buffer_get_iter_at_mark(tb->buffer, &tb->iter, tb->startedit);
    }
    
    tb->line_maxlen = length;
    if(length == 0) {
	gglk_text_line_input_end(tb);
	return;
    }

    /* Add new text */
    gtk_text_buffer_get_end_iter(tb->buffer, &b);
    gtk_text_buffer_move_mark(tb->buffer, tb->startedit, &b);
    line_utf8 = g_ucs4_to_utf8(text, -1, NULL, NULL, NULL);
    gtk_text_buffer_insert(tb->buffer, &b, line_utf8, -1);
    g_free(line_utf8);

    /* Extra character at end to maintain editability */
    gtk_text_buffer_get_end_iter(tb->buffer, &e);
    gtk_text_buffer_insert(tb->buffer, &e, " ", -1);

    /* Make editable */
    gtk_text_buffer_get_iter_at_mark(tb->buffer, &b, tb->startedit);
    gtk_text_buffer_apply_tag_by_name(tb->buffer, "Input", &b, &e);
    gtk_text_buffer_apply_tag_by_name(tb->buffer, "editable", &b, &e);

    /* Set end mark */
    gtk_text_iter_backward_char(&e);
    gtk_text_buffer_move_mark(tb->buffer, tb->endedit, &e);

    gtk_text_buffer_place_cursor(tb->buffer, &e);
    gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(tb), tb->endedit, 0,FALSE,0,0);

    /* Add signals to handle user interaction */
    tb->cursor_handler = g_signal_connect(
	tb->buffer, "mark-set",
	GTK_SIGNAL_FUNC(gglk_text_cursor_event), tb);
    tb->update_handler = g_signal_connect(
	tb->buffer, "end-user-action",
	GTK_SIGNAL_FUNC(gglk_text_update_editable_region), tb);
}
Ejemplo n.º 8
0
static void gglk_textbuffer_put_buffer(GtkWidget *widget,
				       const glui32 *buf, int len)
{
    GglkTextBuffer *tb = GGLK_TEXTBUFFER(widget);
    char *utfstr;
    utfstr = g_ucs4_to_utf8(buf, len, NULL, NULL, NULL);
    gtk_text_buffer_insert_with_tags(tb->base.buffer, &tb->base.iter,
				     utfstr, -1,
				     tb->base.style_tag,
				     NULL);
    g_free(utfstr);
}
Ejemplo n.º 9
0
void
AimWinHandler::commit(const TWCHAR* wstr)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  if (wstr)
  {
    gchar *text = g_ucs4_to_utf8 (wstr, -1, NULL, NULL, NULL);
    aim_engine_emit_commit (m_engine, m_target, text);
    g_free (text);
  }
}
Ejemplo n.º 10
0
static int entry_get(LuaState *L)
{
    Entry *e  = ms_lua_checkclass(L, CLASS, 1);
    if (e->bufused == 0) {
        lua_pushstring(L, "");
        return 1;
    } else {
        char *str = g_ucs4_to_utf8(e->buffer, e->bufused, NULL, NULL, NULL);
        lua_pushstring(L, str);
        g_free(str);
        return 1;
    }
}
Ejemplo n.º 11
0
Archivo: gutf8.c Proyecto: Joe-xXx/mono
static gchar *
utf8_case_conv (const gchar *str, gssize len, gboolean upper)
{
	gunichar *ustr;
	glong i, ulen;
	gchar *utf8;
	
	ustr = g_utf8_to_ucs4_fast (str, (glong) len, &ulen);
	for (i = 0; i < ulen; i++)
		ustr[i] = upper ? g_unichar_toupper (ustr[i]) : g_unichar_tolower (ustr[i]);
	utf8 = g_ucs4_to_utf8 (ustr, ulen, NULL, NULL, NULL);
	g_free (ustr);
	
	return utf8;
}
Ejemplo n.º 12
0
/*
 * html_print_encoded:
 *
 * @output: the stream
 * @str: the string
 *
 * print the string to output encoded all special chars
 *
 */
static void
html_print_encoded (GsfOutput *output, char const *str)
{
    gunichar c;
    gchar *encoded;

	if (str == NULL)
		return;
	for (; *str != '\0' ; str = g_utf8_next_char (str)) {
		switch (*str) {
            case '"':
                gsf_output_puts (output, "\"\"");
                break;
            case '<':
				gsf_output_puts (output, "&lt;");
				break;
			case '>':
				gsf_output_puts (output, "&gt;");
				break;
			case '&':
				gsf_output_puts (output, "&amp;");
				break;
			case '\n':
				gsf_output_puts (output, "<br />\n");
				break;
			case '\r':
				gsf_output_puts (output, "<br />\r");
				if( *(str+1) == '\n' ) {
					gsf_output_puts (output, "\n");
					str++;
				}
				break;
			default:
                c = g_utf8_get_char (str);
                if (((c >= 0x20) && (c < 0x80)) || (c == '\n') || (c == '\r') || (c == '\t')) {
                    gsf_output_write (output, 1, (guint8 *)str);
                } else {
                    c = g_utf8_get_char (str);
                    encoded = g_ucs4_to_utf8(&c, 1, NULL, NULL, NULL);
                    gsf_output_puts (output, encoded);
                    g_free(encoded);
                }
                break;
		}
	}
}
Ejemplo n.º 13
0
gchar *
ucs2_to_utf8(const gunichar2 *ucs2, int length) {
	const gunichar2	*ptr;
	const gunichar2	*end;
	gunichar	*dest;
	gunichar	*uni;
	gchar		*utf8;

	/* Count length */
	if (length == -1) {
		ptr = ucs2;
		length = 0;
		while (*ptr != 0) {
			ptr++;
			length++;
		}
	}


	uni = GdipAlloc((length + 1) * sizeof(gunichar));
	if (uni == NULL) {
		return NULL;
	}

	dest = uni;
	ptr = ucs2;
	end = ptr + length;
	while (ptr != end) {
		if (*ptr < 0xd800 || *ptr >= 0xe000) {
			*dest = *ptr;
			dest++;
		}
		ptr++;
	}
	*dest = 0;
	dest++;
	
	utf8 = (gchar *) g_ucs4_to_utf8 ((const gunichar *)uni, -1, NULL, NULL, NULL);

	GdipFree(uni);

	return utf8;
}
Ejemplo n.º 14
0
std::string toUtf8(const std::wstring &str)
{
    long readed, writed;
    wchar_t *errMsg = NULL;
    
    char *res = g_ucs4_to_utf8(str.c_str(), str.length(), &readed,
            &writed, &errMsg);
    if (! res) {
        if (errMsg)
            throw Exception(errMsg);
        else
            throw Exception(L"Error converting text to UTF-8");
    }

    std::string s(res);
    free(res);

    return s;
}
Ejemplo n.º 15
0
void gen_phrase_file(const char * outfilename, int phrase_index){
    FILE * outfile = fopen(outfilename, "w");
    if (NULL == outfile ) {
        fprintf(stderr, "Can't write file %s.\n", outfilename);
        exit(ENOENT);
    }
    phrase_token_t token = 1;
    char pinyin_buffer[4096];
    //phrase length
    for ( size_t i = 1; i < MAX_PHRASE_LENGTH + 1; ++i){
	GArray * item_array = g_item_array[i];
	//item array
	for( size_t m = 0; m < item_array->len; ++m){
	    item* oneitem = & g_array_index(item_array, item, m);
	    phrase_item * phrase = oneitem->phrase;
	    GArray * pinyin_and_freqs = oneitem->pinyin_and_freq_array;
	    const char * phrase_buffer = g_ucs4_to_utf8(phrase->uniphrase,
						 phrase->length, 
						 NULL, NULL, NULL);
	    //each pinyin
	    for( size_t n = 0 ; n < pinyin_and_freqs->len; ++n){
		pinyin_and_freq_item * pinyin_and_freq = &g_array_index(pinyin_and_freqs, pinyin_and_freq_item, n);
		GArray * pinyin = pinyin_and_freq->pinyin;
		PinyinKey * key = &g_array_index(pinyin, PinyinKey, 0);
		strcpy(pinyin_buffer,key->get_key_string());
		for (size_t k = 1; k < pinyin->len; ++k){
		    strcat(pinyin_buffer, "'");
		    PinyinKey * key = &g_array_index(pinyin, PinyinKey, k);
		    strcat(pinyin_buffer, key->get_key_string ());
		}
		guint32 freq = pinyin_and_freq -> freq;
		if ( freq < 3 ) 
		    freq = 3;
		fprintf( outfile, "%s\t%s\t%d\t%d\n", 
			 pinyin_buffer, phrase_buffer, 
			 PHRASE_INDEX_MAKE_TOKEN(phrase_index, token),
			 freq);
	    }
	    token++;
	}
    }
    fclose(outfile);
}
Ejemplo n.º 16
0
char *gui_entry_get_text(GUI_ENTRY_REC *entry)
{
	char *buf;
        int i;

	g_return_val_if_fail(entry != NULL, NULL);

	if (entry->utf8)
		buf = g_ucs4_to_utf8(entry->text, -1, NULL, NULL, NULL);
	else {
		buf = g_malloc(entry->text_len*6 + 1);
		if (term_type == TERM_TYPE_BIG5)
			unichars_to_big5(entry->text, buf);
		else
			for (i = 0; i <= entry->text_len; i++)
				buf[i] = entry->text[i];
	}
	return buf;
}
Ejemplo n.º 17
0
bool deal_with_segmentable(FacadePhraseTable3 * phrase_table,
                           FacadePhraseIndex * phrase_index,
                           GArray * current_ucs4,
                           FILE * output){

    /* do segment stuff. */
    GArray * strings = g_array_new(TRUE, TRUE, sizeof(SegmentStep));
    segment(phrase_table, phrase_index, current_ucs4, strings);

    /* print out the split phrase. */
    for ( glong i = 0; i < strings->len; ++i ) {
        SegmentStep * step = &g_array_index(strings, SegmentStep, i);
        char * string = g_ucs4_to_utf8( step->m_phrase, step->m_phrase_len, NULL, NULL, NULL);
        fprintf(output, "%d %s\n", step->m_handle, string);
        g_free(string);
    }

    g_array_free(strings, TRUE);
    return true;
}
Ejemplo n.º 18
0
IBusText *
ibus_text_new_from_ucs4 (const gunichar *str)
{
    g_assert (str);

    IBusText *text;
    gchar *buf;

    buf = g_ucs4_to_utf8 (str, -1, NULL, NULL, NULL);

    if (buf == NULL) {
        return NULL;
    }

    text= g_object_new (IBUS_TYPE_TEXT, NULL);

    text->is_static = FALSE;
    text->text = buf;

    return text;
}
Ejemplo n.º 19
0
static VALUE
rg_s_to_utf8(G_GNUC_UNUSED VALUE self, VALUE rb_ucs4)
{
    VALUE result;
    gunichar *ucs4;
    gchar *utf8;
    glong len, items_written;
    GError *error = NULL;

    ucs4 = (gunichar *)StringValuePtr(rb_ucs4);
    len = RSTRING_LEN(rb_ucs4) / sizeof(*ucs4);

    utf8 = g_ucs4_to_utf8(ucs4, len, NULL, &items_written, &error);

    if (error)
        RAISE_GERROR(error);

    result = CSTR2RVAL_LEN(utf8, items_written);
    g_free(utf8);
    return result;
}
Ejemplo n.º 20
0
void
AimWinHandler::updatePreedit(const IPreeditString* ppd)
{
  g_debug (G_STRLOC ": %s", G_STRFUNC);

  AimSunpinyin *pinyin;

  if (ppd)
  {
    if (G_UNLIKELY (ppd->size() >= 1019))
    {
      pinyin = AIM_SUNPINYIN (m_engine);
      pinyin->view->updateWindows(pinyin->view->clearIC());
    }

    const TWCHAR *wstr = ppd->string();
    /* aim_sunpinyin_update_preedit takes text */
    aim_sunpinyin_update_preedit (m_engine, m_target,
                                  g_ucs4_to_utf8 (wstr, -1, NULL, NULL, NULL),
                                  ppd->caret());
  }
}
Ejemplo n.º 21
0
char *gui_entry_get_text_and_pos(GUI_ENTRY_REC *entry, int *pos)
{
	char *buf;
        int i;

	g_return_val_if_fail(entry != NULL, NULL);

	if (entry->utf8) {
		buf = g_ucs4_to_utf8(entry->text, -1, NULL, NULL, NULL);
		*pos = g_utf8_offset_to_pointer(buf, entry->pos) - buf;
	} else {
		buf = g_malloc(entry->text_len*6 + 1);
		if(term_type==TERM_TYPE_BIG5)
			unichars_to_big5_with_pos(entry->text, entry->pos, buf, pos);
		else
		{
			for (i = 0; i <= entry->text_len; i++)
				buf[i] = entry->text[i];
			*pos = entry->pos;
		}
	}
	return buf;
}
Ejemplo n.º 22
0
bool deal_with_segmentable(PhraseLookup * phrase_lookup,
                           GArray * current_ucs4){
    char * result_string = NULL;
    MatchResults results = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
    phrase_lookup->get_best_match(current_ucs4->len,
                                  (ucs4_t *) current_ucs4->data, results);

    phrase_lookup->convert_to_utf8(results, "\n", result_string);

    if (result_string) {
        printf("%s\n", result_string);
    } else {
        char * tmp_string = g_ucs4_to_utf8
            ( (ucs4_t *) current_ucs4->data, current_ucs4->len,
              NULL, NULL, NULL);
        fprintf(stderr, "Un-segmentable sentence encountered:%s\n",
                tmp_string);
        g_array_free(results, TRUE);
        return false;
    }
    g_array_free(results, TRUE);
    g_free(result_string);
    return true;
}
Ejemplo n.º 23
0
/**
  Sprawdzenie słowa, na którym aktualnie znajduje się kursor. Ewentualne dodanie do słownika.
  @param[in] item element menu.
  @param[in] data wskaźnik na wartość.
  */
static void WhatCheck (GtkMenuItem *item, gpointer data) {
  GtkWidget *dialog;
  GtkTextIter start, end;
  char *word;
  gunichar *wword;

  //load_dictionary_from_menu(&dict);
  
  // Znajdujemy pozycję kursora
  gtk_text_buffer_get_iter_at_mark(editor_buf, &start,
                                   gtk_text_buffer_get_insert(editor_buf));

  // Jeśli nie wewnątrz słowa, kończymy
  if (!gtk_text_iter_inside_word(&start)) {
    dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR,
                                    GTK_BUTTONS_OK,
                                    "Kursor musi być w środku słowa");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    return;
  }

  // Znajdujemy początek i koniec słowa, a potem samo słowo 
  end = start;
  gtk_text_iter_backward_word_start(&start);
  gtk_text_iter_forward_word_end(&end); 
  word = gtk_text_iter_get_text(&start, &end);

  // Zamieniamy na wide char (no prawie)
  wword = g_utf8_to_ucs4_fast(word, -1, NULL);
  
  if (!make_lowercase(wword)) {
    dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
                                    "Podane słowo nie jest słowem słownikowym.");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
  }
  else {
    // Sprawdzamy
    if (dictionary_find(dict, (wchar_t *)wword)) {
      dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
                                      "Wszystko w porządku,\nśpij spokojnie");
      gtk_dialog_run(GTK_DIALOG(dialog));
      gtk_widget_destroy(dialog);
    }
    else {
      // Czas korekty
      GtkWidget *vbox, *label, *combo;
      struct word_list hints;
      int i;
      wchar_t **words;

      dictionary_hints(dict, (wchar_t *)wword, &hints);
      words = (wchar_t **) word_list_get(&hints);
      dialog = gtk_dialog_new_with_buttons("Korekta", NULL, 0, 
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           GTK_STOCK_ADD,
                                           GTK_RESPONSE_APPLY,
                                           GTK_STOCK_CANCEL,
                                           GTK_RESPONSE_REJECT,
                                           NULL);
      // W treści dialogu dwa elementy
      vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
      // Tekst
      label = gtk_label_new("Słowo nie znajduje się w słowniku. Wybierz \njedną z propozycji lub dodaj słowa do słownika.");
      gtk_widget_show(label);
      gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 1);

      // Spuszczane menu
      combo = gtk_combo_box_text_new();
      for (i = 0; i < word_list_size(&hints); i++) {
        // Combo box lubi mieć Gtk
        char *uword = g_ucs4_to_utf8((gunichar *)words[i], -1, NULL, NULL, NULL);

        // Dodajemy kolejny element
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), uword);
        g_free(uword);
      }
      gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
      gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 1);
      gtk_widget_show(combo);

      gint click = gtk_dialog_run(GTK_DIALOG(dialog));

      if (click == GTK_RESPONSE_ACCEPT) {
        char *korekta =
          gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(combo));

        // Usuwamy stare
        gtk_text_buffer_delete(editor_buf, &start, &end);
        // Wstawiamy nowe
        gtk_text_buffer_insert(editor_buf, &start, korekta, -1);
        g_free(korekta);
      }
      // Proponujemy dodanie słowa do słownika
      else if (click == GTK_RESPONSE_APPLY)
        dictionary_insert(dict, wword);
      gtk_widget_destroy(dialog);
    }
  }
  g_free(word);
  g_free(wword);
}
Ejemplo n.º 24
0
/**
 * gsdl_tokenizer_next:
 * @self: A valid %GSDLTokenizer.
 * @result: (out callee-allocates): A %GSDLToken to initialize and fill in.
 * @err: (out) (allow-none): Location to store any error, may be %NULL.
 *
 * Fetches the next token from the input. Depending on the source of input, may set an error in one
 * of the %GSDL_SYNTAX_ERROR, %G_IO_CHANNEL_ERROR, or %G_CONVERT_ERROR domains.
 *
 * Returns: Whether a token could be successfully read.
 */
bool gsdl_tokenizer_next(GSDLTokenizer *self, GSDLToken **result, GError **err) {
	gunichar c, nc;
	int line;
	int col;

	retry:
	line = self->line;
	col = self->col;
	if (!_read(self, &c, err)) return false;

	if (G_UNLIKELY(c == EOF)) {
		*result = _maketoken(T_EOF, line, col);
		return true;
	} else if (c == '\r') {
		if (_peek(self, &c, err) && c == '\n') _consume(self);

		*result = _maketoken('\n', line, col);
		FAIL_IF_ERR();

		return true;
	} else if ((c == '/' && _peek(self, &nc, err) && nc == '/') || (c == '-' && _peek(self, &nc, err) && nc == '-') || c == '#') {
		if (c != '#') _consume(self);
		while (_peek(self, &c, err) && !(c == '\n' || c == EOF)) _consume(self);

		goto retry;
	} else if (c == '/' && _peek(self, &nc, err) && nc == '*') {
		while (_read(self, &c, err)) {
			if (c == EOF) {
				_set_error(err,
					self,
					GSDL_SYNTAX_ERROR_UNEXPECTED_CHAR,
					"Unterminated comment"
				);

				return false;
			} else if (c == '*' && _peek(self, &c, err) && c == '/') {
				_consume(self);
				break;
			}
		}

		goto retry;
	} else if (c < 256 && strchr("-+:;./{}=\n", (char) c)) {
		*result = _maketoken(c, line, col);
		return true;
	} else if (c < 256 && isdigit((char) c)) {
		*result = _maketoken(T_NUMBER, line, col);
		return _tokenize_number(self, *result, c, err);
	} else if (g_unichar_isalpha(c) || g_unichar_type(c) == G_UNICODE_CONNECT_PUNCTUATION || g_unichar_type(c) == G_UNICODE_CURRENCY_SYMBOL) {
		*result = _maketoken(T_IDENTIFIER, line, col);
		return _tokenize_identifier(self, *result, c, err);
	} else if (c == '[') {
		*result = _maketoken(T_BINARY, line, col);
		if (!_tokenize_binary(self, *result, err)) return false;

		REQUIRE(_read(self, &c, err));
		if (c == ']') {
			return true;
		} else {
			_set_error(err,
				self,
				GSDL_SYNTAX_ERROR_MISSING_DELIMITER,
				"Missing ']'"
			);
			return false;
		}
	} else if (c == '"') {
		*result = _maketoken(T_STRING, line, col);
		if (!_tokenize_string(self, *result, err)) return false;

		REQUIRE(_read(self, &c, err));
		if (c == '"') {
			return true;
		} else {
			_set_error(err,
				self,
				GSDL_SYNTAX_ERROR_MISSING_DELIMITER,
				"Missing '\"'"
			);
			return false;
		}
	} else if (c == '`') {
		*result = _maketoken(T_STRING, line, col);
		if (!_tokenize_backquote_string(self, *result, err)) return false;

		REQUIRE(_read(self, &c, err));
		if (c == '`') {
			return true;
		} else {
			_set_error(err,
				self,
				GSDL_SYNTAX_ERROR_MISSING_DELIMITER,
				"Missing '`'"
			);
			return false;
		}
	} else if (c == '\'') {
		*result = _maketoken(T_CHAR, line, col);
		(*result)->val = g_malloc0(4);

		_read(self, &c, err);

		if (c == '\\') {
			_read(self, &c, err);

			switch (c) {
				case 'n': c = '\n'; break;
				case 'r': c = '\r'; break;
				case 't': c = '\t'; break;
				case '"': c = '"'; break;
				case '\'': c = '\''; break;
				case '\\': c = '\\'; break;
			}
		}

		g_unichar_to_utf8(c, (*result)->val); 

		REQUIRE(_read(self, &c, err));
		if (c == '\'') {
			return true;
		} else {
			_set_error(err,
				self,
				GSDL_SYNTAX_ERROR_MISSING_DELIMITER,
				"Missing \"'\""
			);
			return false;
		}
	} else if (c == '\\' && _peek(self, &nc, err) && (nc == '\r' || nc == '\n')) {
		_consume(self);

		if (c == '\r') _read(self, &c, err);

		goto retry;
	} else if (c == ' ' || c == '\t') {
		// Do nothing
		goto retry;
	} else {
		_set_error(err,
			self,
			GSDL_SYNTAX_ERROR_UNEXPECTED_CHAR,
		   	g_strdup_printf("Invalid character '%s'(%d)", g_ucs4_to_utf8(&c, 1, NULL, NULL, NULL), c)
		);
		return false;
	}
}
Ejemplo n.º 25
0
static void
process (gint      line,
	 gchar    *utf8,
	 Status    status,
	 gunichar *ucs4,
	 gint      ucs4_len)
{
  const gchar *end;
  gboolean is_valid = g_utf8_validate (utf8, -1, &end);
  GError *error = NULL;
  glong items_read, items_written;

  switch (status)
    {
    case VALID:
      if (!is_valid)
	{
	  fail ("line %d: valid but g_utf8_validate returned FALSE\n", line);
	  return;
	}
      break;
    case NOTUNICODE:
    case INCOMPLETE:
    case OVERLONG:
    case MALFORMED:
      if (is_valid)
	{
	  fail ("line %d: invalid but g_utf8_validate returned TRUE\n", line);
	  return;
	}
      break;
    }

  if (status == INCOMPLETE)
    {
      gunichar *ucs4_result;      

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, NULL, NULL, &error);

      if (!error || !g_error_matches (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT))
	{
	  fail ("line %d: incomplete input not properly detected\n", line);
	  return;
	}
      g_clear_error (&error);

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, NULL, &error);

      if (!ucs4_result || items_read == strlen (utf8))
	{
	  fail ("line %d: incomplete input not properly detected\n", line);
	  return;
	}

      g_free (ucs4_result);
    }

  if (status == VALID || status == NOTUNICODE)
    {
      gunichar *ucs4_result;
      gchar *utf8_result;

      ucs4_result = g_utf8_to_ucs4 (utf8, -1, &items_read, &items_written, &error);
      if (!ucs4_result)
	{
	  fail ("line %d: conversion to ucs4 failed: %s\n", line, error->message);
	  return;
	}
      
      if (!ucs4_equal (ucs4_result, ucs4) ||
	  items_read != strlen (utf8) ||
	  items_written != ucs4_len)
	{
	  fail ("line %d: results of conversion to ucs4 do not match expected.\n", line);
	  return;
	}

      g_free (ucs4_result);

      ucs4_result = g_utf8_to_ucs4_fast (utf8, -1, &items_written);
      
      if (!ucs4_equal (ucs4_result, ucs4) ||
	  items_written != ucs4_len)
	{
	  fail ("line %d: results of conversion to ucs4 do not match expected.\n", line);
	  return;
	}

      utf8_result = g_ucs4_to_utf8 (ucs4_result, -1, &items_read, &items_written, &error);
      if (!utf8_result)
	{
	  fail ("line %d: conversion back to utf8 failed: %s", line, error->message);
	  return;
	}

      if (strcmp (utf8_result, utf8) != 0 ||
	  items_read != ucs4_len ||
	  items_written != strlen (utf8))
	{
	  fail ("line %d: conversion back to utf8 did not match original\n", line);
	  return;
	}

      g_free (utf8_result);
      g_free (ucs4_result);
    }

  if (status == VALID)
    {
      gunichar2 *utf16_expected_tmp;
      gunichar2 *utf16_expected;
      gunichar2 *utf16_from_utf8;
      gunichar2 *utf16_from_ucs4;
      gunichar *ucs4_result;
      gsize bytes_written;
      gint n_chars;
      gchar *utf8_result;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define TARGET "UTF-16LE"
#else
#define TARGET "UTF-16"
#endif

      if (!(utf16_expected_tmp = (gunichar2 *)g_convert (utf8, -1, TARGET, "UTF-8",
							 NULL, &bytes_written, NULL)))
	{
	  fail ("line %d: could not convert to UTF-16 via g_convert\n", line);
	  return;
	}

      /* zero-terminate and remove BOM
       */
      n_chars = bytes_written / 2;
      if (utf16_expected_tmp[0] == 0xfeff) /* BOM */
	{
	  n_chars--;
	  utf16_expected = g_new (gunichar2, n_chars + 1);
	  memcpy (utf16_expected, utf16_expected_tmp + 1, sizeof(gunichar2) * n_chars);
	}
      else if (utf16_expected_tmp[0] == 0xfffe) /* ANTI-BOM */
	{
	  fail ("line %d: conversion via iconv to \"UTF-16\" is not native-endian\n", line);
	  return;
	}
      else
	{
	  utf16_expected = g_new (gunichar2, n_chars + 1);
	  memcpy (utf16_expected, utf16_expected_tmp, sizeof(gunichar2) * n_chars);
	}

      utf16_expected[n_chars] = '\0';
      
      if (!(utf16_from_utf8 = g_utf8_to_utf16 (utf8, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != strlen (utf8) ||
	  utf16_count (utf16_from_utf8) != items_written)
	{
	  fail ("line %d: length error in conversion to ucs16\n", line);
	  return;
	}

      if (!(utf16_from_ucs4 = g_ucs4_to_utf16 (ucs4, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion to ucs16 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != ucs4_len ||
	  utf16_count (utf16_from_ucs4) != items_written)
	{
	  fail ("line %d: length error in conversion to ucs16\n", line);
	  return;
	}

      if (!utf16_equal (utf16_from_utf8, utf16_expected) ||
	  !utf16_equal (utf16_from_ucs4, utf16_expected))
	{
	  fail ("line %d: results of conversion to ucs16 do not match\n", line);
	  return;
	}

      if (!(utf8_result = g_utf16_to_utf8 (utf16_from_utf8, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion back to utf8 failed: %s\n", line, error->message);
	  return;
	}

      if (items_read != utf16_count (utf16_from_utf8) ||
	  items_written != strlen (utf8))
	{
	  fail ("line %d: length error in conversion from ucs16 to utf8\n", line);
	  return;
	}

      if (!(ucs4_result = g_utf16_to_ucs4 (utf16_from_ucs4, -1, &items_read, &items_written, &error)))
	{
	  fail ("line %d: conversion back to utf8/ucs4 failed\n", line);
	  return;
	}

      if (items_read != utf16_count (utf16_from_utf8) ||
	  items_written != ucs4_len)
	{
	  fail ("line %d: length error in conversion from ucs16 to ucs4\n", line);
	  return;
	}

      if (strcmp (utf8, utf8_result) != 0 ||
	  !ucs4_equal (ucs4, ucs4_result))
	{
	  fail ("line %d: conversion back to utf8/ucs4 did not match original\n", line);
	  return;
	}
      
      g_free (utf16_expected_tmp);
      g_free (utf16_expected);
      g_free (utf16_from_utf8);
      g_free (utf16_from_ucs4);
      g_free (utf8_result);
      g_free (ucs4_result);
    }
}
Ejemplo n.º 26
0
int main(int argc, char * argv[]) {
    SystemTableInfo system_table_info;

    bool retval = system_table_info.load("../../data/table.conf");
    if (!retval) {
        fprintf(stderr, "load table.conf failed.\n");
        exit(ENOENT);
    }

    pinyin_option_t options = USE_TONE | PINYIN_INCOMPLETE;
    ChewingLargeTable largetable(options);
    FacadePhraseIndex phrase_index;

    const pinyin_table_info_t * phrase_files =
        system_table_info.get_table_info();

    if (!load_phrase_table(phrase_files, &largetable, NULL, &phrase_index))
        exit(ENOENT);

    MemoryChunk * new_chunk = new MemoryChunk;
    largetable.store(new_chunk);
    largetable.load(new_chunk);

    char* linebuf = NULL; size_t size = 0; ssize_t read;
    while ((read = getline(&linebuf, &size, stdin)) != -1) {
        if ( '\n' == linebuf[strlen(linebuf) - 1] ) {
            linebuf[strlen(linebuf) - 1] = '\0';
        }

	if ( strcmp ( linebuf, "quit" ) == 0)
	    break;

        FullPinyinParser2 parser;
        ChewingKeyVector keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
        ChewingKeyRestVector key_rests =
            g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));

        parser.parse(options, keys, key_rests, linebuf, strlen(linebuf));
        if (0 == keys->len) {
            fprintf(stderr, "Invalid input.\n");
            continue;
        }

        guint32 start = record_time();
        PhraseIndexRanges ranges;
        memset(ranges, 0, sizeof(PhraseIndexRanges));

        phrase_index.prepare_ranges(ranges);

        for (size_t i = 0; i < bench_times; ++i) {
            phrase_index.clear_ranges(ranges);
            largetable.search(keys->len, (ChewingKey *)keys->data, ranges);
        }
        print_time(start, bench_times);

        phrase_index.clear_ranges(ranges);
        largetable.search(keys->len, (ChewingKey *)keys->data, ranges);

        for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
            GArray * & range = ranges[i];
            if (!range)
                continue;

            if (range->len)
                printf("range items number:%d\n", range->len);

            for (size_t k = 0; k < range->len; ++k) {
                PhraseIndexRange * onerange =
                    &g_array_index(range, PhraseIndexRange, k);
                printf("start:%d\tend:%d\n", onerange->m_range_begin,
                       onerange->m_range_end);

                PhraseItem item;
                for ( phrase_token_t token = onerange->m_range_begin;
                      token != onerange->m_range_end; ++token){

                    phrase_index.get_phrase_item( token, item);

                    /* get phrase string */
                    ucs4_t buffer[MAX_PHRASE_LENGTH + 1];
                    item.get_phrase_string(buffer);
                    char * string = g_ucs4_to_utf8
                        ( buffer, item.get_phrase_length(),
                          NULL, NULL, NULL);
                    printf("%s\t", string);
                    g_free(string);

                    ChewingKey chewing_buffer[MAX_PHRASE_LENGTH];
                    size_t npron = item.get_n_pronunciation();
                    guint32 freq;
                    for (size_t m = 0; m < npron; ++m){
                        item.get_nth_pronunciation(m, chewing_buffer, freq);
                        for (size_t n = 0; n < item.get_phrase_length();
                             ++n){
                            gchar * pinyins =
                                chewing_buffer[n].get_pinyin_string();
                            printf("%s'", pinyins);
                            g_free(pinyins);
                        }
                        printf("\b\t%d\t", freq);
                    }
                }
                printf("\n");
            }
            g_array_set_size(range, 0);
        }

        phrase_index.destroy_ranges(ranges);
	g_array_free(keys, TRUE);
	g_array_free(key_rests, TRUE);
    }

    if (linebuf)
        free(linebuf);

    /* mask out all index items. */
    largetable.mask_out(0x0, 0x0);

    return 0;
}
Ejemplo n.º 27
0
unsigned Gosu::pango::textWidth(const std::wstring& text,
    const std::wstring& fontFace, unsigned fontHeight,
    unsigned fontFlags)
{
    g_type_init();

    int dpi_x = 100, dpi_y = 100;

    context = pango_ft2_get_context(dpi_x, dpi_y);

    pango_context_set_language(context, pango_language_from_string ("en_US"));
    PangoDirection init_dir = PANGO_DIRECTION_LTR;
    pango_context_set_base_dir(context, init_dir);

//    static PangoFontDescription *font_description;
    font_description = pango_font_description_new();

    pango_font_description_set_family(font_description,
        g_strdup(narrow(fontFace).c_str()));
    pango_font_description_set_style(font_description,
        (fontFlags & ffItalic) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
    pango_font_description_set_variant(font_description, PANGO_VARIANT_NORMAL);
    pango_font_description_set_weight(font_description,
        (fontFlags & ffBold) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
    pango_font_description_set_stretch(font_description, PANGO_STRETCH_NORMAL);
    int init_scale = int(fontHeight/2.0 + 0.5);
    pango_font_description_set_size(font_description, init_scale * PANGO_SCALE);

    pango_context_set_font_description(context, font_description);


    layout = pango_layout_new(context);


    if(fontFlags & ffUnderline)
    {
//        PangoAttribute *attr;
        attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
        attr->start_index = 0;
        attr->end_index = text.length();
//        PangoAttrList* attrList;
        attrList = pango_attr_list_new();
        pango_attr_list_insert(attrList, attr);
        pango_layout_set_attributes(layout, attrList);
        pango_attr_list_unref(attrList);
    }


    // IMPR: Catch errors? (Last NULL-Pointer)
    gchar* utf8Str = g_ucs4_to_utf8((gunichar*)text.c_str(), text.length(), NULL, NULL, NULL);
    pango_layout_set_text(layout, utf8Str, -1);
    g_free(utf8Str);

    PangoDirection base_dir = pango_context_get_base_dir(context);
    pango_layout_set_alignment(layout,
        base_dir == PANGO_DIRECTION_LTR ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);

    pango_layout_set_width(layout, -1);

    PangoRectangle logical_rect;

    pango_layout_get_pixel_extents(layout, NULL, &logical_rect);
    height = logical_rect.height;
    width = logical_rect.width;

    return width;
}
Ejemplo n.º 28
0
int main(int argc, char * argv[]){
    int i = 1;
    bool gen_extra_enter = false;

    setlocale(LC_ALL, "");
    //deal with options.
    while ( i < argc ){
        if ( strcmp ("--help", argv[i]) == 0) {
            print_help();
            exit(0);
        } else if (strcmp("--generate-extra-enter", argv[i]) == 0) {
            gen_extra_enter = true;
        } else {
            print_help();
            exit(EINVAL);
        }
        ++i;
    }

    /* init phrase table */
    FacadePhraseTable2 phrase_table;
    MemoryChunk * chunk = new MemoryChunk;
    chunk->load("phrase_index.bin");
    phrase_table.load(chunk, NULL);

    /* init phrase index */
    FacadePhraseIndex phrase_index;
    if (!load_phrase_index(&phrase_index))
        exit(ENOENT);

    char * linebuf = NULL;
    size_t size = 0;
    ssize_t read;
    while( (read = getline(&linebuf, &size, stdin)) != -1 ){
        if ( '\n' ==  linebuf[strlen(linebuf) - 1] ) {
            linebuf[strlen(linebuf) - 1] = '\0';
        }

        //check non-ucs4 characters
        const glong num_of_chars = g_utf8_strlen(linebuf, -1);
        glong len = 0;
        ucs4_t * sentence = g_utf8_to_ucs4(linebuf, -1, NULL, &len, NULL);
        if ( len != num_of_chars ) {
            fprintf(stderr, "non-ucs4 characters encountered:%s.\n", linebuf);
            printf("\n");
            continue;
        }

        //do segment stuff
        GArray * strings = g_array_new(TRUE, TRUE, sizeof(SegmentStep));
        segment(&phrase_table, &phrase_index, sentence, len, strings);

        //print out the split phrase
        for ( glong i = 0; i < strings->len; ++i ) {
            SegmentStep * step = &g_array_index(strings, SegmentStep, i);
            char * string = g_ucs4_to_utf8( step->m_phrase, step->m_phrase_len, NULL, NULL, NULL);
            printf("%d %s\n", step->m_handle, string);
            g_free(string);
        }

        /* print extra enter */
        if ( gen_extra_enter )
            printf("\n");

        g_array_free(strings, TRUE);
        g_free(sentence);
    }

    /* print enter at file tail */
    printf("\n");
    return 0;
}
Ejemplo n.º 29
0
void *validate_value(gint value_id, gint input_type, void *value)
{
	// Local variables
	guint				base_type;
	guint				capabilities;
	gboolean			capability_check;
	gchar				*conversion_buffer = NULL;			// Used when converting between unicode character types
	gchar				*decimal_point;
	GString				*error_string;
	gchar				input_char;
	gunichar			input_char_unicode;
	gchar				*input_ptr;
	struct lconv		*locale_info;
	gboolean			match_found;
	gint				output_gint;
	gint				*output_gint_ptr;
	gfloat				output_gfloat;
	gfloat				*output_gfloat_ptr;
	GString				*output_gstring;
	guint				output_guint;
	guint				*output_guint_ptr;
	guint				string_counter;
	gint				string_length;
	gint				string_max;
	guint				string_min;
	gfloat				value_max;
	gfloat				value_min;


	// Initialise various things
	base_type = get_valid_fields_base_type(value_id);
	capabilities = get_valid_fields_capabilities(value_id);
	input_ptr = (gchar *) value;
	output_gstring = g_string_new(NULL);
	locale_info = localeconv();
	decimal_point = locale_info->decimal_point;

	switch (base_type)
	{
		case V_CHAR:

			// * We're validating a char or string *

			// We can only validate string input for this type of value
			if (V_CHAR != input_type)
			{
				return NULL;
			}

			// Get the length of the input string
			string_max = get_valid_fields_max_value(value_id);
			string_min = get_valid_fields_min_value(value_id);
			string_length = g_utf8_strlen(input_ptr, -1);

			// If the length of the string isn't in the acceptable range, return NULL
			if (string_length < string_min)
				return NULL;
			if ((string_length > string_max) && (-1 != string_max))  // -1 for string_max means "no maximum limit"
				return NULL;
			if (0 == string_length)
			{
				// 0 length string, so we just return it
				return output_gstring;
			}

			// Sanitise each character of the input string
			for (string_counter = 0; string_counter < string_length; string_counter++)
			{
				// Get the next character
				input_char_unicode = g_utf8_get_char_validated(input_ptr, -1);
				if ((gunichar)-1 == input_char_unicode)
				{
					// The returned character was not a valid unicode character, so we indicate failure
					return NULL;
				}
				if ((gunichar)-2 == input_char_unicode)
				{
					// The returned character was not a valid unicode character, so we indicate failure
					return NULL;
				}

				// Convert the character to UTF-8 so we can process it
				conversion_buffer = g_ucs4_to_utf8(&input_char_unicode, 1, NULL, NULL, NULL);

				// Determine which character we're examining
				if (TRUE == g_unichar_isalnum(input_char_unicode))
				{
					// It's a standard unicode alphanumeric character, so we accept it as is
					g_string_append_printf(output_gstring, "%s", conversion_buffer);
					input_ptr = g_utf8_find_next_char(input_ptr, NULL);
				}
				else
				{
					// * The input wasn't a standard alphanumic character, so check if it *
					// * is one of the characters in the capabilities list for this field *
					match_found = FALSE;
					capability_check = V_ANY_UNICHAR & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have any valid unicode character
						if (TRUE == g_unichar_validate(input_char_unicode))
						{
							// Yes, this is a valid unicode character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_SPACES & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have spaces
						if (0 == g_strcmp0(" ", conversion_buffer))
						{
							// Yes, this is a space character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_FULL_STOP & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have full stops
						if (0 == g_strcmp0(".", conversion_buffer))
						{
							// Yes, this is a full stop character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_HYPENS & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have hypens
						if (0 == g_strcmp0("-", conversion_buffer))
						{
							// Yes, this is a hypen character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_UNDERSCORES & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have underscores
						if (0 == g_strcmp0("_", conversion_buffer))
						{
							// Yes, this is an underscore character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_PATH_SEP & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have path separator characters ('/', '\')
						if ((0 == g_strcmp0("/", conversion_buffer)) || (0 == g_strcmp0("\\", conversion_buffer)))
						{
							// Yes, this is a path separator character
							match_found = TRUE;
							output_gstring = g_string_append_c(output_gstring, G_DIR_SEPARATOR);  // Output the OS correct version
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_EQUALS & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have equals signs
						if (0 == g_strcmp0("=", conversion_buffer))
						{
							// Yes, this is an equals sign character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_FORWARD_SLASHES & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have forward slashes
						if (0 == g_strcmp0("/", conversion_buffer))
						{
							// Yes, this is a forward slash character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_NEW_LINES & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have new line characters
						if (0 == g_strcmp0("\n", conversion_buffer))
						{
							// Yes, this is a new line character
							match_found = TRUE;
							output_gstring = g_string_append_c(output_gstring, '\n');
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_PLUSES & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have pluses
						if (0 == g_strcmp0("+", conversion_buffer))
						{
							// Yes, this is a plus character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_PERCENT & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have the percent sign
						if (0 == g_strcmp0("%", conversion_buffer))
						{
							// Yes, this is a percent sign
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_COLON & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have colons
						if (0 == g_strcmp0(":", conversion_buffer))
						{
							// Yes, this is a colon character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_AT & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have the at symbol
						if (0 == g_strcmp0("@", conversion_buffer))
						{
							// Yes, this is an at character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_QUESTION & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have the question mark
						if (0 == g_strcmp0("?", conversion_buffer))
						{
							// Yes, this is a question mark character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}
					capability_check = V_AMPERSAND & capabilities;
					if (FALSE != capability_check)
					{
						// This field is allowed to have the ampersand character
						if (0 == g_strcmp0("&", conversion_buffer))
						{
							// Yes, this is an ampersand character
							match_found = TRUE;
							g_string_append_printf(output_gstring, "%s", conversion_buffer);
							input_ptr = g_utf8_find_next_char(input_ptr, NULL);
							continue;
						}
					}

					// The character we are checking is not in the list of valid inputs for this field
					if (FALSE == match_found)
					{
						g_free(conversion_buffer);
						g_string_free(output_gstring, TRUE);
						return NULL;
					}
				}
			}

			// Remove any leading and/or trailing white space
			output_gstring->str = g_strstrip(output_gstring->str);
			output_gstring->len = strlen(output_gstring->str);

			// Recheck the length of the output string
			if (output_gstring->len < string_min)
			{
				g_free(conversion_buffer);
				g_string_free(output_gstring, TRUE);
				return NULL;
			}

			// Free the memory used so far
			if (0 != string_length)
			{
				g_free(conversion_buffer);
			}

			// The string seems to be valid, so return it for use
			return output_gstring;

			break;

		case V_FLOAT_UNSIGNED:

			// * We're validating an unsigned float *

			// If we're working with string input, we need to convert it to a float first
			if (V_CHAR == input_type)
			{
				// * We're working with string input *

				// Get the length of the input string
				string_length = strlen((gchar *) value);
	
				// Sanitise each character of the input string
				for (string_counter = 0; string_counter < string_length; string_counter++)
				{
					input_char = ((gchar *) value)[string_counter];

					// Check for decimal digits
					if (TRUE == g_ascii_isdigit(input_char))
					{
						output_gstring = g_string_append_c(output_gstring, input_char);
					}
					else
					{
						// This field is allowed to have full stops.  Is this character a full stop?
						if (0 == g_ascii_strncasecmp(".", &input_char, 1))
						{
							// Yes, this is a full stop character
							output_gstring = g_string_append_c(output_gstring, *decimal_point);
							match_found = TRUE;
							continue;
						}

						// This field is allowed to have commas (equiv to full stop in some locales).  Is this character a comma?
						if (0 == g_ascii_strncasecmp(",", &input_char, 1))
						{
							// Yes, this is a comma character
							output_gstring = g_string_append_c(output_gstring, *decimal_point);
							match_found = TRUE;
							continue;
						}

						// The character we are checking is not in the list of valid inputs for this field
						if (FALSE == match_found)
						{
							g_string_free(output_gstring, TRUE);
							return NULL;
						}
					}
				}

				// Convert the string to a float
				output_gfloat = (gfloat) g_strtod(output_gstring->str, NULL);
			}
			else
			{
				// We're working with a float input, so just copy the value directly
				output_gfloat = *((gfloat *) value);
			}

			// Is the float value within the defined bounds?
			value_max = get_valid_fields_max_value(value_id);
			value_min = get_valid_fields_min_value(value_id);
			if ((output_gfloat < value_min) || (output_gfloat > value_max))
			{
				// Value is out of bounds, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}

			// The value looks ok, so we copy it to newly allocated memory, to pass it back
			output_gfloat_ptr = g_try_new0(gfloat, 1);
			if (NULL == output_gfloat_ptr)
			{
				// Unable to allocate memory for the new value, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}
			*output_gfloat_ptr = output_gfloat;

			// Free the string memory allocated in this function
			g_string_free(output_gstring, TRUE);

			return output_gfloat_ptr;

		case V_INT_UNSIGNED:

			// * We're validating an unsigned integer *

			// If we're working with string input, we need to convert it to an integer first
			if (V_CHAR == input_type)
			{
				// * We're working with string input *

				// Get the length of the input string
				string_length = strlen((gchar *) value);
	
				// Sanitise each character of the input string
				for (string_counter = 0; string_counter < string_length; string_counter++)
				{
					input_char = ((gchar *) value)[string_counter];

					// Check for decimal digits
					if (TRUE == g_ascii_isdigit(input_char))
					{
						output_gstring = g_string_append_c(output_gstring, input_char);
					}
					else
					{
						// This wasn't a valid character
						g_string_free(output_gstring, TRUE);
						return NULL;
					}
				}

				// Convert the string to an integer
				output_guint = atoi(output_gstring->str);
			}
			else
			{
				// We're working with integer input, so just copy the value directly
				output_guint = *((guint *) value);
			}

			// Is the integer value within the defined bounds?
			value_max = get_valid_fields_max_value(value_id);
			value_min = get_valid_fields_min_value(value_id);
			if ((output_guint < value_min) || (output_guint > value_max))
			{
				// Value is out of bounds, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}

			// The value looks ok, so we copy it to newly allocated memory, to pass it back
			output_guint_ptr = g_try_new0(guint, 1);
			if (NULL == output_guint_ptr)
			{
				// Unable to allocate memory for the new value, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}
			*output_guint_ptr = output_guint;

			// Free the string memory allocated in this function
			g_string_free(output_gstring, TRUE);

			return output_guint_ptr;

		case V_INT_SIGNED:

			// * We're validating a signed integer *

			// If we're working with string input, we need to convert it to an integer first
			if (V_CHAR == input_type)
			{
				// * We're working with string input *

				// Get the length of the input string
				string_length = strlen((gchar *) value);

				// Sanitise each character of the input string
				for (string_counter = 0; string_counter < string_length; string_counter++)
				{
					input_char = ((gchar *) value)[string_counter];

					// Check for decimal digits
					if (TRUE == g_ascii_isdigit(input_char))
					{
						output_gstring = g_string_append_c(output_gstring, input_char);
					}
					else
					{
						// * The input wasn't a standard digit character, so check if it *
						// * is one of the characters in the capabilities list for this field *
						match_found = FALSE;
						capability_check = V_HYPENS & capabilities;
						if (FALSE != capability_check)
						{
							// This field is allowed to have hypens
							if (0 == g_ascii_strncasecmp("-", &input_char, 1))
							{
								// Yes, this is a hypen character
								match_found = TRUE;
								g_string_append_printf(output_gstring, "%s", "-");
							}
						}

						// The character we are checking is not in the list of valid inputs for this field
						if (FALSE == match_found)
						{
							g_string_free(output_gstring, TRUE);
							return NULL;
						}
					}
				}

				// Convert the string to an integer
				output_gint = atoi(output_gstring->str);
			}
			else
			{
				// We're working with integer input, so just copy the value directly
				output_gint = *((gint *) value);
			}

			// Is the integer value within the defined bounds?
			value_max = get_valid_fields_max_value(value_id);
			value_min = get_valid_fields_min_value(value_id);
			if ((output_gint < value_min) || (output_gint > value_max))
			{
				// Value is out of bounds, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}

			// The value looks ok, so we copy it to newly allocated memory, to pass it back
			output_gint_ptr = g_try_new0(gint, 1);
			if (NULL == output_gint_ptr)
			{
				// Unable to allocate memory for the new value, so fail
				g_string_free(output_gstring, TRUE);
				return NULL;
			}
			*output_gint_ptr = output_gint;

			// Free the string memory allocated in this function
			g_string_free(output_gstring, TRUE);

			return output_gint_ptr;

		case V_RESOLUTION:

				// * We're working with a resolution (text string) input.  i.e. '1920x1200 pixels' *

				// Get the length of the input string
				string_length = strlen((gchar *) value);
				string_max = get_valid_fields_max_value(value_id);
				string_min = get_valid_fields_min_value(value_id);

				// If the length of the string isn't in the acceptable range, return NULL
				if ((string_length < string_min) || (string_length > string_max))
					return NULL;

				// Sanitise each character of the input string
				for (string_counter = 0; string_counter < string_length; string_counter++)
				{
					input_char = ((gchar *) value)[string_counter];

					// Check for decimal digits
					if (TRUE == g_ascii_isdigit(input_char))
					{
						output_gstring = g_string_append_c(output_gstring, input_char);
					}
					else
					{
						match_found = FALSE;

						// This field is allowed to have the ' ' and 'x' characters .  Is this character one of those?
						if (0 == g_ascii_strncasecmp(" ", &input_char, 1))
						{
							// Yes, this is a space character, so we've already collected the required resolution
							// info and we can just return the resolution part of the string so far

							// Remove any leading and/or trailing white space
							output_gstring->str = g_strstrip(output_gstring->str);
							output_gstring->len = strlen(output_gstring->str);

							// Recheck the length of the output string
							if ((string_length < string_min) || (string_length > string_max))
								return NULL;

							// The string seems to be valid, so return it for use
							return output_gstring;
						}
						if (0 == g_ascii_strncasecmp("x", &input_char, 1))
						{
							// Yes, this is a 'x' character
							output_gstring = g_string_append_c(output_gstring, 'x');
							match_found = TRUE;
							continue;
						}
						if (FALSE == match_found)
						{
							// This wasn't a valid character
							g_string_free(output_gstring, TRUE);
							return NULL;
						}
					}
				}

				// Remove any leading and/or trailing white space
				output_gstring->str = g_strstrip(output_gstring->str);
				output_gstring->len = strlen(output_gstring->str);

				// Recheck the length of the output string
				if ((string_length < string_min) || (string_length > string_max))
					return NULL;

				// The string seems to be valid, so return it for use
				return output_gstring;

		case V_ZOOM:

			// * We're working with a zoom level.  i.e. "100%" or "Fit to width" *

			// Get the length of the input string
			string_length = g_utf8_strlen((gchar *) value, -1);
			string_max = get_valid_fields_max_value(value_id);
			string_min = get_valid_fields_min_value(value_id);

			// If the length of the string isn't in the acceptable range, return NULL
			if ((string_length < string_min) || (string_length > string_max))
				return NULL;

			// If the string is "Fit to width" or a localised version of it
			if ((0 == g_strcmp0("Fit to width", (gchar *) value)) || (0 == g_strcmp0(_("Fit to width"), (gchar *) value)))
			{
				// Yes, this is the "Fit to width" value
				output_gstring = g_string_assign(output_gstring, value);
				return output_gstring;
			}

			// * The incoming string isn't the "Fit to width" value, *
			// * so should only consist of decimal characters and '%' *

			// Sanitise each character of the input string
			for (string_counter = 0; string_counter < string_length; string_counter++)
			{
				input_char = ((gchar *) value)[string_counter];

				// Check for decimal digits
				if (TRUE == g_ascii_isdigit(input_char))
				{
					output_gstring = g_string_append_c(output_gstring, input_char);
					continue;
				}
				// Check for '%' character
				if (0 == g_ascii_strncasecmp("%", &input_char, 1))
				{
					// Yes, this is a '%' character
					output_gstring = g_string_append_c(output_gstring, '%');
					continue;
				}
				// This wasn't a valid character
				g_string_free(output_gstring, TRUE);
				return NULL;
			}

			return output_gstring;

		default:

			// Unknown value type, we should never get here
			error_string = g_string_new(NULL);
			g_string_printf(error_string, "%s ED119: %s - '%s'", _("Error"), _("Unknown value passed to validation function"), get_valid_fields_name(value_id));
			display_warning(error_string->str);
			g_string_free(error_string, TRUE);

			return NULL;
	}

	// If we get here, then something went wrong!
	return NULL;
}
Ejemplo n.º 30
0
static gint
key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
#if 0
  charpick_data *p_curr_data = data;
  const gunichar *code = NULL;
  gchar inputchar = event->keyval;

  switch (inputchar)
    {
    case 'a' : code = a_code;
               break;
    case 'A' : code = cap_a_code;
               break;
    case 'c' : 
    case 'C' : code = c_code;
               break;
    case 'e' : code = e_code;
               break;
    case 'E' : code = cap_e_code;
               break;
    case 'i' : code = i_code;
               break;
    case 'I' : code =  cap_i_code;
               break;
    case 'n' : 
    case 'N' : code = n_code;
               break;
    case 'o' : code = o_code;
               break;
    case 'O' : code = cap_o_code;
               break;
    case 's' : code = s_code;
               break;
    case 't' : 
    case 'T' : code = t_code;
               break;
    case 'u' : code = u_code;
               break;
    case 'U' : code = cap_u_code;
               break;
    case 'y' : 
    case 'Y' : code = y_code;
               break;
    case '-' : code = dash_code;
               break;
    case '\"' : code = quote_code;
               break;
    case '$' : code = currency_code;
               break;
    case '1' : 
    case '2' :
    case '3' : code = one_code;
               break;
    case 'd' : code = NULL;
               break;
    default :
    		return FALSE;
    }
  /* FIXME: what's wrong here ? */
  if (code)
    p_curr_data->charlist = g_ucs4_to_utf8 (code, -1, NULL, NULL, NULL);
  else
    p_curr_data->charlist = "hello";
  p_curr_data->last_index = NO_LAST_INDEX;
  p_curr_data->last_toggle_button = NULL;
  build_table(p_curr_data);
#endif
  return FALSE;
}