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 <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -8,6 +5,12 @@
|
||||
#include <assert.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 MAX_CONNECTIONS 100
|
||||
|
||||
@@ -21,6 +24,7 @@ int main() {
|
||||
|
||||
fd_set read_fd_set;
|
||||
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
||||
char* dest_address[MAX_CONNECTIONS] = {NULL};
|
||||
FD_ZERO(&read_fd_set);
|
||||
char buffer[BUFFER_SIZE];
|
||||
User* users = create_user_list("user_file.txt");
|
||||
@@ -58,10 +62,17 @@ int main() {
|
||||
if (num_bytes_read <= 0) {
|
||||
close(conn_sockets[i]);
|
||||
conn_sockets[i] = 0;
|
||||
dest_address[i] = NULL;
|
||||
} else {
|
||||
for (int i=0;i<num_bytes_read;i++) {
|
||||
printf("%c",buffer[i]);
|
||||
if (dest_address[i] == NULL) {
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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