Added WSF_FILE_UTILITIES.new_file (p: PATH): detachable G
in order to provide non existing file, but not only for temporary files purpose.
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
note
|
note
|
||||||
description: "[
|
description: "[
|
||||||
This class is used to get a safe temporary file
|
Collection of file system utilities.
|
||||||
in a specific directory, for an optional prefix, and an optional expected filename.
|
|
||||||
|
Such as getting safe temporary file in a specific directory,
|
||||||
|
for an optional prefix, and an optional expected filename.
|
||||||
]"
|
]"
|
||||||
date: "$Date$"
|
date: "$Date$"
|
||||||
revision: "$Revision$"
|
revision: "$Revision$"
|
||||||
@@ -11,32 +13,15 @@ class
|
|||||||
|
|
||||||
feature -- Factory
|
feature -- Factory
|
||||||
|
|
||||||
new_temporary_file (d: DIRECTORY; a_prefix: detachable READABLE_STRING_GENERAL; a_name: detachable READABLE_STRING_GENERAL): detachable G
|
new_file (p: PATH): detachable G
|
||||||
-- New temporary file open for writing inside directory `d', with prefix `a_prefix' is set, and based on name `a_name' is set.
|
-- New temporary file open for writing at location `p' or appended with integer suffix if already exists.
|
||||||
-- If it is unable to create such file opened for writing, then return Void.
|
|
||||||
require
|
|
||||||
d_valid: d.exists and then d.is_writable
|
|
||||||
local
|
local
|
||||||
f: G
|
f: G
|
||||||
fn: PATH
|
bn: STRING_32
|
||||||
bn, tmp: STRING_32
|
|
||||||
dn: PATH
|
|
||||||
n: INTEGER
|
n: INTEGER
|
||||||
do
|
do
|
||||||
from
|
from
|
||||||
if a_prefix /= Void then
|
create f.make_with_path (p)
|
||||||
create tmp.make_from_string_general (a_prefix)
|
|
||||||
else
|
|
||||||
create tmp.make_from_string_general ("tmp")
|
|
||||||
end
|
|
||||||
dn := d.path
|
|
||||||
if a_name /= Void then
|
|
||||||
tmp.append_character ('-')
|
|
||||||
tmp.append_string_general (safe_filename (a_name))
|
|
||||||
end
|
|
||||||
|
|
||||||
fn := dn.extended (tmp)
|
|
||||||
create f.make_with_path (fn)
|
|
||||||
Result := new_file_opened_for_writing (f)
|
Result := new_file_opened_for_writing (f)
|
||||||
n := 0
|
n := 0
|
||||||
until
|
until
|
||||||
@@ -44,17 +29,43 @@ feature -- Factory
|
|||||||
or else n > 1_000
|
or else n > 1_000
|
||||||
loop
|
loop
|
||||||
n := n + 1
|
n := n + 1
|
||||||
create bn.make_from_string (tmp)
|
create bn.make (2 + n // 10)
|
||||||
bn.append_character ('-')
|
bn.append_character ('-')
|
||||||
bn.append_integer (n)
|
bn.append_integer (n)
|
||||||
fn := dn.extended (bn)
|
f.make_with_path (p.appended (bn))
|
||||||
f.make_with_path (fn)
|
|
||||||
Result := new_file_opened_for_writing (f)
|
Result := new_file_opened_for_writing (f)
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
result_opened_for_writing_if_set: Result /= Void implies Result.is_open_write
|
result_opened_for_writing_if_set: Result /= Void implies Result.is_open_write
|
||||||
end
|
end
|
||||||
|
|
||||||
|
new_temporary_file (d: DIRECTORY; a_prefix: detachable READABLE_STRING_GENERAL; a_name: detachable READABLE_STRING_GENERAL): detachable G
|
||||||
|
-- New temporary file open for writing inside directory `d', with prefix `a_prefix' is set, and based on name `a_name' is set.
|
||||||
|
-- If it is unable to create such file opened for writing, then return Void.
|
||||||
|
require
|
||||||
|
d_valid: d.exists and then d.is_writable
|
||||||
|
local
|
||||||
|
fn: PATH
|
||||||
|
tmp: STRING_32
|
||||||
|
dn: PATH
|
||||||
|
do
|
||||||
|
if a_prefix /= Void then
|
||||||
|
create tmp.make_from_string_general (a_prefix)
|
||||||
|
else
|
||||||
|
create tmp.make_from_string_general ("tmp")
|
||||||
|
end
|
||||||
|
dn := d.path
|
||||||
|
if a_name /= Void then
|
||||||
|
tmp.append_character ('-')
|
||||||
|
tmp.append_string_general (safe_filename (a_name))
|
||||||
|
end
|
||||||
|
|
||||||
|
fn := dn.extended (tmp)
|
||||||
|
Result := new_file (fn)
|
||||||
|
ensure
|
||||||
|
result_opened_for_writing_if_set: Result /= Void implies Result.is_open_write
|
||||||
|
end
|
||||||
|
|
||||||
safe_filename (fn: READABLE_STRING_GENERAL): STRING
|
safe_filename (fn: READABLE_STRING_GENERAL): STRING
|
||||||
-- Safe filename that avoid impossible filename, or dangerous one.
|
-- Safe filename that avoid impossible filename, or dangerous one.
|
||||||
local
|
local
|
||||||
@@ -162,7 +173,7 @@ feature {NONE} -- Implementation
|
|||||||
end
|
end
|
||||||
|
|
||||||
note
|
note
|
||||||
copyright: "2011-2014, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
|
copyright: "2011-2015, Jocelyn Fiat, Javier Velilla, Olivier Ligot, Colin Adams, Eiffel Software and others"
|
||||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||||
source: "[
|
source: "[
|
||||||
Eiffel Software
|
Eiffel Software
|
||||||
|
|||||||
Reference in New Issue
Block a user