--- libaitio/example/test_bufio.c 2012/02/02 15:32:02 1.1 +++ libaitio/example/test_bufio.c 2012/02/02 21:32:42 1.2 @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +#include + + +int +main(int argc, char **argv) +{ + FILE *f; + char b[BUFSIZ]; + void *p = NULL; + + f = io_fd2buf(io_dumbFile("xxx", 0644, 12345), "r+"); + fclose(f); + + if (argc < 2) + f = io_fmapopen(NULL, 0, 0, PROT_READ | PROT_WRITE, 0, 2000); + else + f = io_fmapopen(argv[1], O_RDWR, 0, PROT_READ | PROT_WRITE, MAP_SHARED, 0); + if (!f) { + printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); + return 1; + } + + fwrite("123456789", 10, 1, f); + fseek(f, 0, SEEK_SET); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("b=%s\n", b); +// fread(b, sizeof b, 1, f); + fwrite("***", 4, 1, f); + fwrite("*|\n", 3, 1, f); + fseek(f, 100, SEEK_CUR); + fwrite("oOo", 3, 1, f); + fseek(f, -10, SEEK_END); + fwrite("123456789", 10, 1, f); + fseek(f, -10, SEEK_END); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("2.b=%s\n", b); + + fclose(f); + + // part 2 + + f = io_fmemopen(&p, 100); + if (!f) { + printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); + return 2; + } + + fwrite("123456789", 10, 1, f); + fseek(f, 0, SEEK_SET); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("b=%s\n", b); +// fread(b, sizeof b, 1, f); + fwrite("***", 4, 1, f); + fwrite("*|\n", 3, 1, f); + fseek(f, 100, SEEK_CUR); + fwrite("oOo", 3, 1, f); + fseek(f, -10, SEEK_END); + fwrite("123456789", 10, 1, f); + fseek(f, -10, SEEK_END); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("2.b=%s\n", b); + + fclose(f); + + // part 3 + + p = malloc(10000); + f = io_fmemopen(&p, 10000); + if (!f) { + printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError()); + return 3; + } + + fwrite("123456789", 10, 1, f); + fseek(f, 0, SEEK_SET); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("b=%s\n", b); +// fread(b, sizeof b, 1, f); + fwrite("***", 4, 1, f); + fwrite("*|\n", 3, 1, f); + fseek(f, 100, SEEK_CUR); + fwrite("oOo", 3, 1, f); + fseek(f, -10, SEEK_END); + fwrite("123456789", 10, 1, f); + fseek(f, -10, SEEK_END); + memset(b, 0, sizeof b); + fread(b, sizeof b, 1, f); + printf("2.b=%s\n", b); + + fclose(f); + free(p); + + return 0; +}