fe-misc.c 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*-------------------------------------------------------------------------
 *
 *   FILE
 *	fe-misc.c
 *
 *   DESCRIPTION
 *       miscellaneous useful functions
 *   these routines are analogous to the ones in libpq/pqcomm.c
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
14
 *    $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.5 1997/03/16 18:51:29 scrappy Exp $
15 16 17 18 19 20
 *
 *-------------------------------------------------------------------------
 */

#include <stdlib.h>
#include <stdio.h>
21 22 23

#include "postgres.h"

M
Marc G. Fournier 已提交
24
#include "libpq-fe.h"
25

26
/* --------------------------------------------------------------------- */
27 28 29 30 31
/* pqGetc:
   get a character from stream f

   if debug is set, also echo the character fetched
*/
32
int pqGetc(FILE* fin, FILE* debug)
33 34 35 36
{
  int c;

  c = getc(fin);
37

38
  if (debug && c != EOF)
39
    fprintf(debug, "From backend> %c\n", c);
40
    
41 42 43
  return c;
}

44
/* --------------------------------------------------------------------- */
45 46 47 48 49
/* pqPutnchar:
   send a string of exactly len length into stream f 

   returns 1 if there was an error, 0 otherwise.
*/
50
int pqPutnchar(const char* s, int len, FILE *f, FILE *debug)
51 52 53 54
{
    if (f == NULL)
	return 1;

55 56 57 58
   if(debug)
      fprintf(debug, "To backend> %s\n", s);

   if(fwrite(s, 1, len, f) != len)
59
	    return 1;
60

61 62 63
    return 0;
}

64
/* --------------------------------------------------------------------- */
65 66 67
/* pqGetnchar:
   get a string of exactly len length from stream f 
*/
68
int pqGetnchar(char* s, int len, FILE *f, FILE *debug)
69
{
70
   int cnt;
71 72 73 74

  if (f == NULL)
    return 1;
  
75 76 77 78 79 80
   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);
81 82 83 84

  return 0;
}

85
/* --------------------------------------------------------------------- */
86 87 88
/* pqGets:
   get a string of up to length len from stream f
*/
89
int pqGets(char* s, int len, FILE *f, FILE *debug)
90 91
{
  int c;
92
   const char *str = s;
93 94 95 96 97 98 99

  if (f == NULL)
    return 1;
  
  while (len-- && (c = getc(f)) != EOF && c)
    *s++ = c;
  *s = '\0';
100 101 102 103
   /* mjl: actually needs up to len+1 bytes, is this okay? XXX */

   if (debug)
      fprintf(debug, "From backend> \"%s\"\n", str);
104 105 106 107

  return 0;
}

108
/* --------------------------------------------------------------------- */
109
/* pgPutInt
110 111
   send an integer of 2 or 4 bytes to the file stream, compensate
   for host endianness.
112 113
   returns 0 if successful, 1 otherwise
*/
114
int pqPutInt(const int integer, int bytes, FILE* f, FILE *debug)
115
{
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    int retval = 0;

    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 (debug) fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);

    return retval;
134 135
}

136
/* --------------------------------------------------------------------- */
137
/* pgGetInt 
138 139
   read a 2 or 4 byte integer from the stream and swab it around
   to compensate for different endianness
140 141
   returns 0 if successful 
*/
142
int pqGetInt(int* result, int bytes, FILE* f, FILE *debug)
143
{
144
    int retval = 0;
145
  
146
    switch(bytes)
147
    {
148 149 150 151 152 153 154 155 156
      	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;
157 158 159
    }

  if (debug)
160
    	fprintf(debug,"From backend (#%d)> %d\n", bytes, *result);
161

162 163
    return retval;
}
164

165 166
/* --------------------------------------------------------------------- */
int pqPuts(const char* s, FILE *f, FILE *debug)
167 168 169 170
{
  if (f == NULL)
    return 1;
  
171
   if (fputs(s, f) == EOF)
172 173
    return 1;

174
   fputc('\0', f); /* important to send an ending \0 since backend expects it */
175 176 177
  fflush(f);

  if (debug) {
178
      fprintf(debug, "To backend> %s\n", s);
179
  }
180
  
181 182 183
  return 0;
}

184 185
/* --------------------------------------------------------------------- */
void pqFlush(FILE *f, FILE *debug)
186 187 188
{
    if (f)
	fflush(f);
189

190 191 192
    if (debug)
	fflush(debug);
}
193

194
/* --------------------------------------------------------------------- */