mirror of
https://github.com/postgres/postgres.git
synced 2025-10-22 14:32:25 +03:00
om: "Martin J. Laubach" <mjl@CSlab.tuwien.ac.at>
Subject: [HACKERS] Patch for io routines I am currently trying to improve on the front-backend communication routines; and noticed that lots of code are duplicated for libpq and the backend. This is a first patch that tries to share code between the two, more to follow. mjl
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.4 1996/12/31 07:29:17 bryanh Exp $
|
||||
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.5 1997/03/16 18:51:29 scrappy Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -23,76 +23,73 @@
|
||||
|
||||
#include "libpq-fe.h"
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGetc:
|
||||
get a character from stream f
|
||||
|
||||
if debug is set, also echo the character fetched
|
||||
*/
|
||||
int
|
||||
pqGetc(FILE* fin, FILE* debug)
|
||||
int pqGetc(FILE* fin, FILE* debug)
|
||||
{
|
||||
int c;
|
||||
|
||||
c = getc(fin);
|
||||
|
||||
if (debug && c != EOF)
|
||||
fprintf(debug, "From backend> %c\n", c);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqPutnchar:
|
||||
send a string of exactly len length into stream f
|
||||
|
||||
returns 1 if there was an error, 0 otherwise.
|
||||
*/
|
||||
int
|
||||
pqPutnchar(const char* s, int len, FILE *f, FILE *debug)
|
||||
int pqPutnchar(const char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int status;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
if (debug) fputs("To backend>", debug);
|
||||
while (len--) {
|
||||
status = fputc(*s,f);
|
||||
if (debug)
|
||||
fputc(*s,debug);
|
||||
s++;
|
||||
if (status == EOF)
|
||||
if(debug)
|
||||
fprintf(debug, "To backend> %s\n", s);
|
||||
|
||||
if(fwrite(s, 1, len, f) != len)
|
||||
return 1;
|
||||
}
|
||||
if (debug) fputc('\n', debug);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGetnchar:
|
||||
get a string of exactly len length from stream f
|
||||
*/
|
||||
int
|
||||
pqGetnchar(char* s, int len, FILE *f, FILE *debug)
|
||||
int pqGetnchar(char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
int cnt;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
while (len-- && (c = getc(f)) != EOF)
|
||||
*s++ = c;
|
||||
*s = '\0';
|
||||
cnt = fread(s, 1, len, f);
|
||||
s[cnt] = '\0';
|
||||
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
||||
|
||||
if (debug)
|
||||
fprintf(debug, "From backend (%d)> %s\n", len, s);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "From backend> %s\n", s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pqGets:
|
||||
get a string of up to length len from stream f
|
||||
*/
|
||||
int
|
||||
pqGets(char* s, int len, FILE *f, FILE *debug)
|
||||
int pqGets(char* s, int len, FILE *f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
const char *str = s;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
@@ -100,104 +97,98 @@ pqGets(char* s, int len, FILE *f, FILE *debug)
|
||||
while (len-- && (c = getc(f)) != EOF && c)
|
||||
*s++ = c;
|
||||
*s = '\0';
|
||||
/* mjl: actually needs up to len+1 bytes, is this okay? XXX */
|
||||
|
||||
if (debug)
|
||||
fprintf(debug, "From backend> \"%s\"\n", str);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "From backend> %s\n", s);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pgPutInt
|
||||
send an integer of up to 4 bytesto the file stream
|
||||
do this one byte at at time.
|
||||
This insures that machines with different ENDIANness can talk to each other
|
||||
get a n-byte integer from the stream into result
|
||||
send an integer of 2 or 4 bytes to the file stream, compensate
|
||||
for host endianness.
|
||||
returns 0 if successful, 1 otherwise
|
||||
*/
|
||||
int
|
||||
pqPutInt(const int integer, int bytes, FILE* f, FILE *debug)
|
||||
int pqPutInt(const int integer, int bytes, FILE* f, FILE *debug)
|
||||
{
|
||||
int i;
|
||||
int status;
|
||||
int retval = 0;
|
||||
|
||||
i = integer;
|
||||
switch(bytes)
|
||||
{
|
||||
case 2:
|
||||
retval = pqPutShort(integer, f);
|
||||
break;
|
||||
case 4:
|
||||
retval = pqPutLong(integer, f);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "** int size %d not supported\n", bytes);
|
||||
retval = 1;
|
||||
}
|
||||
|
||||
if (bytes > 4)
|
||||
bytes = 4;
|
||||
if (debug) fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
|
||||
|
||||
while (bytes--) {
|
||||
status = fputc(i & 0xff, f);
|
||||
i >>= 8;
|
||||
if (status == EOF) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (debug) fprintf(debug, "To backend (#)> %d\n", integer);
|
||||
return 0;
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
/* pgGetInt
|
||||
reconstructs the integer one byte at a time.
|
||||
This insures that machines with different ENDIANness can talk to each other
|
||||
get a n-byte integer from the stream into result
|
||||
read a 2 or 4 byte integer from the stream and swab it around
|
||||
to compensate for different endianness
|
||||
returns 0 if successful
|
||||
*/
|
||||
int
|
||||
pqGetInt(int* result, int bytes, FILE* f, FILE *debug)
|
||||
int pqGetInt(int* result, int bytes, FILE* f, FILE *debug)
|
||||
{
|
||||
int c;
|
||||
int p;
|
||||
int n;
|
||||
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
int retval = 0;
|
||||
|
||||
p = 0;
|
||||
n = 0;
|
||||
while (bytes && (c = getc(f)) != EOF)
|
||||
switch(bytes)
|
||||
{
|
||||
n |= (c & 0xff) << p;
|
||||
p += 8;
|
||||
bytes--;
|
||||
case 2:
|
||||
retval = pqGetShort(result, f);
|
||||
break;
|
||||
case 4:
|
||||
retval = pqGetLong(result, f);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "** int size %d not supported\n", bytes);
|
||||
retval = 1;
|
||||
}
|
||||
|
||||
if (bytes != 0)
|
||||
return 1;
|
||||
|
||||
*result = n;
|
||||
if (debug)
|
||||
fprintf(debug,"From backend (#)> %d\n",*result);
|
||||
return 0;
|
||||
fprintf(debug,"From backend (#%d)> %d\n", bytes, *result);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
pqPuts(const char* s, FILE *f, FILE *debug)
|
||||
/* --------------------------------------------------------------------- */
|
||||
int pqPuts(const char* s, FILE *f, FILE *debug)
|
||||
{
|
||||
if (f == NULL)
|
||||
return 1;
|
||||
|
||||
if (fputs(s,f) == EOF)
|
||||
if (fputs(s, f) == EOF)
|
||||
return 1;
|
||||
|
||||
fputc('\0',f); /* important to send an ending EOF since backend expects it */
|
||||
fputc('\0', f); /* important to send an ending \0 since backend expects it */
|
||||
fflush(f);
|
||||
|
||||
if (debug) {
|
||||
fprintf(debug, "To backend> %s\n", s);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
pqFlush(FILE *f, FILE *debug)
|
||||
/* --------------------------------------------------------------------- */
|
||||
void pqFlush(FILE *f, FILE *debug)
|
||||
{
|
||||
if (f)
|
||||
fflush(f);
|
||||
|
||||
if (debug)
|
||||
fflush(debug);
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
||||
|
Reference in New Issue
Block a user