]> git.sur5r.net Git - c128-kasse/blobdiff - src/general.c
kasse: use cgetn_input() to simplify logic
[c128-kasse] / src / general.c
index e1fb1736fc7084f04095b146a196438d8477205a..91318aa9faa51269439ab846bd7f3b1000aa614a 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
  * RGB2R-C128-Kassenprogramm
  * © 2007-2009 phil_fry, sECuRE, sur5r
  * See LICENSE for license information
 #include <conio.h>
 
 #include "general.h"
+#include "vdc_patch_charset.h"
+
+/*
+ * get_input_terminated_by() reads input (handling backspace correctly) until
+ * a terminator of |terminators| is encountered or |out| is full (outlen-1
+ * characters were read).
+ *
+ * get_input_terminated_by() returns the terminator it encountered.
+ *
+ */
+input_terminator_t get_input_terminated_by(input_terminator_mask_t terminators,
+                                           char *out, BYTE outlen) {
+  BYTE i = strlen(out);
+  BYTE c, x, y;
+  x = wherex() - i;
+  y = wherey();
+  while (1) {
+    c = cgetc();
+    if (((terminators & INPUT_TERMINATOR_RETURN) == INPUT_TERMINATOR_RETURN) &&
+        (c == PETSCII_CR)) {
+      return INPUT_TERMINATOR_RETURN;
+    } else if (((terminators & INPUT_TERMINATOR_SPACE) ==
+                INPUT_TERMINATOR_SPACE) &&
+               (c == PETSCII_SP)) {
+      return INPUT_TERMINATOR_SPACE;
+    } else if (c == PETSCII_DEL) {
+      /* If you are at the left-most position, do nothing */
+      if (i == 0)
+        continue;
+      out[--i] = '\0';
+      cputcxy(x + i, y, ' ');
+      gotoxy(x + i, y);
+      continue;
+    }
+    if (i == (outlen - 1)) {
+      continue;
+    }
+    cputc(c);
+    out[i++] = c;
+  }
+}
 
 /*
  * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird.
  * erneut aufruft
  *
  */
-char *get_input() {
-       BYTE i = 0;
-       static char output[32];
-       BYTE c;
-       memset(output, '\0', 32);
-       while (1) {
-               if (i == 31)
-                       break;
-               c = getchar();
-               if (c == 13)
-                       break;
-               else output[i++] = c;
-       }
-       return output;
+char *get_input(void) {
+  static char output[32];
+  memset(output, '\0', sizeof(output));
+  get_input_terminated_by(INPUT_TERMINATOR_RETURN, output, sizeof(output));
+  return output;
+}
+
+BYTE cgetn_input(char *s, BYTE len) {
+  memset(s, '\0', len);
+  get_input_terminated_by(INPUT_TERMINATOR_RETURN, s, len);
+  return strlen(s);
+}
+
+void cget_return() {
+  BYTE c;
+  while (1) {
+    c = cgetc();
+    if (c == PETSCII_CR) {
+      return;
+    }
+  }
 }
 
-char retry_or_quit() {
-       char *c;
-       do {
-               cprintf("\r\nr)etry or q)uit?\r\n");
-               c = get_input();
-       } while ((*c != 'r') && (*c != 'q'));
-       return *c;
+char retry_or_quit(void) {
+  char *c;
+  do {
+    cprintf("\r\nr)etry or q)uit?\r\n");
+    c = get_input();
+  } while ((*c != 'r') && (*c != 'q'));
+  return *c;
 }
 
-char *format_euro(char *s, int maxlen, int cent){
-       int tmp = cent;
-       int len = strlen(",EUR");
-       while ((tmp /= 10) > 0)
-               ++len;
-       if (len >= maxlen)
-               return NULL;
-       // workaround to produce a leading zero for cents.. %0.2d won't work 
-       sprintf(s, "%2d,%s%dEUR", cent / 100, ((cent%100)<10?"0":""), cent % 100);
-       return s;
+char *format_euro(char *s, int maxlen, int cent) {
+  if (snprintf(s, maxlen, "%3d,%02d" EURSYM, cent / 100, cent % 100) > maxlen)
+    return NULL;
+  return s;
 }
 
 void c128_perror(BYTE c, char *msg) {
-       cprintf("\r\nError (Code %d) while: %s\r\nOS Error = %d\r\n", c, msg, _oserror);
+  cprintf("\r\nError (Code %d) while: %s\r\nOS Error = %d\r\n", c, msg,
+          _oserror);
 }