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
This commit is contained in:
Brian Barto 2016-04-18 18:39:56 -04:00
parent 4ccde16721
commit 103e164106
3 changed files with 20 additions and 56 deletions

View File

@ -1,13 +1,24 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "nms.h" #include "nms.h"
int main(void) { int main(void) {
char *input;
// Geting input // Geting input
int c; int c, inSize = 0;
while ((c = getchar()) != EOF) { 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; return 0;
} }

View File

@ -14,7 +14,6 @@
#include <ctype.h> #include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include <stdarg.h>
#define SPACE 32 #define SPACE 32
#define NEWLINE 10 #define NEWLINE 10
@ -34,55 +33,17 @@ struct winpos {
struct winpos *next; struct winpos *next;
}; };
// Globals
static char *display = NULL;
// Function prototypes (internal) // Function prototypes (internal)
char getMaskChar(void); char getMaskChar(void);
/* /*
* void nmsprintf(const char *format, ...) * void nmsexec(char *)
* *
* DESCR: * DESCR:
* Used to load characters in to the display queue that will later * Displays the characters stored in the / char * / parameter
* 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.
* *
*/ */
void nmsexec(void) { void nmsexec(char *src) {
struct winpos *list_pointer = NULL; struct winpos *list_pointer = NULL;
struct winpos *start; // Always points to start of list struct winpos *start; // Always points to start of list
struct winpos *temp; // Used for free()ing the list struct winpos *temp; // Used for free()ing the list
@ -112,7 +73,7 @@ void nmsexec(void) {
// Geting input // Geting input
n = 0; n = 0;
while ((c = display[n++]) != '\0') { while ((c = src[n++]) != '\0') {
if (c == NEWLINE) { if (c == NEWLINE) {
++y; ++y;
x = 0; 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'. // Initially display the characters in the terminal with a 'type effect'.
ms = 5; // miliseconds, used for usleep() ms = 5; // miliseconds, used for usleep()
list_pointer = start; list_pointer = start;
@ -266,8 +224,7 @@ void nmsexec(void) {
* void getMaskChar(void) * void getMaskChar(void)
* *
* DESCR: * DESCR:
* Returns a random character from the maskChars string. Used for generating * Returns a random character from the maskChars string.
* a corresponding 'mask' character for each character in the display queue.
* *
*/ */
char getMaskChar(void) { char getMaskChar(void) {

View File

@ -1,13 +1,9 @@
#ifndef NMS_H #ifndef NMS_H
#define NMS_H 1 #define NMS_H 1
// Function prototypes // Function prototypes
// Load characters in to the display queue
void nmsprintf(const char *, ...);
// Display the characters stored in the display queue // Display the characters stored in the display queue
void nmsexec(void); void nmsexec(char *);
#endif #endif