diff options
author | Raúl Benencia <rul@kalgan.cc> | 2015-03-31 15:13:34 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2015-03-31 15:13:34 -0300 |
commit | f7098bcf35b095ce98acaee9f969981e6ef9a43e (patch) | |
tree | 6d8bd3e817849b5762d3d7659294c0fb35584fa3 | |
parent | 85858c2f6dc624c6c32b9c59b187b378cfc89d9e (diff) |
input coordinates from stdin
-rw-r--r-- | include/input.h | 9 | ||||
-rw-r--r-- | lib/input.c | 38 |
2 files changed, 47 insertions, 0 deletions
diff --git a/include/input.h b/include/input.h new file mode 100644 index 0000000..7e1bb3e --- /dev/null +++ b/include/input.h @@ -0,0 +1,9 @@ +#ifndef _INPUT +#define _INPUT + +#include "types.h" + +Coord input_orig_coord(); +Coord input_dest_coord(); + +#endif diff --git a/lib/input.c b/lib/input.c new file mode 100644 index 0000000..63df8c5 --- /dev/null +++ b/lib/input.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <string.h> + +#include "coordinate.h" +#include "input.h" + +#define LENGTH 80 + +static Coord _input_coord() { + char line[LENGTH]; + int done = 0; + + while (!done) { + fgets(line, LENGTH, stdin); + + if (strnlen(line, LENGTH) == 3 && line[2] == '\n') + line[2] = 0; + + if (!coord_is_valid(line)) + printf("Invalid coordinate. Write something like \"e2\": "); + else + done = 1; + } + + return coord_init(line); +} + +Coord input_orig_coord() { + printf("Orig coordinate: "); + + return _input_coord(); +} + +Coord input_dest_coord() { + printf("Dest coordinate: "); + + return _input_coord(); +} |