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


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

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


在下文中一共展示了CStr::GetBufferSetLength方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: if

int
CIniFile::ReadFile( LPCTSTR filename, CStr &content, int size, int cp )
{
    HANDLE file   = NULL;
    char  *buffer = NULL;
    char  *tmpStr = NULL;
    ULONG  fileSize, readSize;
    ULONG  bufSize;
    BOOL   rc     = 0, com;
	int    timeout;

	if ( wcsnicmp( filename, L"COM", 3 ) == 0 && filename[wcslen(filename)-1] == ':' )
	{
		file = CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
		if ( file != INVALID_HANDLE_VALUE )
		{
			timeout = SetComPort( filename, file );
		}
		com = 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;
		}
	    file = CreateFile( fileWithPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
		com = FALSE;
	}

    if ( file == INVALID_HANDLE_VALUE )
    {
        int error = GetLastError();
        rc = -2;
    }

    if ( rc == 0 )
    {
		if ( size <= 0 )
		{
			fileSize = GetFileSize( file, NULL );
		}
		else
		{
			fileSize = size;
		}

	    if ( fileSize > 1024 * 1024 || fileSize == -1 )
	    {
		    // Too big
		    rc = -3;
	    }
    }

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

    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;
//.........这里部分代码省略.........
开发者ID:grainrigi,项目名称:jscripts,代码行数:101,代码来源:IniFile.cpp


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