summaryrefslogtreecommitdiff
path: root/src/tutorial/beard.c
blob: d2254960afd6683688d4d39d36b506ec336f984a (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
/*-------------------------------------------------------------------------
 *
 * beard.c
 *	  sample routines to use large objects
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/tutorial/Attic/beard.c,v 1.2 1999/02/13 23:22:47 momjian 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;

	if ((pic_fd = lo_open(picture, INV_READ)) == -1)
		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
	 */
	if ((beard = lo_creat(INV_MD)) == 0)		/* ?? is this right? */
		elog(ERROR, "Cannot create new large object");

	if ((beard_fd = lo_open(beard, INV_WRITE)) == -1)
		elog(ERROR, "Cannot access beard large object");

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

	lo_close(pic_fd);
	lo_close(beard_fd);

	return beard;
}