www.pudn.com > CRGAB.zip > P2S.L


/* 
* File: P2S.L 
* 
* Program:  
*		 		P2S 
* 
*		 printf() to streams i/o converter 
* 
* Description: 
* 
* Converts printf() calls to << operators 
* Uses Lex to find printf's, fprintf's and sprintf's in C source 
* and then uses p2s() call to convert the printf arguments to C++ streams 
* formatted I/O. 
* Does handle things like %6.2f (width and precision flags) 
* by generating the appropriate streams manipulators - setw() 
* and setprecision(). 
* Does NOT handle things like %-6.2f (yet) since there are no manipulators 
* for left and right justification (if you decide to enhance this to 
* handle this or anything else please submit back to CUG or me) 
* 
* stdin reads inoriginal C source, converted source goes to stdout. 
* 
* Usage:  
* 
* P2S < {.c file with printfs} > {file with streams i/o} 
* Options: 
*	f - don't convert fprintf()'s 
*	p - don't convert printf()'s 
*	s - don't convert sprintf()'s 
*	i - converts by doing  
*		#ifdef __cplusplus 
*		  ... streams code .. 
*		#else 
*		  ... old printf code ... 
*		#endif 
*	    to maintain backward compatibility with C (if desired) 
* 
* Example: 
* 
* For example to only convert fprintf calls and maintain C compatibility 
* the invocation is: 
* 
* 	p2s p s i < foo.c >foo.cpp 
*	 	 
*	 
* Construction: 
* 
* Just flex (or lex) this file, and compile the resultant C source.  
* MKP2S.BAT shows how to make program with Flex and Turbo C. 
* 
* Restrictions: 
*  
* You may use this program without restriction as a tool in your own  
* development efforts.  
* You MAY NOT redistribute either the source or executables, in modified 
* or unmodified form, for any amount of money or along with any other  
* commercial package. You must include this copyright in any program 
* which uses this source and you may not distribute such programs  
* commercially. No restrictions apply to any programs which you converted 
* using this tool (using the tool is unrestricted, distributing it or using 
* its source is). 
*  
* Copyright (c) 1990. Adam Blum, Retrieval Systems Corporation,(703)-524-4534 
*/ 
 
%{ 
#include 
#include 
/* prototypes */ 
int p2s(char *s1,char *s2); 
char *prs(char *p,char *s); 
char *getfmt(char *p,int *prec,int *width); 
void strrepl(char *s,char *srch,char *repl); 
 
#define QUOTE 34 
 
/* variables */ 
int ifdef=0,sprt=1,fprt=1,prt=1,i; 
 
%} 
 
%% 
/*		PATTERNS			*/ 
 
#define.*\n	{ECHO;} 
printf\([^\;]*\)\; { 
		if(prt) 
			p2sprt(yytext,yyleng,yytext+7); 
		else { 
			ECHO; 
		} 
		return 1;} 
fprintf\([^\;]*\)\; { 
		if(fprt) 
			p2sprt(yytext,yyleng,yytext+8); 
		else{ 
			ECHO; 
		} 
		return 1;} 
 
sprintf\([^\;]*\)\; { 
		if(sprt) 
			p2sprt(yytext,yyleng,yytext+8); 
		else { 
			ECHO; 
		} 
		return 1;} 
.|\n	{ECHO;return 1;} 
%% 
/*		FUNCTIONS			*/ 
main(int argc,char **argv) 
{ 
	for(i=0;i{output with streams operators}\n"); 
	for(;;){ 
		if(yylex()<1)break; 
	} 
} 
 
ctparens(char *s,int n) 
{ 
	int i,ct=0; 
	for(i=0;i=0) 
				sprintf(s2+strlen(s2), 
					"<=0) 
				sprintf(s2+strlen(s2), 
					"<