From 81dd6b162351d93dae5d71a45df56ee1a74a36ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Tue, 7 Jul 2020 13:04:26 -0300 Subject: Split application into server and client --- client.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 client.cpp (limited to 'client.cpp') diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..f99dc5d --- /dev/null +++ b/client.cpp @@ -0,0 +1,79 @@ +#define MAX_ARGS 16 +#define pos(x, y) (x) + (y) * window_w + +struct ArgList { + int len, cap; + int *args; + + ArgList(int cap) : len(0), cap(cap) { + args = new int[cap]; + for (int i = 0; i < cap; args[i++] = 0); + } + ~ArgList() { delete[] args; } + + int pop() { + return args[--len]; + } + + void push(int value) { + args[len++] = value; + } + +}; + +struct Client { + int sockfd; + Point cursor; + Point window_start; + ArgList args; + + Client(const Buffer &b) : + cursor(b.storage, 0), + window_start(cursor), + args(MAX_ARGS) + {} + + void show() { + int window_h = args.pop(); + int window_w = args.pop(); + char *view = new char[window_w * window_h]; + + Point window_end(window_start); + for (int i = 0; i < window_h; i++) { + for (int j = 0; j < window_w; j++) { + if (window_end == cursor) { + view[pos(j, i)] = '$'; + } else { + view[pos(j, i)] = window_end.element(); + } + if (window_end.element() == '\n') { + for (int k = j + 1; k < window_w; k++) { + view[pos(k, i)] = 0; + } + j = window_w; + } else if (window_end.element() == '\t') { + for (int k = j + 1; k < j + 8; k++) { + view[pos(k, i)] = 0; + } + j = j + 7; + } + window_end++; + } + } + + write(sockfd, view, window_w * window_h); + delete[] view; + } + + void move() { + int target = args.pop(); + while (target > 0) { + cursor++; + target--; + } + while (target < 0) { + cursor--; + target++; + } + } +}; -- cgit v1.2.3