Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <assert.h>
  6.  
  7. #include <unistd.h>
  8.  
  9. #include <stdbool.h>
  10.  
  11.  
  12. // Assumes: errnum is a valid error number
  13. // denne er ikke implementeret endnu, men det behøver vi nok heller ikke
  14. int print_error(char *path, int errnum) {
  15. return fprintf(stdout, "%s: cannot determine (%s)\n",
  16. path, strerror(errnum));
  17. }
  18.  
  19. bool ASCII(int c){
  20.  
  21. if ((c >= 7 && c <= 13) || (c == 27) || ( c >= 32 && c <= 126 )) {
  22. //printf("%s: ASCII text\n", file );
  23. return true;
  24. } else
  25. {
  26. return false;
  27. }
  28.  
  29. }
  30.  
  31. // hvis både ascii og ISO er true, er det en ascii-fil
  32.  
  33. bool ISO(int c){
  34. if ((c >= 7 && c <= 13) || (c == 27) || (c >= 32 && c <= 126) ||(c >= 160 && c <= 255)){
  35. //printf("%s: ISO text\n", file );
  36. return true;
  37. } else
  38. {
  39. return false;
  40. }
  41.  
  42. }
  43.  
  44. void toArray(int c, int counter, int coolArray){
  45. // coolArray skal i littel være = int coolArray[] = {92, 120, 70, 70, 92, 120, 70, 69 }; lol
  46. coolArray[counter] = c;
  47. }
  48.  
  49.  
  50. bool littleEndian(int coolArray[]){
  51. char checkArray[] = "\xFF\xFE";
  52. for (int i = 0; i < 8; i++){
  53. if ((int)checkArray[i] != coolArray[i]){
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59.  
  60. bool bigEndian(int coolArray[]){
  61. char checkArray[] = "\xFE\xFF";
  62. for (int i = 0; i < 8; i++){
  63. if ((int)checkArray[i] != coolArray[i]){
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69.  
  70.  
  71. int main(int argc, char* argv[]){
  72. bool AsciiPossible = true;
  73. bool ISOPossible = true;
  74. bool LittleEndianPossible = true;
  75. bool BigEndianPossible = true;
  76. int coolArray[8];
  77. if (argc==1) {
  78. fprintf(stdout, "Usage: file path");
  79. return EXIT_FAILURE;
  80. }
  81. char* file = argv[1];
  82.  
  83. FILE* filePath;
  84. int c;
  85.  
  86. int counter = 0;
  87.  
  88. filePath = fopen(file,"r");
  89.  
  90. if (errno == EACCES) {
  91. printf("%s: cannot determine (Permission denied)\n", file);
  92. return EXIT_FAILURE;
  93. }
  94.  
  95. if (filePath == NULL) {
  96. printf("%s: cannot determine (No such file or directory)\n", file);
  97. return(EXIT_SUCCESS);
  98. }
  99. // måske vi skulle bruge det fra A0 istedet. tror ikke det er såå vigtigt
  100.  
  101.  
  102. while(1) {
  103.  
  104. c = fgetc(filePath);
  105.  
  106. if( feof(filePath) ) {
  107. if (counter == 0){
  108. printf("%s: empty\n", file );
  109. return(0);
  110. //hertil tjekker vi om den er tom
  111. }
  112. }
  113. if (feof(filePath)){
  114. break;
  115. }
  116.  
  117. toArray(c, counter, coolArray);
  118.  
  119. if (AsciiPossible){
  120. AsciiPossible = ASCII(c);
  121. }
  122. if (ISOPossible){
  123. ISOPossible = ISO(c);
  124. }
  125. if (LittleEndianPossible && counter ==8){
  126. LittleEndianPossible = littleEndian(coolArray);
  127. }
  128. if (BigEndianPossible && counter == 8 ){
  129. BigEndianPossible = bigEndian(coolArray);
  130. }
  131.  
  132. counter++;
  133.  
  134.  
  135. }
  136. fclose(filePath);
  137.  
  138. return(0);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement