summaryrefslogtreecommitdiffstats
path: root/src/buffer.c
diff options
context:
space:
mode:
authormongo <mongo@iomega>2016-04-15 16:20:17 -0300
committermongo <mongo@iomega>2016-04-15 16:20:17 -0300
commitf686ba184e0af3fd37aa8a743631a7a376f30843 (patch)
treee9a48dc691511a2961f93163944ba0ca1a84e5b3 /src/buffer.c
parentc0a088d7a4bc61e6e69fa5bd8964c39f68507c71 (diff)
Renamed src.scim2 to src
Diffstat (limited to 'src/buffer.c')
-rwxr-xr-xsrc/buffer.c147
1 files changed, 147 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100755
index 0000000..45f6c0d
--- /dev/null
+++ b/src/buffer.c
@@ -0,0 +1,147 @@
+#include <stdlib.h>
+#include <wchar.h>
+
+#include "buffer.h"
+#include "macros.h"
+#include "utils/string.h"
+
+// Create buffer as list of blocks
+struct block * create_buf() {
+ struct block * b = (struct block *) malloc(sizeof(struct block));
+ b->value = '\0';
+ b->pnext = NULL;
+ return b;
+}
+
+// Add an wint_t to a buffer
+void addto_buf(struct block * buf, wint_t d) {
+ struct block * aux = buf;
+
+ if (buf->value == '\0') {
+ buf->value = d;
+ } else {
+ struct block * b = (struct block *) malloc(sizeof(struct block));
+ b->value = d;
+ b->pnext = NULL;
+
+ while (aux->pnext != NULL)
+ aux = aux->pnext;
+ aux->pnext = b;
+ }
+ return;
+}
+
+// Replace the elements of "origen" buffer to "destino" buffer
+void copybuffer(struct block * origen, struct block * destino) {
+ flush_buf(destino);
+ int len = get_bufsize(origen);
+ int i;
+ for (i=0; i < len; i++)
+ addto_buf(destino, get_bufval(origen, i));
+ return;
+}
+
+// Replace the element of a buffer at 'pos' with a '\0'
+//FIXME
+void del_buf (struct block * buf, int pos) {
+ int i;
+ struct block * ant = buf;
+ struct block * cur = buf;
+ for (i = 0; i < pos; i++) {
+ ant = cur;
+ cur = cur->pnext;
+ }
+ if (ant == cur) {
+ cur->value = '\0';
+ //buf = cur->pnext; //FIXME
+ //free(cur);
+ } else {
+ ant->pnext = cur->pnext;
+ free(cur);
+ }
+ return;
+}
+
+void flush_buf (struct block * buf) {
+ if (buf == NULL) return;
+
+ struct block * aux, * np;
+ for (aux = buf->pnext; aux != NULL; aux = np)
+ {
+ np = aux->pnext;
+ free(aux);
+ }
+ buf->value = '\0';
+ buf->pnext = NULL;
+
+ return;
+}
+
+// Delete all blocks of a buffer
+// including the initial node
+void erase_buf (struct block * buf) {
+ flush_buf(buf);
+ free(buf);
+ return;
+}
+
+// get size of buffer (included special chars)
+int get_bufsize(struct block * buf) {
+ struct block * b_aux = buf;
+ if (b_aux == NULL || b_aux->value == '\0') return 0;
+ int c = 0;
+ while (b_aux != NULL) {
+ c++;
+ b_aux = b_aux->pnext;
+ }
+ return c;
+}
+
+// get printable buffer lenght (excluded special chars)
+// (special chars should never be printed in screen)
+int get_pbuflen(struct block * buf) {
+ struct block * b_aux = buf;
+ if (b_aux == NULL || b_aux->value == '\0') return 0;
+ int c = 0;
+ while (b_aux != NULL) {
+ if ( !is_idchar(b_aux->value) ) c++;
+ b_aux = b_aux->pnext;
+ }
+ return c;
+}
+
+// return the int value of the n block
+int get_bufval(struct block * buf, int d) {
+ int i;
+ struct block * b_aux = buf;
+ for (i = 0; i < d; i++) {
+ b_aux = b_aux->pnext;
+ }
+ return b_aux->value;
+}
+
+// return if an int value is found in a buffer
+int find_val(struct block * buf, int value) {
+ struct block * b_aux = buf;
+ while ( b_aux != NULL && b_aux->value != '\0' ) {
+ if (b_aux->value == value) return 1;
+ b_aux = b_aux->pnext;
+ }
+ return 0;
+}
+
+// Delete the first element in a buffer
+struct block * dequeue (struct block * buf) {
+ if (buf == NULL) return buf;
+ struct block * sig;
+ if (buf->value == '\0') return buf;
+
+ if (buf->pnext == NULL) {
+ buf->value = '\0';
+ } else {
+ sig = buf->pnext;
+ //free(buf);
+ buf = sig;
+ }
+ return buf;
+}