Initial commit
This commit is contained in:
16
Assigments/4/Compress/Makefile
Normal file
16
Assigments/4/Compress/Makefile
Normal file
@ -0,0 +1,16 @@
|
||||
COMPILER = gcc
|
||||
C_FLAGS = -Wall -Wextra
|
||||
htags.o: htags.c
|
||||
$(COMPILER) $(C_FLAGS) -c htags.c
|
||||
|
||||
htags: htags.o
|
||||
$(COMPILER) $(C_FLAGS) -o htags htags.c
|
||||
|
||||
test.o: test.c
|
||||
$(COMPILER) $(C_FLAGS) -c test.c
|
||||
|
||||
test: test.o
|
||||
$(COMPILER) $(C_FLAGS) -o test test.o
|
||||
|
||||
run: htags
|
||||
.\htags example.html
|
BIN
Assigments/4/Compress/Shoebottom_Isaac_A4.pdf
Normal file
BIN
Assigments/4/Compress/Shoebottom_Isaac_A4.pdf
Normal file
Binary file not shown.
129
Assigments/4/Compress/htags.c
Normal file
129
Assigments/4/Compress/htags.c
Normal file
@ -0,0 +1,129 @@
|
||||
#define MAX_FILESIZE 100000
|
||||
#define MAX_TAGS 100
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "printStats.h"
|
||||
|
||||
|
||||
|
||||
char *readUntilNext(char *index) {
|
||||
while (*index != EOF) {
|
||||
if (*index == '<') {
|
||||
char *temp = index;
|
||||
while (*temp != '>') {
|
||||
temp++;
|
||||
}
|
||||
if (*(temp-1) == '/') {
|
||||
goto SkipErroneousTags;
|
||||
}
|
||||
|
||||
index++;
|
||||
if (*index == (char)'!' || *index == (char)'/') {
|
||||
if (*(index+1) == '-' && *(index+2) == '-') {
|
||||
while (!(*index == '>' && *(index-1) == '-' && *(index-2) == '-')) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
else {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
SkipErroneousTags:
|
||||
index++;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
bool areEqualTags(char *first, char *second) {
|
||||
int firstCount = 0;
|
||||
int secondCount = 0;
|
||||
|
||||
while (isalnum(*first) != 0) {
|
||||
firstCount++;
|
||||
first++;
|
||||
}
|
||||
while (isalnum(*second) != 0) {
|
||||
secondCount++;
|
||||
second++;
|
||||
}
|
||||
if (firstCount != secondCount) {
|
||||
return false;
|
||||
}
|
||||
first-=firstCount;
|
||||
second-=secondCount;
|
||||
int i;
|
||||
for(i = 0; i < firstCount; i++) {
|
||||
if (*first == *second) {
|
||||
first++;
|
||||
second++;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(__attribute__((unused)) int argc, char **argv) {
|
||||
char *input = (char*)calloc(MAX_FILESIZE, sizeof(char));
|
||||
int i=0;
|
||||
FILE *file = fopen(*(argv+1), "r");
|
||||
while (true){
|
||||
*(input+i) = (char)fgetc(file);
|
||||
if (*(input+i) == EOF) {
|
||||
*(input+i) = EOF;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
char **list = (char**)calloc(MAX_TAGS, sizeof(char*));
|
||||
int *count = (int*)calloc(MAX_TAGS, sizeof (int));
|
||||
|
||||
char *index = input;
|
||||
|
||||
int size = 0;
|
||||
while (*index != EOF) {
|
||||
if (size == 0) {
|
||||
*list = (char*)calloc(MAX_FILESIZE, sizeof(char));
|
||||
strcpy(*list, readUntilNext(index));
|
||||
index = readUntilNext(index);
|
||||
*count = 1;
|
||||
size++;
|
||||
}
|
||||
|
||||
int j;
|
||||
bool repeat = false;
|
||||
char *tempChar = readUntilNext(index);
|
||||
for(j = 0; j < size; j++) {
|
||||
if (*tempChar == **(list + j)) {
|
||||
repeat = areEqualTags(tempChar, *(list + j));
|
||||
}
|
||||
if (repeat) {
|
||||
*(count + j) += 1;
|
||||
index = tempChar;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (repeat == false) {
|
||||
*(list+size) = (char*)calloc(MAX_FILESIZE, sizeof(char));
|
||||
strcpy(*(list+size), readUntilNext(index));
|
||||
index = readUntilNext(index);
|
||||
*(count + size) = 1;
|
||||
size++;
|
||||
}
|
||||
|
||||
}
|
||||
printStats(list, count, size-1);
|
||||
free(input);
|
||||
free(list);
|
||||
free(count);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
20
Assigments/4/Compress/printStats.h
Normal file
20
Assigments/4/Compress/printStats.h
Normal file
@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
void printStats(char **list, const int *count, int size) {
|
||||
int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
while (**list != EOF) {
|
||||
if (isalnum(**list) == 0) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
putchar(**list);
|
||||
(*list)++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\t%d\n", *(count+i));
|
||||
list++;
|
||||
}
|
||||
}
|
16
Assigments/4/Compress/test.c
Normal file
16
Assigments/4/Compress/test.c
Normal file
@ -0,0 +1,16 @@
|
||||
#define MAX_FILESIZE 100000
|
||||
#define MAX_TAGS 100
|
||||
|
||||
#include "printStats.h"
|
||||
|
||||
int main() {
|
||||
char input[MAX_FILESIZE] = "<html><title>Hello!</title></html>\377";
|
||||
char *list[MAX_TAGS];
|
||||
*list = &input[1];
|
||||
*(list+1) = &input[7];
|
||||
int count[MAX_TAGS];
|
||||
*count = 1;
|
||||
*(count+1) = 1;
|
||||
int size = 2;
|
||||
printStats(list, count, size);
|
||||
}
|
Reference in New Issue
Block a user