void keytostr(char *buf, int buf_size, int key) { int i; char buf1[32]; for (i = 0; i < (int)(sizeof(keycodes)/sizeof(keycodes[0])); i++) { if (keycodes[i] == key) { pstrcpy(buf, buf_size, keystr[i]); return; } } if (key >= KEY_META(0) && key <= KEY_META(0xff)) { keytostr(buf1, sizeof(buf1), key & 0xff); snprintf(buf, buf_size, "M-%s", buf1); } else if (key >= KEY_CTRL('a') && key <= KEY_CTRL('z')) { snprintf(buf, buf_size, "C-%c", key + 'a' - 1); } else if (key >= KEY_F1 && key <= KEY_F20) { snprintf(buf, buf_size, "f%d", key - KEY_F1 + 1); } else if (key > 32 && key < 127 && buf_size >= 2) { buf[0] = key; buf[1] = '\0'; } else { char *q; q = utf8_encode(buf1, key); *q = '\0'; pstrcpy(buf, buf_size, buf1); } }
static void encode_string(std::ostream& os, std::string s) { std::vector<uint32_t> str = utf8_decode(s); std::vector<uint32_t>::iterator it; os << "\""; for (it = str.begin(); it != str.end(); it++) { uint32_t c = *it; switch(c) { case '"': os << "\\\""; break; case '\\': os << "\\\\"; break; case '/': os << "\\/"; break; case '\b': os << "\\b"; break; case '\f': os << "\\f"; break; case '\n': os << "\\n"; break; case '\r': os << "\\r"; break; case '\t': os << "\\t"; break; default: utf8_encode(c, os); break; } } os << "\""; }
static void add_tag(vorbis_comment *vc, oe_options *opt,char *name, char *value) { char *utf8; if (opt->isutf8) { if (!utf8_validate(value)) { fprintf(stderr, _("'%s' is not valid UTF-8, cannot add\n"), name?name:"comment"); } else { if(name == NULL) { vorbis_comment_add(vc, value); } else { vorbis_comment_add_tag(vc, name, value); } } } else if(utf8_encode(value, &utf8) >= 0) { if(name == NULL) { vorbis_comment_add(vc, utf8); } else { vorbis_comment_add_tag(vc, name, utf8); } free(utf8); } else { fprintf(stderr, _("Couldn't convert comment to UTF-8, cannot add\n")); } }
void SetChatTopic(const TCHAR *szChatId, TCHAR *szTopic, BOOL bSet) { MCONTACT hContact = find_chat (szChatId); char *szUTFTopic; GCDEST gcd = { SKYPE_PROTONAME, szChatId, GC_EVENT_TOPIC }; GCEVENT gce = { sizeof(gce), &gcd }; gce.ptszText = szTopic; gce.dwFlags = GCEF_ADDTOLOG; gce.time = (DWORD)time (NULL); CallService(MS_GC_EVENT, 0, (LPARAM)&gce); gcd.iType = GC_EVENT_SETSBTEXT; CallService(MS_GC_EVENT, 0, (LPARAM)&gce); if (bSet) { #ifdef _UNICODE szUTFTopic=(char*)make_utf8_string(szTopic); #else if (utf8_encode(szTopic, &szUTFTopic)==-1) szUTFTopic = NULL; #endif if (szUTFTopic) { SkypeSend ("ALTER CHAT "STR" SETTOPIC %s", szChatId, szUTFTopic); free (szUTFTopic); } testfor ("ALTER CHAT SETTOPIC", INFINITE); } if (hContact) db_set_ts(hContact, SKYPE_PROTONAME, "Nick", szTopic); }
String::String(rune code) { rune r[2] = { code, 0 }; s_len = s_cap = 0; s_data = nullptr; grow(utf8_encoded_len(r) + 1); utf8_encode(r, s_data, s_cap); s_len = utf8_len(s_data); }
void utf8_writebom( FILE *outptr ) { int i, nc; unsigned char code[6]; nc = utf8_encode( 0xFEFF, code ); for ( i=0; i<nc; ++i ) fprintf(outptr,"%c",code[i]); }
static int _pipe_utf8x(char **out, unsigned short x) { unsigned char buf[3]; int len = utf8_encode(x, buf); if(*out != NULL) { memcpy(*out, buf, len); *out += len; } return len; }
static inline void strbuf_addch(StringBuffer *b, int ch) { const char *p; if (b->size < b->allocated_size) { /* fast case */ p = utf8_encode((char*)b->buf + b->size, ch); b->size = p - (char *)b->buf; } else { strbuf_addch1(b, ch); } }
static inline short_string_t utf8_char(guint32 codepoint) { short_string_t buf; unsigned len; STATIC_ASSERT(sizeof buf.str > 4); len = utf8_encode(codepoint, buf.str); buf.str[len] = '\0'; return buf; }
std::string getCurrentWorkingDirectory() { #if defined(OS_WIN) wchar_t lpBuffer[MAX_PATH]; ::GetCurrentDirectoryW(MAX_PATH, lpBuffer); return utf8_encode(lpBuffer); #else char result[2*4096]; return ::getcwd(result, sizeof(result)); #endif // OS_WIN }
static void addutf8char( str *s, unsigned int ch, int xmlout ) { unsigned char code[6]; int nc, i; if ( xmlout ) { if ( minimalxmlchars( s, ch ) ) return; if ( ch > 127 && xmlout == STR_CONV_XMLOUT_ENTITIES ) { addentity( s, ch ); return; } } nc = utf8_encode( ch, code ); for ( i=0; i<nc; ++i ) str_addchar( s, code[i] ); }
String::String(const rune *r) { if (r == nullptr) { s_len = 0; s_cap = kSmallestString; s_data = new char[s_cap]; *s_data = 0; } else { s_len = s_cap = 0; s_data = nullptr; grow(utf8_encoded_len(r) + 1); utf8_encode(r, s_data, s_cap); s_len = utf8_len(s_data); } }
static int do_search(struct searchable *s, struct iter *iter, const char *text, enum search_direction dir, int skip_current) { char *u_text = NULL; int r; /* search text is always in locale encoding (because cmdline is) */ if (!using_utf8 && utf8_encode(text, charset, &u_text) == 0) text = u_text; r = do_u_search(s, iter, text, dir, skip_current); free(u_text); return r; }
/* Given a Unicode character, finds the corresponding index on the spool, if any. Returns 0 if not found. */ static int find_index (ModeInfo *mi, flapper *f, long uc) { char string[5]; int L = utf8_encode (uc, string, sizeof(string) - 1); int i; if (L <= 0) return 0; string[L] = 0; for (i = 0; i < f->spool_size; i++) { if (!strcmp (string, f->spool[i])) return i; } return 0; }
GNOKII_API char * gn_todo2icalstr(gn_todo *ctodo) { #ifdef HAVE_LIBICAL icalcomponent *pIcal = NULL; char compuid[64]; snprintf(compuid, sizeof(compuid), "guid.gnokii.org_%d_%d", ctodo->location, rand()); pIcal = icalcomponent_vanew(ICAL_VCALENDAR_COMPONENT, icalproperty_new_version("2.0"), icalproperty_new_prodid(get_prodid()), icalcomponent_vanew(ICAL_VTODO_COMPONENT, icalproperty_new_categories("GNOKII"), icalproperty_new_priority(ctodo->priority), icalproperty_new_summary(ctodo->text), 0), 0); if (pIcal) { char *icalstrbuf = NULL; const char *icalstr = icalcomponent_as_ical_string(pIcal); /* conversion to UTF-8 */ if (string_base64(icalstr)) { int icalstrlen = strlen(icalstr); icalstrbuf = malloc(icalstrlen * 2 + 1); utf8_encode(icalstrbuf, icalstrlen * 2, icalstr, icalstrlen); } else { icalstrbuf = strdup (icalstr); } icalcomponent_free(pIcal); return icalstrbuf; } return NULL; #else ical_string str; memset(&str, 0, sizeof(str)); ical_append_printf(&str, "BEGIN:VCALENDAR\r\n"); ical_append_printf(&str, "VERSION:1.0\r\n"); ical_append_printf(&str, "BEGIN:VTODO\r\n"); ical_append_printf(&str, "PRIORITY:%i\r\n", ctodo->priority); ical_append_printf(&str, "SUMMARY:%s\r\n", ctodo->text); ical_append_printf(&str, "END:VTODO\r\n"); ical_append_printf(&str, "END:VCALENDAR\r\n"); return str.str; #endif /* HAVE_LIBICAL */ }
void EditorScreen::LoadMap() { HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { IFileOpenDialog *pFileOpen = NULL; HRESULT hr = CoCreateInstance(__uuidof(FileOpenDialog), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFileOpen)); if (SUCCEEDED(hr)) { hr = pFileOpen->SetDefaultExtension(L"xml"); hr = pFileOpen->SetFileTypes(ARRAYSIZE(c_rgSaveTypes), c_rgSaveTypes); hr = pFileOpen->Show(NULL); if (SUCCEEDED(hr)) { IShellItem *pItem; hr = pFileOpen->GetResult(&pItem); if (SUCCEEDED(hr)) { PWSTR pszFilePath; hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); // Display the file name to the user. if (SUCCEEDED(hr)) { // Reset current Map information ResetMap(); std::string filePath = utf8_encode(pszFilePath); map->LoadMap(filePath); CoTaskMemFree(pszFilePath); } pItem->Release(); } } pFileOpen->Release(); } CoUninitialize(); } }
void PrintTokens(UChar **tokens, int num_tokens, long *toklen, bool ascii, bool html) { if (html) printf("<html>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<head>\n<style> .token { border: 1px solid gray;background: #f0ffff;}</style>\n</head>\n<body>\n"); for (int i=0;i<num_tokens;i++){ if (html) printf("<span class=\"token\">"); else printf("Token %d: ",i); UChar *tok = tokens[i]; int tlen = toklen[i]; // Doesn't account for 4-byte unicode yet for (int j=0;j<tlen;j++){ if (ascii){ if (tok[j]>=0x20 && tok[j] < 0x80) printf("%c", (char)tok[j]); else printf("[u+%02x]",(int)tok[j]); } else{ char code_point[4]; int num_bytes = utf8_encode((u_long)tok[j], code_point); if (html){ if (code_point[0] == '<') printf("<"); else if (code_point[0] == '>') printf(">"); else if (code_point[0] == '&') printf("&"); else { for(int k=0;k<num_bytes;k++){ putchar(code_point[k]); } } } else for(int k=0;k<num_bytes;k++){ putchar(code_point[k]); } } } if (html) printf("</span>"); printf("\n"); } if (html) printf("</body>\n</html>\n"); }
static void draw_unichar (ModeInfo *mi) { unicrud_configuration *bp = &bps[MI_SCREEN(mi)]; char text[10]; char title[400]; XCharStruct e; int w, h, i, j; GLfloat s; i = utf8_encode (bp->unichar, text, sizeof(text) - 1); text[i] = 0; *title = 0; sprintf (title + strlen(title), "Plane:\t%s\n", bp->charplane); sprintf (title + strlen(title), "Block:\t%s\n", bp->charblock); # ifdef HAVE_JWXYZ sprintf (title + strlen(title), "Name:\t%s\n", (bp->charname ? bp->charname : "")); #endif sprintf (title + strlen(title), "Unicode:\t%04lX\n", bp->unichar); sprintf (title + strlen(title), "UTF-8:\t"); for (j = 0; j < i; j++) sprintf (title + strlen(title), "%02X ", ((unsigned char *)text)[j]); texture_string_metrics (bp->char_font, text, &e, 0, 0); w = e.width; h = e.ascent; s = 9; glScalef (s, s, s); s = 1.0 / (h > w ? h : w); /* Scale to unit */ glScalef (s, s, s); glTranslatef (-w/2, -h/2, 0); glColor4fv (bp->color); print_texture_string (bp->char_font, text); glColor3f (1, 1, 0); if (do_titles) print_texture_label (mi->dpy, bp->title_font, mi->xgwa.width, mi->xgwa.height, 1, title); }
String& String::operator+=(rune code) { if (!code) { return *this; } size_t n = utf8_encoded_size(code); size_t l = std::strlen(s_data); grow(l + n + 1); rune r[2] = { code, 0 }; utf8_encode(r, s_data + l, n + 1); s_len++; return *this; }
int add_comment(char *line, vorbis_comment *vc, int raw) { char *mark, *value, *utf8_value; /* strip any terminal newline */ { int len = strlen(line); if (line[len-1] == '\n') line[len-1] = '\0'; } /* validation: basically, we assume it's a tag * if it has an '=' after one or more valid characters, * as the comment spec requires. For the moment, we * also restrict ourselves to 0-terminated values */ mark = strchr(line, '='); if (mark == NULL) return -1; value = line; while (value < mark) { if(*value < 0x20 || *value > 0x7d || *value == 0x3d) return -1; value++; } /* split the line by turning the '=' in to a null */ *mark = '\0'; value++; if(raw) { vorbis_comment_add_tag(vc, line, value); return 0; } /* convert the value from the native charset to UTF-8 */ else if (utf8_encode(value, &utf8_value) >= 0) { /* append the comment and return */ vorbis_comment_add_tag(vc, line, utf8_value); free(utf8_value); return 0; } else { fprintf(stderr, _("Couldn't convert comment to UTF8, " "cannot add\n")); return -1; } }
static void html_render_entity(struct render_context *ctx, const struct array entity) { uint32 c; c = html_parse_entity(entity); if ((uint32)-1 == c) { html_output_print(ctx->output, array_from_string("&")); html_output_print(ctx->output, array_init(entity.data, entity.size)); html_output_print(ctx->output, array_from_string(";")); } else { size_t len; char buf[4]; len = utf8_encode(c, buf); html_output_print(ctx->output, array_init(buf, len)); } }
static int read_string(readstat_por_ctx_t *ctx, char *data, size_t len) { int retval = 0; double value; retval = read_double(ctx, &value); if (retval != 0) return retval; size_t string_length = (size_t)value; if (string_length <= 0 || string_length > 20000) return -1; if (string_length > ctx->string_buffer_len) { ctx->string_buffer_len = string_length; ctx->string_buffer = realloc(ctx->string_buffer, ctx->string_buffer_len); } int bytes_cached = 0, bytes_copied = 0; if (ctx->buf_used - ctx->buf_pos > 0) { bytes_cached = ctx->buf_used - ctx->buf_pos; if (bytes_cached <= string_length) { memcpy(data, &ctx->buf[ctx->buf_pos], bytes_cached); ctx->buf_pos = 0; ctx->buf_used = 0; bytes_copied = bytes_cached; } else { memcpy(data, &ctx->buf[ctx->buf_pos], string_length); ctx->buf_pos += string_length; bytes_copied = string_length; } } size_t bytes_encoded = 0; if (string_length > bytes_copied) { if (read_bytes(ctx, ctx->string_buffer, string_length - bytes_copied) == -1) { return -1; } bytes_encoded = utf8_encode(ctx->string_buffer, string_length - bytes_copied, data + bytes_copied, len - bytes_copied -1, ctx->lookup); } data[bytes_copied + bytes_encoded] = '\0'; // printf("Read string: %s\n", data); return bytes_encoded; }
// default argument sep = ' ' String String::join(const Array<String>& a, rune sep) const { if (!a.len()) { return String(); } // first calculate total size size_t rune_size = utf8_encoded_size(sep); size_t total = 0; const char *p; for(size_t i = 0; i < a.len(); i++) { p = a[i].c_str(); if (p == nullptr) { throw ReferenceError(); } total += std::strlen(p) + rune_size; } total -= rune_size; // make the joined string String s; s.grow(total + 1); s.s_data[0] = 0; if (a[0].s_data != nullptr) { std::strcpy(s.s_data, a[0].s_data); } rune r[2] = { sep, 0 }; char rs[5]; utf8_encode(r, rs, sizeof(rs)); for(size_t i = 1; i < a.len(); i++) { std::strcat(s.s_data, rs); if (a[i].s_data) { std::strcat(s.s_data, a[i].s_data); } } s.s_len = utf8_len(s.s_data); return s; }
static char *get_property_value_byname(IAppHostElement* ahe, VARIANT* value, BSTR* name, VARTYPE type) { IAppHostProperty *property = NULL; HRESULT hresult; char *ret = NULL; hresult = IAppHostElement_GetPropertyByName(ahe, *name, &property); if (FAILED(hresult) || property == NULL) { fwprintf(stderr, L"get_property_value_byname(%s) failed. Property not found.\n", *name); return NULL; } hresult = IAppHostProperty_get_Value(property, value); if (FAILED(hresult)) { fwprintf(stderr, L"get_property_value_byname(%s) failed. Value not set.\n", *name); if (property != NULL) IAppHostProperty_Release(property); return NULL; } if (value->vt != type) { fwprintf(stderr, L"get_property_value_byname(%s) failed. Property type %d differs from type expected %d.", *name, value->vt, type); if (property != NULL) IAppHostProperty_Release(property); return NULL; } if (type == VT_UI4) { char dec[256]; sprintf_s(dec, sizeof (dec), "%d", value->ulVal); ret = strdup(dec); } else { UINT l = SysStringLen(value->bstrVal); if (l > 0) { wchar_t *tmp = (LPWSTR) malloc(l * sizeof (wchar_t) + 2); if (tmp) { memcpy(tmp, value->bstrVal, l * sizeof (wchar_t)); tmp[l] = 0; ret = utf8_encode(tmp, NULL); free(tmp); } } } if (property != NULL) IAppHostProperty_Release(property); return ret; }
static void strbuf_addch1(StringBuffer *b, int ch) { const char *p; int size1; unsigned char *ptr; size1 = b->allocated_size + STRING_BUF_SIZE; ptr = b->buf; if (b->buf == b->buf1) ptr = NULL; ptr = realloc(ptr, size1); if (ptr) { if (b->buf == b->buf1) memcpy(ptr, b->buf1, STRING_BUF_SIZE); b->buf = ptr; b->allocated_size = size1; p = utf8_encode((char*)b->buf + b->size, ch); b->size = p - (char *)b->buf; } }
static int read_double(readstat_por_ctx_t *ctx, double *out_double) { size_t bytes_read = 0; if (ctx->buf_used == 0) { bytes_read = read_bytes(ctx, ctx->buf, 100); if (bytes_read == -1) { return -1; } ctx->buf_used = bytes_read; ctx->buf_pos = 0; } if (ctx->buf_used - ctx->buf_pos < 40) { memcpy(ctx->buf, &ctx->buf[ctx->buf_pos], ctx->buf_used - ctx->buf_pos); ctx->buf_used -= ctx->buf_pos; ctx->buf_pos = 0; bytes_read = read_bytes(ctx, &ctx->buf[ctx->buf_used], sizeof(ctx->buf)-ctx->buf_used); if (bytes_read == -1) { return -1; } ctx->buf_used += bytes_read; } char utf8_buffer[300]; size_t len = utf8_encode(ctx->buf + ctx->buf_pos, ctx->buf_used - ctx->buf_pos, utf8_buffer, sizeof(utf8_buffer), ctx->lookup); if (utf8_buffer[0] == 'Z') return 1; double value; bytes_read = readstat_por_parse_double(utf8_buffer, len, &value, ctx->error_handler, ctx->user_ctx); if (bytes_read == -1) { return -1; } ctx->buf_pos += bytes_read; if (out_double) { *out_double = value; } // printf("Read double: %lf\n", value); return 0; }
void utf8_regression_2(void) { uint32_t i; for (i = 0; i < 0x10ffffU; i++) { static char buf[5]; uint32_t d; unsigned n; memset(buf, 0, sizeof buf); n = utf8_encode(i, buf); RUNTIME_ASSERT(n <= 4); if (utf32_is_surrogate(i) || utf32_is_non_character(i)) { if (0 != n) { printf("i=U+%x, n=%u\n", n, i); RUNTIME_ASSERT(0 == n); } continue; } if (n <= 0) { printf("n=%u\n", n); RUNTIME_ASSERT(n > 0); } d = utf8_decode(buf, n); if (d != i) { printf("buf: %02x %02x %02x %02x\n", (unsigned char) buf[0], (unsigned char) buf[1], (unsigned char) buf[2], (unsigned char) buf[3]); printf("i: U+%lx\n", (unsigned long) i); printf("d: U+%lx\n", (unsigned long) d); printf("n: %u\n", n); RUNTIME_ASSERT(d == i); } } printf("PASSED\n"); }
std::string getTempDirectory() { #if defined(OS_WIN) wchar_t lpBuffer[MAX_PATH]; ::GetTempPathW(MAX_PATH, lpBuffer); return utf8_encode(lpBuffer); #else const char* vars[4] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; for (int i = 0; i < 4; ++i) { char* val = std::getenv(vars[i]); if (val) { auto p = std::string(val); if (p.back() != '/') { p += '/'; } return p; } } return "/tmp/"; #endif // OS_WIN }
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (uMsg == WM_DEVICECHANGE) { hid_event_monitor* monitor = (hid_event_monitor*)GetWindowLongPtr(hwnd, GWLP_USERDATA); PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)lParam; PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE)lpdb; monitor->proc_real_event = false; if (lpdb->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) return TRUE; if (wParam != DBT_DEVICEARRIVAL && wParam != DBT_DEVICEREMOVECOMPLETE) return TRUE; monitor->proc_real_event = true; if (wParam == DBT_DEVICEARRIVAL) { monitor->proc_event.action = EVENT_ADD; } else if (wParam == DBT_DEVICEREMOVECOMPLETE) { monitor->proc_event.action = EVENT_REMOVE; } //TODO: clean this up std::string ansi_string = utf8_encode(lpdbv->dbcc_name); auto str = ansi_string.c_str(); auto len = strlen(str); monitor->proc_event.device_info->path = (char*) calloc(len+1, sizeof(char)); strncpy(monitor->proc_event.device_info->path, str, len+1); monitor->proc_event.device_info->path[len] = '\0'; return TRUE; //TODO: check this } return DefWindowProc(hwnd, uMsg, wParam, lParam); }
void keytostr(char *buf, int buf_size, int key) { int i; char buf1[32]; for(i=0;i<sizeof(keycodes)/sizeof(keycodes[0]);i++) { if (keycodes[i] == key) { pstrcpy(buf, buf_size, keystr[i]); return; } } if (key >= KEY_META(' ') && key <= KEY_META(127)) { keytostr(buf1, sizeof(buf1), key & 0xff); snprintf(buf, buf_size, "M-%s", buf1); } else if (key >= 1 && key <= 31) { snprintf(buf, buf_size, "C-%c", key + 'a' - 1); } else if (key >= KEY_F1 && key <= KEY_F12) { snprintf(buf, buf_size, "F%d", key - KEY_F1 + 1); } else { char *q; q = utf8_encode(buf, key); *q = '\0'; } }