2 #include <lunaix/fs/twimap.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
6 #include <klibc/stdio.h>
7 #include <klibc/string.h>
9 #define TWIMAP_BUFFER_SIZE 1024
12 __twimap_default_reset(struct twimap* map)
18 __twimap_default_gonext(struct twimap* map)
24 __twimap_file_read(struct v_inode* inode, void* buf, size_t len, size_t fpos)
26 struct twimap* map = (struct twimap*)(inode->data);
27 return twimap_read(map, buf, len, fpos);
31 twimap_read(struct twimap* map, void* buffer, size_t len, size_t fpos)
33 map->buffer = valloc(TWIMAP_BUFFER_SIZE);
36 // FIXME what if TWIMAP_BUFFER_SIZE is not big enough?
43 } while (pos < fpos && map->go_next(map));
54 size_t acc_size = MIN(len, map->size_acc - (pos - fpos)), rdlen = acc_size;
55 memcpy(buffer, map->buffer + (pos - fpos), acc_size);
57 while (acc_size < len && map->go_next(map)) {
60 rdlen = MIN(len - acc_size, map->size_acc);
61 memcpy(buffer + acc_size, map->buffer, rdlen);
70 twimap_printf(struct twimap* mapping, const char* fmt, ...)
75 char* buf = mapping->buffer + mapping->size_acc;
78 __ksprintf_internal(buf, fmt, TWIMAP_BUFFER_SIZE, args);
84 twimap_memcpy(struct twimap* mapping, const void* src, const size_t len)
86 mapping->size_acc = MIN(TWIMAP_BUFFER_SIZE, len);
87 memcpy(mapping->buffer, src, mapping->size_acc);
89 return mapping->size_acc;
93 twimap_memappend(struct twimap* mapping, const void* src, const size_t len)
95 size_t cpy_len = MIN(TWIMAP_BUFFER_SIZE - mapping->size_acc, len);
96 memcpy(mapping->buffer + mapping->size_acc, src, cpy_len);
97 mapping->size_acc += cpy_len;
103 twimap_create(void* data)
105 struct twimap* map = vzalloc(sizeof(struct twimap));
106 map->reset = __twimap_default_reset;
107 map->go_next = __twimap_default_gonext;
113 struct v_file_ops twimap_file_ops = { .close = default_file_close,
114 .read = __twimap_file_read,
115 .readdir = default_file_readdir,
116 .seek = default_file_seek,
117 .write = default_file_write };