rllib  1
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 
100 {
101  if(nmes > 0) return nmes;
102  return 0;
103 }
104 
105 int rlFifo::write(const void *buf, int len)
106 {
107  MessageList *ptr;
108 
109  if(maxmes == 0);
110  else if(nmes >= maxmes) return FIFO_FULL;
111 
113  if(list == NULL)
114  {
115  list = new MessageList;
116  list->mes = new char [len];
117  list->len = len;
118  list->next = NULL;
119  nmes++;
120  memcpy(list->mes,buf,len);
123  return len;
124  }
125 
126  // go to end of list
127  ptr = list;
128  while(ptr->next != NULL) ptr = ptr->next;
129 
130  ptr->next = new MessageList;
131  ptr = ptr->next;
132  ptr->mes = new char [len];
133  ptr->len = len;
134  ptr->next = NULL;
135  nmes++;
136  memcpy(ptr->mes,buf,len);
139  return len;
140 }
141 
142 int rlFifo::printf(const char *format, ...)
143 {
144  int ret;
145  char message[rl_PRINTF_LENGTH]; // should be big enough
146 
147  va_list ap;
148  va_start(ap,format);
149  ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
150  va_end(ap);
151  if(ret < 0) return ret;
152  return write(message,strlen(message));
153 }
int rlwthread_mutex_destroy(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:178
int rlwthread_mutex_init(pthread_mutex_t *mptr, const pthread_mutexattr_t *attr)
Definition: rlwthread.cpp:160
int rlwthread_mutex_lock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:193
WSEMAPHORE semaphore
Definition: rlfifo.h:53
int nmes
Definition: rlfifo.h:51
struct _MessageList_ * next
Definition: rlfifo.h:32
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
MessageList * list
Definition: rlfifo.h:52
struct rlFifo::_MessageList_ MessageList
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
int read(void *buf, int maxlen)
Definition: rlfifo.cpp:51
int nmesAvailable()
Definition: rlfifo.cpp:99
pthread_mutex_t mutex
Definition: rlfifo.h:54
int rlwrapincrement_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:310
int maxmes
Definition: rlfifo.h:50
int rlwrapinit_semaphore(WSEMAPHORE *s, int cmax)
Definition: rlwthread.cpp:268
int printf(const char *format,...)
Definition: rlfifo.cpp:142
int rlwthread_mutex_unlock(pthread_mutex_t *mptr)
Definition: rlwthread.cpp:238
int rlwrapwait_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:341
int poll()
Definition: rlfifo.cpp:93
virtual ~rlFifo()
Definition: rlfifo.cpp:31
int rlwrapdestroy_semaphore(WSEMAPHORE *s)
Definition: rlwthread.cpp:294
int write(const void *buf, int len)
Definition: rlfifo.cpp:105
rlFifo(int maxmessages=0)
Definition: rlfifo.cpp:22