1 /* const2perl.h -- For converting C constants into Perl constant subs
2 * (usually via XS code but can just write Perl code to stdout). */
5 /* #ifndef _INCLUDE_CONST2PERL_H
6 * #define _INCLUDE_CONST2PERL_H 1 */
8 #ifndef CONST2WRITE_PERL /* Default is "const to .xs": */
10 # define newconst( sName, sFmt, xValue, newSV ) \
11 newCONSTSUB( mHvStash, sName, newSV )
13 # define noconst( const ) av_push( mAvExportFail, newSVpv(#const,0) )
15 # define setuv(u) do { \
16 mpSvNew= newSViv(0); sv_setuv(mpSvNew,u); \
25 * # define stderr (&_iob[2])
30 # include <stdio.h> /* Probably already included, but shouldn't hurt */
31 # include <errno.h> /* Possibly already included, but shouldn't hurt */
33 # define newconst( sName, sFmt, xValue, newSV ) \
34 printf( "sub %s () { " sFmt " }\n", sName, xValue )
36 # define noconst( const ) printf( "push @EXPORT_FAIL, '%s';\n", #const )
38 # define setuv(u) /* Nothing */
54 escquote( const char *sValue )
56 Size_t lLen= 1+2*strlen(sValue);
57 char *sEscaped= (char *) malloc( lLen );
58 char *sNext= sEscaped;
59 if( NULL == sEscaped ) {
60 fprintf( stderr, "Can't allocate %"UVuf"-byte buffer (errno=%d)\n",
64 while( '\0' != *sValue ) {
70 *(sNext++)= *(sValue++);
83 char msBuf[64]; /* Must fit sprintf of longest NV */
84 #ifndef CONST2WRITE_PERL
88 _const2perl::_const2perl( char *sModName ) {
89 mHvStash= gv_stashpv( sModName, TRUE );
90 SV **pSv= hv_fetch( mHvStash, "EXPORT_FAIL", 11, TRUE );
92 char *sVarName= (char *) malloc( 15+strlen(sModName) );
93 strcpy( sVarName, sModName );
94 strcat( sVarName, "::EXPORT_FAIL" );
95 gv= gv_fetchpv( sVarName, 1, SVt_PVAV );
96 mAvExportFail= GvAVn( gv );
99 _const2perl::_const2perl( char *sModName ) {
100 ; /* Nothing to do */
102 #endif /* CONST2WRITE_PERL */
103 void mkconst( char *sName, unsigned long uValue ) {
105 newconst( sName, "0x%"UVxf, uValue, mpSvNew );
107 void mkconst( char *sName, unsigned int uValue ) {
109 newconst( sName, "0x%"UVxf, uValue, mpSvNew );
111 void mkconst( char *sName, unsigned short uValue ) {
113 newconst( sName, "0x%"UVxf, uValue, mpSvNew );
115 void mkconst( char *sName, long iValue ) {
116 newconst( sName, "%"IVdf, iValue, newSViv(iValue) );
118 void mkconst( char *sName, int iValue ) {
119 newconst( sName, "%"IVdf, iValue, newSViv(iValue) );
121 void mkconst( char *sName, short iValue ) {
122 newconst( sName, "%"IVdf, iValue, newSViv(iValue) );
124 void mkconst( char *sName, double nValue ) {
125 newconst( sName, "%s",
126 Gconvert(nValue,NV_DIG,0,msBuf), newSVnv(nValue) );
128 void mkconst( char *sName, char *sValue ) {
129 newconst( sName, "'%s'", escquote(sValue), newSVpv(sValue,0) );
131 void mkconst( char *sName, const void *pValue ) {
133 newconst( sName, "0x%"UVxf, (UV)(pValue), mpSvNew );
136 * HAS_QUAD only means pack/unpack deal with them, not that SVs can.
137 * void mkconst( char *sName, Quad_t *qValue ) {
138 * newconst( sName, "0x%"QVxf, qValue, newSVqv(qValue) );
140 *#endif / * HAS_QUAD */
143 #define START_CONSTS( sModName ) _const2perl const2( sModName );
144 #define const2perl( const ) const2.mkconst( #const, const )
146 #else /* __cplusplus */
148 # ifndef CONST2WRITE_PERL
149 # define START_CONSTS( sModName ) \
150 HV *mHvStash= gv_stashpv( sModName, TRUE ); \
153 { char *sVarName= malloc( 15+strlen(sModName) ); \
155 strcpy( sVarName, sModName ); \
156 strcat( sVarName, "::EXPORT_FAIL" ); \
157 gv= gv_fetchpv( sVarName, 1, SVt_PVAV ); \
158 mAvExportFail= GvAVn( gv ); \
161 # define START_CONSTS( sModName ) /* Nothing */
164 #define const2perl( const ) do { \
166 newconst( #const, "%"IVdf, const, newSViv((IV)const) ); \
168 setuv( (UV)const ); \
169 newconst( #const, "0x%"UVxf, const, mpSvNew ); \
173 #endif /* __cplusplus */
177 //#include <const2perl.h>
179 // START_CONSTS( "Package::Name" ) /* No ";" */
181 // const2perl( $const );
183 // noconst( $const );
186 // sub ? { my( $sConstName )= @_;
187 // return $sConstName; # "#ifdef $sConstName"
188 // return FALSE; # Same as above
189 // return "HAS_QUAD"; # "#ifdef HAS_QUAD"
190 // return "#if 5.04 <= VERSION";
192 // return 1; # No #ifdef
193 /* #endif / * _INCLUDE_CONST2PERL_H */