Revision: 5624
http://winmerge.svn.sourceforge.net/winmerge/?rev=5624&view=rev
Author: kimmov
Date: 2008-07-15 15:41:20 -0700 (Tue, 15 Jul 2008)
Log Message:
-----------
CString to String conversion for unicoder's maketchar/maketstring functions.
Modified Paths:
--------------
trunk/Src/Common/UniFile.cpp
trunk/Src/Common/unicoder.cpp
trunk/Src/Common/unicoder.h
trunk/Src/UniMarkdownFile.cpp
Modified: trunk/Src/Common/UniFile.cpp
===================================================================
--- trunk/Src/Common/UniFile.cpp 2008-07-15 20:04:26 UTC (rev 5623)
+++ trunk/Src/Common/UniFile.cpp 2008-07-15 22:41:20 UTC (rev 5624)
@@ -553,7 +553,8 @@
RecordZero(m_txtstats, offset);
}
}
- line = ucr::maketstring((LPCSTR)m_current, eolptr-m_current, m_codepage, lossy);
+ String localLine = ucr::maketstring((LPCSTR)m_current, eolptr-m_current, m_codepage, lossy);
+ line = localLine.c_str();
if (lossy && *lossy)
++m_txtstats.nlosses;
if (!eof)
@@ -615,10 +616,10 @@
// convert from Unicode codepoint to TCHAR string
// could be multicharacter if decomposition took place, for example
bool lossy = false; // try to avoid lossy conversion
- CString sch = ucr::maketchar(ch, lossy);
+ String sch = ucr::maketchar(ch, lossy);
if (lossy)
++m_txtstats.nlosses;
- if (sch.GetLength() >= 1)
+ if (sch.length() >= 1)
ch = sch[0];
else
ch = 0;
@@ -679,7 +680,7 @@
line.ReleaseBuffer(cchLine);
return TRUE;
}
- cchLine = Append(line, cchLine, sch, sch.GetLength());
+ cchLine = Append(line, cchLine, sch.c_str(), sch.length());
}
line.ReleaseBuffer(cchLine);
return TRUE;
Modified: trunk/Src/Common/unicoder.cpp
===================================================================
--- trunk/Src/Common/unicoder.cpp 2008-07-15 20:04:26 UTC (rev 5623)
+++ trunk/Src/Common/unicoder.cpp 2008-07-15 22:41:20 UTC (rev 5624)
@@ -17,6 +17,7 @@
*/
#include "StdAfx.h"
+#include "UnicodeString.h"
#include "unicoder.h"
#include "codepage.h"
#include "Utf8FileDetect.h"
@@ -41,8 +42,7 @@
/**
* @brief fetch current OS version into file level variable & set flag
*/
-static void
-fetch_verinfo()
+static void fetch_verinfo()
{
memset(&f_osvi, 0, sizeof(f_osvi));
f_osvi.dwOSVersionInfoSize = sizeof(f_osvi);
@@ -57,8 +57,7 @@
* returns length of byte string written
* Does not zero-terminate!
*/
-int
-Ucs4_to_Utf8(UINT unich, unsigned char * utf8)
+int Ucs4_to_Utf8(UINT unich, unsigned char * utf8)
{
#pragma warning(disable: 4244) // possible loss of data due to type conversion
if (unich <= 0x7f)
@@ -117,10 +116,11 @@
}
/**
- * @brief return byte length of UTF-8 character from its initial character (-1 if invalid)
+ * @brief Gets a length of UTF-8 character in bytes.
+ * @param [in] ch The character for which to get the length.
+ * @return Byte length of UTF-8 character, -1 if invalid.
*/
-int
-Utf8len_fromLeadByte(unsigned char ch)
+int Utf8len_fromLeadByte(unsigned char ch)
{
if (ch < 0x80) return 1;
if (ch < 0xC0) return -1;
@@ -135,8 +135,7 @@
/**
* @brief return #bytes required to represent Unicode codepoint as UTF-8
*/
-int
-Utf8len_fromCodepoint(UINT ch)
+int Utf8len_fromCodepoint(UINT ch)
{
if (ch <= 0x7F) return 1;
if (ch <= 0x7FF) return 2;
@@ -154,8 +153,7 @@
*
* @bug Fails for files larger than 2gigs
*/
-UINT
-Utf8len_of_string(LPCWSTR text, int size)
+UINT Utf8len_of_string(LPCWSTR text, int size)
{
UINT len=0;
for (int i=0; i<size; ++i)
@@ -173,8 +171,7 @@
*
* @bug Fails for files larger than 2gigs
*/
-UINT
-stringlen_of_utf8(LPCSTR text, int size)
+UINT stringlen_of_utf8(LPCSTR text, int size)
{
UINT len=0;
for (int i=0; i<size; )
@@ -190,8 +187,7 @@
/**
* @brief Read UTF-8 character and return as Unicode
*/
-UINT
-GetUtf8Char(unsigned char * str)
+UINT GetUtf8Char(unsigned char * str)
{
/* test short cases first, as probably much more common */
if (!(*str & 0x80 && *str & 0x40)) {
@@ -297,7 +293,7 @@
/**
* @brief convert character passed (Unicode codepoint) to a TCHAR (set lossy flag if imperfect conversion)
*/
-CString maketchar(UINT unich, bool & lossy)
+String maketchar(UINT unich, bool & lossy)
{
static UINT codepage = CP_ACP;
// NB: Windows always draws in CP_ACP, not CP_THREAD_ACP, so we must use CP_ACP as an internal codepage
@@ -308,21 +304,20 @@
/**
* @brief convert character passed (Unicode codepoint) to a TCHAR (set lossy flag if imperfect conversion)
*/
-CString maketchar(UINT unich, bool & lossy, UINT codepage)
+String maketchar(UINT unich, bool & lossy, UINT codepage)
{
#ifdef _UNICODE
if (unich < 0x10000)
{
- CString s;
- s = (TCHAR)unich;
+ String s(1, (TCHAR)unich);
return s;
}
lossy = TRUE;
- return '?';
+ return _T("?");
#else
if (unich < 0x80)
{
- CString s = (TCHAR)unich;
+ String s(1, (TCHAR)unich);
return s;
}
wchar_t wch = (wchar_t)unich;
@@ -365,8 +360,7 @@
/**
* @brief convert 8-bit character input to Unicode codepoint and return it
*/
-UINT
-byteToUnicode (unsigned char ch)
+UINT byteToUnicode (unsigned char ch)
{
static UINT codepage = CP_ACP;
// NB: Windows always draws in CP_ACP, not CP_THREAD_ACP, so we must use CP_ACP as an internal codepage
@@ -377,8 +371,7 @@
/**
* @brief convert 8-bit character input to Unicode codepoint and return it
*/
-UINT
-byteToUnicode (unsigned char ch, UINT codepage)
+UINT byteToUnicode (unsigned char ch, UINT codepage)
{
if (ch < 0x80)
@@ -412,8 +405,7 @@
* @brief Write appropriate BOM (Unicode byte order marker)
* returns #bytes written
*/
-int
-writeBom(LPVOID dest, UNICODESET unicoding)
+int writeBom(LPVOID dest, UNICODESET unicoding)
{
unsigned char * lpd = reinterpret_cast<unsigned char *>(dest);
// write Unicode byte order marker (BOM)
@@ -444,8 +436,7 @@
* This does not handle MBCS or UTF-8 codepages correctly!
* Client should not use this except for Unicode or SBCS codepages.
*/
-UINT
-get_unicode_char(unsigned char * ptr, UNICODESET codeset, int codepage)
+UINT get_unicode_char(unsigned char * ptr, UNICODESET codeset, int codepage)
{
UINT ch;
switch (codeset)
@@ -471,11 +462,12 @@
* In fact, this doesn't even know. Probably going to have to make
* two passes, the first with MB_ERR_INVALID_CHARS. Ugh. :(
*/
-CString maketstring(LPCSTR lpd, UINT len, int codepage, bool * lossy)
+String maketstring(LPCSTR lpd, UINT len, int codepage, bool * lossy)
{
int defcodepage = getDefaultCodepage();
- if (!len) return _T("");
+ if (!len)
+ return _T("");
// 0 is a valid value (CP_ACP)!
if (codepage == -1)
@@ -484,10 +476,11 @@
#ifdef UNICODE
// Convert input to Unicode, using specified codepage
// TCHAR is wchar_t, so convert into CString (str)
- CString str;
DWORD flags = MB_ERR_INVALID_CHARS;
- int wlen = len*2+6;
- LPWSTR wbuff = str.GetBuffer(wlen);
+ int wlen = len * 2 + 6;
+ String str;
+ str.resize(wlen);
+ LPWSTR wbuff = &*str.begin();
do
{
int n = MultiByteToWideChar(codepage, flags, lpd, len, wbuff, wlen-1);
@@ -508,7 +501,7 @@
ASSERT(FALSE);
--n;
}
- str.ReleaseBuffer(n);
+ str.resize(n);
return str;
}
*lossy = true;
@@ -522,10 +515,10 @@
{
// trivial case, they want the bytes in the file interpreted in our current codepage
// Only caveat is that input (lpd) is not zero-terminated
- return CString(lpd, len);
+ return String(lpd, len);
}
- CString str = CrossConvertToStringA(lpd, len, codepage, defcodepage, lossy);
+ String str = CrossConvertToStringA(lpd, len, codepage, defcodepage, lossy);
return str;
#endif
}
@@ -534,16 +527,15 @@
* @brief (ANSI build only) Convert from one 8 bit codepage to another
*/
#ifndef UNICODE
-CString
-CrossConvertToStringA(LPCSTR src, UINT srclen, int cpin, int cpout, bool * lossy)
+String CrossConvertToStringA(LPCSTR src, UINT srclen, int cpin, int cpout, bool * lossy)
{
-
- CString str;
int wlen = srclen*2+6;
int clen = wlen * 2 + 6;
- LPSTR cbuff = str.GetBuffer(clen);
+ String str;
+ str.resize(clen);
+ LPSTR cbuff = &*str.begin();
int nbytes = CrossConvert(src, srclen, cbuff, clen, cpin, cpout, lossy);
- str.ReleaseBuffer(nbytes);
+ str.resize(nbytes);
return str;
}
#endif
@@ -553,8 +545,7 @@
*
* destsize must be at least 2
*/
-int
-CrossConvert(LPCSTR src, UINT srclen, LPSTR dest, UINT destsize, int cpin, int cpout, bool * lossy)
+int CrossConvert(LPCSTR src, UINT srclen, LPSTR dest, UINT destsize, int cpin, int cpout, bool * lossy)
{
ASSERT(destsize > 1);
@@ -779,4 +770,3 @@
return (cp1 == cp2)
|| (NormalizeCodepage(cp1) == NormalizeCodepage(cp2));
}
-
Modified: trunk/Src/Common/unicoder.h
===================================================================
--- trunk/Src/Common/unicoder.h 2008-07-15 20:04:26 UTC (rev 5623)
+++ trunk/Src/Common/unicoder.h 2008-07-15 22:41:20 UTC (rev 5624)
@@ -43,12 +43,12 @@
UINT stringlen_of_utf8(LPCSTR text, int size);
UINT GetUtf8Char(unsigned char * str);
int to_utf8_advance(UINT u, unsigned char * &lpd);
-CString maketchar(UINT ch, bool & lossy);
+String maketchar(UINT ch, bool & lossy);
int writeBom(LPVOID dest, UNICODESET unicoding);
UINT get_unicode_char(unsigned char * ptr, UNICODESET unicoding, int codepage=0);
-CString maketstring(LPCSTR lpd, UINT len, int codepage, bool * lossy);
-CString maketchar(UINT unich, bool & lossy);
-CString maketchar(UINT unich, bool & lossy, UINT codepage);
+String maketstring(LPCSTR lpd, UINT len, int codepage, bool * lossy);
+String maketchar(UINT unich, bool & lossy);
+String maketchar(UINT unich, bool & lossy, UINT codepage);
UINT byteToUnicode(unsigned char ch);
UINT byteToUnicode(unsigned char ch, UINT codepage);
void getInternalEncoding(UNICODESET * unicoding, int * codepage);
@@ -58,7 +58,7 @@
int CrossConvert(LPCSTR src, UINT srclen, LPSTR dest, UINT destsize, int cpin, int cpout, bool * lossy);
#ifndef UNICODE
-CString CrossConvertToStringA(LPCSTR src, UINT srclen, int cpin, int cpout, bool * lossy);
+String CrossConvertToStringA(LPCSTR src, UINT srclen, int cpin, int cpout, bool * lossy);
#endif
UNICODESET DetermineEncoding(LPBYTE pBuffer, int size, bool * pBom);
Modified: trunk/Src/UniMarkdownFile.cpp
===================================================================
--- trunk/Src/UniMarkdownFile.cpp 2008-07-15 20:04:26 UTC (rev 5623)
+++ trunk/Src/UniMarkdownFile.cpp 2008-07-15 22:41:20 UTC (rev 5624)
@@ -7,6 +7,7 @@
// $Id$
#include "stdafx.h"
+#include "UnicodeString.h"
#include "UniMarkdownFile.h"
#include "markdown.h"
#include "unicoder.h"
@@ -140,8 +141,9 @@
bool bDone = false;
if (m_current < (LPBYTE)m_pMarkdown->lower)
{
- line = ucr::maketstring((const char *)m_current, m_pMarkdown->lower -
+ String localLine = ucr::maketstring((const char *)m_current, m_pMarkdown->lower -
(const char *)m_current, m_codepage, lossy);
+ line = localLine.c_str();
CollapseWhitespace(line);
bDone = !line.IsEmpty();
m_current = (LPBYTE)m_pMarkdown->lower;
@@ -160,7 +162,8 @@
{
++m_current;
}
- line = ucr::maketstring((const char *)current, m_current - current, m_codepage, lossy);
+ String localLine = ucr::maketstring((const char *)current, m_current - current, m_codepage, lossy);
+ line = localLine.c_str();
if (m_current < m_transparent)
{
current = m_current;
@@ -214,16 +217,18 @@
}
if (bDone)
{
- line = ucr::maketstring((const char *)m_current, m_pMarkdown->first -
+ String localLine = ucr::maketstring((const char *)m_current, m_pMarkdown->first -
(const char *)m_current, m_codepage, lossy);
+ line = localLine.c_str();
CollapseWhitespace(line);
m_current = (LPBYTE)m_pMarkdown->first;
}
else if (m_current < m_base + m_filesize)
{
bDone = true;
- line = ucr::maketstring((const char *)m_current, m_base + m_filesize -
+ String localLine = ucr::maketstring((const char *)m_current, m_base + m_filesize -
m_current, m_codepage, lossy);
+ line = localLine.c_str();
CollapseWhitespace(line);
m_current = m_base + m_filesize;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|