Return true if we managed to write entire string including terminator without overrunning nMaxBytes
| 221 | // Return true if we managed to write entire string including terminator |
| 222 | // without overrunning nMaxBytes |
| 223 | bool CDbSigs::BufWriteStr(PBYTE pBuf,CString strIn,unsigned nMaxBytes,bool bUni,unsigned &nOffsetBytes) |
| 224 | { |
| 225 | ASSERT(pBuf); |
| 226 | |
| 227 | bool bRet = false; |
| 228 | char chAsc; |
| 229 | wchar_t chUni; |
| 230 | unsigned nCharSz = ((bUni)?sizeof(wchar_t):sizeof(char)); |
| 231 | PBYTE pBufBase; |
| 232 | LPWSTR pBufUni; |
| 233 | LPSTR pBufAsc; |
| 234 | |
| 235 | pBufBase = pBuf + nOffsetBytes; |
| 236 | pBufUni = (LPWSTR)pBufBase; |
| 237 | pBufAsc = (LPSTR)pBufBase; |
| 238 | |
| 239 | #ifdef UNICODE |
| 240 | // Create non-Unicode version of string |
| 241 | // Ref: http://social.msdn.microsoft.com/Forums/vstudio/en-US/85f02321-de88-47d2-98c8-87daa839a98e/how-to-convert-cstring-to-const-char-?forum=vclanguage |
| 242 | // Added constant specifier |
| 243 | LPCSTR pszNonUnicode; |
| 244 | USES_CONVERSION; |
| 245 | // Not specifying code page explicitly but assume content |
| 246 | // should be ASCII. Default code page is probably Windows-1252. |
| 247 | pszNonUnicode = CW2A( strIn.LockBuffer( ) ); |
| 248 | strIn.UnlockBuffer( ); |
| 249 | #endif |
| 250 | |
| 251 | |
| 252 | unsigned nStrLen; |
| 253 | unsigned nChInd; |
| 254 | nStrLen = strIn.GetLength(); |
| 255 | for (nChInd=0;(nChInd<nStrLen)&&(nOffsetBytes+nCharSz-1<nMaxBytes);nChInd++) { |
| 256 | if (bUni) { |
| 257 | // Normal handling for unicode |
| 258 | chUni = strIn.GetAt(nChInd); |
| 259 | pBufUni[nChInd] = chUni; |
| 260 | } else { |
| 261 | |
| 262 | #ifdef UNICODE |
| 263 | // To avoid Warning C4244: Conversion from 'wchar_t' to 'char' possible loss of data |
| 264 | // We need to implement conversion here |
| 265 | // Ref: http://stackoverflow.com/questions/4786292/converting-unicode-strings-and-vice-versa |
| 266 | |
| 267 | // Since we have compiled for unicode, the CString character fetch |
| 268 | // will be unicode char. Therefore we need to use ANSI-converted form. |
| 269 | chAsc = pszNonUnicode[nChInd]; |
| 270 | #else |
| 271 | // Since we have compiled for non-Unicode, the CString character fetch |
| 272 | // will be single byte char |
| 273 | chAsc = strIn.GetAt(nChInd); |
| 274 | #endif |
| 275 | pBufAsc[nChInd] = chAsc; |
| 276 | } |
| 277 | // Advance pointers |
| 278 | nOffsetBytes += nCharSz; |
| 279 | } |
| 280 |
nothing calls this directly
no outgoing calls
no test coverage detected