/* * Copyright (c) 1996 Gunther Schadow. All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #pragma implementation #include "Xdr.h" typedef int (*fvoid_t)(); typedef int (*fvar_t)(...); typedef int (*fone_t)(void *); typedef int (*ftwo_t)(void *, int); static u_int sb_read(streambuf *sb, char *buf, size_t len) { return sb->xsgetn(buf, len); } static u_int sb_write(streambuf *sb, char *buf, size_t len) { return sb->xsputn(buf, len); } Xdr::Xdr(int fd, op_t op) { xdrrec_create(this, 0, 0, (char *)fd, (fvar_t)read, (fvar_t)write); x_op = (xdr_op)op; } Xdr::Xdr(streambuf *sb, op_t op) { xdrrec_create(this, 0, 0, (char *)sb, (fvar_t)sb_read, (fvar_t)sb_write); x_op = (xdr_op)op; } Xdr::~Xdr() { if(x_op == XDR_ENCODE) xdrrec_endofrecord(this, TRUE); // xdr_destroy(this); gives too many arguments to function!; if(x_ops->x_destroy != NULL) (*(fone_t)x_ops->x_destroy)(this); } void Xdr::decode() { if(x_op == XDR_ENCODE) xdrrec_endofrecord(this, TRUE); bool must_skip = ( x_op != XDR_DECODE ); x_op = XDR_DECODE; if(must_skip) xdrrec_skiprecord(this); } bool Xdr::code(bool &x) { int val = x; if(! xdr_bool(this, &val)) return false; x = val; return true; } #ifdef XDR_TEST #include main() { int i = 0x31323334; Xdr out(1, Xdr::op_encode); cerr << out.code(i) << endl; out.nocode(); cerr << out.code(i) << endl; out.flush(); out.encode(); cerr << out.code(i) << endl; } #endif