示例#1
0
// > 0: http error
// -1: Keine Internet-Verbindung
// -2: URL konnte nicht geöffnet werden
// -3: Zu gro?
// -4: Kein Speicher
// -5: Fehler beim Lesen
int
CIniFile::ReadWebFile( LPCTSTR url, CStr &content, int size, int cp )
{
    HINTERNET web = NULL, file = NULL;
    char  *buffer = NULL;
    char  *tmpStr = NULL;
    ULONG  blockSize, fileSize, readSize;
    ULONG  bufSize;
    BOOL   rc     = 0;

    if ( Proxy.IsEmpty() )
    {
#ifdef DESKTOP
		web  = InternetOpen( L"MortScript", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
#else
        web  = InternetOpen( L"MortScript", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );
#endif
    }
    else
    {
		web  = InternetOpen( L"MortScript", INTERNET_OPEN_TYPE_PROXY, (LPCTSTR)Proxy, NULL, 0 );
    }

    if ( web == NULL )
	{
        int error = GetLastError();
		rc = -1;
	}

    if ( rc == 0 )
    {
	    file = InternetOpenUrl( web, url, NULL, 0, INTERNET_FLAG_RELOAD, 0 );
	    if ( file == NULL )
	    {
            int error = GetLastError();
		    rc = -2;
	    }
    }

    if ( rc == 0 )
    {
	    if ( _tcsncmp( url, L"http", 4 ) == 0 )
	    {
		    // No http error?
		    TCHAR status[MAX_PATH];
            DWORD length=MAX_PATH;
		    if ( HttpQueryInfo( file, HTTP_QUERY_STATUS_CODE, status, &length, NULL ) )
		    {
			    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;
}
示例#2
0
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;
			}
			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;
}