]> git.sur5r.net Git - c128-kasse/commitdiff
remove malloc, fix user interface
authorsECuRE <sECuRE@af93e077-1a23-4f1e-9cbe-9382a9d578f5>
Sun, 21 Oct 2007 15:14:49 +0000 (15:14 +0000)
committersECuRE <sECuRE@af93e077-1a23-4f1e-9cbe-9382a9d578f5>
Sun, 21 Oct 2007 15:14:49 +0000 (15:14 +0000)
git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@48 af93e077-1a23-4f1e-9cbe-9382a9d578f5

Makefile
credit_manager.c
general.c
kasse.c
time.c

index 18ad9b82494b7cbdc4706e5ce07ab2c6b4b64e01..3bc90446f6b7cd301741a3349c03887f164215db 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 # Note: this are my paths to cc65 as there is no gentoo ebuild, sorry for that.
 # Please use the path below if you've installed cc65 system-wide
-CC=~/customSoftware/cc65-2.11.0/src/cc65/cc65 -I ~/customSoftware/cc65-2.11.0/include
+CC=~/customSoftware/cc65-2.11.0/src/cc65/cc65 -I ~/customSoftware/cc65-2.11.0/include -O
 CA=~/customSoftware/cc65-2.11.0/src/ca65/ca65
 CL=~/customSoftware/cc65-2.11.0/src/cl65/cl65
 # CC=cc65
@@ -8,12 +8,12 @@ CL=~/customSoftware/cc65-2.11.0/src/cl65/cl65
 # CL=cl65
 
 %.o: %.c
-       cp /tmp/cc65/lib/c128* .
        ${CC} -t c128 $<
        ${CA} -t c128 $$(basename $< .c).s
 
 all: config.o kasse.o general.o credit_manager.o time.o
        # See above, please just kill the PATH-definition
+       cp /tmp/cc65/lib/c128* .
        PATH=${PATH}:~/customSoftware/cc65-2.11.0/src/ld65:/tmp/cc65/lib ${CL} -t c128 *.o -o kasse
 
 package: all
index 154cccf2b71c3a6baca58b98ddfce683e0988848..a91adda537c0dfac942ea5e400f65c56208df6da 100644 (file)
@@ -6,10 +6,10 @@
 #include "config.h"
 #include "general.h"
 
-char *filter = NULL;
-BYTE filter_len;
+static char *filter = NULL;
+static BYTE filter_len;
 
