rewrite format_for_std_stringout(), eliminate use of 'tmpstr' buffer

This commit is contained in:
Mark Pictor
2012-02-16 20:51:13 -05:00
parent 3fd222218f
commit 1cc2774b52
3 changed files with 28 additions and 65 deletions
+18 -23
View File
@@ -70,6 +70,9 @@ void TypeBody_Description( TypeBody body, char * buf );
* Turn the string into a new string that will be printed the same as the
* original string. That is, turn backslash into a quoted backslash and
* turn \n into "\n" (i.e. 2 chars).
*
* Mostly replaced by format_for_std_stringout, below. This function is
* still used in one place in ENTITYincode_print().
*/
char * format_for_stringout( char * orig_buf, char * return_buf ) {
char * optr = orig_buf;
@@ -94,34 +97,29 @@ char * format_for_stringout( char * orig_buf, char * return_buf ) {
}
/**
* Like format_for_stringout, but splits the static string up
* Like format_for_stringout above, but splits the static string up
* into numerous small ones that are appended to a std::string named
* 'str'. It is assumed that this string already exists and is empty.
*
* This version takes a file pointer and eliminates use of the temp buffer.
*/
char * format_for_std_stringout( char * orig_buf, char * return_buf ) {
char * optr = orig_buf;
char * rptr = return_buf;
void format_for_std_stringout( FILE * f, const char * orig_buf ) {
const char * optr = orig_buf;
char * s_end = "\\n\" );\n";
char * s_begin = " str.append( \"";
strcpy( rptr, s_begin );
rptr += strlen( rptr );
fprintf(f, "%s", s_begin );
while( *optr ) {
if( *optr == '\n' ) {
strcpy( rptr, s_end );
strcat( rptr, s_begin );
rptr += strlen( rptr ) - 1;
fprintf( f, "%s", s_end );
fprintf( f, "%s", s_begin );
} else if( *optr == '\\' ) {
*rptr = '\\';
rptr++;
*rptr = '\\';
fprintf( f, "\\\\" );
} else {
*rptr = *optr;
fprintf( f, "%c", *optr );
}
rptr++;
optr++;
}
strcpy( rptr, s_end );
return return_buf;
fprintf( f, s_end );
}
void USEREFout( Schema schema, Dictionary refdict, Linked_List reflist, char * type, FILE * file ) {
@@ -2017,11 +2015,10 @@ void ENTITYincode_print( Entity entity, FILE * file, Schema schema ) {
fprintf( file, " str.clear();\n str.append( \"ABSTRACT SUPERTYPE OF ( \" );\n" );
tmp = SUBTYPEto_string( entity->u.entity->subtype_expression );
tmp2 = ( char * )malloc( sizeof( char ) * ( strlen( tmp ) + BUFSIZ ) );
fprintf( file, "%s\n str.append( \")\" );\n", format_for_std_stringout( tmp, tmp2 ) );
format_for_std_stringout( file, tmp );
fprintf( file, "\n str.append( \")\" );\n" );
fprintf( file, " %s::%s%s->AddSupertype_Stmt( str );", schema_name, ENT_PREFIX, entity_name );
free( tmp );
free( tmp2 );
} else {
fprintf( file, " %s::%s%s->AddSupertype_Stmt( \"ABSTRACT SUPERTYPE\" );\n",
schema_name, ENT_PREFIX, entity_name );
@@ -2030,12 +2027,10 @@ void ENTITYincode_print( Entity entity, FILE * file, Schema schema ) {
if( entity->u.entity->subtype_expression ) {
fprintf( file, " str.clear();\n str.append( \"SUPERTYPE OF ( \" );\n" );
tmp = SUBTYPEto_string( entity->u.entity->subtype_expression );
tmp2 = ( char * )malloc( sizeof( char ) * ( strlen( tmp ) + BUFSIZ ) );
fprintf( file, "%s\n str.append( \")\" );\n", format_for_std_stringout( tmp, tmp2 ) );
format_for_std_stringout( file, tmp );
fprintf( file, "\n str.append( \")\" );\n" );
free( tmp );
free( tmp2 );
fprintf( file, " %s::%s%s->AddSupertype_Stmt( str );", schema_name, ENT_PREFIX, entity_name );
}
}
LISTdo( ENTITYget_supertypes( entity ), sup, Entity )
+1 -1
View File
@@ -91,7 +91,7 @@ struct SelectTag_ {
const char * GetTypeDescriptorName( Type t );
char * format_for_stringout( char * orig_buf, char * return_buf );
char * format_for_std_stringout( char * orig_buf, char * return_buf );
void format_for_std_stringout( FILE * f, const char * orig_buf );
const char * CheckWord( const char * );
const char * StrToLower( const char * );
const char * StrToUpper( const char * );
+9 -41
View File
@@ -360,8 +360,6 @@ void SCHEMAprint( Schema schema, FILES * files, Express model, void * complexCol
Function f;
Procedure p;
DictionaryEntry de;
char * tmpstr = 0;
unsigned int tmpstr_size = 0;
/********** create files based on name of schema ***********/
/* return if failure */
/* 1. header file */
@@ -462,58 +460,28 @@ void SCHEMAprint( Schema schema, FILES * files, Express model, void * complexCol
/* add global RULEs to Schema dictionary entry */
DICTdo_type_init( schema->symbol_table, &de, OBJ_RULE );
while( 0 != ( r = ( Rule )DICTdo( &de ) ) ) {
if( tmpstr_size < ( strlen( RULEto_string( r ) ) * 2 ) ) {
if( tmpstr != 0 ) {
free( tmpstr );
}
tmpstr_size = strlen( RULEto_string( r ) ) * 2;
tmpstr = ( char * )malloc( sizeof( char ) * tmpstr_size );
tmpstr[0] = '\0';
}
fprintf( createall, " str.clear();\n%s", format_for_std_stringout( RULEto_string( r ), tmpstr ) );
fprintf( createall,
" gr = new Global_rule(\"%s\",%s::schema, str );\n",
fprintf( createall, " str.clear();\n" );
format_for_std_stringout( createall, RULEto_string( r ) );
fprintf( createall, " gr = new Global_rule(\"%s\",%s::schema, str );\n",
r->symbol.name, SCHEMAget_name( schema ) );
fprintf( createall,
" %s::schema->AddGlobal_rule(gr);\n", SCHEMAget_name( schema ) );
fprintf( createall, " %s::schema->AddGlobal_rule(gr);\n", SCHEMAget_name( schema ) );
fprintf( createall, "/*\n%s\n*/\n", RULEto_string( r ) );
}
/**************/
/* add FUNCTIONs to Schema dictionary entry */
DICTdo_type_init( schema->symbol_table, &de, OBJ_FUNCTION );
while( 0 != ( f = ( Function )DICTdo( &de ) ) ) {
if( tmpstr_size < ( strlen( FUNCto_string( f ) ) * 2 ) ) {
if( tmpstr != 0 ) {
free( tmpstr );
}
tmpstr_size = strlen( FUNCto_string( f ) ) * 2;
tmpstr = ( char * )malloc( sizeof( char ) * tmpstr_size );
}
fprintf( createall, " str.clear();\n%s",
format_for_std_stringout( FUNCto_string( f ), tmpstr ));
fprintf( createall,
"%s::schema->AddFunction( str );\n",
SCHEMAget_name( schema ) );
fprintf( createall, "/*\n%s\n*/\n", FUNCto_string( f ) );
fprintf( createall, " str.clear();\n" );
format_for_std_stringout( createall, FUNCto_string( f ) );
fprintf( createall, "%s::schema->AddFunction( str );\n", SCHEMAget_name( schema ) );
}
/* add PROCEDUREs to Schema dictionary entry */
DICTdo_type_init( schema->symbol_table, &de, OBJ_PROCEDURE );
while( 0 != ( p = ( Procedure )DICTdo( &de ) ) ) {
if( tmpstr_size < ( strlen( PROCto_string( p ) ) * 2 ) ) {
if( tmpstr != 0 ) {
free( tmpstr );
}
tmpstr_size = strlen( PROCto_string( p ) ) * 2;
tmpstr = ( char * )malloc( sizeof( char ) * tmpstr_size );
}
fprintf( createall, " str.clear();\n%s", format_for_std_stringout( PROCto_string( p ), tmpstr ) );
fprintf( createall, " str.clear();\n" );
format_for_std_stringout( createall, PROCto_string( p ) );
fprintf( createall, " %s::schema->AddProcedure( str );\n", SCHEMAget_name( schema ) );
fprintf( createall, "/*\n%s\n*/\n", PROCto_string( p ) );
}
if( tmpstr_size > 0 ) {
free( tmpstr );
}
fprintf( files->classes, "\n// Schema: %s", schnm );