Hash :
1ae6ad0e
Author :
Thomas de Grivel
Date :
2024-07-15T19:59:52
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "alloc.h"
#include "assert.h"
#include <stdio.h>
#include "buf.h"
#include "buf_file.h"
#include "buf_save.h"
typedef struct buf_file {
FILE *fp;
} s_buf_file;
sw buf_file_open_r_refill (s_buf *buf);
sw buf_file_open_w_flush (s_buf *buf);
sw buf_file_open_w_seek (s_buf *buf, sw offset, u8 whence);
void buf_file_close (s_buf *buf)
{
assert(buf);
buf_flush(buf);
buf->flush = NULL;
buf->refill = NULL;
free(buf->user_ptr);
buf->user_ptr = NULL;
}
bool buf_file_is_open (s_buf *buf)
{
s_buf_file *buf_file;
assert(buf);
if (buf->user_ptr) {
buf_file = (s_buf_file *) buf->user_ptr;
if (! ferror(buf_file->fp) && ! feof(buf_file->fp))
return true;
}
return false;
}
s_buf * buf_file_open_r (s_buf *buf, FILE *fp)
{
s_buf_file *buf_file;
assert(buf);
assert(fp);
buf_file = alloc(sizeof(s_buf_file));
if (! buf_file)
return NULL;
buf_file->fp = fp;
buf->refill = buf_file_open_r_refill;
buf->user_ptr = buf_file;
return buf;
}
sw buf_file_open_r_refill (s_buf *buf)
{
uw r;
uw size;
assert(buf);
assert(buf->user_ptr);
if (buf->rpos > buf->wpos ||
buf->wpos > buf->size)
return -1;
size = buf->size - buf->wpos;
r = fread(buf->ptr.pchar + buf->wpos, 1, size,
((s_buf_file *) (buf->user_ptr))->fp);
if (buf->wpos + r > buf->size) {
err_puts("buf_file_open_r_refill: buffer overflow");
assert(! "buf_file_open_r_refill: buffer overflow");
return -1;
}
buf->wpos += r;
return r;
}
s_buf * buf_file_open_w (s_buf *buf, FILE *fp)
{
s_buf_file *buf_file;
assert(buf);
assert(fp);
buf_file = alloc(sizeof(s_buf_file));
if (! buf_file)
return NULL;
buf_file->fp = fp;
buf->flush = buf_file_open_w_flush;
buf->seek = buf_file_open_w_seek;
buf->user_ptr = buf_file;
return buf;
}
sw buf_file_open_w_flush (s_buf *buf)
{
s_buf_file *buf_file;
uw min_wpos;
s_buf_save *save;
sw size;
assert(buf);
assert(buf->user_ptr);
if (buf->rpos)
return -1;
if (buf->rpos > buf->wpos)
return -1;
if (buf->wpos > buf->size)
return -1;
min_wpos = buf_save_min_wpos(buf);
size = min_wpos;
if (size == 0)
return buf->size - buf->wpos;
buf_file = buf->user_ptr;
if (fwrite(buf->ptr.p, size, 1, buf_file->fp) != 1) {
err_puts("buf_file_open_w_flush: fwrite");
return -1;
}
fflush(buf_file->fp);
buf->wpos -= size;
save = buf->save;
while (save) {
save->wpos -= size;
save = save->next;
}
return size;
}
sw buf_file_open_w_seek (s_buf *buf, sw offset, u8 whence)
{
s_buf_file *buf_file;
sw r;
assert(buf);
assert(! buf->save);
assert(buf->user_ptr);
buf_file = buf->user_ptr;
if ((r = buf_flush(buf)) < 0)
return r;
if (fseek(buf_file->fp, offset, whence)) {
err_puts("buf_file_open_w_seek: fseek");
return -1;
}
return 0;
}