diff options
| author | Julian Weigt <juw@posteo.de> | 2026-01-15 22:11:12 +0100 |
|---|---|---|
| committer | Julian Weigt <juw@posteo.de> | 2026-02-04 15:55:52 +0100 |
| commit | 10802b807e001bce40c2d668a7ff7caa072340f7 (patch) | |
| tree | e4b3c62a908f6db0a0de97e85ac5947fb3f82fd1 /charf.c | |
| parent | d78cc5836306c3231bb958bffe88ce54c898b512 (diff) | |
Introduce reading from stdin.
Diffstat (limited to 'charf.c')
| -rw-r--r-- | charf.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -4,6 +4,8 @@ //for multithreading #include <pthread.h> #include <stdbool.h> +//for reading from stdin +#include <string.h> #define DOUBLEMODE 0 #define DOUBLEERRORMODE 1 @@ -258,6 +260,34 @@ void* compute_chunk(void* arguments){ } } +#define OK 0 +#define NO_INPUT 1 +#define TOO_LONG 2 +static int getLine (char *prmpt, char *buff, size_t sz) { + int ch, extra; + + // Get line with buffer overrun protection. + if (prmpt != NULL) { + printf ("%s", prmpt); + fflush (stdout); + } + if (fgets (buff, sz, stdin) == NULL) + return NO_INPUT; + + // If it was too long, there'll be no newline. In that case, we flush + // to end of line so that excess doesn't affect the next call. + if (buff[strlen(buff)-1] != '\n') { + extra = 0; + while (((ch = getchar()) != '\n') && (ch != EOF)) + extra = 1; + return (extra == 1) ? TOO_LONG : OK; + } + + // Otherwise remove newline and give string back to caller. + buff[strlen(buff)-1] = '\0'; + return OK; +} + int main() { pthread_t threads[NUM_THREADS]; Args args[NUM_THREADS]; @@ -276,6 +306,15 @@ int main() { result_code = pthread_create(&threads[i], NULL, compute_chunk, args+i); } + size_t sz = 1024; + char prmpt[] = "To exit enter q,quit or exit.\nTo print current records to terminal enter r.\nTo print current records to a file enter p.\nTo do so with tex formatting enter t.\n"; + char buff[sz]; + bool cont = true; + do { + getLine(prmpt, buff, sz); + if( 0 == strcmp(buff,"q") || 0 == strcmp(buff,"quit") || 0 == strcmp(buff,"exit") ) exit(EXIT_SUCCESS); + } while(cont); + // wait for each thread to complete for (int i = 0; i < NUM_THREADS; i++) { result_code = pthread_join(threads[i], NULL); |
