fixed some infinite loop bug

This commit is contained in:
Lexi Quinn 2023-07-20 20:10:23 +10:00
parent d3f7a25a64
commit c7387f5acb
2 changed files with 18 additions and 6 deletions

View File

@ -72,6 +72,8 @@ int main(int argc, char *argv[]) {
commandcode = 11; commandcode = 11;
} else if (!strcmp(argv[1], "background")) { } else if (!strcmp(argv[1], "background")) {
commandcode = 12; commandcode = 12;
} else if (!strcmp(argv[1], "save")) {
commandcode = 13;
} else { } else {
perror("No valid command given"); perror("No valid command given");
exit(1); exit(1);

View File

@ -16,6 +16,7 @@ int pausedTime = 0;
bool timerActive = false; bool timerActive = false;
bool paused = false; bool paused = false;
bool alive = true; bool alive = true;
bool runUnsaved = false;
int timerOffset = 0; int timerOffset = 0;
enum event_type { enum event_type {
START, START,
@ -127,8 +128,7 @@ void start()
//TODO: Save the old run to the file before the new one starts, //TODO: Save the old run to the file before the new one starts,
//the reason to do this here is it gives the runner a chance to undo //the reason to do this here is it gives the runner a chance to undo
//if they accidentally hit the stop button //if they accidentally hit the stop button
if (run[runMarker - 1].type == STOP) appendRunToFile();
appendRunToFile();
//TODO: Clear the run data first //TODO: Clear the run data first
timerActive = true; timerActive = true;
add_event(START); add_event(START);
@ -141,6 +141,7 @@ void stop()
//this makes sure the time clients recieve from time //this makes sure the time clients recieve from time
//requests match the time on the stop event //requests match the time on the stop event
finish = run[runMarker - 1].time; finish = run[runMarker - 1].time;
runUnsaved = true;
} }
void split() void split()
@ -233,6 +234,8 @@ void resume()
void appendRunToFile() void appendRunToFile()
{ {
if (!runUnsaved)
return;
char* save_path = NULL; char* save_path = NULL;
if (files <= 0) if (files <= 0)
save_path = default_file_name; save_path = default_file_name;
@ -291,6 +294,7 @@ void appendRunToFile()
fprintf(fp, "\n"); fprintf(fp, "\n");
fclose(fp); fclose(fp);
runUnsaved = false;
} }
void timespecToRFC3339(struct timespec t, char buf[]) void timespecToRFC3339(struct timespec t, char buf[])
@ -310,15 +314,17 @@ void loadFiles()
char buff2[255]; char buff2[255];
for (int i = 0; i < files; i++) { for (int i = 0; i < files; i++) {
printf("loading file: \"%s\"\n", filePaths[i]);
fp = fopen(filePaths[i], "r"); fp = fopen(filePaths[i], "r");
while(1) { while(1) {
char *x = fgets(buff, 255, fp); if (!fgets(buff, 255, fp))
break;
if (buff[0] == '/' && buff[1] == '/' || buff[0] == '\n') if (buff[0] == '/' && buff[1] == '/' || buff[0] == '\n')
continue; continue;
if (!strcmp(buff, "Segment\n") || !strcmp(buff, "Route\n") || x == NULL) if (!strcmp(buff, "Segment\n") || !strcmp(buff, "Route\n"))
break;
if (!fgets(buff2, 255, fp))
break; break;
fgets(buff2, 255, fp);
if (buff2[0] == '\t') { if (buff2[0] == '\t') {
valuecount++; valuecount++;
@ -438,6 +444,9 @@ void doprocessing (int sock)
} else if (commandcode == 12) { } else if (commandcode == 12) {
printf("Recieved request for background color\n"); printf("Recieved request for background color\n");
sendValue(sock, "Background-Color"); sendValue(sock, "Background-Color");
} else if (commandcode == 13) {
printf("Recieved save command\n");
appendRunToFile();
} else { } else {
printf("Recieved invalid command code, ignoring...\n"); printf("Recieved invalid command code, ignoring...\n");
} }
@ -479,6 +488,7 @@ int main(int argc, char *argv[])
listen(sockfd,5); listen(sockfd,5);
clilen = sizeof(cli_addr); clilen = sizeof(cli_addr);
printf("Ready!\n");
while (alive) { while (alive) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) { if (newsockfd < 0) {