xs/shared/DVBT-common.h

filter: Statementstmt 0.0% 0 / 23 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 47 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/DVBT-record.c ↑ Summary xs/DVBT-tuning.c »
1 /*---------------------------------------------------------------------------------------------------*/
2 #include <unistd.h>
3 #include "list.h"
4  
5 /*---------------------------------------------------------------------------------------------------*/
6  
7 /** ARRAY store macros **/
8  
9 #define AVS_H(arr, h) av_push(arr, newRV((SV *)h))
10 #define AVS_A(arr, a) av_push(arr, newRV((SV *)a))
11 #define AVS_I(arr, i) av_push(arr, newSViv(i))
12 #define AVS_S(arr, s) av_push(arr, newSVpv(s, 0))
13  
14  
15 /** HASH store macros **/
16  
17 /* Use 'name' as structure field name AND HASH key name */
18 #define HVS(h, name, sv) hv_store(h, #name, sizeof(#name)-1, sv, 0)
19 #define HVS_S(h, sp, name) if (sp->name) hv_store(h, #name, sizeof(#name)-1, newSVpv(sp->name, 0), 0)
20 #define HVS_I(h, sp, name) if (sp->name >= 0) hv_store(h, #name, sizeof(#name)-1, newSViv(sp->name), 0)
21 #define HVS_BIT(h, var, name) hv_store(h, #name, sizeof(#name)-1, newSViv(var & name ? 1 : 0), 0)
22  
23 /* Specify the structure field name and HASH key name separately */
24 #define HVSN_S(h, sp, name, key) if (sp->name) hv_store(h, #key, sizeof(#key)-1, newSVpv(sp->name, 0), 0)
25 #define HVSN_I(h, sp, name, key) if (sp->name >= 0) hv_store(h, #key, sizeof(#key)-1, newSViv(sp->name), 0)
26  
27 /* Convert string before storing in hash */
28 #define HVS_STRING(h, sp, name) hv_store(h, #name, sizeof(#name)-1, newSVpv(_to_string(sp->name), 0), 0)
29 #define HVS_STRING_STRIP(h, sp, name) hv_store(h, #name, sizeof(#name)-1, newSVpv(_to_string_strip(sp->name), 0), 0)
30  
31 /* non-struct member versions */
32 #define HVS_INT(h, name, i) hv_store(h, #name, sizeof(#name)-1, newSViv(i), 0)
33 #define HVS_STR(h, name, s) hv_store(h, #name, sizeof(#name)-1, newSVpv(s, 0), 0)
34  
35  
36 /** HASH read macros **/
37 #define HVF_I(hv,var) \
38 if ( (val = hv_fetch (hv, #var, sizeof (#var) - 1, 0)) ) { \
39 if ( val != NULL ) { \
40 var = SvIV (*val); \
41 if (DVBT_DEBUG) fprintf(stderr, " set %s = %d\n", #var, var); \
42 } \
43 }
44  
45 #define HVF_SV(hv,var) \
46 if ( (val = hv_fetch (hv, #var, sizeof (#var) - 1, 0)) ) { \
47 if ( val != NULL ) { \
48 var = SvSV (*val); \
49 } \
50 }
51  
52 #define HVF_IV(hv,var,ival) \
53 if ( (val = hv_fetch (hv, #var, sizeof (#var) - 1, 0)) ) { \
54 if ( val != NULL ) { \
55 ival = SvIV (*val); \
56 } \
57 }
58  
59 #define HVF_SVV(hv,var,sval) \
60 if ( (val = hv_fetch (hv, #var, sizeof (#var) - 1, 0)) ) { \
61 if ( val != NULL ) { \
62 sval = (*val); \
63 } \
64 }
65  
66 #define HVF(hv, var) hv_fetch (hv, #var, sizeof (#var) - 1, 0)
67  
68  
69 /* get the HASH ref using the specified key. If not currently set, then create a new HASH and add it to the parent */
70 #define GET_HREF(hv, key, var) \
71 if ( (val = hv_fetch (hv, #key, sizeof (#key) - 1, 0)) ) { \
72 if ( val != NULL ) { \
73 var = (HV *)sv_2mortal(*val); \
74 } \
75 else { \
76 var = (HV *)sv_2mortal((SV *)newHV()); \
77 hv_store(hv, #key, sizeof(#key)-1, newRV((SV *)var), 0) ; \
78 } \
79 }
80  
81  
82 /*---------------------------------------------------------------------------------------------------*/
83 // Copies string into a static area so it can be used for creating Perl hash entries
84 #define TO_STRING_MAX 8191
85 static char ret_str[TO_STRING_MAX+1] ;
86 0 static char *_to_string(char *str)
87 {
88 0 int i, j, len = strlen(str);
89  
90 0 ▶ for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++) 0 0 0
for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++) 0 0 0
91 {
92 0 ret_str[j++] = str[i] ;
93  
94 /* terminate */
95 0 ret_str[j] = 0 ;
96 }
97 0 return ret_str ;
98 }
99  
100 /*---------------------------------------------------------------------------------------------------*/
101 // Copies string into a static area so it can be used for creating Perl hash entries. Also strips out
102 // any invalid chars
103 static unsigned valid_char[0xff] ;
104 static unsigned valid_char_init = 0 ;
105 0 static char *_to_string_strip(char *str)
106 {
107 0 int i, j, len = strlen(str);
108  
109 // set up lookup table once
110 0 ▶ if (!valid_char_init)
Branch: 1 branch
BranchTrueFalseTotal
if (!valid_char_init) 0 0 0
111 {
112 0 ▶ for (i=0; (i < 0xff); i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=0; (i < 0xff); i++) 0 0 0
113 {
114 0 valid_char[i] = 0 ;
115 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ( 0 0 0
116 0 ▶ ((i >= ' ') && (i <= '~'))
Branch: 1 branch
BranchTrueFalseTotal
((i >= ' ') && (i <= '~')) 0 0 0
117 0 ▶ || (i == '\n')
Branch: 1 branch
BranchTrueFalseTotal
|| (i == '\n') 0 0 0
118 0 ▶ || (i == '\r')
Branch: 1 branch
BranchTrueFalseTotal
|| (i == '\r') 0 0 0
119 0 ▶ || (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
|| (i == 0) 0 0 0
120 )
121 {
122 0 valid_char[i] = 1 ;
123 }
124 }
125 0 valid_char_init = 1 ;
126 }
127  
128 // copy valid chars ro destination
129 0 ▶ for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++) 0 0 0
for (i=0, j=0; (i < len) && (i < TO_STRING_MAX); i++) 0 0 0
130 {
131 0 ▶ if (valid_char[(unsigned char)(str[i]) & 0xff])
Branch: 1 branch
BranchTrueFalseTotal
if (valid_char[(unsigned char)(str[i]) & 0xff]) 0 0 0
132 {
133 0 ret_str[j++] = str[i] ;
134 }
135  
136 /* terminate */
137 0 ret_str[j] = 0 ;
138 }
139 0 return ret_str ;
140 }
141  
« xs/DVBT-record.c ↑ Summary xs/DVBT-tuning.c »