MCPcopy Create free account
hub / github.com/Tracktion/choc / main

Function main

examples/file_watcher_example.cpp:7–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include "../choc/text/choc_Files.h"
6
7int main()
8{
9 std::cout << "FileWatcher Example" << std::endl;
10 std::cout << "Monitoring current directory for changes... (Press Ctrl+C to exit)" << std::endl;
11
12 choc::file::TempFile tempDir ("file_watcher_test_dir");
13 std::cout << "Temporary directory created: " << tempDir.file.string() << std::endl;
14
15 choc::file::Watcher watcher (tempDir.file, [] (const choc::file::Watcher::Event& event)
16 {
17 std::cout << "Event: " << event.file.filename().string() << " - ";
18 switch (event.eventType)
19 {
20 case choc::file::Watcher::EventType::created: std::cout << "Added"; break;
21 case choc::file::Watcher::EventType::destroyed: std::cout << "Removed"; break;
22 case choc::file::Watcher::EventType::modified: std::cout << "Modified"; break;
23 case choc::file::Watcher::EventType::renamed: std::cout << "Moved"; break;
24 default: break;
25 }
26 std::cout << std::endl;
27 });
28
29 // Create a file
30 choc::file::replaceFileWithContent (tempDir.file / "test_file.txt", "Hello, FileWatcher!");
31 std::this_thread::sleep_for (std::chrono::milliseconds (100)); // Give watcher time to process
32
33 // Modify the file
34 choc::file::replaceFileWithContent (tempDir.file / "test_file.txt", "Hello again, FileWatcher!");
35 std::this_thread::sleep_for (std::chrono::milliseconds (100));
36
37 // Create another file
38 choc::file::replaceFileWithContent (tempDir.file / "another_file.txt", "Another one!");
39 std::this_thread::sleep_for (std::chrono::milliseconds (100));
40
41 // Delete a file
42 std::filesystem::remove (tempDir.file / "test_file.txt");
43 std::this_thread::sleep_for (std::chrono::milliseconds (100));
44
45 // Keep the main thread alive to allow the watcher to run
46 for (;;)
47 std::this_thread::sleep_for (std::chrono::seconds (1));
48
49 return 0;
50}

Callers

nothing calls this directly

Calls 1

replaceFileWithContentFunction · 0.85

Tested by

no test coverage detected