Niko + Fie + mig
This commit is contained in:
2
A0/.gitignore
vendored
Normal file
2
A0/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
file.dsym/
|
||||
test_files/
|
14
A0/Makefile
Normal file
14
A0/Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
CC=gcc
|
||||
CFLAGS=-std=c11 -Wall -Werror -Wextra -pedantic -g3
|
||||
|
||||
file: file.c Makefile
|
||||
$(CC) $(CFLAGS) -o file file.c
|
||||
|
||||
../src.zip: file.c Makefile test.sh
|
||||
cd .. && zip src.zip src/file.c src/Makefile src/test.sh
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f file
|
||||
|
||||
.PHONY: clean
|
132
A0/file.c
132
A0/file.c
@ -0,0 +1,132 @@
|
||||
#include <stdio.h> // fprintf, stdout, stderr.
|
||||
#include <stdlib.h> // exit, EXIT_FAILURE, EXIT_SUCCESS.
|
||||
#include <string.h> // strerror.
|
||||
#include <errno.h> // errno.
|
||||
#include <stdbool.h>
|
||||
|
||||
enum FileType{ASCII, ISO, UTF, data};
|
||||
|
||||
bool is_utf8(FILE* f) {
|
||||
rewind(f);
|
||||
char byte;
|
||||
bool utf8_byte;
|
||||
int char_length;
|
||||
for (;;) {
|
||||
if (fread(&byte, 1, 1, f) == 0) {
|
||||
break;
|
||||
}
|
||||
utf8_byte = true;
|
||||
for (int j = 0 ; 1 ; j++) {
|
||||
if (byte >> 7 == 0) {
|
||||
char_length = max(j, 1);
|
||||
break;
|
||||
}
|
||||
byte = byte << 1;
|
||||
}
|
||||
for (int i = 1 ; i < char_length ; i++) {
|
||||
printf("esnathusrcaho");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_iso8859(FILE* f) {
|
||||
rewind(f);
|
||||
int iso8859_CHARACTERS[197];
|
||||
|
||||
for (int i = 0 ; i < 7 ; i++) {
|
||||
iso8859_CHARACTERS[i] = i+7;
|
||||
}
|
||||
iso8859_CHARACTERS[7] = 27;
|
||||
for (int i = 0 ; i < 94 ; i++) {
|
||||
iso8859_CHARACTERS[i+8] = i+32;
|
||||
}
|
||||
|
||||
for (int i = 0 ; i < 95 ; i++) {
|
||||
iso8859_CHARACTERS[i+102] = i+160;
|
||||
}
|
||||
char byte;
|
||||
bool is_iso8859;
|
||||
for (;;) {
|
||||
if (fread(&byte, 1, 1, f) == 0) {
|
||||
break;
|
||||
}
|
||||
is_iso8859 = false;
|
||||
for (int i = 0 ; i < 95 ; i++) {
|
||||
if (byte == iso8859_CHARACTERS[i]) {
|
||||
is_iso8859 = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! is_iso8859) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_ascii(FILE* f) {
|
||||
rewind(f);
|
||||
int ASCII_CHARACTERS[102];
|
||||
|
||||
for (int i = 0 ; i < 7 ; i++) {
|
||||
ASCII_CHARACTERS[i] = i+7;
|
||||
}
|
||||
ASCII_CHARACTERS[7] = 27;
|
||||
for (int i = 0 ; i < 94 ; i++) {
|
||||
ASCII_CHARACTERS[i+8] = i+32;
|
||||
}
|
||||
|
||||
char byte;
|
||||
bool ascii_byte;
|
||||
for (;;) {
|
||||
if (fread(&byte, 1, 1, f) == 0) {
|
||||
break;
|
||||
}
|
||||
ascii_byte = false;
|
||||
for (int i = 0 ; i < 102 ; i++) {
|
||||
if (byte == ASCII_CHARACTERS[i]) {
|
||||
ascii_byte = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! ascii_byte) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
int print_error(char* path, int errnum) {
|
||||
return fprintf(stderr, "%s: cannot determine (%s)\n", path, strerror(errnum));
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Wrong number of arguments!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
FILE* f = fopen(argv[1], "r");
|
||||
|
||||
if (f == NULL) {
|
||||
print_error(argv[2], errno);
|
||||
}
|
||||
char byte;
|
||||
if (fread(&byte, sizeof(char), 1, f) == 0) {
|
||||
fprintf(stdout, "%s: empty\n", argv[1]);
|
||||
} else if (is_ascii(f)) {
|
||||
fprintf(stdout, "%s: ASCII text\n", argv[1]);
|
||||
} else if (is_iso8859(f)) {
|
||||
fprintf(stdout, "%s: ISO-8859 text\n", argv[1]);
|
||||
} else {
|
||||
fprintf(stdout, "%s: data\n", argv[1]);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
42
A0/test.sh
Executable file
42
A0/test.sh
Executable file
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Exit immediately if any command below fails.
|
||||
set -e
|
||||
|
||||
make
|
||||
|
||||
|
||||
echo "Generating a test_files directory.."
|
||||
mkdir -p test_files
|
||||
rm -f test_files/*
|
||||
|
||||
|
||||
echo "Generating test files.."
|
||||
printf "Hello, World!\n" > test_files/ascii.input
|
||||
printf "Hello, World!" > test_files/ascii2.input
|
||||
printf "Hello,\x00World!\n" > test_files/data.input
|
||||
printf "Hællo,\x00World!\n" > test_files/iso1.input
|
||||
printf "Hello,\x00Wørld!\n" > test_files/iso2.input
|
||||
printf "Hello,\x00World!\n" > test_files/utf81.input
|
||||
printf "Hello,\x00World!\n" > test_files/utf82.input
|
||||
printf "" > test_files/empty.input
|
||||
### TODO: Generate more test files ###
|
||||
|
||||
|
||||
echo "Running the tests.."
|
||||
exitcode=0
|
||||
for f in test_files/*.input
|
||||
do
|
||||
echo ">>> Testing ${f}.."
|
||||
file "${f}" | sed 's/ASCII text.*/ASCII text/' > "${f}.expected"
|
||||
./file "${f}" > "${f}.actual"
|
||||
|
||||
if ! diff -u "${f}.expected" "${f}.actual"
|
||||
then
|
||||
echo ">>> Failed :-("
|
||||
exitcode=1
|
||||
else
|
||||
echo ">>> Success :-)"
|
||||
fi
|
||||
done
|
||||
exit $exitcodeASCII
|
Reference in New Issue
Block a user