Mercurial > hg > th-libs
comparison th_ioctx_stdio.c @ 771:c17eadc60c3d
Rename th_ioctx struct to th_ioctx_t, for consistency. Breaks API.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 20 Feb 2023 23:33:45 +0200 |
parents | 6b778871f770 |
children | 1959ba53e9dc |
comparison
equal
deleted
inserted
replaced
770:79f888e4616f | 771:c17eadc60c3d |
---|---|
9 #include <stdio.h> | 9 #include <stdio.h> |
10 | 10 |
11 #define CTX_FH ((FILE *) ctx->data) | 11 #define CTX_FH ((FILE *) ctx->data) |
12 | 12 |
13 | 13 |
14 static int th_stdio_fopen(th_ioctx *ctx) | 14 static int th_stdio_fopen(th_ioctx_t *ctx) |
15 { | 15 { |
16 ctx->data = (void *) fopen(ctx->filename, ctx->mode); | 16 ctx->data = (void *) fopen(ctx->filename, ctx->mode); |
17 ctx->status = th_get_error(); | 17 ctx->status = th_get_error(); |
18 return (ctx->data != NULL) ? THERR_OK : ctx->status; | 18 return (ctx->data != NULL) ? THERR_OK : ctx->status; |
19 } | 19 } |
20 | 20 |
21 | 21 |
22 static void th_stdio_fclose(th_ioctx *ctx) | 22 static void th_stdio_fclose(th_ioctx_t *ctx) |
23 { | 23 { |
24 if (CTX_FH != NULL) | 24 if (CTX_FH != NULL) |
25 { | 25 { |
26 fclose(CTX_FH); | 26 fclose(CTX_FH); |
27 ctx->data = NULL; | 27 ctx->data = NULL; |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
31 | 31 |
32 static int th_stdio_ferror(th_ioctx *ctx) | 32 static int th_stdio_ferror(th_ioctx_t *ctx) |
33 { | 33 { |
34 return ctx->status; | 34 return ctx->status; |
35 } | 35 } |
36 | 36 |
37 | 37 |
38 static off_t th_stdio_ftell(th_ioctx *ctx) | 38 static off_t th_stdio_ftell(th_ioctx_t *ctx) |
39 { | 39 { |
40 return ftello(CTX_FH); | 40 return ftello(CTX_FH); |
41 } | 41 } |
42 | 42 |
43 | 43 |
44 static int th_stdio_fseek(th_ioctx *ctx, const off_t pos, const int whence) | 44 static int th_stdio_fseek(th_ioctx_t *ctx, const off_t pos, const int whence) |
45 { | 45 { |
46 int ret = fseeko(CTX_FH, pos, whence); | 46 int ret = fseeko(CTX_FH, pos, whence); |
47 ctx->status = th_get_error(); | 47 ctx->status = th_get_error(); |
48 return ret; | 48 return ret; |
49 } | 49 } |
50 | 50 |
51 | 51 |
52 static int th_stdio_freset(th_ioctx *ctx) | 52 static int th_stdio_freset(th_ioctx_t *ctx) |
53 { | 53 { |
54 return th_stdio_fseek(ctx, 0, SEEK_SET); | 54 return th_stdio_fseek(ctx, 0, SEEK_SET); |
55 } | 55 } |
56 | 56 |
57 | 57 |
58 static off_t th_stdio_fsize(th_ioctx *ctx) | 58 static off_t th_stdio_fsize(th_ioctx_t *ctx) |
59 { | 59 { |
60 off_t savePos, fileSize; | 60 off_t savePos, fileSize; |
61 | 61 |
62 // Check if the size is cached | 62 // Check if the size is cached |
63 if (ctx->size != 0) | 63 if (ctx->size != 0) |
79 ctx->size = fileSize; | 79 ctx->size = fileSize; |
80 return fileSize; | 80 return fileSize; |
81 } | 81 } |
82 | 82 |
83 | 83 |
84 static bool th_stdio_feof(th_ioctx *ctx) | 84 static bool th_stdio_feof(th_ioctx_t *ctx) |
85 { | 85 { |
86 return feof(CTX_FH); | 86 return feof(CTX_FH); |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 static int th_stdio_fgetc(th_ioctx *ctx) | 90 static int th_stdio_fgetc(th_ioctx_t *ctx) |
91 { | 91 { |
92 int ret = fgetc(CTX_FH); | 92 int ret = fgetc(CTX_FH); |
93 ctx->status = th_get_error(); | 93 ctx->status = th_get_error(); |
94 return ret; | 94 return ret; |
95 } | 95 } |
96 | 96 |
97 | 97 |
98 static int th_stdio_fputc(int v, th_ioctx *ctx) | 98 static int th_stdio_fputc(int v, th_ioctx_t *ctx) |
99 { | 99 { |
100 int ret = fputc(v, CTX_FH); | 100 int ret = fputc(v, CTX_FH); |
101 ctx->status = th_get_error(); | 101 ctx->status = th_get_error(); |
102 return ret; | 102 return ret; |
103 } | 103 } |
104 | 104 |
105 | 105 |
106 static size_t th_stdio_fread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) | 106 static size_t th_stdio_fread(void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx) |
107 { | 107 { |
108 size_t ret = fread(ptr, size, nmemb, CTX_FH); | 108 size_t ret = fread(ptr, size, nmemb, CTX_FH); |
109 ctx->status = th_get_error(); | 109 ctx->status = th_get_error(); |
110 return ret; | 110 return ret; |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 static size_t th_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) | 114 static size_t th_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx_t *ctx) |
115 { | 115 { |
116 size_t ret = fwrite(ptr, size, nmemb, CTX_FH); | 116 size_t ret = fwrite(ptr, size, nmemb, CTX_FH); |
117 ctx->status = th_get_error(); | 117 ctx->status = th_get_error(); |
118 return ret; | 118 return ret; |
119 } | 119 } |
120 | 120 |
121 | 121 |
122 static char * th_stdio_fgets(char *str, int size, th_ioctx *ctx) | 122 static char * th_stdio_fgets(char *str, int size, th_ioctx_t *ctx) |
123 { | 123 { |
124 char *ret = fgets(str, size, CTX_FH); | 124 char *ret = fgets(str, size, CTX_FH); |
125 ctx->status = th_get_error(); | 125 ctx->status = th_get_error(); |
126 return ret; | 126 return ret; |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 static int th_stdio_fputs(const char *str, th_ioctx *ctx) | 130 static int th_stdio_fputs(const char *str, th_ioctx_t *ctx) |
131 { | 131 { |
132 int ret = fputs(str, CTX_FH); | 132 int ret = fputs(str, CTX_FH); |
133 ctx->status = th_get_error(); | 133 ctx->status = th_get_error(); |
134 return ret; | 134 return ret; |
135 } | 135 } |
136 | 136 |
137 | 137 |
138 static int th_stdio_vfprintf(th_ioctx *ctx, const char *fmt, va_list ap) | 138 static int th_stdio_vfprintf(th_ioctx_t *ctx, const char *fmt, va_list ap) |
139 { | 139 { |
140 int ret = vfprintf(CTX_FH, fmt, ap); | 140 int ret = vfprintf(CTX_FH, fmt, ap); |
141 ctx->status = th_get_error(); | 141 ctx->status = th_get_error(); |
142 return ret; | 142 return ret; |
143 } | 143 } |
144 | 144 |
145 | 145 |
146 const th_ioctx_ops th_stdio_io_ops = | 146 const th_ioctx_t_ops th_stdio_io_ops = |
147 { | 147 { |
148 "stdio", | 148 "stdio", |
149 | 149 |
150 th_stdio_fopen, | 150 th_stdio_fopen, |
151 th_stdio_fclose, | 151 th_stdio_fclose, |