Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. int EOF_flag = 0;
  7.  
  8. char* get_input(FILE *file)
  9. {
  10.     char *input = NULL;
  11.     size_t buf_size = 0;
  12.     EOF_flag = getline(&input, &buf_size, file);
  13.     return input;
  14. }
  15.  
  16.  
  17. int main(int argc, char** argv) {
  18.    
  19.     short opcode;
  20.     int argcode;
  21.     FILE *input;
  22.     FILE *output;
  23.     char *line = NULL;
  24.     char *commandName;
  25.     char *commandArg;
  26.     size_t bufSize = 0;
  27.     const int tabSize = 13;
  28.     char *commands[] = {"JNS", "LOAD", "STORE", "ADD", "SUBT", "INPUT", "OUTPUT", "HALT", "SKIPCOND", "JUMP", "CLEAR", "ADDI", "JUMPI"};
  29.    
  30.    
  31.     input = fopen(argv[1], "r");
  32.     if(input == NULL) {
  33.         printf("Błąd podczas otwierania pliku źródłowego\n");
  34.         exit(EXIT_FAILURE);
  35.     }
  36.    
  37.     output = fopen(argv[2], "w");
  38.     if(output == NULL) {
  39.         printf("Błąd podczas tworzenia pliku docelowego\n");
  40.         exit(EXIT_FAILURE);
  41.     }
  42.    
  43.    
  44.     while(getline(&line, &bufSize, input) != -1) {
  45.         char *temp;
  46.  
  47.         int p = 0;
  48.  
  49.         commandName = strtok(line, " ");
  50.         commandArg = strtok(NULL, " \n");
  51.        
  52.         for(int i = 0; i < tabSize; i++) {
  53.             if(strcmp(commandName, commands[i]) == 0) {
  54.                 opcode = i;
  55.                 if(commandArg != NULL) {
  56.                     argcode = strtol(commandArg, NULL, 16);
  57.                     short opcode_number = opcode << 12;
  58.                     short arg_number = (short) argcode;
  59.                     short result = opcode_number + arg_number;
  60.                     fwrite(&result, sizeof result, 1, output);
  61.                 }
  62.             }
  63.         }
  64.     }
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement