String-Random-Regexp-regxstring.xs

filter: Statementstmt 100% 19 / 19 Branchbran 52.5% 21 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 67.8% 40 / 59 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« harness.cpp ↑ Summary regxstring_impl.h »
1 // Function name mangling!!!!!!!!!!!
2 // use this so that function names in object files are as
3 // specified in the proto
4  
5 /* don't forget that we are using a C++ compiler and so these
6 need to be protected else ... function-name mangling ooouuouuuuoouu :
7 */
8  
9 /*
10 our $VERSION = '1.04';
11 */
12  
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 #include "EXTERN.h"
17 #include "perl.h"
18 #include "XSUB.h"
19  
20 #include <stdio.h>
21 #include <stdlib.h>
22  
23 /* helper func to check if SV contains undef */
24 183 int _SV_contains_undef(SV *ansv){ SvGETMAGIC(ansv); return(!SvOK(ansv)); }
25  
26 #ifdef __cplusplus
27 } // extern "C" {
28 #endif
29  
30 #include "harness.h"
31  
32 /* Perl redefines libc's malloc() and free().
33 When memory is allocated externally,
34 we must use free() which is Perl's.
35 Do the same for malloc() if you need to use it.
36 Not any more with this:
37 */
38 17788 void MyFree( void *p ) {
39 #undef free
40 17788 free( p );
41 17788 }
42  
43 /* NOTE: the ... in the function signature below
44 denotes optional; parameters: int debug
45 */
46  
47 MODULE = String::Random::Regexp::regxstring PACKAGE = String::Random::Regexp::regxstring
48  
49 PROTOTYPES: ENABLE
50  
51 AV *
52 generate_random_strings_xs(SV *regx_SV, int N, ...)
53 PROTOTYPE: @
54  
55 PREINIT:
56 STRLEN regxstr_len;
57 183 ▶ int debug = 0; // default value for optional parameter, see above
Branch: 1 branch
BranchTrueFalseTotal
int debug = 0; // default value for optional parameter, see above 183 0 183
58 INIT:
59 // TODO: try using croak
60 185 ▶ if( _SV_contains_undef(regx_SV) ){ fprintf(stderr, "generate_random_strings_xs() : error, input regexp string can not be undefined.\n"); XSRETURN_UNDEF; }
Branch: 3 branches
BranchTrueFalseTotal
if( _SV_contains_undef(regx_SV) ){ fprintf(stderr, "generate_random_strings_xs() : error, input regexp string can not be undefined.\n"); XSRETURN_UNDEF; } 183 0 183
if( _SV_contains_undef(regx_SV) ){ fprintf(stderr, "generate_random_strings_xs() : error, input regexp string can not be undefined.\n"); XSRETURN_UNDEF; } 0 183 183
if( _SV_contains_undef(regx_SV) ){ fprintf(stderr, "generate_random_strings_xs() : error, input regexp string can not be undefined.\n"); XSRETURN_UNDEF; } 0 0 0
61 183 ▶ if( N < 1 ){ fprintf(stderr, "generate_random_strings_xs() : error, the number of strings to return must be a positive integer (and not %d).\n", N); XSRETURN_UNDEF; }
Branch: 2 branches
BranchTrueFalseTotal
if( N < 1 ){ fprintf(stderr, "generate_random_strings_xs() : error, the number of strings to return must be a positive integer (and not %d).\n", N); XSRETURN_UNDEF; } 2 181 183
if( N < 1 ){ fprintf(stderr, "generate_random_strings_xs() : error, the number of strings to return must be a positive integer (and not %d).\n", N); XSRETURN_UNDEF; } 2 0 2
62 CODE:
63 181 ▶ if( items > 2 ){
Branch: 1 branch
BranchTrueFalseTotal
if( items > 2 ){ 181 0 181
64 // we have the optional parameter
65 181 ▶ debug = SvIV(ST(2));
Branch: 1 branch
BranchTrueFalseTotal
debug = SvIV(ST(2)); 181 0 181
66 }
67  
68 181 char *regxstr = SvUTF8(regx_SV)
69 245 ▶ ? SvPVutf8(regx_SV, regxstr_len)
Branch: 1 branch
BranchTrueFalseTotal
? SvPVutf8(regx_SV, regxstr_len) 64 117 181
70 117 : SvPVbyte(regx_SV, regxstr_len)
71 ;
72 /* this is in harness.cpp */
73 /* we need to free char **results when done
74 they were allocated in harness.cpp with malloc()
75 */
76 181 ▶ char **results = regxstring_generate_random_strings_from_regex(
Branch: 1 branch
BranchTrueFalseTotal
char **results = regxstring_generate_random_strings_from_regex( 181 0 181
77 regxstr,
78 N,
79 debug
80 );
81 181 ▶ if( results == NULL ){ fprintf(stderr, "generate_random_strings_xs() : error, call to 'regxstring_generate_random_strings_from_regex()' has failed for the regex '%s' and N=%d.\n", regxstr, N); XSRETURN_UNDEF; }
Branch: 2 branches
BranchTrueFalseTotal
if( results == NULL ){ fprintf(stderr, "generate_random_strings_xs() : error, call to 'regxstring_generate_random_strings_from_regex()' has failed for the regex '%s' and N=%d.\n", regxstr, N); XSRETURN_UNDEF; } 0 181 181
if( results == NULL ){ fprintf(stderr, "generate_random_strings_xs() : error, call to 'regxstring_generate_random_strings_from_regex()' has failed for the regex '%s' and N=%d.\n", regxstr, N); XSRETURN_UNDEF; } 0 0 0
82
83 181 ▶ RETVAL = (AV*)sv_2mortal((SV*)newAV());
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = (AV*)sv_2mortal((SV*)newAV()); 181 0 181
RETVAL = (AV*)sv_2mortal((SV*)newAV()); 181 0 181
84 17788 ▶ for(int i=0;i<N;i++){
Branch: 1 branch
BranchTrueFalseTotal
for(int i=0;i<N;i++){ 17607 181 17788
85 /* newSVpvn_flags() and newSVpvn() create a new Perl string from each results string
86 So, we can safely free results[i]
87 */
88 17607 ▶ av_push(RETVAL, newSVpvn_flags(results[i], strlen(results[i]), SVf_UTF8));
Branch: 2 branches
BranchTrueFalseTotal
av_push(RETVAL, newSVpvn_flags(results[i], strlen(results[i]), SVf_UTF8)); 17607 0 17607
av_push(RETVAL, newSVpvn_flags(results[i], strlen(results[i]), SVf_UTF8)); 17607 0 17607
89 17607 ▶ if( results[i] != NULL ) MyFree(results[i]);
Branch: 2 branches
BranchTrueFalseTotal
if( results[i] != NULL ) MyFree(results[i]); 17607 0 17607
if( results[i] != NULL ) MyFree(results[i]); 17607 0 17607
90 }
91 181 ▶ MyFree(results);
Branch: 1 branch
BranchTrueFalseTotal
MyFree(results); 181 0 181
92 // end of program
93  
94 OUTPUT:
95 RETVAL
« harness.cpp ↑ Summary regxstring_impl.h »