Mercurial > hg > th-libs
comparison th_ioctx_stdio.c @ 678:7e207f1023d9
Split stdio and memio stuff to separate files from th_stdio.c
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Feb 2020 12:19:02 +0200 |
parents | |
children | 49c818acbbbe |
comparison
equal
deleted
inserted
replaced
677:927772fb0745 | 678:7e207f1023d9 |
---|---|
1 /* | |
2 * Simple I/O abstraction and context handling layer | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
4 * (C) Copyright 2012-2020 Tecnic Software productions (TNSP) | |
5 * | |
6 * Please read file 'COPYING' for information on license and distribution. | |
7 */ | |
8 #include "th_ioctx.h" | |
9 #include <stdio.h> | |
10 | |
11 #define CTX_FH ((FILE *) ctx->data) | |
12 | |
13 | |
14 static int th_stdio_fopen(th_ioctx *ctx) | |
15 { | |
16 ctx->data = (void *) fopen(ctx->filename, ctx->mode); | |
17 ctx->status = th_get_error(); | |
18 return (ctx->data != NULL) ? THERR_OK : THERR_FOPEN; | |
19 } | |
20 | |
21 | |
22 static void th_stdio_fclose(th_ioctx *ctx) | |
23 { | |
24 if (CTX_FH != NULL) | |
25 { | |
26 fclose(CTX_FH); | |
27 ctx->data = NULL; | |
28 } | |
29 } | |
30 | |
31 | |
32 static int th_stdio_ferror(th_ioctx *ctx) | |
33 { | |
34 return ctx->status; | |
35 } | |
36 | |
37 | |
38 static off_t th_stdio_ftell(th_ioctx *ctx) | |
39 { | |
40 return ftello(CTX_FH); | |
41 } | |
42 | |
43 | |
44 static int th_stdio_fseek(th_ioctx *ctx, const off_t pos, const int whence) | |
45 { | |
46 int ret = fseeko(CTX_FH, pos, whence); | |
47 ctx->status = th_get_error(); | |
48 return ret; | |
49 } | |
50 | |
51 | |
52 static int th_stdio_freset(th_ioctx *ctx) | |
53 { | |
54 if (CTX_FH != NULL) | |
55 return th_stdio_fseek(ctx, 0, SEEK_SET); | |
56 else | |
57 return THERR_OK; | |
58 } | |
59 | |
60 | |
61 static off_t th_stdio_fsize(th_ioctx *ctx) | |
62 { | |
63 off_t savePos, fileSize; | |
64 | |
65 // Check if the size is cached | |
66 if (ctx->size != 0) | |
67 return ctx->size; | |
68 | |
69 // Get file size | |
70 if ((savePos = th_stdio_ftell(ctx)) < 0) | |
71 return -1; | |
72 | |
73 if (th_stdio_fseek(ctx, 0, SEEK_END) != 0) | |
74 return -1; | |
75 | |
76 if ((fileSize = th_stdio_ftell(ctx)) < 0) | |
77 return -1; | |
78 | |
79 if (th_stdio_fseek(ctx, savePos, SEEK_SET) != 0) | |
80 return -1; | |
81 | |
82 ctx->size = fileSize; | |
83 return fileSize; | |
84 } | |
85 | |
86 | |
87 static BOOL th_stdio_feof(th_ioctx *ctx) | |
88 { | |
89 return feof(CTX_FH); | |
90 } | |
91 | |
92 | |
93 static int th_stdio_fgetc(th_ioctx *ctx) | |
94 { | |
95 int ret = fgetc(CTX_FH); | |
96 ctx->status = th_get_error(); | |
97 return ret; | |
98 } | |
99 | |
100 | |
101 static int th_stdio_fputc(int v, th_ioctx *ctx) | |
102 { | |
103 int ret = fputc(v, CTX_FH); | |
104 ctx->status = th_get_error(); | |
105 return ret; | |
106 } | |
107 | |
108 | |
109 static size_t th_stdio_fread(void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) | |
110 { | |
111 size_t ret = fread(ptr, size, nmemb, CTX_FH); | |
112 ctx->status = th_get_error(); | |
113 return ret; | |
114 } | |
115 | |
116 | |
117 static size_t th_stdio_fwrite(const void *ptr, size_t size, size_t nmemb, th_ioctx *ctx) | |
118 { | |
119 size_t ret = fwrite(ptr, size, nmemb, CTX_FH); | |
120 ctx->status = th_get_error(); | |
121 return ret; | |
122 } | |
123 | |
124 | |
125 static char * th_stdio_fgets(char *str, int size, th_ioctx *ctx) | |
126 { | |
127 char *ret = fgets(str, size, CTX_FH); | |
128 ctx->status = th_get_error(); | |
129 return ret; | |
130 } | |
131 | |
132 | |
133 static int th_stdio_fputs(const char *str, th_ioctx *ctx) | |
134 { | |
135 int ret = fputs(str, CTX_FH); | |
136 ctx->status = th_get_error(); | |
137 return ret; | |
138 } | |
139 | |
140 | |
141 static int th_stdio_vfprintf(th_ioctx *ctx, const char *fmt, va_list ap) | |
142 { | |
143 int ret = vfprintf(CTX_FH, fmt, ap); | |
144 ctx->status = th_get_error(); | |
145 return ret; | |
146 } | |
147 | |
148 | |
149 const th_ioctx_ops th_stdio_io_ops = | |
150 { | |
151 "stdio", | |
152 | |
153 th_stdio_fopen, | |
154 th_stdio_fclose, | |
155 | |
156 th_stdio_freset, | |
157 th_stdio_ferror, | |
158 th_stdio_fseek, | |
159 th_stdio_fsize, | |
160 th_stdio_ftell, | |
161 th_stdio_feof, | |
162 th_stdio_fgetc, | |
163 th_stdio_fputc, | |
164 th_stdio_fread, | |
165 th_stdio_fwrite, | |
166 | |
167 th_stdio_fgets, | |
168 th_stdio_fputs, | |
169 th_stdio_vfprintf, | |
170 }; |