Advertisement
Bewin

absoluteLoader

Nov 10th, 2024 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define Max_Line_Length 100     // Maximum length of each line in the input file
  6. #define Max_Text_Records 10     // Maximum number of text records that can be stored
  7.  
  8. // Structure to store details of each text record
  9. typedef struct TextRecords {
  10.     int address;                 // Address field of the text record
  11.     int length;                  // Length of the text record
  12.     char restOfLine[Max_Line_Length];  // Remaining part of the line after address and length
  13. } TextRecords;
  14.  
  15. TextRecords textRecords[Max_Text_Records];  // Array to hold text records
  16. int textRecordCount = 0;                    // Counter for the number of text records stored
  17.  
  18. int main() {
  19.     char line[Max_Line_Length], restOfLine[Max_Line_Length];
  20.     int address, text_length;
  21.  
  22.     FILE *f1, *f2;
  23.  
  24.     // Open input and output files
  25.     f1 = fopen("input.txt", "r");
  26.     f2 = fopen("output.txt", "w");
  27.  
  28.     // Check if files were opened successfully
  29.     if (f1 == NULL || f2 == NULL) {
  30.         printf("Error! File not found.");
  31.         exit(0);
  32.     }
  33.  
  34.     // Read each line from the input file
  35.     while (fgets(line, sizeof(line), f1)) {
  36.         // Check if the line starts with 'H' (Header record)
  37.         if (line[0] == 'H') {
  38.             int starting_address, length;
  39.             char name[7];
  40.             // Parse the header record to extract name, starting address, and length
  41.             sscanf(line, "H^%4s^%X^%d", name, &starting_address, &length);
  42.             printf("Starting address is %06X\n", starting_address);
  43.         }
  44.             // Check if the line starts with 'T' (Text record)
  45.         else if (line[0] == 'T') {
  46.             // Parse the text record to extract address, length, and remaining data
  47.             sscanf(line, "T^%X^%X^%[^\n]", &address, &text_length, restOfLine);
  48.             // Store parsed values into the textRecords array
  49.             snprintf(textRecords[textRecordCount].restOfLine, Max_Line_Length, "%s", restOfLine);
  50.             textRecords[textRecordCount].address = address;
  51.             textRecords[textRecordCount].length = text_length;
  52.             textRecordCount++;    // Increment the text record count
  53.         }
  54.             // Check if the line starts with 'E' (End record)
  55.         else if (line[0] == 'E') {
  56.             break;    // End the loop if 'E' record is found
  57.         }
  58.     }
  59.  
  60.     // Process each stored text record and write to the output file
  61.     for (int i = 0; i < textRecordCount; ++i) {
  62.         // Tokenize the restOfLine string based on the '^' delimiter
  63.         char *token = strtok(textRecords[i].restOfLine, "^");
  64.         while (token != NULL) {
  65.             int len = strlen(token);
  66.             // Write pairs of characters to output file in specific format
  67.             while (len > 0) {
  68.                 fprintf(f2, "%06X %c%c\n", textRecords[i].address++, token[0], token[1]);
  69.                 len -= 2;         // Move to the next two characters
  70.                 token += 2;       // Advance the pointer
  71.             }
  72.             token = strtok(NULL, "^"); // Get the next token
  73.         }
  74.     }
  75.  
  76.     printf("\n--------Output written to output.txt--------\n");
  77.  
  78.     // Close input and output files
  79.     fclose(f1);
  80.     fclose(f2);
  81.  
  82.     return 0;
  83. }
  84.  
  85.  
  86. /*
  87. input.txt
  88. -----------------------------------------------------
  89. H^COPY^001000^00001F
  90. T^001000^12^123456^011020^051022^231025^678369^000002
  91. T^001021^12^123456^011020^051022^231025^678369^000002
  92. E^001000
  93. -----------------------------------------------------
  94. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement