From 723bef258c46b8ad0d42ef6451f94fa660dfc377 Mon Sep 17 00:00:00 2001 From: sabine <6594573+sabine@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:51:35 +0100 Subject: [PATCH] edit and rearrange tasks --- .../basic-file-system-operations/00-stdlib.ml | 88 +++++++++++++++++++ data/cookbook/file-system/00-stdlib.ml | 26 ------ .../read-and-write-text-file/00-stdlib.ml | 22 +++++ data/cookbook/read-text-file/00-in_channel.ml | 9 -- data/cookbook/tasks.yml | 59 ++++--------- 5 files changed, 125 insertions(+), 79 deletions(-) create mode 100644 data/cookbook/basic-file-system-operations/00-stdlib.ml delete mode 100644 data/cookbook/file-system/00-stdlib.ml create mode 100644 data/cookbook/read-and-write-text-file/00-stdlib.ml delete mode 100644 data/cookbook/read-text-file/00-in_channel.ml diff --git a/data/cookbook/basic-file-system-operations/00-stdlib.ml b/data/cookbook/basic-file-system-operations/00-stdlib.ml new file mode 100644 index 0000000000..2b240b0b95 --- /dev/null +++ b/data/cookbook/basic-file-system-operations/00-stdlib.ml @@ -0,0 +1,88 @@ +--- +packages: [] +discussion: | + - All presented functions are available in the [Sys module reference](https://ocaml.org/api/Stdlib.Sys.html). +--- + +(* Checks if a file or directory exists by testing + the filename. *) +let file_exists filename = + if Sys.file_exists filename then + Printf.printf "The file/directory '%s' exists.\n" + filename + else + Printf.printf "The file/directory '%s' does not \ + exist.\n" filename + +(* Checks if the given path is a directory or a file. *) +let check_if_directory path = + if Sys.file_exists path then + (if Sys.is_directory path then + Printf.printf "'%s' is a directory.\n" path + else + Printf.printf "'%s' is a file.\n" path) + else + Printf.printf "'%s' does not exist.\n" path + +(* Copies the content of a source file to a destination + file. *) +let copy_file src dst = + let content = + In_channel.with_open_text src In_channel.input_all + in + Out_channel.with_open_text dst (fun out_channel -> + Out_channel.output_string out_channel content); + Printf.printf "Copied '%s' to '%s'.\n" src dst + +(* Moves (renames) a file to a new location or name. *) +let move_file src dst = + if Sys.file_exists src then ( + Sys.rename src dst; + Printf.printf "Moved '%s' to '%s'.\n" src dst + ) else + Printf.printf "File '%s' does not exist.\n" src + +(* Deletes a specified file if it exists and is not + a directory. *) +let delete_file filename = + if Sys.file_exists filename && + not (Sys.is_directory filename) then ( + Sys.remove filename; + Printf.printf "Deleted file '%s'.\n" filename + ) else + Printf.printf "File '%s' does not exist or is \ + a directory.\n" filename + +(* Changes the current working directory to the + specified directory path. *) +let change_directory dir = + if Sys.file_exists dir && Sys.is_directory dir then ( + Sys.chdir dir; + Printf.printf "Changed current directory to '%s'.\n" + dir + ) else + Printf.printf "'%s' does not exist or is not \ + a directory.\n" dir + +(* Deletes a specified directory if it exists. *) +let delete_directory dir = + if Sys.file_exists dir && Sys.is_directory dir then ( + Sys.rmdir dir; + Printf.printf "Deleted directory '%s'.\n" dir + ) else + Printf.printf "'%s' does not exist or is not \ + a directory.\n" dir + +(* Lists all files and directories within a specified + directory. Prints the names of the contents or an + error if the path does not exist or is not a + directory. *) +let list_directory dir = + if Sys.file_exists dir && Sys.is_directory dir then ( + let file_array = Sys.readdir dir in + Printf.printf "Contents of directory '%s':\n" dir; + Array.iter (fun filename -> + Printf.printf " %s\n" filename) file_array + ) else + Printf.printf "'%s' does not exist or is not \ + a directory.\n" dir diff --git a/data/cookbook/file-system/00-stdlib.ml b/data/cookbook/file-system/00-stdlib.ml deleted file mode 100644 index a88c0889e8..0000000000 --- a/data/cookbook/file-system/00-stdlib.ml +++ /dev/null @@ -1,26 +0,0 @@ ---- -packages: [] -discussion: | - - **Reference:** All the presented functions are available in the [Sys module reference](https://v2.ocaml.org/api/Stdlib.Sys.html) ---- -(* Testing the presence of a file/directory *) -let () = if Sys.file_exists "my_file" then - print_string "The file/directory exists." -(* Testing if file or directory which exists is a directory. *) -let () = if Sys.is_directory "my_file" then - print_string "It is a directory." - else - print_string "It is a file (regular or not)." -(* Deleting a file *) -let () = Sys.remove "my_file" -(* Renaming a file *) - let () = Sys.rename "my_file" "new_name" -(* Changing the current directory *) - let () = Sys.chdir "new_directory_path" -(* Deleting a directory *) - let () = Sys.rmdir "directory_name" -(* Listing a directory *) - let () = - let file_array = Sys.readdir "directory_name" in - Array.iter (fun filename -> Printf.printf "%s\n" filename) file_array - diff --git a/data/cookbook/read-and-write-text-file/00-stdlib.ml b/data/cookbook/read-and-write-text-file/00-stdlib.ml new file mode 100644 index 0000000000..c941fd51e7 --- /dev/null +++ b/data/cookbook/read-and-write-text-file/00-stdlib.ml @@ -0,0 +1,22 @@ +--- +packages: [] +--- +(* `In_channel.with_open_text` opens a channel the file at the given path. + `input_all` reads all data from the input channel and returns it as a string. + These functions can raise `Sys_error` exceptions. + *) +let read_text_from_file filename = + try + In_channel.with_open_text + filename + In_channel.input_all + with Sys_error msg -> + failwith ("Failed to read from file: " ^ msg) + +(* `Out_channel.with_open_text` safely opens the file, writes text to it, and closes it automatically.*) +let write_text_to_file filename text = + Out_channel.with_open_text + filename + (fun out_channel -> + Out_channel.output_string out_channel text) + \ No newline at end of file diff --git a/data/cookbook/read-text-file/00-in_channel.ml b/data/cookbook/read-text-file/00-in_channel.ml deleted file mode 100644 index de0d6d1967..0000000000 --- a/data/cookbook/read-text-file/00-in_channel.ml +++ /dev/null @@ -1,9 +0,0 @@ ---- -packages: [] ---- -(* `with_open_text` opens a channel the file at the given path. - `input_all` reads all data from the input channel. - These functions can raise `Sys_error` exceptions. - *) -let text = - In_channel.(with_open_text "/etc/passwd" input_all) diff --git a/data/cookbook/tasks.yml b/data/cookbook/tasks.yml index 54033e78a2..6271fba2c0 100644 --- a/data/cookbook/tasks.yml +++ b/data/cookbook/tasks.yml @@ -8,10 +8,6 @@ categories: description: > Use ANSI escape codes for cursor location, font, and colour in video text terminal emulators. -- title: File System - tasks: - - title: Miscellaneous File System Operations - slug: file-system - title: Compilation & Linking tasks: - title: Compile and Link to a Bundled C Library @@ -142,46 +138,21 @@ categories: - title: Read and Write Integers in Little-Endian Byte Order slug: read-write-integers-little-endian - title: File System - subcategories: - - title: Files - tasks: - - title: Open File - slug: open-file - - title: Read a Text File - slug: read-text-file - - title: Write to a File - slug: write-file - - title: Rename a File - slug: rename-file - - title: Delete a File - slug: delete-file - - title: Check if a File Exists - slug: check-exist-file - - title: File Size - slug: file-size - - title: Change File Permissions - slug: change-file-permissions - - title: Create Temporary File - slug: create-temporary-file - - title: Directories - tasks: - - title: Create Directory - slug: create-directory - - title: List Directory Contents - slug: list-directory - - title: Get Current Directory - slug: get-current-directory - - title: Change Current Directory - slug: change-current-directory - - title: Find Files Matching a Pattern - slug: find-files-matching-a-pattern - - title: Walk File Tree - slug: walk-file-tree - - title: File Paths -#- title: Garbage Collector -# tasks: -# - title: Trigger a Minor / Major Collection -# slug: trigger-minor-major-collection + tasks: + - title: Read and Write from a Text File + slug: read-and-write-text-file + - title: Basic File and Directory Operations + slug: basic-file-system-operations + description: | + Exists, copy, move (rename), delete, change directory, list directory, delete directory. + - title: File Size + slug: file-size + - title: Change File Permissions + slug: change-file-permissions + - title: Walk File Tree + slug: walk-file-tree + - title: Find Files Matching a Pattern + slug: find-files-matching-a-pattern - title: Random Values tasks: - title: Generate Random `int`, `float`, and `char`