-void print_credit_screen() {
+static void print_credit_screen() {
        BYTE i = 0;
        clrscr();
        cprintf("C128-Kassenprogramm: Credit Manager\r\n\r\n");
@@ -22,49 +22,40 @@ void print_credit_screen() {
 /* Guthabenverwalter */
 void credit_manager() {
        int negative = 1;
-       BYTE c;
-       char *nickname, *credits_input;
+       char *c, *nickname, *credits_input;
        /* credits_int is a stupid name, but overlaps with struct credits_t credits; else */
        int credits_int;
        while (1) {
                print_credit_screen();
-               c = getchar();
-               if (c == 'n') {
-                       cprintf("Nickname eingeben:\r\n");
-                       nickname = get_input();
-                       if (nickname[0] == '\0') {
-                               free(nickname);
-                               continue;
-                       }
+               c = get_input();
+               if (c == NULL || *c == '\0')
+                       continue;
+               if (*c == 'n') {
                        cprintf("\r\nGuthaben eingeben:\r\n");
                        credits_input = get_input();
-                       if (credits_input[0] == '\0') {
-                               free(credits_input);
+
+                       if (credits_input == NULL || credits_input[0] == '\0')
                                continue;
-                       }
-                       if (credits_input[0] == '-') {
+                       else if (credits_input[0] == '-') {
                                negative = -1;
                                ++credits_input;
                        }
                        credits_int = atoi(credits_input) * negative;
                        if (credits_int > 0) {
-                               strcpy(credits.credits[credits.num_items].nickname, nickname);
+                               cprintf("Nickname eingeben:\r\n");
+                               nickname = get_input();
+                               if (nickname == NULL || nickname[0] == '\0')
+                                       continue;
+                               strncpy(credits.credits[credits.num_items].nickname, nickname, 9);
                                credits.credits[credits.num_items].credit = credits_int;
                                ++credits.num_items;
                        }
-                       if (negative == -1)
-                               --credits_input;
-                       free(credits_input);
-               } else if (c == 'f') {
+               } else if (*c == 'f') {
                        cprintf("Filter eingeben:\r\n");
-                       if (filter != NULL)
-                               free(filter);
                        filter = get_input();
-                       if (filter[0] == '0') {
-                               free(filter);
+                       if (filter == NULL || *filter == 32 || (filter_len = strlen(filter)) == 0)
                                filter = NULL;
-                       } else filter_len = strlen(filter);
-               } else if (c == 'z' || c == 'q')
+               } else if (*c == 'z' || *c == 'q')
                        break;
        }
 }
index bb42e01310f18ba3d19f4641449896a83d0814e4..904764e2acfcda1bb9af315330be3aeeae39e644 100644 (file)
--- a/general.c
+++ b/general.c
@@ -6,18 +6,16 @@
 #include "general.h"
 
 /*
- * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird. Der zurückgegebene 
- * char-pointer muss anschließend ge-free()d werden!
+ * Liest (maximal 31) Zeichen ein, bis Enter gedrückt wird.
+ * Vorsicht: Es wird ein statischer Buffer benutzt, sodass man
+ * das Ergebnis via strdup() retten muss, bevor man get_input()
+ * erneut aufruft
  *
  */
 char *get_input() {
        BYTE i = 0;
-       char *output = malloc(32 * sizeof(char));
+       static char output[32];
        BYTE c;
-       if (output == NULL) {
-               perror("malloc()");
-               exit(1);
-       }
        memset(output, '\0', 32);
        while (1) {
                if (i == 31)
@@ -30,14 +28,14 @@ char *get_input() {
        return output;
 }
 
-char * format_euro(char * s, int maxlen, int cent){
+char *format_euro(char *s, int maxlen, int cent){
        int tmp = cent;
-       int len = 5; // 1 char at least + 4 (== strlen(",EUR"))
-       while ((tmp/=10) > 0)
-               len++;
-       if (len>maxlen)
+       int len = strlen(",EUR");
+       while ((tmp /= 10) > 0)
+               ++len;
+       if (len >= maxlen)
                return NULL;
-       sprintf(s, "%d,%dEUR", cent/100, cent%100);
+       sprintf(s, "%d,%dEUR", cent / 100, cent % 100);
        return s;
 }
 
diff --git a/kasse.c b/kasse.c
index 2979fd6940b6063c03036ef4e13efb11a9968028..1fbcefaa2249703ac606645eee0f0a5715e94a98 100644 (file)
--- a/kasse.c
+++ b/kasse.c
@@ -20,7 +20,6 @@ void print_screen() {
        char *time = get_time();
        clrscr();
        cprintf("C128-Kassenprogramm\r\n\r\nUhrzeit: %s (wird nicht aktualisiert)\r\nEingenommen: %ld Cents, Verkauft: %ld Flaschen, Drucken: %s\r\n\r\n", time, money, items_sold, (printing == 1 ? "ein" : "aus"));
-       free(time);
        for (; i < status.num_items; ++i)
                cprintf("Eintrag %x: %s (%d Cents, %d mal verkauft)\r\n",
                        i, status.status[i].item_name, status.status[i].price, status.status[i].times_sold);
@@ -73,62 +72,54 @@ void buy(BYTE n) {
        BYTE c, nickname_len, single_match;
        int einheiten;
        char *nickname;
-       if (status.status[n].item_name == NULL)
+
+       if (status.status[n].item_name == NULL) {
                cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
-       else {
-               cprintf("Wieviel Einheiten \"%s\"?\r\n", status.status[n].item_name);
-               while (1) {
-                       c = getchar();
-                       if (c == 13)
-                               break;
-                       else if (c == '-'&& i == 0)
-                               negative = -1;
-                       else if (c > 47 && c < 58)
-                               entered[i++] = c;
-               }
-               einheiten = atoi(entered) * negative;
-               cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
-               nickname = get_input();
-               if (nickname[0] == '\0') {
-                       free(nickname);
-                       nickname = NULL;
-               } else {
-                       nickname_len = strlen(nickname);
-                       /* go through credits and remove the amount of money or set nickname
-                        * to NULL if no such credit could be found */
-                       for (c = 0; c < credits.num_items; ++c)
-                               if (strncmp(nickname, credits.credits[c].nickname, nickname_len) == 0) {
-                                       if (++matches == 2)
-                                               break;
-                                       else single_match = c;
-                               }
-                       if (matches == 1) {
+               return;
+       }
+       cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", status.status[n].item_name);
+       while (1) {
+               c = getchar();
+               if (c == 13)
+                       break;
+               else if (c == 27) {
+                       cprintf("Kauf abgebrochen, druecke ANYKEY...\r\n");
+                       getchar();
+                       return;
+               } else if (c == '-' && i == 0)
+                       negative = -1;
+               else if (c > 47 && c < 58)
+                       entered[i++] = c;
+       }
+       einheiten = atoi(entered) * negative;
+       cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
+       nickname = get_input();
+       if (nickname != NULL && nickname[0] != '\0') {
+               nickname_len = strlen(nickname);
+               /* go through credits and remove the amount of money or set nickname
+                * to NULL if no such credit could be found */
+               for (c = 0; c < credits.num_items; ++c)
+                       if (strncmp(nickname, credits.credits[c].nickname, nickname_len) == 0) {
                                if (credits.credits[single_match].credit < (status.status[n].price * einheiten)) {
                                        cprintf("Sorry, %s hat nicht genug Geld :-(\r\n", nickname);
-                                       free(nickname);
                                        return;
-                               } else {
-                                       /* Geld abziehen */
-                                       credits.credits[single_match].credit -= (status.status[n].price * einheiten);
-                                       cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke ANYKEY...\r\n",
-                                               nickname, credits.credits[single_match].credit);
-                                       getchar();
                                }
-                       } else if (matches == 0) {
-                               // TODO
-                       } else {
-                               free(nickname);
-                               nickname = NULL;
+                               /* Geld abziehen */
+                               credits.credits[single_match].credit -= (status.status[n].price * einheiten);
+                               cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke ANYKEY...\r\n",
+                                       nickname, credits.credits[single_match].credit);
+                               getchar();
+                               break;
                        }
-               }
-               status.status[n].times_sold += einheiten;
-               money += status.status[n].price * einheiten;
-               items_sold += einheiten;
-               if (printing == 1)
-                       print_log(n, einheiten, nickname);
-               if (nickname != NULL)
-                       free(nickname);
+       } else {
+               /* Ensure that nickname is NULL if it's empty because it's used in print_log */
+               nickname = NULL;
        }
+       status.status[n].times_sold += einheiten;
+       money += status.status[n].price * einheiten;
+       items_sold += einheiten;
+       if (printing == 1)
+               print_log(n, einheiten, nickname);
 }
 
 void set_time_interactive() {
@@ -150,15 +141,17 @@ void set_time_interactive() {
 
        time = get_time();
        cprintf("Zeit gesetzt: %s\r\n", time);
-       free(time);
 }
 
 int main() {
-       BYTE c;
+       char *c;
        toggle_videomode();
        /* Zeit erstmalig setzen */
        set_time_interactive();
        POKE(216, 255);
+       /* Variablen zurechtbiegen */
+       credits.num_items = 0;
+       status.num_items = 0;
        /* Konfigurationsdatei laden */
        load_config();
        /* Einträge (=Getränke) und Zustand laden */
@@ -169,33 +162,34 @@ int main() {
                /* Bildschirm anzeigen */
                print_screen();
                /* Tastatureingaben abfragen */
-               c = getchar();
+               c = get_input();
                /* und eventuell weitere Dialoge anzeigen */
-               if (c > 47 && c < 58)
-                       buy(c - 48);
-               else if (c == 's') {
+               if (*c > 47 && *c < 58)
+                       buy((*c) - 48);
+               else if (*c == 's') {
                        /* Zustandsdatei schreiben */
                        save_items();
                        save_credits();
-                       cprintf("Statefile/Creditfile gesichert, druecke ANYKEY...\r\n");
-                       getchar();
-               } else if (c == 'd') {
+                       cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n");
+                       get_input();
+               } else if (*c == 'd') {
                        /* Drucken an- oder ausschalten */
                        printing = (printing == 1 ? 0 : 1);
-                       cprintf("Drucken ist nun %s, druecke ANYKEY...\r\n", (printing == 1 ? "eingeschaltet" : "ausgeschaltet"));
-                       getchar();
-               } else if (c == 'g') {
+                       cprintf("Drucken ist nun %s, druecke RETURN...\r\n", 
+                               (printing == 1 ? "eingeschaltet" : "ausgeschaltet"));
+                       get_input();
+               } else if (*c == 'g') {
                        /* Guthabenverwalter aufrufen */
                        credit_manager();
-               } else if (c == 'z') {
+               } else if (*c == 'z') {
                        /* Zeit setzen */
                        set_time_interactive();
-               } else if (c == 'n') {
+               } else if (*c == 'n') {
                        strcpy(status.status[status.num_items].item_name, "mate");
                        status.status[status.num_items].price = 23;
                        status.status[status.num_items].times_sold = 5;
                        status.num_items++;
-               } else if (c == 'q')
+               } else if (*c == 'q')
                        break;
        }
        cprintf("BYEBYE\r\n");
diff --git a/time.c b/time.c
index 4552fe02f171117e9bdbde37c158000cbc0d7047..7b9f4cb5279b5ac12467be242a954ef03ef36996 100644 (file)
--- a/time.c
+++ b/time.c
@@ -7,12 +7,8 @@ char *get_time() {
        long int h = PEEK(0x00A0) * 65536,
                m = PEEK(0x00A1) * 256,
                s = PEEK(0x00A2);
-       char *buffer = malloc(9 * sizeof(char));
+       static char buffer[9];
        BYTE hrs, min;
-       if (buffer == NULL) {
-               perror("malloc()");
-               exit(1);
-       }
        h = (h + m + s) / 60;
        hrs = (h / 3600);
        h -= (hrs * 3600);