/* allocates space and returns the index part of a string */ static char * _new_get_index(const char *_string) { if (!_string) return NULL; size_t size; gunichar u; char *string = NULL; if (g_ascii_isalnum(_string[0])) { size = sizeof(char); string = malloc(size+1); string[0] = g_ascii_toupper(_string[0]); } else { u = g_utf8_get_char_validated(_string, -1); if ((u != (gunichar)-1 || u != (gunichar)-2) && g_unichar_isalnum(u)) { u = g_unichar_toupper(u); size = g_unichar_to_utf8(u, NULL); string = malloc(size+1); g_unichar_to_utf8(u, string); } } if (string) string[size] = '\0'; return string; }
char *str_replace_char(const char *str, gunichar orig, gunichar repl) { int result_size = strlen(str) + 6; char *result = malloc(result_size); const char *r = str; char *w = result; while (*r) { int len; gunichar c = g_utf8_get_char(r); /* ensure there is room for at least 1 more character */ if (w - result + 7 > result_size) { int offset = w - result; result_size += max(6, 0.5 * result_size); result = realloc(result, result_size); w = result + offset; } if (c == orig) len = g_unichar_to_utf8(repl, w); else len = g_unichar_to_utf8(c, w); w += len; r = g_utf8_next_char(r); } *w = 0; return result; }
/* append text to the end of 'f' with attribute 'attr' and color * 'color' */ void owl_fmtext_append_attr(owl_fmtext *f, const char *text, char attr, short fgcolor, short bgcolor) { char attrbuff[6]; int newlen, a = 0, fg = 0, bg = 0; if (attr != OWL_FMTEXT_ATTR_NONE) a=1; if (fgcolor != OWL_COLOR_DEFAULT) fg=1; if (bgcolor != OWL_COLOR_DEFAULT) bg=1; /* Plane-16 characters in UTF-8 are 4 bytes long. */ newlen = strlen(f->textbuff) + strlen(text) + (8 * (a + fg + bg)); _owl_fmtext_realloc(f, newlen); /* Set attributes */ if (a) strncat(f->textbuff, attrbuff, g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff)); if (fg) strncat(f->textbuff, attrbuff, g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff)); if (bg) strncat(f->textbuff, attrbuff, g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff)); strcat(f->textbuff, text); /* Reset attributes */ if (bg) strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); if (fg) strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); if (a) strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); f->textlen=newlen; }
static bool _tokenize_identifier(GSDLTokenizer *self, GSDLToken *result, gunichar c, GError **err) { int length = 7; char *output = result->val = g_malloc(length); GUnicodeType type; int i = g_unichar_to_utf8(c, output); while (_peek(self, &c, err) && (c == '-' || c == '.' || g_unichar_isalpha(c) || g_unichar_isdigit(c) || (type = g_unichar_type(c)) == G_UNICODE_CURRENCY_SYMBOL || type == G_UNICODE_CONNECT_PUNCTUATION || type == G_UNICODE_LETTER_NUMBER || type == G_UNICODE_SPACING_MARK || type == G_UNICODE_NON_SPACING_MARK)) { GROW_IF_NEEDED(output = result->val, i + 5, length); _consume(self); i += g_unichar_to_utf8(c, output + i); } FAIL_IF_ERR(); output[i] = '\0'; if ( strcmp(output, "true") == 0 || strcmp(output, "on") == 0 || strcmp(output, "false") == 0 || strcmp(output, "off") == 0) { result->type = T_BOOLEAN; } else if (strcmp(output, "null") == 0) { result->type = T_NULL; } return true; }
static char *convert_title_case(const char *str) { int result_size = strlen(str) + 12; char *result = malloc(result_size); const char *r = str; char *w = result; gboolean upcase_next = TRUE; while (*r) { int len; gunichar c = g_utf8_get_char(r); /* ensure there is room for at least 1 more character */ if (w - result + 7 > result_size) { int offset = w - result; result_size += max(6, 0.5 * result_size); result = realloc(result, result_size); w = result + offset; } if (upcase_next) len = g_unichar_to_utf8(g_unichar_toupper(c), w); else len = g_unichar_to_utf8(g_unichar_tolower(c), w); w += len; upcase_next = !g_unichar_isalnum(c) && c != '\''; r = g_utf8_next_char(r); } *w = 0; return result; }
/* utiliti function, that copy characters from cheked to actual until ident is * smaller than to_ident */ static gboolean utf8_tool_copy_chars_to (struct utf8_tool *tool, int to_ident) { tool->compose = FALSE; while (tool->cheked[0] != '\0') { gunichar uni; size_t left; int w = 0; uni = g_utf8_get_char (tool->cheked); if (str_unichar_iscombiningmark (uni)) tool->compose = TRUE; else { w = 1; if (g_unichar_iswide (uni)) w++; if (tool->ident + w > to_ident) return TRUE; } left = g_unichar_to_utf8 (uni, NULL); if (tool->remain <= left) return FALSE; left = g_unichar_to_utf8 (uni, tool->actual); tool->actual += left; tool->remain -= left; tool->cheked = g_utf8_next_char (tool->cheked); tool->ident += w; } return TRUE; }
static char *convert_sentence_case(const char *str) { int result_size = strlen(str) + 12; char *result = malloc(result_size); const char *r = str; char *w = result; while (*r) { int len; gunichar c = g_utf8_get_char(r); /* ensure there is room for at least 1 more character */ if (w - result + 7 > result_size) { int offset = w - result; result_size += max(6, 0.5 * result_size); result = realloc(result, result_size); w = result + offset; } if (r == str) len = g_unichar_to_utf8(g_unichar_toupper(c), w); else len = g_unichar_to_utf8(g_unichar_tolower(c), w); w += len; r = g_utf8_next_char(r); } *w = 0; return result; }
/* check for characters that are invalid in XML */ static gboolean is_valid_string (const gchar * s, gchar * * subst) { if (! g_utf8_validate (s, -1, NULL)) goto NOT_VALID; const gchar * p = s; while (* p) { gunichar c = g_utf8_get_char (p); if (IS_VALID_CHAR (c)) p = g_utf8_next_char (p); else goto NOT_VALID; } return TRUE; NOT_VALID:; gint len = 0; p = s; while (* p) { gunichar c = g_utf8_get_char_validated (p, -1); if (IS_VALID_CHAR (c)) { len += g_unichar_to_utf8 (c, NULL); p = g_utf8_next_char (p); } else p ++; } * subst = g_malloc (len + 1); gchar * w = * subst; p = s; while (* p) { gunichar c = g_utf8_get_char_validated (p, -1); if (IS_VALID_CHAR (c)) { w += g_unichar_to_utf8 (c, w); p = g_utf8_next_char (p); } else p ++; } * w = 0; return FALSE; }
static UniToStrPair ipa_to_unicode_make_pair(gunichar from, gunichar to1, gunichar to2) { gchar buf[7]; UniToStrPair res; res.first = from; buf[g_unichar_to_utf8(to1, buf)] = '\0'; res.second = buf; buf[g_unichar_to_utf8(to2, buf)] = '\0'; res.second += buf; return res; }
unsigned char convert_from_utf_to_current_c (const int input_char, GIConv conv) { unsigned char str[6 + 1]; unsigned char buf_ch[6 + 1]; unsigned char ch = '.'; int res = 0; res = g_unichar_to_utf8 (input_char, (char *) str); if (res == 0) { return ch; } str[res] = '\0'; switch (str_translate_char (conv, (char *) str, -1, (char *) buf_ch, sizeof (buf_ch))) { case ESTR_SUCCESS: ch = buf_ch[0]; break; case ESTR_PROBLEM: case ESTR_FAILURE: ch = '.'; break; } return ch; }
static gboolean on_input(UNUSED GIOChannel *src, GIOCondition cond, gpointer ud) /*{{{*/ { LuaState *L = ud; if (cond & G_IO_IN) { lua_getglobal(L, "on_input"); if (!lua_isnil(L, -1)) { gunichar ch = 0; gboolean ok = ms_term_getkey(&ch); if (ok) { char buf[8]; memset(buf, 0, sizeof(buf)); g_unichar_to_utf8(ch, buf); lua_pushstring(L, buf); } else { lua_pushnil(L); } if (lua_pcall(L, 1, 0, 0) != 0) { g_warning("lua error in on_input(): %s", lua_tostring(L, -1)); } } else { lua_pop(L, 1); } return TRUE; } return FALSE; }/* }}} */
gchar * gebr_geoxml_line_create_key(const gchar *title) { gchar *lower_no_accents = g_utf8_strdown(title, -1); lower_no_accents = gebr_g_string_remove_accents(lower_no_accents); GString *buffer = g_string_new(NULL); gchar *tmp = lower_no_accents; gunichar c; for (; tmp && *tmp; tmp = g_utf8_next_char(tmp)) { c = g_utf8_get_char(tmp); if (c == ' ' || !g_ascii_isalnum((char)c)) { g_string_append_c(buffer, '_'); } else { gchar str[7]; gint len = g_unichar_to_utf8(c, str); g_string_append_len(buffer, str, len); } } g_string_append_c(buffer, '\0'); g_free(lower_no_accents); return g_string_free(buffer, FALSE); }
void tty_print_anychar (int c) { char str[6 + 1]; if (c > 255) { int res = g_unichar_to_utf8 (c, str); if (res == 0) { str[0] = '.'; str[1] = '\0'; } else { str[res] = '\0'; } SLsmg_write_string ((char *) str_term_form (str)); } else { if (!is_printable (c)) c = '.'; SLsmg_write_char ((SLwchar_Type) ((unsigned int) c)); } }
PyObject * _pygi_marshal_to_py_unichar (PyGIInvokeState *state, PyGICallableCache *callable_cache, PyGIArgCache *arg_cache, GIArgument *arg) { PyObject *py_obj = NULL; /* Preserve the bidirectional mapping between 0 and "" */ if (arg->v_uint32 == 0) { py_obj = PYGLIB_PyUnicode_FromString (""); } else if (g_unichar_validate (arg->v_uint32)) { gchar utf8[6]; gint bytes; bytes = g_unichar_to_utf8 (arg->v_uint32, utf8); py_obj = PYGLIB_PyUnicode_FromStringAndSize ((char*)utf8, bytes); } else { /* TODO: Convert the error to an exception. */ PyErr_Format (PyExc_TypeError, "Invalid unicode codepoint %" G_GUINT32_FORMAT, arg->v_uint32); } return py_obj; }
static void text_insert_char(Text *text, gunichar c) { gchar ch[7]; int unilen; int row; gchar *line, *str; gchar *utf8_before; gchar *str1; /* Make a string of the the char */ unilen = g_unichar_to_utf8 (c, ch); ch[unilen] = 0; row = text->cursor_row; /* Copy the before and after parts with the new char in between */ line = text_get_line(text, row); utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos)); str1 = g_strndup(line, utf8_before - line); str = g_strconcat(str1, ch, utf8_before, NULL); text_set_line_text(text, row, str); g_free(str); g_free(str1); text->cursor_pos++; text->max_width = MAX(text->max_width, text_get_line_width(text, row)); }
GList* get_radical_of_kanji(gunichar kanji, GHashTable *kanji_info_hash) { GList *kanji_info_list = NULL; GList *radical_list = NULL; //list of radical to be returned //convert to UTF8 gchar utf8kanji[3]; int at = g_unichar_to_utf8(kanji, utf8kanji); utf8kanji[at] = '\0'; //lookup in the kanji info hash and get the kanji info KanjiInfo *kanji_info = g_hash_table_lookup(kanji_info_hash, utf8kanji); if(kanji_info == NULL){ return NULL; } //puts the radical field of the kanji_info into a list for (kanji_info_list = kanji_info->rad_info_list; kanji_info_list != NULL; kanji_info_list = kanji_info_list->next) { radical_list = g_list_prepend(radical_list, (gpointer) ((RadInfo *) kanji_info_list->data)->radical ); } return radical_list; }
static void unichar_changed (GObject *object, GParamSpec *pspec, gpointer data) { GtkEntry *entry = GTK_ENTRY (data); gunichar new_val; gunichar old_val = unichar_get_value (entry); GValue val = G_VALUE_INIT; gchar buf[7]; gint len; g_value_init (&val, pspec->value_type); get_property_value (object, pspec, &val); new_val = (gunichar)g_value_get_uint (&val); if (new_val != old_val) { if (!new_val) len = 0; else len = g_unichar_to_utf8 (new_val, buf); buf[len] = '\0'; block_controller (G_OBJECT (entry)); gtk_entry_set_text (entry, buf); unblock_controller (G_OBJECT (entry)); } }
/** Converts unicode strings such as \003d into = * Code blatantly nicked from the Facebook plugin */ gchar * convert_unicode(const gchar *input) { gunichar unicode_char; gchar unicode_char_str[6]; gint unicode_char_len; gchar *next_pos; gchar *input_string; gchar *output_string; if (input == NULL) return NULL; next_pos = input_string = g_strdup(input); while ((next_pos = strstr(next_pos, "\\u"))) { /* grab the unicode */ sscanf(next_pos, "\\u%4x", &unicode_char); /* turn it to a char* */ unicode_char_len = g_unichar_to_utf8(unicode_char, unicode_char_str); /* shove it back into the string */ g_memmove(next_pos, unicode_char_str, unicode_char_len); /* move all the data after the \u0000 along */ g_stpcpy(next_pos + unicode_char_len, next_pos + 6); } output_string = g_strcompress(input_string); g_free(input_string); return output_string; }
static gboolean option_print_cb (const gchar *option_name, const gchar *value, gpointer data, GError **error) { const char *p; for (p = value; *p; p = g_utf8_next_char (p)) { gunichar c; char utf[7]; c = g_utf8_get_char (p); if (c == (gunichar)-1) continue; utf[g_unichar_to_utf8 (c, utf)] = '\0'; g_print("%s\tU+%04X\t%s\n", utf, c, gucharmap_get_unicode_name (c)); } exit (EXIT_SUCCESS); return FALSE; }
static gchar* enchant_utf8_strtitle(const gchar*str, gssize len) { gunichar title_case_char; gchar* result; gchar* upperStr, * upperTail, * lowerTail; gchar title_case_utf8[7]; gint utf8len; upperStr = g_utf8_strup(str, len); /* for locale sensitive casing */ title_case_char = g_unichar_totitle(g_utf8_get_char(upperStr)); utf8len = g_unichar_to_utf8(title_case_char, title_case_utf8); title_case_utf8[utf8len] = '\0'; upperTail = g_utf8_next_char(upperStr); lowerTail = g_utf8_strdown(upperTail, -1); result = g_strconcat(title_case_utf8, lowerTail, NULL); g_free(upperStr); g_free(lowerTail); return result; }
static guchar* rhk_sm3_read_string(const guchar **buffer, gsize len) { gchar *s, *p; guint i, n; gunichar chr; if (len < 2) return NULL; n = gwy_get_guint16_le(buffer); len -= 2; if (len < 2*n) return NULL; if (!n) return g_strdup(""); p = s = g_new(gchar, 6*n + 1); for (i = 0; i < n; i++) { chr = gwy_get_guint16_le(buffer); p += g_unichar_to_utf8(chr, p); } *p = '\0'; g_strstrip(s); gwy_debug("String: <%s>", s); return s; }
static PangoGlyph pango_font_get_glyph(PangoFont* font, PangoContext* context, gunichar wc) { PangoGlyph result = 0; gchar buffer[7]; gint length = g_unichar_to_utf8(wc, buffer); g_return_val_if_fail(length, 0); GList* items = pango_itemize(context, buffer, 0, length, NULL, NULL); if (g_list_length(items) == 1) { PangoItem* item = reinterpret_cast<PangoItem*>(items->data); PangoFont* tmpFont = item->analysis.font; item->analysis.font = font; PangoGlyphString* glyphs = pango_glyph_string_new(); pango_shape(buffer, length, &item->analysis, glyphs); item->analysis.font = tmpFont; if (glyphs->num_glyphs == 1) result = glyphs->glyphs[0].glyph; else g_warning("didn't get 1 glyph but %d", glyphs->num_glyphs); pango_glyph_string_free(glyphs); } g_list_foreach(items, (GFunc)pango_item_free, NULL); g_list_free(items); return result; }
static const gchar * get_invisible_text (void) { GtkWidget *entry; gunichar invisible_char; static gchar invisible_text[40]; gchar *p; gint i; entry = gtk_entry_new (); invisible_char = gtk_entry_get_invisible_char (GTK_ENTRY (entry)); if (invisible_char == 0) invisible_char = 0x2022; g_object_ref_sink (entry); g_object_unref (entry); /* five bullets */ p = invisible_text; for (i = 0; i < 5; i++) p += g_unichar_to_utf8 (invisible_char, p); *p = 0; return invisible_text; }
int plugin_emit_keypress (session *sess, unsigned int state, unsigned int keyval, gunichar key) { char *word[PDIWORDS]; char keyval_str[16]; char state_str[16]; char len_str[16]; char key_str[7]; int i, len; if (!hook_list) return 0; sprintf (keyval_str, "%u", keyval); sprintf (state_str, "%u", state); if (!key) len = 0; else len = g_unichar_to_utf8 (key, key_str); key_str[len] = '\0'; sprintf (len_str, "%d", len); word[0] = "Key Press"; word[1] = keyval_str; word[2] = state_str; word[3] = key_str; word[4] = len_str; for (i = 5; i < PDIWORDS; i++) word[i] = "\000"; return plugin_hook_run (sess, word[0], word, NULL, NULL, HOOK_PRINT); }
static void _vte_pango_x_draw_text(struct _vte_draw *draw, struct _vte_draw_text_request *requests, gsize n_requests, GdkColor *color, guchar alpha) { Display *display; GC gc; struct _vte_pango_x_data *data; char buf[VTE_UTF8_BPC]; gsize i, length; GdkColor wcolor; data = (struct _vte_pango_x_data*) draw->impl_data; wcolor = *color; gdk_rgb_find_color(gdk_drawable_get_colormap(draw->widget->window), &wcolor); gdk_gc_set_foreground(data->gc, &wcolor); display = gdk_x11_drawable_get_xdisplay(draw->widget->window); gc = gdk_x11_gc_get_xgc(data->gc); for (i = 0; i < n_requests; i++) { length = g_unichar_to_utf8(requests[i].c, buf); pango_layout_set_text(data->layout, buf, length); pango_x_render_layout(display, data->drawable, gc, data->layout, requests[i].x - data->x_offs, requests[i].y - data->y_offs); } }
unsigned utf8_width(const char *str) { assert(str != NULL); #if defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED) return g_utf8_strlen(str, -1); #else #ifdef HAVE_CURSES_ENHANCED if (g_utf8_validate(str, -1, NULL)) { size_t len = g_utf8_strlen(str, -1); unsigned width = 0; gunichar c; while (len--) { c = g_utf8_get_char(str); width += unicode_char_width(c); str += g_unichar_to_utf8(c, NULL); } return width; } else #endif return strlen(str); #endif }
static char * random_utf8_string (void *ctx, size_t char_count) { size_t offset = 0; size_t i; gchar *buf = NULL; size_t buf_size = 0; for (i = 0; i < char_count; i++) { gunichar randomchar; size_t written; /* 6 for one glyph, one for null, one for luck */ while (buf_size <= offset + 8) { buf_size = 2 * buf_size + 8; buf = talloc_realloc (ctx, buf, gchar, buf_size); } do { randomchar = random_unichar (); } while (randomchar == '\n'); written = g_unichar_to_utf8 (randomchar, buf + offset); if (written <= 0) { fprintf (stderr, "error converting to utf8\n"); exit (1); } offset += written; } buf[offset] = 0; return buf; }
static void charprop_reset_widget(CharProperty *prop, WIDGET *widget) { gchar ch[7]; int unilen = g_unichar_to_utf8 (prop->char_data, ch); ch[unilen] = 0; gtk_entry_set_text(GTK_ENTRY(widget), ch); }
static VALUE rg_char(VALUE self) { gchar buf[10]; gint len = g_unichar_to_utf8(gtk_text_iter_get_char(_SELF(self)), buf); buf[len] = '\0'; return CSTR2RVAL(buf); }
/* markup unescape code stolen and adapted from gmarkup.c */ static gchar * char_str (gunichar c, gchar *buf) { memset (buf, 0, 8); g_unichar_to_utf8 (c, buf); return buf; }