diff --git a/README.md b/README.md index 53913ef..e93b8e4 100644 --- a/README.md +++ b/README.md @@ -40,30 +40,24 @@ This program reads from the file `file.txt` and prints its contents to the termi int main(int argc, char *argv[]) { - char buf[512]; - size_t bytes_read; - ls_handle file; + char buf[512]; + size_t bytes_read; + ls_handle file; - // Initialize the library - ls_init(NULL); + // Open file `file.txt` for reading + file = ls_open("file.txt", LS_FILE_READ, LS_SHARE_READ, LS_OPEN_EXISTING); - // Open file `file.txt` for reading - file = ls_open("file.txt", LS_A_READ, LS_S_READ, LS_OPEN_EXISTING); + // Read in file contents + bytes_read = ls_read(file, buf, sizeof(buf) - 1); + buf[bytes_read] = 0; - // Read in file contents - bytes_read = ls_read(file, buf, sizeof(buf) - 1, NULL); - buf[bytes_read] = 0; // null terminate + // Close the file + ls_close(file); - // Close the file - ls_close(file); + // Write to terminal + puts(buf); - // Write to terminal - puts(buf); - - // Cleanup library - ls_shutdown(); - - return 0; + return 0; } ``` diff --git a/example/aio/main.c b/example/aio/main.c new file mode 100644 index 0000000..ca0bc6e --- /dev/null +++ b/example/aio/main.c @@ -0,0 +1,69 @@ +#include + +int main(int argc, char *argv[]) +{ + ls_handle file; + ls_handle aio; + int rc; + void *buf; + struct ls_stat st; + + // Open a file for reading asynchronously. + file = ls_open("large.bin", LS_FILE_READ | LS_FLAG_ASYNC, + LS_SHARE_READ, LS_OPEN_EXISTING); + if (!file) + { + ls_perror("ls_open"); + exit(1); + } + + // Get the size of the file. + rc = ls_fstat(file, &st); + if (rc == -1) + { + ls_perror("ls_fstat"); + exit(1); + } + + // Allocate a buffer to read the file into. + buf = ls_malloc(st.size); + if (!buf) + { + ls_perror("ls_malloc"); + exit(1); + } + + // Create an asynchronous I/O handle. + aio = ls_aio_open(file); + if (!aio) + { + ls_perror("ls_aio_open"); + exit(1); + } + + // Dispatch an asynchronous read operation. + rc = ls_aio_read(aio, 0, buf, st.size); + if (rc == -1) + { + ls_perror("ls_aio_read"); + exit(1); + } + + // Pretend to do something else. + ls_sleep(1000); + + // Wait for the read to complete. + rc = ls_wait(aio); + if (rc == -1) + { + ls_perror("ls_wait"); + exit(1); + } + + printf("File read!\n"); + + ls_close(aio); + ls_close(file); + + return 0; +} diff --git a/example/clipboard/main.c b/example/clipboard/main.c new file mode 100644 index 0000000..b97fa6f --- /dev/null +++ b/example/clipboard/main.c @@ -0,0 +1,47 @@ +#include + +static const char string[] = "Hello, world!"; + +int main(int argc, char *argv[]) +{ + char buf[sizeof(string)]; + intptr_t fmt; + int rc; + size_t len; + + // Register a new clipboard format. + fmt = ls_register_clipboard_format("my_format"); + if (fmt == -1) + { + ls_perror("ls_register_clipboard_format"); + exit(1); + } + + // Set the data in our format. + rc = ls_set_clipboard_data(fmt, string, sizeof(string)); + if (rc == -1) + { + ls_perror("ls_set_clipboard_data"); + exit(1); + } + + // Get the data back. + len = ls_get_clipboard_data(fmt, buf, sizeof(buf)); + if (len == -1) + { + ls_perror("ls_get_clipboard_data"); + exit(1); + } + + // Our data was likely overwritten by another application. + if (len == 0) + { + printf("Data not available in requested format\n"); + return 0; + } + + // Print the data. + printf("Data: %s\n", buf); + + return 0; +}