Skip to content

Commit

Permalink
Updated readme and added example programs
Browse files Browse the repository at this point in the history
  • Loading branch information
evrhel committed May 6, 2024
1 parent cb3c022 commit 1e9b500
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 19 deletions.
32 changes: 13 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
69 changes: 69 additions & 0 deletions example/aio/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <lysys/lysys.h>

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;
}
47 changes: 47 additions & 0 deletions example/clipboard/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <lysys/lysys.h>

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;
}

0 comments on commit 1e9b500

Please sign in to comment.