summaryrefslogtreecommitdiffstats
path: root/src/stream.c
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-13 21:26:49 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2021-03-13 21:26:49 +0200
commitc68444ff76eff88fbfa63a3ddbd6b9ce72337159 (patch)
tree90158ba6272fbb53c5df5852d3415493305c4deb /src/stream.c
parent4dcfe9c0a8b03915d93420d523d6bdc1884bd137 (diff)
downloadalgos-ld1-c68444ff76eff88fbfa63a3ddbd6b9ce72337159.tar.gz
algos-ld1-c68444ff76eff88fbfa63a3ddbd6b9ce72337159.tar.bz2
algos-ld1-c68444ff76eff88fbfa63a3ddbd6b9ce72337159.zip
complete core featureset with linked list files.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/stream.c')
-rw-r--r--src/stream.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/stream.c b/src/stream.c
index da8d9c8..6a84940 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -119,6 +119,28 @@ err:
return ret;
}
+struct entry_l *file_get_list(struct stream * const in, struct entry_l * const store)
+{
+ struct entry_l ent;
+ struct entry_l *ret = NULL;
+ ssize_t bytes_read;
+
+ if (in->index < in->n) {
+ in->cnode = in->cnode ? in->cnode : &in->cnode_f;
+ do {
+ try(0 > (bytes_read = pread(in->fd, &ent, sizeof(ent), in->cnode->file_offset)), err, NULL, "Writing to stream failed with %i", errno);
+ } while (bytes_read != sizeof(ent));
+
+ in->cnode->file_offset = ent.file_offset;
+ ++in->index;
+ store->val = ent.val;
+ ret = store;
+ }
+
+err:
+ return ret;
+}
+
int file_put_array(struct stream * const in, const struct entry_l * const data)
{
int ret = 0;
@@ -135,6 +157,37 @@ err:
return ret;
}
+int file_put_list(struct stream * const in, const struct entry_l * const node)
+{
+ int ret = 0;
+ ssize_t bytes_written;
+ struct entry_l tmp = {0};
+
+ try(in->index >= in->n, err, EINVAL, "can't add element: out of cache bounds");
+
+ if (!in->cnode) { /* if this is the very first one */
+ in->cnode = &in->cnode_f;
+ } else {
+ in->cnode->file_offset += sizeof(*node);
+ /* writing the offset of the node being added to the current node's offset field */
+ do {
+ try(0 > (bytes_written = pwrite(in->fd, &in->cnode->file_offset, sizeof(in->cnode->file_offset), in->cnode->file_offset - sizeof(*node) + sizeof(node->val))), err, errno, "Writing to stream failed with %i", ret);
+ } while (bytes_written != sizeof(in->cnode->file_offset));
+ }
+
+ tmp.val = node->val;
+
+ if (in->index < in->n) {
+ do {
+ try(0 > (bytes_written = pwrite(in->fd, &tmp, sizeof(tmp), in->cnode->file_offset)), err, errno, "Writing to stream failed with %i", ret);
+ } while (bytes_written != sizeof(tmp));
+ }
+
+ ++in->index;
+err:
+ return ret;
+}
+
int file_transfer(struct stream * const src, struct stream * const dest)
{
int ret = 0;