Create a Wyrd Document

Working with wyrd means working with documents of sorts - a collection of properties. But before you can do that, you need to initialize the library.

Initialize wyrd library
1#include <wyrd/api.h>
2
3wyrd_error_t err = WYRD_ERR_SUCCESS;
4struct wyrd_api * api = NULL;
5
6err = wyrd_create(&api);
7assert(WYRD_ERR_SUCCESS == err);
8assert(api != NULL);

This call initializes global library context. You will need to provide this pointer to some other functions to inform them of this state. When you’re done with your use of the library, you can safely dispose of this pointer again.

Destroy wyrd API instance
1wyrd_destroy(&api);
2assert(api == NULL);

With the API instance and a document file name, you can now go about creating your document file. Similar to open(2), we can pass flags to tell the function how to treat the file. In fact, most of the flags are modelled on those of the underlying system call.

Create document handle
1#include <wyrd/handle.h>
2
3wyrd_error_t err = WYRD_ERR_SUCCESS;
4struct wyrd_handle * handle = NULL;
5
6err = wyrd_open_file(api, &handle, "testfile", WYRD_O_RW | WYRD_O_CREATE);
7assert(WYRD_ERR_SUCCESS == err);
8assert(handle != NULL);

Similarly, to close a document handle, simply invoke the wyrd_close() function.

Close document handle
1wyrd_close(&handle);
2assert(api == NULL);

Now let’s put all of this together into a possible main function for your program.

Example main() with initialization and cleanup
 1#include <wyrd/api.h>
 2#include <wyrd/handle.h>
 3
 4int main(int argc, char **argv)
 5{
 6  wyrd_error_t err = WYRD_ERR_SUCCESS;
 7  struct wyrd_api * api = NULL;
 8  struct wyrd_handle * handle = NULL;
 9  int ret = 0;
10
11  err = wyrd_create(&api);
12  if (WYRD_ERR_SUCCESS != err) {
13    fprintf(stderr, "Could not initialize wyrd: %s\n", wyrd_error_message(err));
14    ret = 1;
15    goto cleanup;
16  }
17
18  err = wyrd_open_file(api, &handle, "testfile", WYRD_O_RW | WYRD_O_CREATE);
19  if (WYRD_ERR_SUCCESS != err) {
20    fprintf(stderr, "Could not create document: %s\n", wyrd_error_message(err));
21    ret = 2;
22    goto cleanup;
23  }
24
25  // Dispatch to document processing
26  process_document(handle);
27
28cleanup:
29  wyrd_close(&handle);
30  wyrd_destroy(&api);
31  exit(ret);
32}

And there you have it - a document created, ready to work with in the next tutorial.