-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.c
244 lines (172 loc) · 5.28 KB
/
stream.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*Domenic Bianchi
CIS 2750 Assignment 2
February 19, 2017
This program creates files neccessary to create a stream, add a user to a user file, or remove a user from a user file. Also, this program adds posts to the appropriate stream*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "stream.h"
#include "addauthor.h"
void updateStream(userPost * st) {
FILE * dataFile = NULL;
int byteCount = 0;
bool foundUser = false;
char fileName[256];
char lineString[256];
char name[256];
clearArray(fileName, 256);
clearArray(lineString, 256);
clearArray(name, 256);
strcpy(fileName, "./messages/");
strcat(fileName, st->streamname);
strcat(fileName, "StreamUsers.txt");
dataFile = fopen(fileName, "r");
if (dataFile == NULL) {
printf("Stream does not exist\n");
return;
}
else {
/*Search though all the usernames that have access to the stream and check if the user trying to post to the stream is allowed to*/
while (fgets(lineString, 256, dataFile) != NULL) {
int i = 0;
/*Starting from the end of the string, search for the first space. Everything before the space is the username*/
for (i = strlen(lineString)-1; i >= 0; i--) {
if (lineString[i] == ' ') {
strncpy(name, lineString, i);
name[i] = '\0';
break;
}
}
/*If the username from the file matches the user trying to post, then the user can post*/
if (strcmp(name, st->username) == 0) {
foundUser = true;
break;
}
}
fclose(dataFile);
}
if (foundUser == false) {
printf("User does not have permission to post in this stream\n");
return;
}
/*Add the post to the stream*/
strcpy(fileName, "./messages/");
strcat(fileName, st->streamname);
strcat(fileName, "Stream.txt");
dataFile = fopen(fileName, "a");
fprintf(dataFile, "Sender: %s\nDate: %s%s", st->username, st->date, st->text);
fclose(dataFile);
clearArray(fileName, 256);
/*Add the byte index to the stream data file*/
strcpy(fileName, "./messages/");
strcat(fileName, st->streamname);
strcat(fileName, "StreamData.txt");
dataFile = fopen(fileName, "r");
/*Calculate number of bytes for the post*/
while (fgets(lineString, 256, dataFile) != NULL) {
byteCount = atoi(lineString);
}
byteCount = byteCount + strlen(st->username) + strlen(st->date) + strlen(st->text) + 15;
fclose(dataFile);
dataFile = fopen(fileName, "a");
fprintf(dataFile, "%d\n", byteCount);
fclose(dataFile);
}
void addUser(char * username, char * list) {
FILE * dataFile = NULL;
char * streamToAdd;
char temp[256];
char name[256];
char lineString[256];
/*Tokenize all streams that the user is being added to*/
streamToAdd = strtok(list,",");
while (streamToAdd != NULL) {
bool addUserToStream = true;
clearArray(temp, 256);
clearArray(name, 256);
clearArray(lineString, 256);
strcpy(temp, "./messages/");
strcat(temp, streamToAdd);
strcat(temp, "StreamUsers.txt");
dataFile = fopen(temp, "r");
/*If the stream exists, check if the user already exists in that stream*/
while (dataFile != NULL && fgets(lineString, 256, dataFile) != NULL) {
int i = 0;
for (i = strlen(lineString)-1; i >= 0; i--) {
if (lineString[i] == ' ') {
strncpy(name, lineString, i);
name[i] = '\0';
break;
}
}
if (strcmp(name, username) == 0) {
addUserToStream = false;
printf("User already exists for the %s stream\n", streamToAdd);
}
}
if (dataFile != NULL) {
fclose(dataFile);
}
/*Add user to stream*/
if (addUserToStream == true) {
dataFile = fopen(temp, "a");
fprintf(dataFile, "%s 0\n", username);
fclose(dataFile);
}
strcpy(temp, "./messages/");
strcat(temp, streamToAdd);
strcat(temp, "Stream.txt");
dataFile = fopen(temp, "a");
fclose(dataFile);
strcpy(temp, "./messages/");
strcat(temp, streamToAdd);
strcat(temp, "StreamData.txt");
dataFile = fopen(temp, "a");
fclose(dataFile);
streamToAdd = strtok(NULL, " ,");
}
}
void removeUser(char * username, char * list) {
FILE * originalFile = NULL;
FILE * newFile = NULL;
char * streamToRemove;
char streamName[256];
char lineString[256];
char tempString[256];
clearArray(lineString, 256);
clearArray(tempString, 256);
streamToRemove = strtok(list,",");
/*Loop through each stream the user entered*/
while (streamToRemove != NULL) {
clearArray(streamName, 256);
strcpy(streamName, "./messages/");
strcat(streamName, streamToRemove);
strcat(streamName, "StreamUsers.txt");
originalFile = fopen(streamName, "r");
/*Remove the user from the user file by deleting the line that contains their username*/
if (originalFile != NULL) {
newFile = fopen("./messages/temp.txt", "w");
while (fgets(lineString, 256, originalFile) != NULL) {
int i = 0;
clearArray(tempString, 256);
/*Get username from line*/
for (i = strlen(lineString)-1; i >= 0; i--) {
if (lineString[i] == ' ') {
strncpy(tempString, lineString, i);
tempString[i] = '\0';
break;
}
}
/*If the username found on the line is not the username to be deleted, than save the line to a temp file*/
if (strcmp(username, tempString) != 0) {
fprintf(newFile, "%s", lineString);
}
}
fclose(originalFile);
fclose(newFile);
rename("./messages/temp.txt", streamName);
}
streamToRemove = strtok(NULL, " ,");
}
}