Index: src/natools-s_expressions-file_writers.adb ================================================================== --- src/natools-s_expressions-file_writers.adb +++ src/natools-s_expressions-file_writers.adb @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------ --- Copyright (c) 2014, Natacha Porté -- +-- Copyright (c) 2014-2017, Natacha Porté -- -- -- -- Permission to use, copy, modify, and distribute this software for any -- -- purpose with or without fee is hereby granted, provided that the above -- -- copyright notice and this permission notice appear in all copies. -- -- -- @@ -72,12 +72,40 @@ begin Finalize (Self.Holder); Stream_IO.Open (Self.Holder.File, Stream_IO.Append_File, Name, Form); end Open; + + function Open_Or_Create (Name : String; Form : String := "") + return Writer is + begin + return Result : Writer do + Open_Or_Create (Result, Name, Form); + end return; + end Open_Or_Create; + + + procedure Open_Or_Create + (Self : in out Writer; + Name : in String; + Form : in String := "") is + begin + Finalize (Self.Holder); + + Open_Attempt : + begin + Stream_IO.Open (Self.Holder.File, Stream_IO.Append_File, Name, Form); + return; + exception + when Stream_IO.Name_Error => null; + end Open_Attempt; + + Stream_IO.Create (Self.Holder.File, Stream_IO.Append_File, Name, Form); + end Open_Or_Create; + function Name (Self : Writer) return String is begin return Stream_IO.Name (Self.Holder.File); end Name; end Natools.S_Expressions.File_Writers; Index: src/natools-s_expressions-file_writers.ads ================================================================== --- src/natools-s_expressions-file_writers.ads +++ src/natools-s_expressions-file_writers.ads @@ -1,7 +1,7 @@ ------------------------------------------------------------------------------ --- Copyright (c) 2014, Natacha Porté -- +-- Copyright (c) 2014-2017, Natacha Porté -- -- -- -- Permission to use, copy, modify, and distribute this software for any -- -- purpose with or without fee is hereby granted, provided that the above -- -- copyright notice and this permission notice appear in all copies. -- -- -- @@ -39,10 +39,20 @@ procedure Open (Self : in out Writer; Name : in String; Form : in String := ""); -- Reinitialize Self using Stream_IO.Create or Stream_IO.Open + + function Open_Or_Create (Name : String; Form : String := "") return Writer; + procedure Open_Or_Create + (Self : in out Writer; + Name : in String; + Form : in String := ""); + -- Construct or reinitialize a writer, trying a regular open first + -- and attempting to create if it doesn't work. + -- Note that there is some time between failure to open and actual + -- file creation, which might lead to race conditions. function Name (Self : Writer) return String; -- Return the underlying file name private