ref: 502841cc354c028d2ef5d7b602aa65e7fb3974af
parent: 3627677c561bb7c24d2ce524ae47d3bccdd1246d
author: mkf <mkf@cloud9p.org>
date: Tue May 7 08:36:40 EDT 2024
util.[ch]: generic functions, mostly lifted frrom netclient
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@
Lib=\
engine.o\
unix.o\
+ util.o\
Cli= cli.o
Srv= srv.o
--- a/mkfile
+++ b/mkfile
@@ -2,6 +2,7 @@
CFILES=\
netclient.c\
engine.c\
+ util.c\
gui9.c\
DOC=\
--- /dev/null
+++ b/util.c
@@ -1,0 +1,97 @@
+#ifdef unix
+#include "unix.h"
+#else
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#endif
+
+#include "engine.h"
+
+
+int
+isnum(char *s, unsigned int n)
+{
+ if(strlen(s) < n)
+ n = strlen(s);
+
+ for(int i = 0 ; i < n ; i++)
+ {
+ if(s[i] > '9' || s[i] < '0')
+ return 0;
+ }
+ return 1;
+}
+
+int
+parsemove(char *s)
+{
+ int d;
+
+ if(strcmp(s, "NE") == 0)
+ d = NE;
+ else if(strcmp(s, "E") == 0)
+ d = E;
+ else if(strcmp(s, "SE") == 0)
+ d = SE;
+ else if(strcmp(s, "W") == 0)
+ d = W;
+ else if(strcmp(s, "SW") == 0)
+ d = SW;
+ else if(strcmp(s, "NW") == 0)
+ d = NW;
+ else
+ sysfatal("parsemove(): invalid direction");
+
+ return d;
+}
+
+char*
+dirtostr(int dir)
+{
+ switch(dir)
+ {
+ case NE:
+ return "NE";
+ case E:
+ return "E";
+ case SE:
+ return "SE";
+ case SW:
+ return "SW";
+ case W:
+ return "W";
+ case NW:
+ return "NW";
+ default:
+ return nil;
+ }
+}
+
+/* xx yy\0
+static Point
+parseput(char *s)
+{
+ int x, y;
+ int len;
+
+ x = atoi(s);
+ s = strchr(s, ' ');
+ if(end == nil)
+ {
+ dprint("parseput(): end nil\n");
+ sysfatal("parseput(): incomplete line");
+ }
+ y = atoi(s);
+ return Pt(x, y);
+}
+*/
+
+Point
+parseput(char *x, char *y)
+{
+ if(isnum(x, 2) != 1 && isnum(y, 2) != 1)
+ sysfatal("parseput(): input isnt a number?");
+
+ return Pt(atoi(x), atoi(y));
+}
\ No newline at end of file
--- /dev/null
+++ b/util.h
@@ -1,0 +1,4 @@
+int isnum(char *s, unsigned int n);
+int parsemove(char *s);
+char* dirtostr(int dir);
+Point parseput(char *x, char *y);
\ No newline at end of file