aboutsummaryrefslogtreecommitdiff
path: root/src/tutorial/beard.c
blob: 4157d2195d30ee19a18930a38bff72ccbc9cc864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*-------------------------------------------------------------------------
 *
 * beard.c
 *	  sample routines to use large objects
 *
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $PostgreSQL: pgsql/src/tutorial/beard.c,v 1.12 2004/12/31 22:04:05 pgsql Exp $
 *
 *-------------------------------------------------------------------------
 */

typedef struct ImageHdr
{
	int			size;
}	ImageHdr;

#define BUFSIZE 10

/*
 * beard -
 *	 clips lower 1/3 of picture and return as large object
 */
Oid
beard(Oid picture)
{
	Oid			beard;
	int			pic_fd,
				beard_fd;
	ImageHdr	ihdr;
	char		buf[BUFSIZE];
	int			cc;

	pic_fd = DatumGetInt32(DirectFunctionCall2(lo_open,
											   ObjectIdGetDatum(picture),
											   Int32GetDatum(INV_READ)));
	if (pic_fd < 0)
		elog(ERROR, "Cannot access picture large object");

	if (lo_read(pic_fd, (char *) &ihdr, sizeof(ihdr)) != sizeof(ihdr))
		elog(ERROR, "Picture large object corrupted");

	beardOffset = (ihdr.size / 3) * 2;

	/*
	 * new large object
	 */
	beard = DatumGetObjectId(DirectFunctionCall1(lo_creat,
												 Int32GetDatum(INV_MD)));
	if (beard == InvalidOid)
		elog(ERROR, "Cannot create new large object");

	beard_fd = DatumGetInt32(DirectFunctionCall2(lo_open,
												 ObjectIdGetDatum(beard),
											  Int32GetDatum(INV_WRITE)));
	if (beard_fd < 0)
		elog(ERROR, "Cannot access beard large object");

	if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
										  Int32GetDatum(pic_fd),
										  Int32GetDatum(beardOffset),
										  Int32GetDatum(SEEK_SET))) < 0)
		elog(ERROR, "Cannot seek in picture large object");

	while ((cc = lo_read(pic_fd, buf, BUFSIZE)) > 0)
	{
		if (lo_write(beard_fd, buf, cc) != cc)
			elog(ERROR, "error while writing large object");
	}

	DirectFunctionCall1(lo_close, Int32GetDatum(pic_fd));
	DirectFunctionCall1(lo_close, Int32GetDatum(beard_fd));

	return beard;
}