Moved file manipulation functions to helper files
This commit is contained in:
69
main.c
69
main.c
@@ -1,6 +1,3 @@
|
|||||||
#include "message.h"
|
|
||||||
#include "user.h"
|
|
||||||
#include "easysock.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -8,6 +5,12 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "message.h"
|
||||||
|
#include "user.h"
|
||||||
|
#include "easysock.h"
|
||||||
|
#include "file_helpers.h"
|
||||||
|
#include "message_helpers.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 10000
|
#define BUFFER_SIZE 10000
|
||||||
#define MAX_CONNECTIONS 100
|
#define MAX_CONNECTIONS 100
|
||||||
|
|
||||||
@@ -21,6 +24,7 @@ int main() {
|
|||||||
|
|
||||||
fd_set read_fd_set;
|
fd_set read_fd_set;
|
||||||
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
||||||
|
char* dest_address[MAX_CONNECTIONS] = {NULL};
|
||||||
FD_ZERO(&read_fd_set);
|
FD_ZERO(&read_fd_set);
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
User* users = create_user_list("user_file.txt");
|
User* users = create_user_list("user_file.txt");
|
||||||
@@ -58,10 +62,17 @@ int main() {
|
|||||||
if (num_bytes_read <= 0) {
|
if (num_bytes_read <= 0) {
|
||||||
close(conn_sockets[i]);
|
close(conn_sockets[i]);
|
||||||
conn_sockets[i] = 0;
|
conn_sockets[i] = 0;
|
||||||
|
dest_address[i] = NULL;
|
||||||
} else {
|
} else {
|
||||||
for (int i=0;i<num_bytes_read;i++) {
|
if (dest_address[i] == NULL) {
|
||||||
printf("%c",buffer[i]);
|
dest_address[i] = fetch_address(buffer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// for (int i=0;i<num_bytes_read;i++) {
|
||||||
|
// printf("%c",buffer[i]);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -106,51 +117,3 @@ User* create_user_list(char* filename) {
|
|||||||
return users;
|
return users;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
char* file_to_string(char* filename) {
|
|
||||||
FILE* fp = fopen(filename,"r");
|
|
||||||
|
|
||||||
fseek(fp,0,SEEK_END);
|
|
||||||
long file_size = ftell(fp);
|
|
||||||
rewind(fp);
|
|
||||||
|
|
||||||
char* buffer = calloc(sizeof(char),file_size+1);
|
|
||||||
|
|
||||||
fread(buffer,1,file_size,fp);
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
return buffer;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Finds the number of lines in a file (Which MUST end in a new-line, if the last
|
|
||||||
line is to be counted) */
|
|
||||||
|
|
||||||
int num_of_lines(char* filename) {
|
|
||||||
|
|
||||||
int num_lines = 0;
|
|
||||||
FILE* fp = fopen(filename,"r");
|
|
||||||
|
|
||||||
if (fp == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int c = getc(fp); c != EOF; c = getc(fp)) {
|
|
||||||
if (c == '\n') {
|
|
||||||
num_lines++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fclose(fp);
|
|
||||||
|
|
||||||
return num_lines;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user