Beispiel #1
0
void FileImpl::handleLastErrorImpl(const std::string& path)
{
	DWORD err = GetLastError();
	switch (err)
	{
	case ERROR_FILE_NOT_FOUND:
		throw FileNotFoundException(path, err);
	case ERROR_PATH_NOT_FOUND:
	case ERROR_BAD_NETPATH:
	case ERROR_CANT_RESOLVE_FILENAME:
	case ERROR_INVALID_DRIVE:
		throw PathNotFoundException(path, err);
	case ERROR_ACCESS_DENIED:
		throw FileAccessDeniedException(path, err);
	case ERROR_ALREADY_EXISTS:
	case ERROR_FILE_EXISTS:
		throw FileExistsException(path, err);
	case ERROR_INVALID_NAME:
	case ERROR_DIRECTORY:
	case ERROR_FILENAME_EXCED_RANGE:
	case ERROR_BAD_PATHNAME:
		throw PathSyntaxException(path, err);
	case ERROR_FILE_READ_ONLY:
		throw FileReadOnlyException(path, err);
	case ERROR_CANNOT_MAKE:
		throw CreateFileException(path, err);
	case ERROR_DIR_NOT_EMPTY:
		throw FileException("directory not empty", path, err);
	case ERROR_WRITE_FAULT:
		throw WriteFileException(path, err);
	case ERROR_READ_FAULT:
		throw ReadFileException(path, err);
	case ERROR_SHARING_VIOLATION:
		throw FileException("sharing violation", path, err);
	case ERROR_LOCK_VIOLATION:
		throw FileException("lock violation", path, err);
	case ERROR_HANDLE_EOF:
		throw ReadFileException("EOF reached", path, err);
	case ERROR_HANDLE_DISK_FULL:
	case ERROR_DISK_FULL:
		throw WriteFileException("disk is full", path, err);
	case ERROR_NEGATIVE_SEEK:
		throw FileException("negative seek", path, err);
	default:
		throw FileException(path, err);
	}
}
Beispiel #2
0
int PipeImpl::readBytes(void* buffer, int length)
{
    pi_assert (_readHandle != INVALID_HANDLE_VALUE);

	DWORD bytesRead = 0;
	BOOL ok = ReadFile(_readHandle, buffer, length, &bytesRead, NULL);
	if (ok || GetLastError() == ERROR_BROKEN_PIPE)
		return bytesRead;
	else
		throw ReadFileException("anonymous pipe");
}
Beispiel #3
0
int PipeImpl::readBytes(void* buffer, int length)
{
	poco_assert (_readfd != -1);

	int n;
	do
	{
		n = read(_readfd, buffer, length);
	}
	while (n < 0 && errno == EINTR);
	if (n >= 0)
		return n;
	else
		throw ReadFileException("anonymous pipe");
}
Beispiel #4
0
PNGImage::PNGImage(const char *full_file_path)
{
    FILE *file = fopen(full_file_path, "rb");

    if(file == nullptr)
    {
        throw ReadFileException("PNGImage", "PNG file specified is not valid.");
    }

    png_byte png_header[8];

    fread(png_header, 1u, 8u, file);

    if(ferror(file) != 0 || feof(file) != 0)
    {
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot read the PNG file.");
    }

    if(png_sig_cmp(png_header, 0, 8) != 0)
    {
        fclose(file);
        throw ReadFileException("PNGImage", "Please select a valid PNG file.");
    }

    png_structp png_read_struct = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);

    if(png_read_struct == nullptr)
    {
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot initialize PNG read struct.");
    }

    png_infop png_info_struct = png_create_info_struct(png_read_struct);

    if(png_info_struct == nullptr)
    {
        png_destroy_read_struct(&png_read_struct, nullptr, nullptr);
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot initialize PNG info struct.");
    }

    png_infop png_end_struct = png_create_info_struct(png_read_struct);

    if(png_info_struct == nullptr)
    {
        png_destroy_read_struct(&png_read_struct, &png_info_struct, nullptr);
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot initialize PNG info struct.");
    }

    if(setjmp(png_jmpbuf(png_read_struct)))
    {
        png_destroy_read_struct(&png_read_struct, &png_info_struct, &png_end_struct);
        fclose(file);
        throw ReadFileException("PNGImage", "Error in libpng.");
    }

    png_init_io(png_read_struct, file);
    png_set_sig_bytes(png_read_struct, 8);
    png_read_info(png_read_struct, png_info_struct);

    int bit_depth, color_type;

    png_get_IHDR(png_read_struct, png_info_struct, &width, &height, &bit_depth, &color_type, nullptr, nullptr, nullptr);

    if(bit_depth == 16)
    {
        png_set_scale_16(png_read_struct);
        bit_depth = 8;
    }

    switch(color_type)
    {
        case PNG_COLOR_TYPE_RGB:
            color_space = GL_RGB;
            break;
        case PNG_COLOR_TYPE_RGB_ALPHA:
            color_space = GL_RGBA;
            break;
        default:
            png_destroy_read_struct(&png_read_struct, &png_info_struct, &png_end_struct);
            fclose(file);
            throw ReadFileException("PNGImage", "Unsupported color space.");
    }

    int row_bytes = png_get_rowbytes(png_read_struct, png_info_struct);

    row_bytes += 3 - ((row_bytes-1) % 4);

    output_buffer.reset(new (std::nothrow)png_byte[row_bytes * height * sizeof(png_byte) + 15]);

    if(output_buffer.get() == nullptr)
    {
        png_destroy_read_struct(&png_read_struct, &png_info_struct, &png_end_struct);
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot allocate required memory.");
    }

    std::unique_ptr<png_byte *> row_pointers(new png_byte *[height]);

    if(row_pointers.get() == nullptr)
    {
        png_destroy_read_struct(&png_read_struct, &png_info_struct, &png_end_struct);
        fclose(file);
        throw ReadFileException("PNGImage", "Cannot allocate required memory.");
    }

    png_byte **_row_pointers = row_pointers.get();
    unsigned char *_output_buffer = output_buffer.get();

    for(unsigned int i = 0; i < height; ++i)
    {
        _row_pointers[height - 1 - i] = _output_buffer + i * row_bytes;
    }

    png_read_image(png_read_struct, _row_pointers);
    png_destroy_read_struct(&png_read_struct, &png_info_struct, &png_end_struct);
    fclose(file);
}