1996-07-09 02:22:35 -04:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
*
|
1999-02-13 18:22:53 -05:00
|
|
|
* beard.c
|
1997-09-07 01:04:48 -04:00
|
|
|
* sample routines to use large objects
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
2004-12-31 17:04:05 -05:00
|
|
|
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
|
2000-01-26 00:58:53 -05:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* IDENTIFICATION
|
2004-12-31 17:04:05 -05:00
|
|
|
* $PostgreSQL: pgsql/src/tutorial/beard.c,v 1.12 2004/12/31 22:04:05 pgsql Exp $
|
1996-07-09 02:22:35 -04:00
|
|
|
*
|
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
typedef struct ImageHdr
|
|
|
|
|
{
|
1997-09-07 22:41:22 -04:00
|
|
|
int size;
|
2001-11-05 12:46:40 -05:00
|
|
|
} ImageHdr;
|
1996-07-09 02:22:35 -04:00
|
|
|
|
|
|
|
|
#define BUFSIZE 10
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* beard -
|
1997-09-07 01:04:48 -04:00
|
|
|
* clips lower 1/3 of picture and return as large object
|
1996-07-09 02:22:35 -04:00
|
|
|
*/
|
|
|
|
|
Oid
|
|
|
|
|
beard(Oid picture)
|
|
|
|
|
{
|
1997-09-07 22:41:22 -04:00
|
|
|
Oid beard;
|
|
|
|
|
int pic_fd,
|
|
|
|
|
beard_fd;
|
|
|
|
|
ImageHdr ihdr;
|
|
|
|
|
char buf[BUFSIZE];
|
|
|
|
|
int cc;
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
pic_fd = DatumGetInt32(DirectFunctionCall2(lo_open,
|
|
|
|
|
ObjectIdGetDatum(picture),
|
|
|
|
|
Int32GetDatum(INV_READ)));
|
|
|
|
|
if (pic_fd < 0)
|
1998-01-07 16:07:04 -05:00
|
|
|
elog(ERROR, "Cannot access picture large object");
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
if (lo_read(pic_fd, (char *) &ihdr, sizeof(ihdr)) != sizeof(ihdr))
|
1998-01-07 16:07:04 -05:00
|
|
|
elog(ERROR, "Picture large object corrupted");
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
beardOffset = (ihdr.size / 3) * 2;
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
/*
|
|
|
|
|
* new large object
|
|
|
|
|
*/
|
2000-06-08 21:11:16 -04:00
|
|
|
beard = DatumGetObjectId(DirectFunctionCall1(lo_creat,
|
|
|
|
|
Int32GetDatum(INV_MD)));
|
|
|
|
|
if (beard == InvalidOid)
|
1998-01-07 16:07:04 -05:00
|
|
|
elog(ERROR, "Cannot create new large object");
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
beard_fd = DatumGetInt32(DirectFunctionCall2(lo_open,
|
|
|
|
|
ObjectIdGetDatum(beard),
|
2001-03-21 23:01:46 -05:00
|
|
|
Int32GetDatum(INV_WRITE)));
|
2000-06-08 21:11:16 -04:00
|
|
|
if (beard_fd < 0)
|
1998-01-07 16:07:04 -05:00
|
|
|
elog(ERROR, "Cannot access beard large object");
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
if (DatumGetInt32(DirectFunctionCall3(lo_lseek,
|
|
|
|
|
Int32GetDatum(pic_fd),
|
|
|
|
|
Int32GetDatum(beardOffset),
|
|
|
|
|
Int32GetDatum(SEEK_SET))) < 0)
|
|
|
|
|
elog(ERROR, "Cannot seek in picture large object");
|
|
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
while ((cc = lo_read(pic_fd, buf, BUFSIZE)) > 0)
|
|
|
|
|
{
|
|
|
|
|
if (lo_write(beard_fd, buf, cc) != cc)
|
1998-01-07 16:07:04 -05:00
|
|
|
elog(ERROR, "error while writing large object");
|
1997-09-07 01:04:48 -04:00
|
|
|
}
|
1996-07-09 02:22:35 -04:00
|
|
|
|
2000-06-08 21:11:16 -04:00
|
|
|
DirectFunctionCall1(lo_close, Int32GetDatum(pic_fd));
|
|
|
|
|
DirectFunctionCall1(lo_close, Int32GetDatum(beard_fd));
|
1996-07-09 02:22:35 -04:00
|
|
|
|
1997-09-07 01:04:48 -04:00
|
|
|
return beard;
|
1996-07-09 02:22:35 -04:00
|
|
|
}
|