File Coverage

util.c
Criterion Covered Total %
statement 21 54 38.8
branch 11 36 30.5
condition n/a
subroutine n/a
pod n/a
total 32 90 35.5


line stmt bran cond sub pod time code
1             /* --8<--8<--8<--8<--
2             *
3             * Copyright (C) 2000-2009 Smithsonian Astrophysical Observatory
4             *
5             * This file is part of IPC-XPA
6             *
7             * IPC-XPA is free software: you can redistribute it and/or modify
8             * it under the terms of the GNU General Public License as published by
9             * the Free Software Foundation, either version 3 of the License, or (at
10             * your option) any later version.
11             *
12             * This program is distributed in the hope that it will be useful,
13             * but WITHOUT ANY WARRANTY; without even the implied warranty of
14             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             * GNU General Public License for more details.
16             *
17             * You should have received a copy of the GNU General Public License
18             * along with this program. If not, see .
19             *
20             * -->8-->8-->8-->8-- */
21              
22             #include "EXTERN.h"
23             #include "perl.h"
24             #include "XSUB.h"
25              
26             #include "util.h"
27              
28             /* convert a hash to a string, with the format "key=val,key=val" */
29             char *
30 0           hash2str( HV* hash )
31             {
32             SV* val; /* temp for iterating over hash */
33             char* key; /* temp for iterating over hash */
34             I32 keylen; /* temp for iterating over hash */
35              
36 0           int len = 0; /* length of final string, including EOS */
37             int n; /* number of elements in hash */
38              
39             char* str; /* final string */
40             char* ptr; /* temp ptr */
41              
42             /* iterate over hash, determining the length of the final string */
43 0           hv_iterinit(hash);
44 0 0         while( val = hv_iternextsv(hash, &key, &keylen) )
45             {
46             /* complain if the value is undefined or if it's a reference */
47 0 0         if ( !SvOK(val) || SvROK(val) )
    0          
    0          
    0          
48 0           croak( "hash entry for `%s' not a scalar", key );
49              
50 0           n++;
51 0           len += keylen + SvCUR(val);
52             }
53              
54 0           len += n /* '=' */
55             + n-1 /* ',' */
56 0           + 1; /* EOS */
57              
58             /* now, fill in string */
59 0           New( 0, str, len, char );
60 0           ptr = str;
61              
62 0           hv_iterinit(hash);
63 0 0         while( val = hv_iternextsv(hash, &key, &keylen) )
64             {
65             STRLEN cur;
66             char *pv;
67              
68 0           strcpy(ptr, key);
69 0           ptr += keylen;
70 0           *ptr++ = '=';
71 0 0         pv = SvPV(val, cur);
72 0           strncpy(ptr, pv, cur);
73 0           ptr += cur;
74 0           *ptr++ = ',';
75             }
76              
77             /* the EOS position now contains a ',', and ptr is one
78             past that. fix that */
79 0           *--ptr = '\0';
80              
81 0           return str;
82             }
83              
84              
85             /* convert XPAGet client data to a Perl hash */
86             HV *
87 2           cdata2hash_Get( char *buf, int len, char *name, char *message )
88             {
89             SV *sv;
90             SV *ref;
91             /* create hash which will contain buf, name, message */
92 2           HV *hash = newHV();
93              
94             /* buf may be big, so try to get perl to use it directly */
95 2           sv = NEWSV(0,0);
96 2           sv_usepvn( sv, buf, len );
97 2 50         if ( NULL == hv_store( hash, "buf", 3, sv, 0 ) )
98 0           croak( "error storing length for response\n" );
99              
100 2 50         if ( NULL == hv_store( hash, "name", 4, newSVpv( name, 0 ), 0 ) )
101 0           croak( "error storing name for response\n" );
102              
103 2 100         if ( message )
104             {
105 1 50         if ( NULL == hv_store( hash, "message", 7, newSVpv( message, 0 ), 0 ) )
106 0           croak( "error storing message for response\n" );
107             }
108              
109 2           return hash;
110             }
111              
112             /* convert XPASet/XPAInfo/XPAAccess client data to a Perl hash */
113             HV *
114 6           cdata2hash_Set( char *name, char *message )
115             {
116             /* create hash which will contain name, message */
117 6           HV *hash = newHV();
118              
119 6 50         if ( NULL == hv_store( hash, "name", 4, newSVpv( name, 0 ), 0 ) )
120 0           croak( "error storing name for response\n" );
121              
122 6 50         if ( message )
123             {
124 0 0         if ( NULL == hv_store( hash, "message", 7, newSVpv( message, 0 ), 0 ) )
125 0           croak( "error storing message for response\n" );
126             }
127 6           return hash;
128             }
129              
130             /* convert XPALookup client data to a Perl hash */
131             HV *
132 1           cdata2hash_Lookup( char *class, char *name, char *method, char *info )
133             {
134             /* create hash which will contain name, message */
135 1           HV *hash = newHV();
136              
137 1 50         if ( NULL == hv_store( hash, "name", 4, newSVpv(name,0), 0 ) )
138 0           croak( "error storing name for response\n" );
139              
140 1 50         if ( NULL == hv_store( hash, "class", 5, newSVpv(class,0), 0 ) )
141 0           croak( "error storing class for response\n" );
142              
143 1 50         if ( NULL == hv_store( hash, "method", 6, newSVpv(method,0), 0 ) )
144 0           croak( "error storing method for response\n" );
145              
146 1 50         if ( NULL == hv_store( hash, "info", 4, newSVpv(info,0), 0 ) )
147 0           croak( "error storing info for response\n" );
148              
149 1           return hash;
150             }