Skip to content

Commit

Permalink
edit and rearrange tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
sabine committed Oct 31, 2024
1 parent 087757f commit 723bef2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 79 deletions.
88 changes: 88 additions & 0 deletions data/cookbook/basic-file-system-operations/00-stdlib.ml
Original file line number Diff line number Diff line change
@@ -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
26 changes: 0 additions & 26 deletions data/cookbook/file-system/00-stdlib.ml

This file was deleted.

22 changes: 22 additions & 0 deletions data/cookbook/read-and-write-text-file/00-stdlib.ml
Original file line number Diff line number Diff line change
@@ -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)

9 changes: 0 additions & 9 deletions data/cookbook/read-text-file/00-in_channel.ml

This file was deleted.

59 changes: 15 additions & 44 deletions data/cookbook/tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 723bef2

Please sign in to comment.