Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <unistd.h>
- #include <stdbool.h>
- // Assumes: errnum is a valid error number
- // denne er ikke implementeret endnu, men det behøver vi nok heller ikke
- int print_error(char *path, int errnum) {
- return fprintf(stdout, "%s: cannot determine (%s)\n",
- path, strerror(errnum));
- }
- bool ASCII(int c){
- if ((c >= 7 && c <= 13) || (c == 27) || ( c >= 32 && c <= 126 )) {
- //printf("%s: ASCII text\n", file );
- return true;
- } else
- {
- return false;
- }
- }
- // hvis både ascii og ISO er true, er det en ascii-fil
- bool ISO(int c){
- if ((c >= 7 && c <= 13) || (c == 27) || (c >= 32 && c <= 126) ||(c >= 160 && c <= 255)){
- //printf("%s: ISO text\n", file );
- return true;
- } else
- {
- return false;
- }
- }
- void toArray(int c, int counter, int coolArray){
- // coolArray skal i littel være = int coolArray[] = {92, 120, 70, 70, 92, 120, 70, 69 }; lol
- coolArray[counter] = c;
- }
- bool littleEndian(int coolArray[]){
- char checkArray[] = "\xFF\xFE";
- for (int i = 0; i < 8; i++){
- if ((int)checkArray[i] != coolArray[i]){
- return false;
- }
- }
- return true;
- }
- bool bigEndian(int coolArray[]){
- char checkArray[] = "\xFE\xFF";
- for (int i = 0; i < 8; i++){
- if ((int)checkArray[i] != coolArray[i]){
- return false;
- }
- }
- return true;
- }
- int main(int argc, char* argv[]){
- bool AsciiPossible = true;
- bool ISOPossible = true;
- bool LittleEndianPossible = true;
- bool BigEndianPossible = true;
- int coolArray[8];
- if (argc==1) {
- fprintf(stdout, "Usage: file path");
- return EXIT_FAILURE;
- }
- char* file = argv[1];
- FILE* filePath;
- int c;
- int counter = 0;
- filePath = fopen(file,"r");
- if (errno == EACCES) {
- printf("%s: cannot determine (Permission denied)\n", file);
- return EXIT_FAILURE;
- }
- if (filePath == NULL) {
- printf("%s: cannot determine (No such file or directory)\n", file);
- return(EXIT_SUCCESS);
- }
- // måske vi skulle bruge det fra A0 istedet. tror ikke det er såå vigtigt
- while(1) {
- c = fgetc(filePath);
- if( feof(filePath) ) {
- if (counter == 0){
- printf("%s: empty\n", file );
- return(0);
- //hertil tjekker vi om den er tom
- }
- }
- if (feof(filePath)){
- break;
- }
- toArray(c, counter, coolArray);
- if (AsciiPossible){
- AsciiPossible = ASCII(c);
- }
- if (ISOPossible){
- ISOPossible = ISO(c);
- }
- if (LittleEndianPossible && counter ==8){
- LittleEndianPossible = littleEndian(coolArray);
- }
- if (BigEndianPossible && counter == 8 ){
- BigEndianPossible = bigEndian(coolArray);
- }
- counter++;
- }
- fclose(filePath);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement