/* * not correct * * @implemented */ unsigned char * _mbspbrk(const unsigned char *s1, const unsigned char *s2) { const unsigned char* p; while (*s1) { for (p = s2; *p; p += (isleadbyte(*p) ? 2 : 1)) { if (*p == *s1) if (!isleadbyte(*p) || (*(p+1) == *(s1 + 1))) return (unsigned char*)s1; } s1 += (isleadbyte(*s1) ? 2 : 1); } return NULL; }
void convertBackSlashToSlash() { for(std::wstring::size_type i = 0; i < rn_.size(); i++) { if(isleadbyte(rn_[i])) i++; else if(rn_[i] == L'\\') rn_[i] = L'/'; } }
basic_win32console_streambuf<char>::int_type basic_win32console_streambuf<char>::overflow(int_type ch) { DWORD n; if(isleadbyte(ch)) { prev_char_ = ch; } else if(isleadbyte(prev_char_)) { char buffer[3] = {prev_char_, ch, 0}; prev_char_ = 0; WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), buffer, 2, &n, NULL); } else { char buffer[2] = {ch, 0}; WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), buffer, 1, &n, NULL); } return 0; }
/// すべてを小文字に変換 inline WURI &convertLower() { for(std::wstring::size_type i = 0; i < rn_.size(); i++) { if(isleadbyte(rn_[i])) i++; else rn_[i] = tolower(rn_[i]); } return *this; }
void CXTPSyntaxEditDrawTextProcessor::SetRowTabPositions(int nRow, LPCTSTR pcszOrigRowText) { CXTPRowInfo* pRowI = GetRowInfo(nRow); ASSERT(pRowI); if (!pRowI) return; pRowI->arTabs.RemoveAll(); pRowI->arDispCol2StrPos.RemoveAll(); pRowI->arStrPos2DispCol.RemoveAll(); int nDispPos = 0; int nStrPos = 0; for (LPCTSTR p = pcszOrigRowText; p && *p != 0; p = _tcsinc(p), nStrPos++) { pRowI->arStrPos2DispCol.AddData(nDispPos); if (*p == _T('\t')) { int nSpaces = m_nTabSize - (nDispPos % m_nTabSize); for (int i = 0; i < nSpaces; i++) { if (i == 0) { pRowI->arTabs.AddData(0); pRowI->arDispCol2StrPos.AddData(nStrPos); } else { pRowI->arTabs.AddData((BYTE)(nSpaces - i)); //pRowI->arDispCol2StrPos.AddData(nStrPos + 1); //Corrected, only with the next real character we have the next nStrPos. pRowI->arDispCol2StrPos.AddData(nStrPos); } } nDispPos += nSpaces; } else { #ifdef XTP_FIXED // multi-byte character incorrect. for (int index = 0; index < ( isleadbyte(*p) != 0 ? 2 : 1 ); index ++) { pRowI->arTabs.AddData(0); pRowI->arDispCol2StrPos.AddData(nStrPos); nDispPos++; } #else pRowI->arTabs.AddData(0); pRowI->arDispCol2StrPos.AddData(nStrPos); nDispPos++; #endif } } pRowI->arStrPos2DispCol.AddData(nDispPos); pRowI->arDispCol2StrPos.AddData(nStrPos); }
int __cdecl mblen ( const char * s, size_t n ) { _ASSERTE (MB_CUR_MAX == 1 || MB_CUR_MAX == 2); if ( !s || !(*s) || (n == 0) ) /* indicate do not have state-dependent encodings, empty string length is 0 */ return 0; #if !defined(_NTSUBSET_) && !defined(_POSIX_) if ( isleadbyte((unsigned char)*s) ) { /* multi-byte char */ /* verify valid MB char */ #ifdef _WIN32 if ( MB_CUR_MAX <= 1 || (int)n < MB_CUR_MAX || MultiByteToWideChar(__lc_codepage, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, s, MB_CUR_MAX, NULL, 0) == 0 ) #else /* validate high byte of mbcs char */ if ((n<(size_t)MB_CUR_MAX) || (!*(s+1))) #endif /* _WIN32 */ /* bad MB char */ return -1; else return MB_CUR_MAX; } else { /* single byte char */ #ifdef _WIN32 /* verify valid SB char */ if ( MultiByteToWideChar(__lc_codepage, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, s, 1, NULL, 0) == 0 ) return -1; #endif /* _WIN32 */ return sizeof(char); } #else /* _NTSUBSET_ */ { char *s1 = (char *)s; RtlAnsiCharToUnicodeChar( &s1 ); return s1 - s; } #endif /* _NTSUBSET_ */ }
size_t __cdecl _mbstowcs_lk #else size_t __cdecl mbstowcs #endif ( wchar_t *pwcs, const char *s, size_t n ) { size_t count = 0; if (pwcs && n == 0) /* dest string exists, but 0 bytes converted */ return (size_t) 0; _ASSERTE(s != NULL); #ifdef _WIN32 #if !defined(_NTSUBSET_) && !defined(_POSIX_) /* if destination string exists, fill it in */ if (pwcs) { if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) { /* C locale: easy and fast */ while (count < n) { *pwcs = (wchar_t) ((unsigned char)s[count]); if (!s[count]) return count; count++; pwcs++; } return count; } else { int bytecnt, charcnt; unsigned char *p; /* Assume that the buffer is large enough */ if ((count=MultiByteToWideChar(__lc_codepage, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, s, -1, pwcs, n)) != 0) return count - 1; /* don't count NUL */ if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { errno = EILSEQ; return (size_t)-1; } /* User-supplied buffer not large enough. */ /* How many bytes are in n characters of the string? */ charcnt = n; for (p = (unsigned char *)s; (charcnt-- && *p); p++) { if (isleadbyte(*p)) p++; } bytecnt = ((int) ((char *)p - (char *)s)); if ((count = MultiByteToWideChar (__lc_codepage, MB_PRECOMPOSED, s, bytecnt, pwcs, n)) == 0) { errno = EILSEQ; return (size_t)-1; } return count; /* no NUL in string */ } } else { /* pwcs == NULL, get size only, s must be NUL-terminated */ if (__lc_handle[LC_CTYPE] == _CLOCALEHANDLE) return strlen(s); else { if ((count=MultiByteToWideChar(__lc_codepage, MB_PRECOMPOSED|MB_ERR_INVALID_CHARS, s, -1, NULL, 0)) == 0) { errno = EILSEQ; return (size_t)-1; } return count - 1; } } #else /* _NTSUBSET_/_POSIX_ */ if (pwcs) { NTSTATUS Status; int size; size = _mbstrlen(s); Status = RtlMultiByteToUnicodeN(pwcs, n * sizeof( *pwcs ), (PULONG)&size, (char *)s, size+1 ); if (!NT_SUCCESS(Status)) { errno = EILSEQ; size = -1; } else { size = size / sizeof(*pwcs); if (pwcs[size-1] == L'\0') { size -= 1; } } return size; } else { /* pwcs == NULL, get size only, s must be NUL-terminated */ return strlen(s); } #endif /* _NTSUBSET_/_POSIX_ */ #else /* _WIN32 */ /* if destination string exists, fill it in */ if (pwcs) { /* C locale: easy and fast */ while (count < n) { *pwcs = (wchar_t) ((unsigned char)s[count]); if (!s[count]) return count; count++; pwcs++; } return count; } else { /* pwcs == NULL, get size only, s must be NUL-terminated */ return strlen(s); } #endif /* _WIN32 */ }
unsigned long aFont::height( const char* st, int areaWidth ) const { unsigned long width, height; gos_TextSetAttributes (gosFont, 0, size, false, true, false, false); gos_TextSetRegion( 0, 0, areaWidth, Environment.screenHeight ); gos_TextStringLength(&width,&height,st); unsigned long lineCount = 1; if ( width > areaWidth - 1 ) { unsigned long curLineWidth = 0; gosASSERT( strlen( st) < 2048 ); char pLine[2048]; // should be more than adequate char* pLastWord = (char*)st; char* pTmp = (char*)st; char* pTmpLine = (char*)pLine; int numberOfWordsPerLine = 0; bool bHasSpaces = true; if ( !strstr( st, " " ) ) { if ( !strstr( st, "\n" ) ) bHasSpaces = false; } while( *pTmp != NULL ) { if ( *pTmp == '\n' ) { lineCount++; numberOfWordsPerLine = 0; curLineWidth = 0; pTmpLine = pLine; pLastWord = pTmp+1; } else if ( !bHasSpaces ) { if ( pTmp > st ) { char tmp = *(pTmp-1); if ( !isleadbyte( tmp ) ) { *(pTmpLine) = NULL; gos_TextStringLength( &curLineWidth, &height, pLine ); if ( curLineWidth > areaWidth ) { lineCount++; pTmp--; pTmpLine = pLine; curLineWidth = 0; numberOfWordsPerLine = 0; } } } } else if ( isspace( *pTmp ) ) { *(pTmpLine) = NULL; gos_TextStringLength( &curLineWidth, &height, pLine ); if ( curLineWidth > areaWidth ) { gos_TextStringLength( &curLineWidth, &height, pLastWord ); if ( numberOfWordsPerLine == 0 || curLineWidth > areaWidth ) { static bool firstTime = true; if (firstTime) { Assert( true, 0, "this list box item contains a word of greater " " area than the list box, giving up" ); firstTime = false; } /*There are times when you just can't guarantee that this won't occur, so we have to just continue and deal with it.*/ //return height; pLastWord = pTmp; } lineCount++; pTmpLine = pLine; pTmp = pLastWord - 1; curLineWidth = 0; numberOfWordsPerLine = 0; } pLastWord = pTmp; numberOfWordsPerLine++; } *pTmpLine = *pTmp; if ( isleadbyte( *pTmpLine ) ) { *(pTmpLine+1) = *(pTmp+1); } pTmpLine = (char*)_mbsinc( (unsigned char*)pTmpLine ); pTmp = (char*)_mbsinc( (unsigned char*)pTmp ); } // one last check *pTmpLine = NULL; gos_TextStringLength( &curLineWidth, &height, pLine ); if ( curLineWidth > areaWidth ) { lineCount++; } if ( *pTmp == NULL ) lineCount++; } gos_TextStringLength( &width, &height, "A" ); return (height) * lineCount ; }
extern "C" wint_t __cdecl _fgetwc_nolock(FILE* const public_stream) { __crt_stdio_stream const stream(public_stream); // If the stream is backed by a real file and is open in a Unicode text mode, // we need to read two bytes (note that we read two bytes for both UTF-8 and // UTF-16 backed streams, since lowio translates UTF-8 to UTF-16 when we read. if (!stream.is_string_backed() && _textmode_safe(_fileno(stream.public_stream())) != __crt_lowio_text_mode::ansi) { wchar_t wc; // Compose the wide character by reading byte-by-byte from the stream: char* const wc_first = reinterpret_cast<char*>(&wc); char* const wc_last = wc_first + sizeof(wc); for (char* it = wc_first; it != wc_last; ++it) { int const c = _getc_nolock(stream.public_stream()); if (c == EOF) return WEOF; *it = static_cast<char>(c); } return wc; } if (!stream.is_string_backed() && (_osfile_safe(_fileno(stream.public_stream())) & FTEXT)) { int size = 1; int ch; char mbc[4]; wchar_t wch; /* text (multi-byte) mode */ if ((ch = _getc_nolock(stream.public_stream())) == EOF) return WEOF; mbc[0] = static_cast<char>(ch); if (isleadbyte(static_cast<unsigned char>(mbc[0]))) { if ((ch = _getc_nolock(stream.public_stream())) == EOF) { ungetc(mbc[0], stream.public_stream()); return WEOF; } mbc[1] = static_cast<char>(ch); size = 2; } if (mbtowc(&wch, mbc, size) == -1) { // Conversion failed! Set errno and return failure: errno = EILSEQ; return WEOF; } return wch; } // binary (Unicode) mode if (stream->_cnt >= static_cast<int>(sizeof(wchar_t))) { stream->_cnt -= static_cast<int>(sizeof(wchar_t)); return *reinterpret_cast<wchar_t*&>(stream->_ptr)++; } else { return static_cast<wint_t>(__acrt_stdio_refill_and_read_wide_nolock(stream.public_stream())); } }
/* now define version that doesn't lock/unlock, validate fh */ int __cdecl _write_nolock ( int fh, const void *buf, unsigned cnt ) { int lfcount; /* count of line feeds */ int charcount; /* count of chars written so far */ int written; /* count of chars written on this write */ ULONG dosretval; /* o.s. return value */ char tmode ; /* textmode - ANSI or UTF-16 */ BOOL toConsole = 0; /* true when writing to console */ BOOL isCLocale = 0; /* true when locale handle is C locale */ lfcount = charcount = 0; /* nothing written yet */ if (cnt == 0) return 0; /* nothing to do */ _VALIDATE_CLEAR_OSSERR_RETURN( (buf != NULL), EINVAL, -1 ); tmode = _textmode(fh); if(tmode == __IOINFO_TM_UTF16LE || tmode == __IOINFO_TM_UTF8) { /* For a UTF-16 file, the count must always be an even number */ _VALIDATE_CLEAR_OSSERR_RETURN(((cnt & 1) == 0), EINVAL, -1); } if (_osfile(fh) & FAPPEND) { /* appending - seek to end of file; ignore error, because maybe file doesn't allow seeking */ #if _INTEGRAL_MAX_BITS >= 64 (void)_lseeki64_nolock(fh, 0, FILE_END); #else /* _INTEGRAL_MAX_BITS >= 64 */ (void)_lseek_nolock(fh, 0, FILE_END); #endif /* _INTEGRAL_MAX_BITS >= 64 */ } /* check for text mode with LF's in the buffer */ /* * Note that in case the handle belongs to Console, write file will * generate garbage output. For user to print these characters * correctly, we will need to print ANSI. * * Also note that in case of printing to Console, we still have to * convert the characters to console codepage. */ if (_isatty(fh) && (_osfile(fh) & FTEXT)) { DWORD dwMode; _ptiddata ptd = _getptd(); isCLocale = (ptd->ptlocinfo->lc_handle[LC_CTYPE] == _CLOCALEHANDLE); toConsole = GetConsoleMode((HANDLE)_osfhnd(fh), &dwMode); } /* don't need double conversion if it's ANSI mode C locale */ if (toConsole && !(isCLocale && (tmode == __IOINFO_TM_ANSI))) { UINT consoleCP = GetConsoleCP(); char mboutbuf[MB_LEN_MAX]; wchar_t tmpchar; int size = 0; int written = 0; char *pch; for (pch = (char *)buf; (unsigned)(pch - (char *)buf) < cnt; ) { BOOL bCR; if (tmode == __IOINFO_TM_ANSI) { bCR = *pch == LF; /* * Here we need to do double convert. i.e. convert from * multibyte to unicode and then from unicode to multibyte in * Console codepage. */ if (!isleadbyte(*pch)) { if (mbtowc(&tmpchar, pch, 1) == -1) { break; } } else if ((cnt - (pch - (char*)buf)) > 1) { if (mbtowc(&tmpchar, pch, 2) == -1) { break; } /* * Increment pch to accomodate DBCS character. */ ++pch; } else { break; } ++pch; } else if (tmode == __IOINFO_TM_UTF8 || tmode == __IOINFO_TM_UTF16LE) { /* * Note that bCR set above is not valid in case of UNICODE * stream. We need to set it using unicode character. */ tmpchar = *(wchar_t *)pch; bCR = tmpchar == LF; pch += 2; } if (tmode == __IOINFO_TM_ANSI) { if( (size = WideCharToMultiByte(consoleCP, 0, &tmpchar, 1, mboutbuf, sizeof(mboutbuf), NULL, NULL)) == 0) { break; } else { if ( WriteFile( (HANDLE)_osfhnd(fh), mboutbuf, size, (LPDWORD)&written, NULL) ) { charcount += written; if (written < size) break; } else { dosretval = GetLastError(); break; } } if (bCR) { size = 1; mboutbuf[0] = CR; if (WriteFile((HANDLE)_osfhnd(fh), mboutbuf, size, (LPDWORD)&written, NULL) ) { if (written < size) break; lfcount ++; charcount++; } else { dosretval = GetLastError(); break; } } } else if ( tmode == __IOINFO_TM_UTF8 || tmode == __IOINFO_TM_UTF16LE) { if ( _putwch_nolock(tmpchar) == tmpchar ) { charcount++; } else { dosretval = GetLastError(); break; } if (bCR) /* emit carriage return */ { size = 1; tmpchar = CR; if ( _putwch_nolock(tmpchar) == tmpchar ) { charcount++; lfcount++; } else { dosretval = GetLastError(); break; } } } } } else if ( _osfile(fh) & FTEXT ) { /* text mode, translate LF's to CR/LF's on output */ dosretval = 0; /* no OS error yet */ if(tmode == __IOINFO_TM_ANSI) { char ch; /* current character */ char *p = NULL, *q = NULL; /* pointers into buf and lfbuf resp. */ char lfbuf[BUF_SIZE]; p = (char *)buf; /* start at beginning of buffer */ while ( (unsigned)(p - (char *)buf) < cnt ) { q = lfbuf; /* start at beginning of lfbuf */ /* fill the lf buf, except maybe last char */ while ( q - lfbuf < sizeof(lfbuf) - 1 && (unsigned)(p - (char *)buf) < cnt ) { ch = *p++; if ( ch == LF ) { ++lfcount; *q++ = CR; } *q++ = ch; } /* write the lf buf and update total */ if ( WriteFile( (HANDLE)_osfhnd(fh), lfbuf, (int)(q - lfbuf), (LPDWORD)&written, NULL) ) { charcount += written; if (written < q - lfbuf) break; } else { dosretval = GetLastError(); break; } } } else if ( tmode == __IOINFO_TM_UTF16LE ){ char lfbuf[BUF_SIZE]; wchar_t wch; /* current wide char */ wchar_t *pu = (wchar_t *)buf; wchar_t *qu = NULL; while ( (unsigned)((char *)pu - (char *)buf) < cnt ) { qu = (wchar_t *)lfbuf; /* start at beginning of lfbuf */ /* fill the lf buf, except maybe last wchar_t */ while ( (((char *)qu - lfbuf) < (sizeof(lfbuf) - 2)) && ((unsigned)((char *)pu - (char *)buf) < cnt )) { wch = *pu++; if ( wch == LF ) { lfcount+=2; *qu++ = CR; } *qu++ = wch; } /* write the lf buf and update total */ if ( WriteFile( (HANDLE)_osfhnd(fh), lfbuf, (int)((char*)qu - lfbuf), (LPDWORD)&written, NULL) ) { charcount += written; if (written < ((char *)qu - lfbuf)) break; } else { dosretval = GetLastError(); break; } } } else { /* * Let's divide the lfbuf in 1:2 wher 1 is for storing * widecharacters and 2 if for converting it to UTF8. This takes * into account the worst case scenario where all the UTF8 * characters are 4 byte long. */ char utf8_buf[(BUF_SIZE*2)/3]; wchar_t utf16_buf[BUF_SIZE/6]; wchar_t wch; /* current wide char */ wchar_t *pu = (wchar_t *)buf; wchar_t *qu = NULL; pu = (wchar_t *)buf; while ((unsigned)((char *)pu - (char *)buf) < cnt) { int bytes_converted = 0; qu = utf16_buf; /* start at beginning of lfbuf */ while ( (((char *)qu - (char *)utf16_buf) < (sizeof(utf16_buf) - 2)) && ((unsigned)((char *)pu - (char *)buf) < cnt )) { wch = *pu++; if ( wch == LF ) { /* no need to count the linefeeds here: we calculate the written chars in another way */ *qu++ = CR; } *qu++ = wch; } bytes_converted = WideCharToMultiByte( CP_UTF8, 0, utf16_buf, ((int)((char *)qu - (char *)utf16_buf))/2, utf8_buf, sizeof(utf8_buf), NULL, NULL); if (bytes_converted == 0) { dosretval = GetLastError(); break; } else { /* * Here we need to make every attempt to write all the * converted characters. The resaon behind this is, * incase half the bytes of a UTF8 character is * written, it may currupt whole of the stream or file. * * The loop below will make sure we exit only if all * the bytes converted are written (which makes sure no * partial MBCS is written) or there was some error in * the stream. */ int bytes_written = 0; do { if (WriteFile( (HANDLE)_osfhnd(fh), utf8_buf + bytes_written, bytes_converted - bytes_written, &written, NULL)) { bytes_written += written; } else { dosretval = GetLastError(); break; } } while ( bytes_converted > bytes_written); /* * Only way the condition below could be true is if * there was en error. In case of error we need to * break this loop as well. */ if (bytes_converted > bytes_written) { break; } /* if this chunk has been committed successfully, update charcount */ charcount = (int)((char *)pu - (char *)buf); } } } } else { /* binary mode, no translation */ if ( WriteFile( (HANDLE)_osfhnd(fh), (LPVOID)buf, cnt, (LPDWORD)&written, NULL) ) { dosretval = 0; charcount = written; } else dosretval = GetLastError(); } if (charcount == 0) { /* If nothing was written, first check if an o.s. error, otherwise we return -1 and set errno to ENOSPC, unless a device and first char was CTRL-Z */ if (dosretval != 0) { /* o.s. error happened, map error */ if (dosretval == ERROR_ACCESS_DENIED) { /* wrong read/write mode should return EBADF, not EACCES */ errno = EBADF; _doserrno = dosretval; } else _dosmaperr(dosretval); return -1; } else if ((_osfile(fh) & FDEV) && *(char *)buf == CTRLZ) return 0; else { errno = ENOSPC; _doserrno = 0; /* no o.s. error */ return -1; } } else /* return adjusted bytes written */ return charcount - lfcount; }
int CXTPSyntaxEditDrawTextProcessor::ExpandChars(LPCTSTR pszChars, CString& strBuffer, int nDispPos, BOOL bEnableWhiteSpace) { m_arExpandCharsBuffer.RemoveAll(); for (LPCTSTR p = pszChars; p && *p != 0; p = _tcsinc(p)) { if (*p == _T('\t')) { int nSpaces = m_nTabSize - (nDispPos % m_nTabSize); BOOL bFirstWhiteSpaceChar = bEnableWhiteSpace; for (int i = 0; i < nSpaces; i++) { if (bFirstWhiteSpaceChar) { //strBuffer += (TCHAR)(unsigned char)0xBB; m_arExpandCharsBuffer.AddData((TCHAR)(unsigned char)0xBB); bFirstWhiteSpaceChar = FALSE; } else { //strBuffer += _T(' '); m_arExpandCharsBuffer.AddData(_T(' ')); } } nDispPos += nSpaces; } else { if (bEnableWhiteSpace && *p == _T(' ')) m_arExpandCharsBuffer.AddData((TCHAR)(unsigned char)0xB7); //strBuffer += (TCHAR)(unsigned char)0xB7; else { m_arExpandCharsBuffer.AddData(*p); //strBuffer += *p; #ifndef _UNICODE if (_tcsinc(p) > p + 1) { ASSERT(_tcsinc(p) == p + 2); m_arExpandCharsBuffer.AddData(*(p+1)); } #endif } #ifdef XTP_FIXED // multi-byte character : display length 2 // single-byte character : display length 1 if (isleadbyte( *p )) nDispPos += 2; else nDispPos++; #else nDispPos++; #endif } } m_arExpandCharsBuffer.AddData(_T('\0')); strBuffer = m_arExpandCharsBuffer.GetData(); return nDispPos; }