-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputreader.c
100 lines (97 loc) · 3.07 KB
/
inputreader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
////////////////////////////////////////////////////////////////////////////////
// Main File: main.c
// This File: inputreader.c
// Other Files: inputreader.h munch1.c munch1.h munch2.c munch2.h
// outputwriter.c outputwriter.h Queue.c Queue.h
// Semester: CS 537 Fall 2018
//
// Author: Youmin Han
// Email: [email protected]
// CS Login: youmin
//
// Author: Xianjie Zheng
// Email: [email protected]
// CS Login: xianjie
//
/////////////////////////// OTHER SOURCES OF HELP //////////////////////////////
// fully acknowledge and credit all sources of help,
// other than Instructors and TAs.
//
// Persons: NULL
//
// Online sources: NULL
//
//////////////////////////// 80 columns wide ///////////////////////////////////
#include "Queue.h"
#include "inputreader.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#define buffersize 1024
#define qsize 10
/*
* Read from standard input, one line at a time. Reader will take the each line
* of the input and pass it to thread Munch1 through a queue of character
* strings.
* @param: array
* struct that store three different queues
*/
void *inputreader(void *array) {
// get the reader queue from queuelist
Queue *qreader = ((queuelist *)array)->qreader;
int c = 0;
int index = 0;
// allocate memory to each string
char *str = calloc(buffersize,sizeof(char));
if(!str) {
fprintf(stderr, "Error allocating memory\n");
exit(1);
}
// traverse all characters from stdin until reach EOF
while((c = fgetc(stdin)) != EOF){
// case when string length is greater than buffersize
if(index >= buffersize) {
// read the string and flush it
if(c != '\n') {
continue;
}
// reset index
index = 0;
fprintf(stderr, "This string line is too long\n");
}
// normal case
else {
// when a string reaches end
if(c == '\n') {
// attach end of string character
str[index] = '\0';
// add the string to queue
EnqueueString(qreader, str);
// reset index
index = 0;
// allocate str with new memory
str = calloc(buffersize, sizeof(char));
if(!str) {
fprintf(stderr, "Error allocating memory\n");
exit(1);
}
}
else{
// fill sring with characters
str[index] = c;
index++;
}
}
}
// case when there are characters before EOF
if( index !=0 ) {
// attach end of string character
str[index] = '\0';
// add the string to queue
EnqueueString(qreader, str);
}
// add NULL to queue as a sign of there will be no more input
EnqueueString(qreader, NULL);
pthread_exit(NULL);
}