LCOV - code coverage report
Current view: top level - girepository/cmph - buffer_entry.c (source / functions) Coverage Total Hit
Test: unnamed Lines: 0.0 % 63 0
Test Date: 2024-11-26 05:23:01 Functions: 0.0 % 7 0
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : #include "buffer_entry.h"
       2                 :             : #include <stdio.h>
       3                 :             : #include <assert.h>
       4                 :             : #include <stdlib.h>
       5                 :             : #include <string.h>
       6                 :             : 
       7                 :             : struct __buffer_entry_t
       8                 :             : {
       9                 :             :         FILE *fd;
      10                 :             :         cmph_uint8 * buff;
      11                 :             :         cmph_uint32 capacity, // buffer entry capacity
      12                 :             :                     nbytes,   // buffer entry used bytes
      13                 :             :                     pos;      // current read position in buffer entry
      14                 :             :         cmph_uint8  eof;      // flag to indicate end of file
      15                 :             : };
      16                 :             : 
      17                 :           0 : buffer_entry_t * buffer_entry_new(cmph_uint32 capacity)
      18                 :             : {
      19                 :           0 :         buffer_entry_t *buff_entry = (buffer_entry_t *)malloc(sizeof(buffer_entry_t));
      20                 :           0 :         assert(buff_entry);
      21                 :           0 :         buff_entry->fd = NULL;
      22                 :           0 :         buff_entry->buff = NULL;
      23                 :           0 :         buff_entry->capacity = capacity;
      24                 :           0 :         buff_entry->nbytes = capacity;
      25                 :           0 :         buff_entry->pos = capacity;
      26                 :           0 :         buff_entry->eof = 0;
      27                 :           0 :         return buff_entry;
      28                 :             : }
      29                 :             : 
      30                 :           0 : void buffer_entry_open(buffer_entry_t * buffer_entry, char * filename)
      31                 :             : {
      32                 :           0 :         buffer_entry->fd = fopen(filename, "rb");
      33                 :           0 : }
      34                 :             : 
      35                 :           0 : void buffer_entry_set_capacity(buffer_entry_t * buffer_entry, cmph_uint32 capacity)
      36                 :             : {
      37                 :           0 :         buffer_entry->capacity = capacity;
      38                 :           0 : }
      39                 :             : 
      40                 :             : 
      41                 :           0 : cmph_uint32 buffer_entry_get_capacity(buffer_entry_t * buffer_entry)
      42                 :             : {
      43                 :           0 :         return buffer_entry->capacity;
      44                 :             : }
      45                 :             : 
      46                 :           0 : static void buffer_entry_load(buffer_entry_t * buffer_entry)
      47                 :             : {
      48                 :           0 :         free(buffer_entry->buff);
      49                 :           0 :         buffer_entry->buff = (cmph_uint8 *)calloc((size_t)buffer_entry->capacity, sizeof(cmph_uint8));
      50                 :           0 :         buffer_entry->nbytes = (cmph_uint32)fread(buffer_entry->buff, (size_t)1, (size_t)buffer_entry->capacity, buffer_entry->fd);
      51                 :           0 :         if (buffer_entry->nbytes != buffer_entry->capacity) buffer_entry->eof = 1;
      52                 :           0 :         buffer_entry->pos = 0;
      53                 :           0 : }
      54                 :             : 
      55                 :           0 : cmph_uint8 * buffer_entry_read_key(buffer_entry_t * buffer_entry, cmph_uint32 * keylen)
      56                 :             : {
      57                 :           0 :         cmph_uint8 * buf = NULL;
      58                 :           0 :         cmph_uint32 lacked_bytes = sizeof(*keylen);
      59                 :           0 :         cmph_uint32 copied_bytes = 0;
      60                 :           0 :         if(buffer_entry->eof && (buffer_entry->pos == buffer_entry->nbytes)) // end
      61                 :             :         {
      62                 :           0 :                 free(buf);
      63                 :           0 :                 return NULL;
      64                 :             :         }
      65                 :           0 :         if((buffer_entry->pos + lacked_bytes) > buffer_entry->nbytes) 
      66                 :             :         {
      67                 :           0 :                 copied_bytes = buffer_entry->nbytes - buffer_entry->pos;
      68                 :           0 :                 lacked_bytes = (buffer_entry->pos + lacked_bytes) - buffer_entry->nbytes;
      69                 :           0 :                 if (copied_bytes != 0) memcpy(keylen, buffer_entry->buff + buffer_entry->pos, (size_t)copied_bytes);
      70                 :           0 :                 buffer_entry_load(buffer_entry);
      71                 :             :         }
      72                 :           0 :         memcpy(keylen + copied_bytes, buffer_entry->buff + buffer_entry->pos, (size_t)lacked_bytes);
      73                 :           0 :         buffer_entry->pos += lacked_bytes;
      74                 :             :         
      75                 :           0 :         lacked_bytes = *keylen;
      76                 :           0 :         copied_bytes = 0;
      77                 :           0 :         buf = (cmph_uint8 *)malloc(*keylen + sizeof(*keylen));
      78                 :           0 :         memcpy(buf, keylen, sizeof(*keylen));
      79                 :           0 :         if((buffer_entry->pos + lacked_bytes) > buffer_entry->nbytes) {
      80                 :           0 :                 copied_bytes = buffer_entry->nbytes - buffer_entry->pos;
      81                 :           0 :                 lacked_bytes = (buffer_entry->pos + lacked_bytes) - buffer_entry->nbytes;
      82                 :           0 :                 if (copied_bytes != 0) {
      83                 :           0 :                         memcpy(buf + sizeof(*keylen), buffer_entry->buff + buffer_entry->pos, (size_t)copied_bytes);
      84                 :             :                 }
      85                 :           0 :                 buffer_entry_load(buffer_entry);
      86                 :             :         }        
      87                 :           0 :         memcpy(buf+sizeof(*keylen)+copied_bytes, buffer_entry->buff + buffer_entry->pos, (size_t)lacked_bytes);
      88                 :           0 :         buffer_entry->pos += lacked_bytes;
      89                 :           0 :         return buf;
      90                 :             : }
      91                 :             : 
      92                 :           0 : void buffer_entry_destroy(buffer_entry_t * buffer_entry)
      93                 :             : {
      94                 :           0 :   fclose(buffer_entry->fd);
      95                 :           0 :   buffer_entry->fd = NULL;
      96                 :           0 :   free(buffer_entry->buff);
      97                 :           0 :   buffer_entry->buff = NULL;
      98                 :           0 :   buffer_entry->capacity = 0;
      99                 :           0 :   buffer_entry->nbytes = 0;
     100                 :           0 :   buffer_entry->pos = 0;  
     101                 :           0 :   buffer_entry->eof = 0;
     102                 :           0 :   free(buffer_entry);
     103                 :           0 : }
        

Generated by: LCOV version 2.0-1