]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/fe-misc.c
From: Phil Thompson <phil@river-bank.demon.co.uk>
[postgresql] / src / interfaces / libpq / fe-misc.c
1 /*-------------------------------------------------------------------------
2  *
3  *       FILE
4  *              fe-misc.c
5  *
6  *       DESCRIPTION
7  *               miscellaneous useful functions
8  *       these routines are analogous to the ones in libpq/pqcomm.c
9  *
10  * Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  *        $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.9 1998/01/26 01:42:36 scrappy Exp $
15  *
16  *-------------------------------------------------------------------------
17  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "postgres.h"
23
24 #include "libpq-fe.h"
25
26 /* --------------------------------------------------------------------- */
27 /* pqGetc:
28    get a character from stream f
29
30    if debug is set, also echo the character fetched
31 */
32 int
33 pqGetc(FILE *fin, FILE *debug)
34 {
35         int                     c;
36
37         c = getc(fin);
38
39         if (debug && c != EOF)
40                 fprintf(debug, "From backend> %c\n", c);
41
42         return c;
43 }
44
45 /* --------------------------------------------------------------------- */
46 /* pqPutnchar:
47    send a string of exactly len length into stream f
48
49    returns 1 if there was an error, 0 otherwise.
50 */
51 int
52 pqPutnchar(const char *s, int len, FILE *f, FILE *debug)
53 {
54         if (debug)
55                 fprintf(debug, "To backend> %s\n", s);
56
57         return (pqPutNBytes(s, len, f) == EOF ? 1 : 0);
58 }
59
60 /* --------------------------------------------------------------------- */
61 /* pqGetnchar:
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'.
64 */
65 int
66 pqGetnchar(char *s, int len, FILE *f, FILE *debug)
67 {
68         int status;
69
70         status = pqGetNBytes(s, len, f);
71
72         if (debug)
73                 fprintf(debug, "From backend (%d)> %s\n", len, s);
74
75         return (status == EOF ? 1 : 0);
76 }
77
78 /* --------------------------------------------------------------------- */
79 /* pqGets:
80    get a string of up to length len from stream f
81 */
82 int
83 pqGets(char *s, int len, FILE *f, FILE *debug)
84 {
85         int status;
86
87         status = pqGetString(s, len, f);
88
89         if (debug)
90                 fprintf(debug, "From backend> \"%s\"\n", s);
91
92         return (status == EOF ? 1 : 0);
93 }
94
95 /* --------------------------------------------------------------------- */
96 /* pgPutInt
97    send an integer of 2 or 4 bytes to the file stream, compensate
98    for host endianness.
99    returns 0 if successful, 1 otherwise
100 */
101 int
102 pqPutInt(const int integer, int bytes, FILE *f, FILE *debug)
103 {
104         int                     retval = 0;
105
106         switch (bytes)
107         {
108                 case 2:
109                         retval = pqPutShort(integer, f);
110                         break;
111                 case 4:
112                         retval = pqPutLong(integer, f);
113                         break;
114                 default:
115                         fprintf(stderr, "** int size %d not supported\n", bytes);
116                         retval = 1;
117         }
118
119         if (debug)
120                 fprintf(debug, "To backend (%d#)> %d\n", bytes, integer);
121
122         return retval;
123 }
124
125 /* --------------------------------------------------------------------- */
126 /* pgGetInt
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
130 */
131 int
132 pqGetInt(int *result, int bytes, FILE *f, FILE *debug)
133 {
134         int                     retval = 0;
135
136         switch (bytes)
137         {
138                 case 2:
139                         retval = pqGetShort(result, f);
140                         break;
141                 case 4:
142                         retval = pqGetLong(result, f);
143                         break;
144                 default:
145                         fprintf(stderr, "** int size %d not supported\n", bytes);
146                         retval = 1;
147         }
148
149         if (debug)
150                 fprintf(debug, "From backend (#%d)> %d\n", bytes, *result);
151
152         return retval;
153 }
154
155 /* --------------------------------------------------------------------- */
156 int
157 pqPuts(const char *s, FILE *f, FILE *debug)
158 {
159         if (pqPutString(s, f) == EOF)
160                 return 1;
161
162         fflush(f);
163
164         if (debug)
165                 fprintf(debug, "To backend> %s\n", s);
166
167         return 0;
168 }
169
170 /* --------------------------------------------------------------------- */
171 void
172 pqFlush(FILE *f, FILE *debug)
173 {
174         if (f)
175                 fflush(f);
176
177         if (debug)
178                 fflush(debug);
179 }
180
181 /* --------------------------------------------------------------------- */