rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
rlfifo.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlfifo.cpp - description
3  -------------------
4  begin : Tue Jan 02 2001
5  copyright : (C) 2001 by R. Lehrig
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include "rlfifo.h"
19 #include "rlcutil.h"
20 #include "string.h"
21 
22 rlFifo::rlFifo(int maxmessages)
23 {
24  maxmes = maxmessages;
25  nmes = 0;
26  list = NULL;
29 }
30 
32 {
33 MessageList *ptr,*lastptr;
34 
35  ptr = list;
36  if(ptr != NULL)
37  {
38  do
39  {
40  lastptr = ptr;
41  ptr = ptr->next;
42  delete [] lastptr->mes;
43  delete lastptr;
44  }
45  while(ptr != NULL);
46  }
49 }
50 
51 int rlFifo::read(void *buf, int maxlen)
52 {
53  int retlen;
54  MessageList *ptr;
55  char *cbuf;
56 
57  cbuf = (char *) buf;
60 
61  if(list->len > maxlen)
62  {
65  return MESSAGE_TO_BIG;
66  }
67 
68  ptr = list;
69  if(ptr->next == NULL)
70  {
71  retlen = ptr->len;
72  memcpy(buf,ptr->mes,retlen);
73  delete [] ptr->mes;
74  delete ptr;
75  list = NULL;
76  nmes--;
78  if(retlen < maxlen && retlen >= 0) cbuf[retlen] = '\0';
79  return retlen;
80  }
81  ptr = ptr->next;
82  retlen = list->len;
83  memcpy(buf,list->mes,retlen);
84  delete [] list->mes;
85  delete list;
86  list = ptr;
87  nmes--;
89  if(retlen < maxlen && retlen >= 0) cbuf[retlen] = '\0';
90  return retlen;
91 }
92 
94 {
95  if(nmes > 0) return DATA_AVAILABLE;
96  return NO_DATA_AVAILABLE;
97 }
98 
99 int rlFifo::write(const void *buf, int len)
100 {
101  MessageList *ptr;
102 
103  if(maxmes == 0);
104  else if(nmes >= maxmes) return FIFO_FULL;
105 
107  if(list == NULL)
108  {
109  list = new MessageList;
110  list->mes = new char [len];
111  list->len = len;
112  list->next = NULL;
113  nmes++;
114  memcpy(list->mes,buf,len);
117  return len;
118  }
119 
120  // go to end of list
121  ptr = list;
122  while(ptr->next != NULL) ptr = ptr->next;
123 
124  ptr->next = new MessageList;
125  ptr = ptr->next;
126  ptr->mes = new char [len];
127  ptr->len = len;
128  ptr->next = NULL;
129  nmes++;
130  memcpy(ptr->mes,buf,len);
133  return len;
134 }
135 
136 int rlFifo::printf(const char *format, ...)
137 {
138  int ret;
139  char message[rl_PRINTF_LENGTH]; // should be big enough
140 
141  va_list ap;
142  va_start(ap,format);
143  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
144  va_end(ap);
145  if(ret < 0) return ret;
146  return write(message,strlen(message));
147 }