1 /*-------------------------------------------------------------------------
7 * miscellaneous useful functions
8 * these routines are analogous to the ones in libpq/pqcomm.c
10 * Copyright (c) 1994, Regents of the University of California
14 * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.9 1998/01/26 01:42:36 scrappy Exp $
16 *-------------------------------------------------------------------------
26 /* --------------------------------------------------------------------- */
28 get a character from stream f
30 if debug is set, also echo the character fetched
33 pqGetc(FILE *fin, FILE *debug)
39 if (debug && c != EOF)
40 fprintf(debug, "From backend> %c\n", c);
45 /* --------------------------------------------------------------------- */
47 send a string of exactly len length into stream f
49 returns 1 if there was an error, 0 otherwise.
52 pqPutnchar(const char *s, int len, FILE *f, FILE *debug)
55 fprintf(debug, "To backend> %s\n", s);
57 return (pqPutNBytes(s, len, f) == EOF ? 1 : 0);
60 /* --------------------------------------------------------------------- */
62 get a string of exactly len bytes in buffer s (which must be 1 byte
63 longer) from stream f and terminate it with a '\0'.
66 pqGetnchar(char *s, int len, FILE *f, FILE *debug)
70 status = pqGetNBytes(s, len, f);
73 fprintf(debug, "From backend (%d)> %s\n", len, s);
75 return (status == EOF ? 1 : 0);
78 /* --------------------------------------------------------------------- */
80 get a string of up to length len from stream f
83 pqGets(char *s, int len, FILE *f, FILE *debug)
87 status = pqGetString(s, len, f);
90 fprintf(debug, "From backend> \"%s\"\n", s);
92 return (status == EOF ? 1 : 0);
95 /* --------------------------------------------------------------------- */
97 send an integer of 2 or 4 bytes to the file stream, compensate
99 returns 0 if successful, 1 otherwise
102 pqPutInt(const int integer, int bytes, FILE *f, FILE *debug)
109 retval = pqPutShort(integer, f);
112 retval = pqPutLong(integer, f);
115 fprintf(stderr, "** int size %d not supported\n", bytes);
120 fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
125 /* --------------------------------------------------------------------- */
127 read a 2 or 4 byte integer from the stream and swab it around
128 to compensate for different endianness
129 returns 0 if successful
132 pqGetInt(int *result, int bytes, FILE *f, FILE *debug)
139 retval = pqGetShort(result, f);
142 retval = pqGetLong(result, f);
145 fprintf(stderr, "** int size %d not supported\n", bytes);
150 fprintf(debug, "From backend (#%d)> %d\n", bytes, *result);
155 /* --------------------------------------------------------------------- */
157 pqPuts(const char *s, FILE *f, FILE *debug)
159 if (pqPutString(s, f) == EOF)
165 fprintf(debug, "To backend> %s\n", s);
170 /* --------------------------------------------------------------------- */
172 pqFlush(FILE *f, FILE *debug)
181 /* --------------------------------------------------------------------- */