diff intercom/cirbuf.cpp @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/intercom/cirbuf.cpp	Fri Jun 25 09:57:52 2010 +0200
@@ -0,0 +1,69 @@
+/* cirbuf.cpp
+ *
+ * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005). 
+ * All Rights Reserved.
+ *
+ * Circular buffers
+ *
+ * Version 0.2
+ */
+
+#include <string.h>
+#include <strings.h>
+
+#include "oss.h"
+#include "cirbuf.h"
+#include "intercomd.h"
+
+CIRBUF::CIRBUF()
+{
+  bzero(buf, CIRBUFSIZE);
+  in = out = len = 0;
+}
+
+void CIRBUF::init()
+{
+  bzero(buf, CIRBUFSIZE);
+  in = out = len = 0;
+}
+
+int CIRBUF::push(char *from, int size)
+{
+  memcpy(buf + in, from, size);
+  in += size;
+  if (in >= CIRBUFSIZE) {
+    in -= CIRBUFSIZE;
+  }
+  len += size;
+  if (len > CIRBUFSIZE) {
+    int oversize = (((len - CIRBUFSIZE) / FRAGSIZE)) * FRAGSIZE;
+    if (oversize < len - CIRBUFSIZE) {
+      oversize += FRAGSIZE;
+    }
+    // delete oldest if overrun
+    out += oversize;
+    if (out >= CIRBUFSIZE) {
+      out -= CIRBUFSIZE;
+    }
+    len -= oversize;
+    return -oversize;
+  } else {
+    return OKAY;
+  }
+}
+
+int CIRBUF::pop(char *to, int size)
+{
+  if (len < size) {
+    // play out silence if underrun
+    bzero(to, size);
+    return ERROR;
+  }
+  memcpy(to, buf + out, size);
+  out += size;
+  if (out >= CIRBUFSIZE) {
+    out -= CIRBUFSIZE;
+  }
+  len -= size;
+  return OKAY;
+}

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.