@@ -821,6 +821,7 @@ static void run(FFdata* data)
821821 if (instance .state .resultDoc )
822822 {
823823 yyjson_mut_write_fp (stdout , instance .state .resultDoc , YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES , NULL , NULL );
824+ //TODO should use YYJSON_WRITE_NEWLINE_AT_END when it is available
824825 putchar ('\n' );
825826 }
826827 else
@@ -840,24 +841,23 @@ static void writeConfigFile(FFdata* data, const FFstrbuf* filename)
840841 ffOptionsGenerateLibraryJsonConfig (& instance .config .library , doc );
841842 ffMigrateCommandOptionToJsonc (data , doc );
842843
843- if (ffStrbufEqualS (filename , "-" ))
844- yyjson_mut_write_fp (stdout , doc , YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES , NULL , NULL );
845- else
844+ FILE * fp = stdout ;
845+ bool writeToStdout = ffStrbufEqualS (filename , "-" );
846+ if (!writeToStdout )
847+ fp = fopen (filename -> chars , "w" );
848+
849+ bool ok = yyjson_mut_write_fp (fp , doc , YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES , NULL , NULL );
850+ //TODO should use YYJSON_WRITE_NEWLINE_AT_END when it is available
851+ fputc ('\n' , fp );
852+ if (!ok )
846853 {
847- size_t len ;
848- FF_AUTO_FREE const char * str = yyjson_mut_write (doc , YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES , & len );
849- if (!str )
850- {
851- printf ("Error: failed to generate config file\n" );
852- exit (1 );
853- }
854- if (ffWriteFileData (filename -> chars , len , str ))
855- printf ("The generated config file has been written in `%s`\n" , filename -> chars );
856- else
857- {
858- printf ("Error: failed to write file in `%s`\n" , filename -> chars );
859- exit (1 );
860- }
854+ fprintf (stderr , "Error: failed to generate config in `%s`\n" , writeToStdout ? "stdout" : filename -> chars );
855+ exit (1 );
856+ }
857+ if (ok && !writeToStdout )
858+ {
859+ fclose (fp );
860+ printf ("The generated config file has been written in `%s`\n" , filename -> chars );
861861 }
862862
863863 yyjson_mut_doc_free (doc );
0 commit comments