static int downcase_is_in_dict(Dictionary dict, char * word) { int i, rc; char low[MB_LEN_MAX]; char save[MB_LEN_MAX]; wchar_t c; int nbl, nbh; if (!is_utf8_upper(word)) return FALSE; nbh = mbtowc (&c, word, 4); c = towlower(c); nbl = wctomb(low, c); if (nbh != nbl) { fprintf(stderr, "Error: can't downcase multi-byte string!\n"); return FALSE; } /* Downcase */ for (i=0; i<nbl; i++) { save[i] = word[i]; word[i] = low[i]; } /* Look it up, then restore old value */ rc = boolean_dictionary_lookup(dict, word); for (i=0; i<nbh; i++) { word[i] = save[i]; } return rc; }
bool CInputField::ValidChar(wchar_t *ch) { if (IsTAB(*ch) || !iswprint(*ch)) return false; if ((m_eInputType == INT) || (m_eInputType == FLOAT)) { lconv *lc = localeconv(); if (strchr(lc->decimal_point, ',')) { if (*ch == L'.') *ch = L','; } std::string legal = LegalNrTokens((m_eInputType == FLOAT), m_Text, GetStrPosition()); char mb[MB_CUR_MAX]; int ret = wctomb(mb, *ch); mb[ret] = 0; if (legal.find(mb) == std::string::npos) return false; // Illegal char } return true; }
/* c is a multibyte non-control character. We return that multibyte * character. If crep is an invalid multibyte sequence, it will be * replaced with Unicode 0xFFFD (Replacement Character). */ char *mbrep(const char *c, char *crep, int *crep_len) { assert(c != NULL && crep != NULL && crep_len != NULL); #ifdef ENABLE_UTF8 if (use_utf8) { wchar_t wc; /* Reject invalid Unicode characters. */ if (mbtowc(&wc, c, MB_CUR_MAX) < 0 || !is_valid_unicode(wc)) { mbtowc_reset(); *crep_len = bad_mbchar_len; strncpy(crep, bad_mbchar, *crep_len); } else { *crep_len = wctomb(crep, wc); if (*crep_len < 0) { wctomb_reset(); *crep_len = 0; } } } else { #endif *crep_len = 1; *crep = *c; #ifdef ENABLE_UTF8 } #endif return crep; }
/* Convert the Unicode value in chr to a multibyte character with the * same wide character value as chr, if possible. If the conversion * succeeds, return the (dynamically allocated) multibyte character and * its length. Otherwise, return an undefined (dynamically allocated) * multibyte character and a length of zero. */ char *make_mbchar(long chr, int *chr_mb_len) { char *chr_mb; assert(chr_mb_len != NULL); #ifdef ENABLE_UTF8 if (use_utf8) { chr_mb = charalloc(MB_CUR_MAX); *chr_mb_len = wctomb(chr_mb, (wchar_t)chr); /* Reject invalid Unicode characters. */ if (*chr_mb_len < 0 || !is_valid_unicode((wchar_t)chr)) { wctomb_reset(); *chr_mb_len = 0; } } else { #endif *chr_mb_len = 1; chr_mb = mallocstrncpy(NULL, (char *)&chr, 1); #ifdef ENABLE_UTF8 } #endif return chr_mb; }
static void cswitch(char *dst, int *dn, const char *src, int *sn) { int c; #ifdef MB if (mb_cur_max > 1) { nextc(c, src, *sn); if (c & INVBIT) { *dst = *src; *dn = *sn = 1; } else { if (iswupper(c)) c = towlower(c); else if (iswlower(c)) c = towupper(c); if ((*dn = wctomb(dst, c)) > *sn) { *dst = *src; *dn = *sn = 1; } } } else #endif /* MB */ { c = *src & 0377; if (isupper(c)) *dst = tolower(c); else if (islower(c)) *dst = toupper(c); else *dst = c; *dn = *sn = 1; } }
/* c is a multibyte control character. It displays as ^@, ^?, or ^[ch], * where ch is (c + 64). We return that multibyte character. If crep * is an invalid multibyte sequence, it will be replaced with Unicode * 0xFFFD (Replacement Character). */ char *control_mbrep(const char *c, char *crep, int *crep_len) { assert(c != NULL && crep != NULL && crep_len != NULL); #ifdef ENABLE_UTF8 if (use_utf8) { wchar_t wc; if (mbtowc(&wc, c, MB_CUR_MAX) < 0) { mbtowc_reset(); *crep_len = bad_mbchar_len; strncpy(crep, bad_mbchar, *crep_len); } else { *crep_len = wctomb(crep, control_wrep(wc)); if (*crep_len < 0) { wctomb_reset(); *crep_len = 0; } } } else { #endif *crep_len = 1; *crep = control_rep(*c); #ifdef ENABLE_UTF8 } #endif return crep; }
//(None-windows-specific) Convert wchar_* to string void CTextFileBase::ConvertWcharToString(const wchar_t* from, string &to, bool* datalost, char unknownchar) { to = ""; char* temp = new char[MB_CUR_MAX]; while(*from != L'\0') { size_t len = wctomb(temp, *from); //Found end if(len == 0) break; else if(len == (size_t)-1) { //Replace with unknown character to += unknownchar; if(datalost != NULL) *datalost=true; } else { //Copy all characters for(size_t i=0; i<len; i++) to += temp[i]; } from++; } delete [] temp; }
static void write_html_escape_char(ang_file *fp, wchar_t c) { switch (c) { case L'<': file_putf(fp, "<"); break; case L'>': file_putf(fp, ">"); break; case L'&': file_putf(fp, "&"); break; default: { char *mbseq = (char*) mem_alloc(sizeof(char)*(MB_CUR_MAX+1)); byte len; len = wctomb(mbseq, c); if (len > MB_CUR_MAX) len = MB_CUR_MAX; mbseq[len] = '\0'; file_putf(fp, "%s", mbseq); mem_free(mbseq); break; } } }
CBasicEventListener::EAction CBasicEventListener::OnChar(HWND hWnd, WPARAM wParam) { if (gEnv && gEnv->pInput) { // TODO: This code doesn't belong here, should be integrated into CKeyboard to make OnInputEvent and OnInputEventUI consistent across all devices and when in the editor SInputEvent event; event.modifiers = gEnv->pInput->GetModifiers(); event.deviceId = eDI_Keyboard; event.state = eIS_UI; event.value = 1.0f; event.pSymbol = 0;//m_rawKeyboard->GetSymbol((lParam>>16)&0xff); if (event.pSymbol) event.keyId = event.pSymbol->keyId; wchar_t tmp[2] = { 0 }; MultiByteToWideChar(CP_ACP, 0, (char*)&wParam, 1, tmp, 2); char szKeyName[4] = {0}; if (wctomb(szKeyName, (WCHAR)wParam) != -1) { if (szKeyName[1]==0 && ((unsigned char)szKeyName[0])>=32) { event.inputChar = tmp[0]; event.keyName = szKeyName; gEnv->pInput->PostInputEvent(event); } } } return eA_Default; }
std::ostream & operator<<(std::ostream & o, wchar_t const c) { char buf[MB_LEN_MAX]; int const i = wctomb(buf, c); if(i < 0) o << '?'; else std::copy(buf, buf + i, std::ostreambuf_iterator<char>(o)); return o; }
size_t wcrtomb(FAR char *s, wchar_t wc, FAR mbstate_t *ps) { int retval = 0; char buf[10]; if (s == NULL) { retval = wctomb((char *)buf, L'\0'); } else { retval = wctomb(s, wc); } if (retval == -1) { return (size_t)(-1); } else { return (size_t) retval; } }
/** * @internal walk the given UTF8 string, looking for non-ASCII characters. * @return 0 if none were found, or, if non-ASCII strings were found, * answer the length of the buffer if it were converted to platform * encoding * * @note this relies on the assumption that wide chars are Unicode. * If not, the platform will need different support for this */ static IDATA walkUTF8String (const U_8 * buf, IDATA nbytes) { const U_8 *end = buf + nbytes; const U_8 *cursor = buf; IDATA newLength = 0; int hasHighChars = 0; /* reset the shift state */ wctomb (NULL, 0); while (cursor < end) { if ((*cursor & 0x80) == 0x80) { char temp[MB_CUR_MAX]; int wcresult; U_16 unicode; U_32 numberU8Consumed = decodeUTF8CharN (cursor, &unicode, end - cursor); if (numberU8Consumed == 0) { /* an illegal encoding was encountered! Don't try to decode the string */ return 0; } cursor += numberU8Consumed; /* calculate the encoded length of this character */ wcresult = wctomb (temp, (wchar_t) unicode); if (wcresult == -1) { /* an un-encodable char was encountered */ newLength += 1; } else { newLength += wcresult; } hasHighChars = 1; } else { newLength += 1; cursor += 1; } } return hasHighChars ? newLength : 0; }
static void h_mbtowc(const char *locale, const char *illegal, const char *legal) { char buf[64]; size_t stateful, ret; char *str; ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); #ifdef __NetBSD__ ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL); #else if (setlocale(LC_CTYPE, locale) == NULL) { fprintf(stderr, "Locale %s not found.\n", locale); return; } #endif ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); (void)printf("Using locale: %s\n", str); stateful = wctomb(NULL, L'\0'); (void)printf("Locale is state-%sdependent\n", stateful ? "in" : ""); /* initialize internal state */ ret = mbtowc(NULL, NULL, 0); ATF_REQUIRE(stateful ? ret : !ret); (void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL); (void)printf("Checking illegal sequence: \"%s\"\n", buf); ret = mbtowc(NULL, illegal, strlen(illegal)); (void)printf("mbtowc() returned: %zd\n", ret); ATF_REQUIRE_EQ(ret, (size_t)-1); (void)printf("errno: %s\n", strerror(errno)); ATF_REQUIRE_EQ(errno, EILSEQ); /* if this is stateless encoding, this re-initialization is not required. */ if (stateful) { /* re-initialize internal state */ ret = mbtowc(NULL, NULL, 0); ATF_REQUIRE(stateful ? ret : !ret); } /* valid multibyte sequence case */ (void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL); (void)printf("Checking legal sequence: \"%s\"\n", buf); errno = 0; ret = mbtowc(NULL, legal, strlen(legal)); (void)printf("mbtowc() returned: %zd\n", ret); ATF_REQUIRE(ret != (size_t)-1); (void)printf("errno: %s\n", strerror(errno)); ATF_REQUIRE_EQ(errno, 0); (void)printf("Ok.\n"); }
wint_t fputwc(wchar_t c, struct FILE *fp) { char ch[5]; wctomb(ch, c); if (fputs(ch, fp) == 0) return c; else return WEOF; }
int wctob(wint_t wc) { unsigned char pmb[MB_LEN_MAX]; if (wc == WEOF) { return EOF; } return wctomb((char *)pmb, wc) == 1 ? (int)pmb[0] : EOF; }
static int wputc(int c, FILE *f) { char buf[MB_CUR_MAX]; int i, len = wctomb(buf, c); for(i=0; i<len; i++) putc(buf[i], f); return c; }
static const char* get_character_string(int character) { static char result[6 + 1]; int length = wctomb(result, character); if (length == -1) length = 0; result[length] = '\0'; return result; }
static void PutWideChar(wchar_t const wc) { char buf[MB_CUR_MAX+1]; int len; len = wctomb(buf, wc); if (len > 0) { buf[len] = 0; fputs(buf, stdout); } }
static const char* get_character_string(int codepoint) { // This assumes UTF-8, which is stupid static char result[6 + 1]; int length = wctomb(result, codepoint); if (length == -1) length = 0; result[length] = '\0'; return result; }
void DataProvider::printCharTable() { char mbs[16]; int nBytes; for (auto it = char2int_.begin(); it != char2int_.end(); ++it) { nBytes = wctomb(mbs, it->first); printf("%lc : %x : %d : ", it->first, it->first, it->second); for (int i=0; i<nBytes; i++) { printf("%x ", 0xff & mbs[i]); } printf("\n"); } }
int wctob (wint_t wc) { char buf[64]; if (!(MB_CUR_MAX <= sizeof (buf))) abort (); if (wctomb (buf, wc) == 1) return (unsigned char) buf[0]; else return EOF; }
/* For some obscure reason, there's no standard function for this (Linux's wctombs does it, but it's not standard) */ int wcs_mb_size(const wchar_t *wstr) /* Returns: number of bytes to be allocated for a C string buffer that will successfully hold the result of wcstombs(buffer, wstr, ?), or -1 if wstr cannot be converted */ { size_t len = 0; char tmp[MB_CUR_MAX]; wctomb(NULL, 0); while (*wstr) { int mblen = wctomb(tmp, *wstr++); if (mblen < 0) return -1; len += mblen; } return len + 1; }
char * tstostr(char *to, tchar *from) { tchar *ptc; wchar_t wc; char *pmb; int len; if (to == (char *)NULL) { /* Need to xalloc(). */ int i; int i1; char junk[MB_LEN_MAX]; /* Get sum of byte counts for each char in from. */ i = 0; ptc = from; while (wc = (wchar_t)((*ptc++)&TRIM)) { if ((i1 = wctomb(junk, wc)) <= 0) { i1 = 1; } i += i1; } /* Allocate that much. */ to = (char *)xalloc(i + 1); } ptc = from; pmb = to; while (wc = (wchar_t)((*ptc++)&TRIM)) { if ((len = wctomb(pmb, wc)) <= 0) { *pmb = (unsigned char)wc; len = 1; } pmb += len; } *pmb = (char)0; return (to); }
static void putchr(wchar_t c) { char mb[MB_LEN_MAX]; int i, n; if (mb_cur_max > 1) { n = wctomb(mb, c); for (i = 0; i < n; i++) putchar(mb[i] & 0377); } else putchar(c&0377); }
int _Xaw_iswalnum(wchar_t ch) { #ifdef HAVE_ISWALNUM return iswalnum(ch); #else unsigned char mb[MB_LEN_MAX]; wctomb((char*)mb, ch); return (isalnum(*mb)); #endif }
std::string fromXMLString(const XMLString& str) { std::string result; result.reserve(str.size()); for (XMLString::const_iterator it = str.begin(); it != str.end(); ++it) { char c; wctomb(&c, *it); result += c; } return result; }
int wctob (wint_t wc) { char buf[64]; if (!(MB_CUR_MAX <= sizeof (buf))) abort (); /* Handle the case where WEOF is a value that does not fit in a wchar_t. */ if (wc == (wchar_t)wc) if (wctomb (buf, (wchar_t)wc) == 1) return (unsigned char) buf[0]; return EOF; }
ARCH_STRING::ARCH_STRING() { s_mutex = ARCH->newMutex(); #if HAVE_LOCALE_H // see if we can convert a Latin-1 character char mb[MB_LEN_MAX]; if (wctomb(mb, 0xe3) == -1) { // can't convert. try another locale so we can convert latin-1. setlocale(LC_CTYPE, "en_US"); } #endif }
static void h_wctomb(const struct test *t, char tc) { wchar_t wcs[16 + 2]; char buf[128]; char cs[MB_LEN_MAX]; const char *pcs; char *str; mbstate_t st; mbstate_t *stp = NULL; size_t sz, ret, i; ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); (void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL); (void)printf("Checking sequence: \"%s\"\n", buf); ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); (void)printf("Using locale: %s\n", str); if (tc == TC_WCRTOMB_ST) { (void)memset(&st, 0, sizeof(st)); stp = &st; } wcs[t->wclen] = L'X'; /* poison */ pcs = t->data; sz = mbsrtowcs(wcs, &pcs, t->wclen + 2, NULL); ATF_REQUIRE_EQ_MSG(sz, t->wclen, "mbsrtowcs() returned: " "%zu, expected: %zu", sz, t->wclen); ATF_REQUIRE_EQ(wcs[t->wclen], 0); for (i = 0; i < t->wclen + 1; i++) { if (tc == TC_WCTOMB) ret = wctomb(cs, wcs[i]); else ret = wcrtomb(cs, wcs[i], stp); if (ret == t->mblen[i]) continue; (void)printf("At position %zd:\n", i); (void)printf(" expected: %zd\n", t->mblen[i]); (void)printf(" got : %zd\n", ret); atf_tc_fail("Test failed"); /* NOTREACHED */ } (void)printf("Ok.\n"); }
// Convert and restore mapped inconvertible Unicode characters. // We use it for extended ASCII names in Unix. bool WideToCharMap(const wchar *Src,char *Dest,size_t DestSize,bool &Success) { // String with inconvertible characters mapped to private use Unicode area // must have the mark code somewhere. if (wcschr(Src,(wchar)MappedStringMark)==NULL) return false; Success=true; uint SrcPos=0,DestPos=0; while (DestPos<DestSize-MB_CUR_MAX) { if (Src[SrcPos]==0) { Dest[DestPos]=0; break; } if (uint(Src[SrcPos])==MappedStringMark) { SrcPos++; continue; } // For security reasons do not retore low ASCII codes, so mapping cannot // be used to hide control codes like path separators. if (uint(Src[SrcPos])>=MapAreaStart+0x80 && uint(Src[SrcPos])<MapAreaStart+0x100) Dest[DestPos++]=char(uint(Src[SrcPos++])-MapAreaStart); else { ignore_result( wctomb(NULL,0) ); // Reset shift state. if (wctomb(Dest+DestPos,Src[SrcPos])==-1) Success=false; SrcPos++; ignore_result( mblen(NULL,0) ); // Reset shift state. int Length=mblen(Dest+DestPos,MB_CUR_MAX); DestPos+=Max(Length,1); } } return true; }