Groupies working hard! :D
This commit is contained in:
21
A0/file.c
21
A0/file.c
@ -8,12 +8,15 @@ enum FileType{ASCII, ISO, UTF, data};
|
|||||||
|
|
||||||
bool is_utf8(FILE* f) {
|
bool is_utf8(FILE* f) {
|
||||||
rewind(f);
|
rewind(f);
|
||||||
char byte;
|
unsigned char byte;
|
||||||
int char_length = -1;
|
int char_length = -1;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (fread(&byte, 1, 1, f) == 0) {
|
if (fread(&byte, 1, 1, f) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (byte == 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
for (int j = 0 ; j <= 8 ; j++) {
|
for (int j = 0 ; j <= 8 ; j++) {
|
||||||
if (byte >> 7 == 0) {
|
if (byte >> 7 == 0) {
|
||||||
char_length = j;
|
char_length = j;
|
||||||
@ -48,20 +51,20 @@ bool is_iso8859(FILE* f) {
|
|||||||
for (int i = 0 ; i < 95 ; i++) {
|
for (int i = 0 ; i < 95 ; i++) {
|
||||||
iso8859_CHARACTERS[i+102] = i+160;
|
iso8859_CHARACTERS[i+102] = i+160;
|
||||||
}
|
}
|
||||||
char byte;
|
unsigned char byte;
|
||||||
bool is_iso8859;
|
bool iso_byte;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (fread(&byte, 1, 1, f) == 0) {
|
if (fread(&byte, 1, 1, f) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
is_iso8859 = false;
|
iso_byte = false;
|
||||||
for (int i = 0 ; i < 95 ; i++) {
|
for (int i = 0 ; i < 197 ; i++) {
|
||||||
if (byte == iso8859_CHARACTERS[i]) {
|
if (byte == iso8859_CHARACTERS[i]) {
|
||||||
is_iso8859 = true;
|
iso_byte = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (! is_iso8859) {
|
if (! iso_byte) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +84,7 @@ bool is_ascii(FILE* f) {
|
|||||||
ASCII_CHARACTERS[i+8] = i+32;
|
ASCII_CHARACTERS[i+8] = i+32;
|
||||||
}
|
}
|
||||||
|
|
||||||
char byte;
|
unsigned char byte;
|
||||||
bool ascii_byte;
|
bool ascii_byte;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (fread(&byte, 1, 1, f) == 0) {
|
if (fread(&byte, 1, 1, f) == 0) {
|
||||||
@ -126,6 +129,8 @@ int main(int argc, char* argv[]) {
|
|||||||
fprintf(stdout, "%s: ASCII text\n", argv[1]);
|
fprintf(stdout, "%s: ASCII text\n", argv[1]);
|
||||||
} else if (is_iso8859(f)) {
|
} else if (is_iso8859(f)) {
|
||||||
fprintf(stdout, "%s: ISO-8859 text\n", argv[1]);
|
fprintf(stdout, "%s: ISO-8859 text\n", argv[1]);
|
||||||
|
} else if (is_utf8(f)) {
|
||||||
|
fprintf(stdout, "%s: UTF-8 Unicode text\n", argv[1]);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stdout, "%s: data\n", argv[1]);
|
fprintf(stdout, "%s: data\n", argv[1]);
|
||||||
}
|
}
|
||||||
|
26
A0/test.sh
26
A0/test.sh
@ -12,26 +12,28 @@ rm -f test_files/*
|
|||||||
|
|
||||||
|
|
||||||
echo "Generating test files.."
|
echo "Generating test files.."
|
||||||
printf "Hello, World!\n" > test_files/ascii.input
|
printf "Hello, World\x04!\n" > test_files/ascii.input
|
||||||
printf "Hello, World!" > test_files/ascii2.input
|
printf "Hello, World!" > test_files/ascii2.input
|
||||||
|
printf "Test file\nTest\nThis is a test file\n\n\n\n" > test_files/ascii3.input
|
||||||
printf "Hello,\x00World!\n" > test_files/data.input
|
printf "Hello,\x00World!\n" > test_files/data.input
|
||||||
printf "Hællo,\x00World!\n" > test_files/iso1.input
|
printf "Hello,\x00World!\n" > test_files/data2.input
|
||||||
printf "Hello,\x00Wørld!\n" > test_files/iso2.input
|
printf "Hello,\xa0World!\n" > test_files/iso1.input
|
||||||
printf "Hello,\x00World!\n" > test_files/utf81.input
|
printf "Hello,\xa1\xbbWorld!\n" > test_files/iso2.input
|
||||||
printf "Hello,\x00World!\n" > test_files/utf82.input
|
printf "Hello, \xaaWorld!\n" > test_files/iso3.input
|
||||||
|
printf "\x24\xe0\xa4\xb9\xf0\x90\x8d\x88\n" > test_files/utf81.input
|
||||||
|
printf "مرحبا عالمي\n" > test_files/utf82.input
|
||||||
|
printf "Hello, World or should I say, 안녕하세요 세계!\n" > test_files/utf83.input
|
||||||
printf "" > test_files/empty.input
|
printf "" > test_files/empty.input
|
||||||
### TODO: Generate more test files ###
|
|
||||||
|
|
||||||
|
|
||||||
echo "Running the tests.."
|
echo "Running the tests.."
|
||||||
exitcode=0
|
exitcode=0
|
||||||
for f in test_files/*.input
|
for f in test_files/*.input # For loop
|
||||||
do
|
do
|
||||||
echo ">>> Testing ${f}.."
|
echo ">>> Testing ${f}.." # print
|
||||||
file "${f}" | sed 's/ASCII text.*/ASCII text/' > "${f}.expected"
|
file "${f}" | sed 's/ASCII text.*/ASCII text/' > "${f}.expected" # fjerner tekst der kun bliver givet ved ASCII filer og gemmer svaret i ".expected" filen.
|
||||||
./file "${f}" > "${f}.actual"
|
./file "${f}" > "${f}.actual" # giver vores *egen* kodes svar i ".actual" filen.
|
||||||
|
|
||||||
if ! diff -u "${f}.expected" "${f}.actual"
|
if ! diff -u "${f}.expected" "${f}.actual" # sammenligner vores egen kodes svar og "file" kommandoens svar
|
||||||
then
|
then
|
||||||
echo ">>> Failed :-("
|
echo ">>> Failed :-("
|
||||||
exitcode=1
|
exitcode=1
|
||||||
|
Reference in New Issue
Block a user