Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / lib / db / tt_db_rpc_message_routines.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: tt_db_rpc_message_routines.C /main/3 1995/10/23 10:05:09 rswiston $                                                         
28 /* @(#)tt_db_rpc_message_routines.C     1.10 94/11/17
29  * tt_db_rpc_message_routines.cc - Defines routines for converting TT 
30  *                                 message classes to RPC arguments.
31  *
32  * Copyright (c) 1992 by Sun Microsystems, Inc.
33  */
34
35 #include "mp/mp_message.h"
36 #include "util/tt_string.h"
37 #include "util/tt_xdr_utils.h"
38 #include "db/tt_db_rpc_message_routines.h"
39
40 // If this file is being compiled with -g, then DEFINE_NEW_AND_DELETE
41 // must be defined in order to prevent a dependency on the libC library.
42 // The dependency is due to the fact that cfront generates an extern
43 // reference to vec_new and vec_delete if it sees any access to arrays
44 // of calsses or structures.  In the case of this file, tt_message_list
45 // contains a pointer to a structure that causes the references to be
46 // generated.
47 #ifdef DEFINE_NEW_AND_DELETE
48 extern "C" {
49 void *__vec_new (void *, int , int , void *)
50 { return (void *)NULL; }
51
52 void __vec_delete (void *, int , int , void *, int , int )
53 {}
54 }
55 #endif
56
57 void
58 _tt_get_rpc_message (const _tt_message &rpc_message,
59 _Tt_message_ptr   &message)
60 {
61         message = (_Tt_message *)NULL;
62
63         if (rpc_message.body.body_len) {
64                 XDR xdrs;
65                 
66                 xdrmem_create(&xdrs,
67                               rpc_message.body.body_val,
68                               (u_int)rpc_message.body.body_len,
69                               XDR_DECODE);
70                 
71                 message = new _Tt_message;
72                 (void)message->xdr(&xdrs);
73         }
74 }
75
76 void
77 _tt_get_rpc_messages (const _tt_message_list &rpc_messages,
78                       _Tt_message_list_ptr   &messages)
79 {
80         messages = (_Tt_message_list *)NULL;
81
82         if (rpc_messages.messages_len) {
83                 messages = new _Tt_message_list;
84                 
85                 for (int i=0; i < rpc_messages.messages_len; i++) {
86                         _Tt_message_ptr message_ptr;
87
88                         _tt_get_rpc_message(rpc_messages.messages_val [i],
89                                             message_ptr);
90                         (void)messages->append(message_ptr);
91                 }
92         }
93 }
94
95 _Tt_db_results
96 _tt_set_rpc_message (const _Tt_message_ptr &message,
97                      _tt_message           &rpc_message)
98 {
99         if (message.is_null()) {
100                 rpc_message.body.body_val = (char *)NULL;
101                 rpc_message.body.body_len = 0;
102         } else {
103                 XDR    xdrs;
104                 _Tt_xdr_size_stream xdrsz;
105                 u_int  length;
106                 
107                 if (!message->xdr((XDR *)xdrsz)) {
108                         return TT_DB_ERR_ILLEGAL_MESSAGE;
109                 } else {
110                         length = (u_int)xdrsz.getsize();
111                 }
112
113                 _Tt_string temp_string((int)length);
114                 xdrmem_create(&xdrs, (char *)temp_string, length, XDR_ENCODE);
115                 if (!message->xdr(&xdrs)) {
116                         return TT_DB_ERR_ILLEGAL_MESSAGE;
117                 }
118                 
119                 rpc_message.body.body_val = (char *)malloc(length+1);
120                 (void)memcpy(rpc_message.body.body_val, (char *)temp_string, length);
121                 rpc_message.body.body_val[length] = '\0';
122                 rpc_message.body.body_len = length;
123         }
124
125         return TT_DB_OK;
126 }