From 103e164106ca426acedbeaffc182fd5a18133325 Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Mon, 18 Apr 2016 18:39:56 -0400 Subject: [PATCH] Decided I didn't need the nmsprintf() function. Instead teh main program should build the character array however it pleases and pass it in to nmsexec(). modified: src/main.c modified: src/nms.c modified: src/nms.h --- src/main.c | 17 ++++++++++++++--- src/nms.c | 53 +++++------------------------------------------------ src/nms.h | 6 +----- 3 files changed, 20 insertions(+), 56 deletions(-) diff --git a/src/main.c b/src/main.c index d21064e..d8bd498 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,24 @@ #include +#include #include "nms.h" int main(void) { + char *input; + // Geting input - int c; + int c, inSize = 0; while ((c = getchar()) != EOF) { - nmsprintf("%c", c); + ++inSize; + input = realloc(input, inSize + 1); + input[inSize - 1] = c; + input[inSize] = '\0'; } - nmsexec(); + + // Display characters + nmsexec(input); + + // Don't forget to free the allocated memory! + free(input); return 0; } diff --git a/src/nms.c b/src/nms.c index b605c49..415d7ef 100644 --- a/src/nms.c +++ b/src/nms.c @@ -14,7 +14,6 @@ #include #include #include -#include #define SPACE 32 #define NEWLINE 10 @@ -34,55 +33,17 @@ struct winpos { struct winpos *next; }; -// Globals -static char *display = NULL; - // Function prototypes (internal) char getMaskChar(void); /* - * void nmsprintf(const char *format, ...) + * void nmsexec(char *) * * DESCR: - * Used to load characters in to the display queue that will later - * be rendered on tothe screen via nmsexec(). It is a wrapper for - * sprintf(), and the parameters that it accepts are the same. - * - * PARAMS: - * const char *format - printf-style format string - */ -void nmsprintf(const char *format, ...) { - char *nmsprintBuffer = malloc(PRINT_BUFFER); - int fmtSize; - - va_list argp; - va_start(argp, format); - if ((fmtSize = vsnprintf(nmsprintBuffer, PRINT_BUFFER, format, argp)) >= PRINT_BUFFER) { - nmsprintBuffer = realloc(nmsprintBuffer, fmtSize + 1); - vsnprintf(nmsprintBuffer, fmtSize + 1, format, argp); - } - va_end(argp); - - if (display == NULL) { - display = malloc(strlen(nmsprintBuffer) + 1); - strcpy(display, nmsprintBuffer); - } else { - display = realloc(display, strlen(display) + strlen(nmsprintBuffer) + 1); - strcat(display, nmsprintBuffer); - } - - free(nmsprintBuffer); -} - - -/* - * void nmsexec(void) - * - * DESCR: - * Displays the characters stored in the display queue. + * Displays the characters stored in the / char * / parameter * */ -void nmsexec(void) { +void nmsexec(char *src) { struct winpos *list_pointer = NULL; struct winpos *start; // Always points to start of list struct winpos *temp; // Used for free()ing the list @@ -112,7 +73,7 @@ void nmsexec(void) { // Geting input n = 0; - while ((c = display[n++]) != '\0') { + while ((c = src[n++]) != '\0') { if (c == NEWLINE) { ++y; x = 0; @@ -155,9 +116,6 @@ void nmsexec(void) { } } - // Freeing the display character array - free(display); - // Initially display the characters in the terminal with a 'type effect'. ms = 5; // miliseconds, used for usleep() list_pointer = start; @@ -266,8 +224,7 @@ void nmsexec(void) { * void getMaskChar(void) * * DESCR: - * Returns a random character from the maskChars string. Used for generating - * a corresponding 'mask' character for each character in the display queue. + * Returns a random character from the maskChars string. * */ char getMaskChar(void) { diff --git a/src/nms.h b/src/nms.h index bff7aa6..ead1988 100644 --- a/src/nms.h +++ b/src/nms.h @@ -1,13 +1,9 @@ #ifndef NMS_H #define NMS_H 1 - // Function prototypes -// Load characters in to the display queue -void nmsprintf(const char *, ...); - // Display the characters stored in the display queue -void nmsexec(void); +void nmsexec(char *); #endif