Skip to content

Commit

Permalink
Add sorting of fractions based on index and organize folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DeLuks2006 committed Sep 14, 2024
1 parent 1ee9932 commit 50984f9
Show file tree
Hide file tree
Showing 16 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/client/Makefile → client/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc
FLAGS = -Wall -Wextra -Wshadow
SRC = main.c sock.c http.c utils.c fraction.c
SRC = src/main.c src/sock.c src/http.c src/utils.c src/fraction.c
OUT = client

all:
Expand Down
1 change: 1 addition & 0 deletions src/client/fraction.h → client/include/fraction.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ int fraction_parse(char *data, size_t size, fraction_t *fraction);
int check_magic(uint32_t data);
void print_fraction(fraction_t fraction);
void fraction_free(fraction_t *fraction);
int compare_fractions(const void* a, const void* b);
#endif
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/client/utils.h → client/include/utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef UTILS_H
#define UTILS_H

#include <cstdint>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
Expand Down
14 changes: 13 additions & 1 deletion src/client/fraction.c → client/src/fraction.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "fraction.h"
#include "../include/fraction.h"
#include <stdint.h>


Expand Down Expand Up @@ -108,3 +108,15 @@ void fraction_free(fraction_t *fraction) {
fraction->index = 0;
fraction->crc = 0;
}

// Function used by qsort() to compare the index of our fractions
int compare_fractions(const void* a, const void* b) {
const fraction_t* frac_a = (const fraction_t*)a;
const fraction_t* frac_b = (const fraction_t*)b;

if (frac_a->index < frac_b->index) {
return -1;
} else {
return 1;
}
}
2 changes: 1 addition & 1 deletion src/client/http.c → client/src/http.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "http.h"
#include "../include/http.h"

#define HTTP_BUFFER_SIZE 1024

Expand Down
14 changes: 9 additions & 5 deletions src/client/main.c → client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
#include <sys/types.h>
#include <unistd.h>

#include "fraction.h"
#include "http.h"
#include "sock.h"
#include "utils.h"
#include "../include/fraction.h"
#include "../include/http.h"
#include "../include/sock.h"
#include "../include/utils.h"

/* Networking constants */
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT "8000"

int main() {
int main(void) {
struct addrinfo hints, *ainfo;
int sfd; // socket file descriptor
char hostname[NI_MAXHOST];
Expand Down Expand Up @@ -66,6 +66,7 @@ int main() {
goto cleanup_socket;
}

// Storing the fractions in a array
fraction_t *fractions = malloc(lines_read * sizeof(fraction_t));
for (int i=0; i<lines_read; i++) {
if (download_fraction(sfd, fraction_links[i], &fractions[i]) != 0) {
Expand All @@ -88,6 +89,9 @@ int main() {
http_free(&http_fraction_res);
http_free(&http_post_res);

// Sort the fractions based on index
qsort(fractions, lines_read, sizeof(fraction_t), compare_fractions);

// Free fractions and links
for (int i = 0; i < lines_read; i++) {

Expand Down
2 changes: 1 addition & 1 deletion src/client/sock.c → client/src/sock.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "sock.h"
#include "../include/sock.h"
/* Wrapper for getaddrinfo, handles error */
int h_getaddrinfo(const char *ip, const char *port, struct addrinfo *hints,
struct addrinfo **ainfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/utils.c → client/src/utils.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "utils.h"
#include "../include/utils.h"

// if this dont work Skelly is to blame ;)
int split_fraction_links(char *data, char *data_arr[], int maxlines) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 50984f9

Please sign in to comment.