/tutorial/basic_directory/example5.e

http://github.com/tybor/Liberty · Specman e · 102 lines · 90 code · 8 blank · 4 comment · 5 complexity · 592f566e40eae09886649dbd0fd1f4d9 MD5 · raw file

  1. class EXAMPLE5
  2. --
  3. -- This example shows how to create a new directory, how to add files
  4. -- in this new created directory and finally how to remove it.
  5. --
  6. create {ANY}
  7. make
  8. feature {}
  9. make
  10. local
  11. directory_path, file_path: STRING; basic_directory: BASIC_DIRECTORY; text_file_write: TEXT_FILE_WRITE
  12. do
  13. basic_directory.connect_to_current_working_directory
  14. directory_path := basic_directory.last_entry.twin
  15. basic_directory.disconnect
  16. basic_directory.compute_subdirectory_with(directory_path, "foo")
  17. directory_path := basic_directory.last_entry.twin
  18. io.put_string("Trying to create a new directory (%"")
  19. io.put_string(directory_path)
  20. io.put_string("%").%N")
  21. basic_directory.connect_to(directory_path)
  22. if basic_directory.is_connected then
  23. io.put_string("This directory already exists (sorry).%N")
  24. basic_directory.disconnect
  25. die_with_code(exit_failure_code)
  26. end
  27. if not basic_directory.create_new_directory(directory_path) then
  28. io.put_string("Unable to create this directory.%N")
  29. die_with_code(exit_failure_code)
  30. end
  31. basic_directory.compute_file_path_with(directory_path, "bar")
  32. file_path := basic_directory.last_entry.twin
  33. io.put_string("Try to create a new file (%"")
  34. io.put_string(file_path)
  35. io.put_string("%").%N")
  36. create text_file_write.connect_to(file_path)
  37. if text_file_write.is_connected then
  38. text_file_write.disconnect
  39. io.put_string("File created.%N")
  40. else
  41. io.put_string("Unable to create this file.%N")
  42. die_with_code(exit_failure_code)
  43. end
  44. list_directory(directory_path)
  45. io.put_string("Removing file %"")
  46. io.put_string(file_path)
  47. io.put_string("%".%N")
  48. (create {FILE_TOOLS}).delete(file_path)
  49. if basic_directory.remove_directory(directory_path) then
  50. io.put_string("Directory %"")
  51. io.put_string(directory_path)
  52. io.put_string("%" is removed.%N")
  53. else
  54. io.put_string("Unable to remove directory %"")
  55. io.put_string(directory_path)
  56. io.put_string("%".%N")
  57. end
  58. end
  59. list_directory (path: STRING)
  60. local
  61. basic_directory: BASIC_DIRECTORY
  62. do
  63. io.put_string("Trying to list %"")
  64. io.put_string(path)
  65. io.put_string("%".%N")
  66. basic_directory.connect_to(path)
  67. if basic_directory.is_connected then
  68. display_content_of(basic_directory)
  69. else
  70. io.put_string("Unable to open directory %"")
  71. io.put_string(path)
  72. io.put_string("%".%N")
  73. die_with_code(exit_failure_code)
  74. end
  75. end
  76. display_content_of (basic_directory: BASIC_DIRECTORY)
  77. require
  78. basic_directory.is_connected
  79. do
  80. from
  81. basic_directory.read_entry
  82. until
  83. basic_directory.end_of_input
  84. loop
  85. io.put_character('%T')
  86. io.put_string(basic_directory.last_entry)
  87. io.put_character('%N')
  88. basic_directory.read_entry
  89. end
  90. basic_directory.disconnect
  91. ensure
  92. not basic_directory.is_connected
  93. end
  94. end -- class EXAMPLE5