I am trying to learn how to use libuv. I am on Mac OS X and download and install the library. I can compile and run small test programs, it only starts the callback loop, and then exits because there are no observers, and the other creates an idle observer and exits when the time runs out.
I am trying to pass samples for an io file and am running into problems. The prototype of the function for obtaining the file descriptor is:
int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, // second argument const char* path, int flags, int mode, uv_fs_cb cb)
I have reduced the sample code to a minimum that will still have an error.
#include <stdio.h> #include <uv.h> uv_fs_t open_req; void on_open(uv_fs_t); void on_read(uv_fs_t); void on_write(uv_fs_t); int main(int argc, char **argv) { uv_fs_open(uv_default_loop(), &open_req, argv[1], O_RDONLY, 0, on_open); uv_run(uv_default_loop(), UV_RUN_DEFAULT); return 0; } void on_open(uv_fs_t *req) { if (req->result != -1) { fprintf(stderr, "function called \n"); } else { fprintf(stderr, "error opening file: %d\n", req->errorno); } uv_fs_req_cleanup(req); }
My open_req declaration exactly matches the close_req declaration, which appears in the full code example and does not cause an error. I added an ad for open_req because the compiler gave me the error "open_req was not declared in this area." When I added the ad, he changed the error to "invalid conversion from" void (*) (uv_fs_t) to "void (*) (uv_fs_t *)". I get the same error, and the declaration inside main or not. If I changed the declaration to a pointer declaration
uv_fs_t* open_req;
then I get the error:
cannot convert uv_fs_t** to uv_fs_t* for argument '2' to int uv_fs_open(uv_loop_t*, uv_fs_t*, const char*, int, int, void (*)(uv_fs_t*))
I am trying to go through the actual libuv code to find out if I can figure it out, but the code is huge. I use the "Introduction to libuv" found here: http://nikhilm.github.com/uvbook/ , and that is where the code sample appeared.
Is there any other source of sample code that I can use to try to figure out this library? Is there something obvious that I'm wrong in my declaration? I'm not sure where to look for clarifications or examples of how to use libuv.
Edit update In libuv code in uv.h. I find
typedef struct uv_fs_s uv_fs_t;
one of the things I was looking for in code is where this structure is actually defined. I was looking to use google for typedef and learned a little about how typedef is used to create a name, so you do not need to enter a struct every time you want to declare an instance of a structure. While some people seem to think this is a terrible practice, others think itβs great. Part of what I removed to get a minimal sample as another declaration of type us_fs_t called close_req. I copied the format for my ad directly from this.
I will see that I can find about function pointers, I am only vaguely familiar with them, and this at least gives me the opportunity to start looking.
I found a function definition for uv_fs_open.
int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, int mode, uv_fs_cb cb) { INIT(OPEN); PATH; req->flags = flags; req->mode = mode; POST; }