Skip to main content
Sign in
Snippets Groups Projects
Select Git revision
  • 9e27c274b999306510ba22454598630707a9d87b
  • master default
  • labcomm2014
  • labcomm2006
  • python_sig_hash
  • typedefs
  • anders.blomdell
  • typeref
  • pragma
  • compiler-refactoring
  • labcomm2013
  • v2014.6
  • v2015.0
  • v2014.5
  • v2014.4
  • v2006.0
  • v2014.3
  • v2014.2
  • v2014.1
  • v2014.0
  • v2013.0
21 results

labcomm_fd_reader_writer.c

Blame
  • labcomm_fd_reader_writer.c 2.14 KiB
    #include <errno.h>
    #include <unistd.h>
    #include "labcomm.h"
    
    #define BUFFER_SIZE 2048
    
    int labcomm_fd_reader(
      labcomm_reader_t *r, 
      labcomm_reader_action_t action)
    {
      int result = -EINVAL;
      int *fd = r->context;
      
      switch (action) {
        case labcomm_reader_alloc: {
          r->data = malloc(BUFFER_SIZE);
          if (r->data) {
            r->data_size = BUFFER_SIZE;
            result = r->data_size;
          } else {
            r->data_size = 0;
            result = -ENOMEM;
          }
          r->count = 0;
          r->pos = 0;
        } break;
        case labcomm_reader_start: 
        case labcomm_reader_continue: {
          if (r->pos < r->count) {
            result = r->count - r->pos;
          } else {
            int err;
    
            r->pos = 0;
            err = read(*fd, r->data, r->data_size);
            if (err <= 0) {
              r->count = 0;
              result = -1;
            } else {
              r->count = err;
              result = r->count - r->pos;
            }
          }
        } break;
        case labcomm_reader_end: {
          result = 0;
        } break;
        case labcomm_reader_free: {
          free(r->data);
          r->data = 0;
          r->data_size = 0;
          r->count = 0;
          r->pos = 0;
          result = 0;
        } break;
      }
      return result;
    }
    
    int labcomm_fd_writer(
      labcomm_writer_t *w, 
      labcomm_writer_action_t action)
    {
      int result = 0;
      int *fd = w->context;
    
      switch (action) {
        case labcomm_writer_alloc: {
          w->data = malloc(BUFFER_SIZE);
          if (! w->data) {