fragmentation + refactor page in struct
This commit is contained in:
parent
10f68191ec
commit
4c73f89184
|
@ -3,3 +3,4 @@ out
|
||||||
test.sh
|
test.sh
|
||||||
test.c
|
test.c
|
||||||
a.out
|
a.out
|
||||||
|
*.o
|
||||||
|
|
9
Makefile
9
Makefile
|
@ -1,17 +1,18 @@
|
||||||
SRC=main.c
|
|
||||||
BIN=webpage
|
BIN=webpage
|
||||||
|
SRCS=main.c config.c page.c
|
||||||
|
INC=.
|
||||||
LIB=curl
|
LIB=curl
|
||||||
FLAGS=-Wall -Wextra -Og -g
|
FLAGS=-Wall -Wextra -Og -g
|
||||||
|
|
||||||
all: $(BIN)
|
all: $(BIN)
|
||||||
|
|
||||||
$(BIN) : $(SRC)
|
$(BIN) : $(SRCS)
|
||||||
gcc $(FLAGS) -o $(BIN) $(SRC) -l$(LIB)
|
gcc $(FLAGS) -o $(BIN) $(SRCS) -I$(INC) -l$(LIB)
|
||||||
|
|
||||||
run : $(BIN)
|
run : $(BIN)
|
||||||
./$(BIN) $(input)
|
./$(BIN) $(input)
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm $(BIN)
|
rm -f $(BIN) *.o
|
||||||
|
|
||||||
.PHONY : run clean
|
.PHONY : run clean
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
UserConfig config = {
|
||||||
|
.port = 443,
|
||||||
|
.addr = "https://github.com/CaptainBoulbi/webpage",
|
||||||
|
};
|
||||||
|
|
||||||
|
void getUserConfig(int argc, char* argv[]){
|
||||||
|
for (int i=1; i<argc; i++){
|
||||||
|
if (strcmp(argv[i], "-p") == 0){
|
||||||
|
if (argc <= (i+1)){
|
||||||
|
perror("ERROR: Port not defined by user");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
config.port = atoi(argv[++i]);
|
||||||
|
} else {
|
||||||
|
config.addr = argv[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef CONFIG_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct UserConfig {
|
||||||
|
int port;
|
||||||
|
char* addr;
|
||||||
|
} UserConfig;
|
||||||
|
|
||||||
|
extern UserConfig config;
|
||||||
|
|
||||||
|
void getUserConfig(int argc, char* argv[]);
|
||||||
|
|
||||||
|
#endif // CONFIG_H
|
91
main.c
91
main.c
|
@ -1,92 +1,5 @@
|
||||||
#include <stddef.h>
|
#include "config.h"
|
||||||
#include <stdio.h>
|
#include "page.h"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <curl/curl.h>
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
#define DEFAULT_PAGE_LEN 16
|
|
||||||
|
|
||||||
int pageChunkLen = DEFAULT_PAGE_LEN;
|
|
||||||
int nbchunck = 0;
|
|
||||||
char** pageChunk;
|
|
||||||
|
|
||||||
typedef struct UserConfig {
|
|
||||||
int port;
|
|
||||||
char* addr;
|
|
||||||
} UserConfig;
|
|
||||||
|
|
||||||
UserConfig config = {
|
|
||||||
.port = 443,
|
|
||||||
.addr = "https://github.com/CaptainBoulbi/webpage",
|
|
||||||
};
|
|
||||||
|
|
||||||
void getUserConfig(int argc, char* argv[]){
|
|
||||||
for (int i=1; i<argc; i++){
|
|
||||||
if (strcmp(argv[i], "-p") == 0){
|
|
||||||
if (argc <= (i+1)){
|
|
||||||
perror("ERROR: Port not defined by user");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
config.port = atoi(argv[++i]);
|
|
||||||
} else {
|
|
||||||
config.addr = argv[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t save_chunk(char* buffer, size_t itemsize, size_t nitems, void* ignore){
|
|
||||||
size_t bytes = itemsize * nitems;
|
|
||||||
|
|
||||||
if (nbchunck >= pageChunkLen){
|
|
||||||
pageChunkLen *= 1.5;
|
|
||||||
pageChunk = realloc(pageChunk, sizeof(*pageChunk) * pageChunkLen);
|
|
||||||
|
|
||||||
if (pageChunk == NULL){
|
|
||||||
perror("ERROR: Buy more ram.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&pageChunk[nbchunck], 0, pageChunkLen - nbchunck);
|
|
||||||
}
|
|
||||||
|
|
||||||
pageChunk[nbchunck] = malloc(bytes+1);
|
|
||||||
pageChunk[nbchunck][bytes] = '\0';
|
|
||||||
strncpy(pageChunk[nbchunck], buffer, bytes);
|
|
||||||
nbchunck++;
|
|
||||||
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
|
|
||||||
void getPage(void){
|
|
||||||
pageChunk = malloc(sizeof(*pageChunk) * DEFAULT_PAGE_LEN);
|
|
||||||
if (pageChunk == NULL){
|
|
||||||
perror("ERROR: Buy more ram.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
CURL* curl = curl_easy_init();
|
|
||||||
if (curl == NULL){
|
|
||||||
perror("ERROR: curl init failed.\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, config.addr);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_chunk);
|
|
||||||
|
|
||||||
CURLcode result = curl_easy_perform(curl);
|
|
||||||
if (result != CURLE_OK){
|
|
||||||
fprintf(stderr, "CURL ERROR: %s\n", curl_easy_strerror(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printPage(void){
|
|
||||||
for (int i=0; i<nbchunck; i++){
|
|
||||||
printf("%s", pageChunk[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]){
|
int main(int argc, char* argv[]){
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,61 @@
|
||||||
|
#include "config.h"
|
||||||
|
#include "page.h"
|
||||||
|
|
||||||
|
PageHTML page = {
|
||||||
|
.chunks = NULL,
|
||||||
|
.cap = DEFAULT_PAGE_LEN,
|
||||||
|
.len = 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
size_t save_chunk(char* buffer, size_t itemsize, size_t nitems, void* ignore){
|
||||||
|
size_t bytes = itemsize * nitems;
|
||||||
|
|
||||||
|
if (page.len >= page.cap){
|
||||||
|
page.cap *= 1.5;
|
||||||
|
page.chunks = realloc(page.chunks, sizeof(*page.chunks) * page.cap);
|
||||||
|
|
||||||
|
if (page.chunks == NULL){
|
||||||
|
perror("ERROR: Buy more ram.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&page.chunks[page.len], 0, page.cap - page.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
page.chunks[page.len] = malloc(bytes+1);
|
||||||
|
page.chunks[page.len][bytes] = '\0';
|
||||||
|
strncpy(page.chunks[page.len], buffer, bytes);
|
||||||
|
page.len++;
|
||||||
|
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void getPage(void){
|
||||||
|
page.chunks = malloc(sizeof(*page.chunks) * DEFAULT_PAGE_LEN);
|
||||||
|
if (page.chunks == NULL){
|
||||||
|
perror("ERROR: Buy more ram.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
CURL* curl = curl_easy_init();
|
||||||
|
if (curl == NULL){
|
||||||
|
perror("ERROR: curl init failed.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_easy_setopt(curl, CURLOPT_URL, config.addr);
|
||||||
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, save_chunk);
|
||||||
|
|
||||||
|
CURLcode result = curl_easy_perform(curl);
|
||||||
|
if (result != CURLE_OK){
|
||||||
|
fprintf(stderr, "CURL ERROR: %s\n", curl_easy_strerror(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
curl_easy_cleanup(curl);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printPage(void){
|
||||||
|
for (int i=0; i<page.len; i++){
|
||||||
|
printf("%s", page.chunks[i]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef PAGE_H
|
||||||
|
#define PAGE_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define DEFAULT_PAGE_LEN 16
|
||||||
|
|
||||||
|
typedef struct PageHTML {
|
||||||
|
char** chunks;
|
||||||
|
int cap;
|
||||||
|
int len;
|
||||||
|
} PageHTML;
|
||||||
|
|
||||||
|
extern PageHTML page;
|
||||||
|
|
||||||
|
size_t save_chunk(char* buffer, size_t itemsize, size_t nitems, void* ignore);
|
||||||
|
|
||||||
|
void getPage(void);
|
||||||
|
|
||||||
|
void printPage(void);
|
||||||
|
|
||||||
|
#endif // PAGE_H
|
Loading…
Reference in New Issue