当前位置: 首页>>代码示例>>C++>>正文


C++ CStr::ReleaseBuffer方法代码示例

本文整理汇总了C++中CStr::ReleaseBuffer方法的典型用法代码示例。如果您正苦于以下问题:C++ CStr::ReleaseBuffer方法的具体用法?C++ CStr::ReleaseBuffer怎么用?C++ CStr::ReleaseBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CStr的用法示例。


在下文中一共展示了CStr::ReleaseBuffer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: InternetOpen


//.........这里部分代码省略.........
		    {
			    if ( _ttol( status ) != 200 )
			    {
				    rc = _ttol( status );
			    }
		    }
	    }
    }

    if ( rc == 0 )
    {
		fileSize = 0;

        BOOL more = InternetQueryDataAvailable( file, &blockSize, 0, 0 );
        tmpStr = (char*)malloc(blockSize+1);

		while ( rc == 0 && more && blockSize > 0 && tmpStr != NULL )
		{
			int stat = InternetReadFile( file, (void*)(tmpStr+fileSize), blockSize, &readSize );
			if ( stat == 0 )
			{
				int error = GetLastError();
				rc = -5;
			}
			else
			{
				tmpStr[fileSize+readSize] = '\0';
				fileSize += readSize;
				more = InternetQueryDataAvailable( file, &blockSize, 0, 0 );
				if ( more && blockSize > 0 )
				{
					if ( fileSize + blockSize > 256 * 1024 )
					{
						// Too big
						rc = -3;
					}
					else
					{
						void *movedStr = realloc( tmpStr, fileSize+blockSize+1 );
						if ( movedStr == NULL )
						{
							more = FALSE;
							rc = -4;
						}
						else
							tmpStr = (char*)movedStr;
					}
				}
			}
		}
    }

    if ( rc == 0 )
    {
        bufSize  = (fileSize+1)*sizeof(TCHAR);
        buffer   = (char*)content.GetBufferSetLength( fileSize+1 );
        if ( buffer == NULL )
	    {
		    rc = -4;
        }
    }

    if ( rc == 0 )
    {
    	tmpStr[fileSize] = '\0';

        if ( cp == CP_ACP ) // Default: Device's encoding
		{
			// Copy first few lines
			char scanText[256];
			strncpy( scanText, tmpStr, 255 );
			scanText[255] = '\0';
			_strlwr( scanText );

			// Unicode?
			if (   strstr( scanText, "utf8" )
				|| strstr( scanText, "utf-8" )
			   )
			{
				cp = CP_UTF8;
			}
			if ( strstr( scanText, "iso-8835-1" ) )
			{
				cp = 1252;
			}
		}

		if ( cp != CP_UNICODE )
			MultiByteToWideChar( cp, 0, tmpStr, fileSize+1, (LPTSTR)buffer, bufSize );
		else
			strcpy( buffer, tmpStr );
    }

    if ( file   != NULL ) InternetCloseHandle( file );
    if ( web    != NULL ) InternetCloseHandle( web );
    if ( buffer != NULL ) content.ReleaseBuffer();
    if ( tmpStr != NULL ) free(tmpStr);

    return rc;
}
开发者ID:grainrigi,项目名称:jscripts,代码行数:101,代码来源:IniFile.cpp

示例2: CreateFile

