NoPaste

testprogramm.c

von blan

SNIPPET_TEXT:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define FIFO_SUCCESS      0x00
  6. #define FIFO_ERROR       -0x01
  7.  
  8. #define TRUE             0x01
  9. #define FALSE            0x00
  10. #define NULLPOINTER      0x00
  11. #define NULLBYTE         '\0'
  12.  
  13. typedef unsigned char uint8_t;
  14.  
  15. typedef struct
  16. {
  17.   uint8_t size;
  18. } fragment_t;
  19.  
  20. typedef struct
  21. {
  22.   uint8_t  size;
  23.   int8_t  *data;
  24.   fragment_t fragment;
  25. } fifo_t;
  26.  
  27. fifo_t *fifo_new(uint8_t size, uint8_t fragment);
  28.  
  29. int8_t fifo_push(volatile fifo_t *fifo, const int8_t *data);
  30.  
  31. int8_t fifo_pop(volatile const fifo_t *fifo, int8_t *data);
  32.  
  33. uint8_t fifo_get_size(volatile const fifo_t *fifo);
  34.  
  35. uint8_t fifo_get_fragment_size(volatile const fifo_t *fifo);
  36.  
  37. uint8_t fifo_get_data_size(volatile const fifo_t *fifo);
  38.  
  39. void fifo_destroy(volatile fifo_t *fifo);
  40.  
  41. int8_t fifo_flush(volatile fifo_t *fifo)
  42. {
  43.   if(fifo == NULLPOINTER)
  44.   {
  45.     return FIFO_ERROR;
  46.   }
  47.  
  48.   if(memset((int8_t*) fifo->data, NULLBYTE, fifo->size))
  49.   {
  50.     return FIFO_SUCCESS;
  51.   }
  52.   else
  53.   {
  54.     return FIFO_ERROR;
  55.   }
  56. }
  57.  
  58. int main()
  59. {
  60.   fifo_t *fifo_rx = fifo_new(8, 1);
  61.  
  62.   fifo_push(fifo_rx, "halloabc");
  63.  
  64.   int i;
  65.   for(i = 0; i < 9; i++)
  66.   {
  67.     printf("%i\n", fifo_rx->data[i]);
  68.   }
  69.  
  70.   //fifo_flush(fifo_rx);
  71.  
  72.   printf("----------------------------\n");
  73.  
  74.   int8_t data = 0x69;
  75.   fifo_pop(fifo_rx, &data);
  76.   printf("%c\n", data);
  77.   //fifo_pop(fifo_rx, &data);
  78.   //printf("%c\n", data);
  79.  
  80.   printf("----------------------------\n");
  81.  
  82.   for(i = 0; i < 9; i++)
  83.   {
  84.     printf("%i\n", fifo_rx->data[i]);
  85.   }
  86.  
  87.   fifo_destroy(fifo_rx);
  88. }
  89.  
  90. fifo_t *fifo_new(uint8_t size, uint8_t fragment)
  91. {
  92.   if(size % fragment != 0)
  93.   {
  94.     return NULLPOINTER;
  95.   }
  96.  
  97.   fifo_t *fifo = (fifo_t*) malloc(sizeof(fifo_t));
  98.  
  99.   if(fifo == NULLPOINTER)
  100.   {
  101.     return NULLPOINTER;
  102.   }
  103.   else
  104.   {
  105.     fifo->size          = size;
  106.     fifo->fragment.size = fragment;
  107.  
  108.     fifo->data = (int8_t*) malloc(size + sizeof(NULLBYTE));
  109.  
  110.     if(fifo->data)
  111.     {
  112.       if(memset((int8_t*) fifo->data, NULLBYTE, fifo->size + sizeof(NULLBYTE)))
  113.       {
  114.         return fifo;
  115.       }
  116.       else
  117.       {
  118.         return NULLPOINTER;
  119.       }
  120.     }
  121.     else
  122.     {
  123.       return NULLPOINTER;
  124.     }
  125.   }
  126. }
  127.  
  128. int8_t fifo_push(volatile fifo_t *fifo, const int8_t *data)
  129. {
  130.   if(fifo == NULLPOINTER)
  131.   {
  132.     return FIFO_ERROR;
  133.   }
  134.  
  135.   if(data == NULLPOINTER)
  136.   {
  137.     return FIFO_ERROR;
  138.   }
  139.  
  140.   if(strlen(fifo->data) + strlen(data) > fifo->size)
  141.   {
  142.     return FIFO_ERROR;
  143.   }
  144.   else
  145.   {
  146.     if(memcpy((int8_t*) fifo->data + strlen(fifo->data), (int8_t*) data, strlen(data)))
  147.     {
  148.       return FIFO_SUCCESS;
  149.     }
  150.     else
  151.     {
  152.       return FIFO_ERROR;
  153.     }
  154.   }
  155. }
  156.  
  157. int8_t fifo_pop(volatile const fifo_t *fifo, int8_t *data)
  158. {
  159.   if(fifo == NULLPOINTER)
  160.   {
  161.     return FIFO_ERROR;
  162.   }
  163.  
  164.   if(data == NULLPOINTER)
  165.   {
  166.     return FIFO_ERROR;
  167.   }
  168.  
  169.   if(strlen(fifo->data) >= fifo->fragment.size)
  170.   {
  171.     if(memcpy(data, fifo->data, fifo->fragment.size))
  172.     {
  173.       if(memmove(fifo->data, fifo->data + fifo->fragment.size, strlen(fifo->data) - fifo->fragment.size + 1))
  174.       {
  175.         return FIFO_SUCCESS;
  176.       }
  177.       else
  178.       {
  179.         return FIFO_ERROR;
  180.       }
  181.     }
  182.     else
  183.     {
  184.       return FIFO_ERROR;
  185.     }
  186.   }
  187.   else
  188.   {
  189.     return FIFO_ERROR;
  190.   }
  191. }
  192.  
  193. void fifo_destroy(volatile fifo_t *fifo)
  194. {
  195.   if(fifo != NULLPOINTER)
  196.   {
  197.     free((int8_t*) fifo->data);
  198.     free((fifo_t*) fifo);
  199.   }
  200. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN