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