BOOL
CIniFile::WriteFile( LPCTSTR filename, CStr &content, BOOL append, UINT cp )
{
    HANDLE  file;
    char   *buffer = NULL;
    char   *tmpStr = NULL;
    ULONG   iLen, feLen;
    BOOL    rc = TRUE, comPort = FALSE;

	if ( filename[wcslen(filename)-1] == ':' )
	{
		file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
		if ( file != INVALID_HANDLE_VALUE )
			SetComPort( filename, file );
		comPort = TRUE;
	}
	else
	{
		CStr fileWithPath;
		fileWithPath = filename;

		if ( CurrentInterpreter != NULL && ( fileWithPath.GetLength() < 2 || ( fileWithPath.GetAt(0) != '\\' && fileWithPath.GetAt(1) != ':' ) ) )
		{
			int len = CurrentInterpreter->ScriptFile.ReverseFind('\\');
			if ( len == -1 )
				fileWithPath = L"\\" + fileWithPath;
			else
				fileWithPath = CurrentInterpreter->ScriptFile.Left( len+1 ) + fileWithPath;
		}

		if ( append )
		{
			file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS,   FILE_ATTRIBUTE_NORMAL, NULL );
			if ( SetFilePointer( file, 0, NULL, FILE_END ) == 0 )
				append = FALSE;
		}
		else
			file = CreateFile( fileWithPath, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
	}

    if ( file == INVALID_HANDLE_VALUE ) rc = FALSE;

    if ( rc == TRUE )
    {
        iLen   = content.GetLength();
        if ( cp != CP_UNICODE )
        {
	        tmpStr = new char[iLen*2+1]; // special characters might use 2 bytes!
		    if ( tmpStr == NULL ) rc = FALSE;
		}
		else
			tmpStr = NULL;
    }

    if ( rc == TRUE )
    {
        ULONG written;
        int   stat;


        if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX )
        {
			if ( cp == CP_UTF8_PREFIX  )
			{
				if ( ! comPort && ! append )
					::WriteFile( file, (void*)"\xEF\xBB\xBF", 3, &written, NULL ); // omit the \0!
				cp = CP_UTF8;
			}
            feLen = WideCharToMultiByte( cp, 0, (LPCTSTR)content, -1, tmpStr, iLen*2+1, NULL, NULL );
			if ( !comPort )
	            stat  = ::WriteFile( file, (void*)tmpStr, feLen-1, &written, NULL ); // omit the \0!
			else
			{
				stat = 1, written = 1;
				for ( ULONG i=0; i<feLen-1 && stat != 0 && written == 1; i++ )
				{
		            stat  = ::WriteFile( file, (void*)&(tmpStr[i]), 1, &written, NULL ); // omit the \0!
				}
			}
        }
        else
        {
			if ( cp == CP_UNICODE_PREFIX  )
			{
				if ( ! comPort && ! append )
					::WriteFile( file, (void*)"\xFF\xFE", 2, &written, NULL ); // omit the \0!
			}
            stat = ::WriteFile( file, (void*)(LPCTSTR)content, (content.GetLength())*2, &written, NULL ); // omit the \0!
        }

        if ( stat == 0 ) rc = FALSE;
    }

    if ( file   != NULL ) CloseHandle( file );
    if ( buffer != NULL ) content.ReleaseBuffer();
    if ( tmpStr != NULL ) delete[] tmpStr;

    return rc;
}
开发者ID:grainrigi,项目名称:jscripts,代码行数:99,代码来源:IniFile.cpp

示例3: if


//.........这里部分代码省略.........

    if ( rc == 0 )
    {
        tmpStr = new char[fileSize+2]; // if it's unicode, two binary zeroes are required at the end
		if ( tmpStr == NULL ) rc = -4;
    }
     
    if ( rc == 0 )
    {
		if ( ! com )
		{
			int stat = ::ReadFile( file, (void*)tmpStr, fileSize, &readSize, NULL );
			if ( stat == 0 )
			{
				int error = GetLastError();
				rc = -5;
			}
		}
		else
		{
			comPort = &file;
			comTargetSize = fileSize+1;
			comTargetBuffer = tmpStr;

			DWORD dwThreadID;
			HANDLE readThread = CreateThread( NULL, 0, PortReadThread, 0, 0, &dwThreadID );
			if ( readThread != NULL )
			{
				WaitForSingleObject( readThread, timeout );
				TerminateThread( readThread, 0 );
				CloseHandle( readThread );
			    SetCommMask( file, 0 );
				readSize = comReadSize;
			}
			else
			{
				rc = -6;
				readSize = 0;
			}
		}
    }

    if ( rc == 0 )
    {
		if ( cp == CP_ACP )
		{
			if ( readSize >= 3 && memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 )
			{
				cp = CP_UTF8_PREFIX;
			}
			if ( readSize >= 2 && memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 )
			{
				cp = CP_UNICODE_PREFIX;
			}
		}

		if ( cp != CP_UNICODE && cp != CP_UNICODE_PREFIX && cp != -1)
		{
			int offset = 0;

			// Strip first 3 bytes for prefixed UFT8
			if ( cp == CP_UTF8_PREFIX )
			{
				if ( memcmp( tmpStr, (void*)"\xEF\xBB\xBF", 3 ) == 0 )
				{
					offset = 3;
				}
				cp = CP_UTF8;
			}

			// set binary zero at end
			tmpStr[readSize] = '\0';
			MultiByteToWideChar( cp, 0, tmpStr+offset, readSize-offset+1, (LPTSTR)buffer, bufSize );
		}
		else if (cp != -1)
		{
			int offset = 0;
			// Strip first 2 bytes for prefixed UFT8
			if ( cp == CP_UNICODE_PREFIX )
			{
				if ( memcmp( tmpStr, (void*)"\xFF\xFE", 2 ) == 0 )
				{
					offset = 2;
				}
				cp = CP_UNICODE;
			}

			// for unicode, two binary zeroes are required at the end
			tmpStr[readSize] = '\0';
			tmpStr[readSize+1] = '\0';
			wcscpy( (TCHAR*)buffer, (TCHAR*)(tmpStr+offset) );
		}
    }

    if ( file   != NULL ) CloseHandle( file );
    if ( buffer != NULL ) content.ReleaseBuffer();
    if ( tmpStr != NULL ) delete[] tmpStr;

    return rc;
}
开发者ID:grainrigi,项目名称:jscripts,代码行数:101,代码来源:IniFile.cpp


注:本文中的CStr::ReleaseBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。