English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 08 June 2024, 19:49   #1
alkis
Registered User
 
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 722
Gpt4o: Write me a hexdump for the amiga

Gpt4o wrote a decent hexdump. I was testing it with SAS C on Amiga 500 Os 1.3.

I did 2 corrections on follow-up prompts.

1. "I think the compiler is C89"
2. "It doesn't have getopt"

Not bad.

Code:
#include <dos/dos.h>
#include <exec/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h> // for size_t

#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif

void hexdump(const char *filename, size_t offset, size_t length) {
    FILE *file;
    unsigned char buffer[16];
    size_t bytesRead;
    size_t totalRead;
    size_t i;

    file = fopen(filename, "rb"); // Open file in binary read mode
    if (!file) {
        printf("Failed to open file: %s\n", filename);
        return;
    }

    fseek(file, offset, SEEK_SET); // Set the file position to the offset

    totalRead = 0; // Total bytes read

    // Read the file and display its contents in hexadecimal format
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), file)) > 0 && totalRead < length) {
        printf("%08lx ", (unsigned long)(offset + totalRead)); // Print the offset

        // Print the hexadecimal values of the bytes
        for (i = 0; i < bytesRead; ++i) {
            printf("%02x ", buffer[i]);
            if (i == 7) printf(" ");
        }

        // If the line is not full, pad with spaces
        if (bytesRead < 16) {
            for (i = bytesRead; i < 16; ++i) {
                printf("   ");
                if (i == 7) printf(" ");
            }
        }

        printf(" |");

        // Print the ASCII representation of the bytes
        for (i = 0; i < bytesRead; ++i) {
            if (buffer[i] >= 32 && buffer[i] <= 126) {
                printf("%c", buffer[i]);
            } else {
                printf(".");
            }
        }

        printf("|\n");
        totalRead += bytesRead; // Update total bytes read
    }

    fclose(file); // Close the file
}

int main(int argc, char **argv) {
    size_t offset = 0; // Default offset is 0
    size_t length = SIZE_MAX; // Default length is maximum size
    int i;

    // Manually parse command-line options
    for (i = 1; i < argc; i++) {
        if (strcmp(argv[i], "-o") == 0) {
            if (i + 1 < argc) {
                offset = strtoul(argv[++i], NULL, 0); // Set offset
            } else {
                printf("Option -o requires an argument.\n");
                return 1;
            }
        } else if (strcmp(argv[i], "-n") == 0) {
            if (i + 1 < argc) {
                length = strtoul(argv[++i], NULL, 0); // Set length
            } else {
                printf("Option -n requires an argument.\n");
                return 1;
            }
        } else {
            // Assume the remaining argument is the filename
            break;
        }
    }

    // Ensure a filename is provided
    if (i >= argc) {
        printf("Usage: hexdump [-o offset] [-n length] <filename>\n");
        return 1;
    }

    // Perform hexdump on the provided file
    hexdump(argv[i], offset, length);
    return 0;
}
alkis is offline  
Old 09 June 2024, 12:33   #2
phx
Natteravn
 
phx's Avatar
 
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,546
You shouldn't need any AmigaOS headers in a completely portable source text.
phx is offline  
Old 09 June 2024, 12:39   #3
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,475
My comment is the opposite. If the application is for the Amiga, why is it not using dos.library IO/ReadArgs and why is it not using exec.library RawDoFmt (or a simple custom online nybble to char conversion) and types?
Karlos is offline  
Old 09 June 2024, 12:46   #4
Dunny
Registered User
 
Dunny's Avatar
 
Join Date: Aug 2006
Location: Scunthorpe/United Kingdom
Posts: 2,092
Quote:
Originally Posted by Karlos View Post
My comment is the opposite. If the application is for the Amiga, why is it not using dos.library IO/ReadArgs and why is it not using exec.library RawDoFmt (or a simple custom online nybble to char conversion) and types?
I'd guess that ChatGPT hasn't had a lot of Amiga sources trained into it?
Dunny is offline  
Old 09 June 2024, 14:34   #5
Karlos
Alien Bleed
 
Karlos's Avatar
 
Join Date: Aug 2022
Location: UK
Posts: 4,475
Quote:
Originally Posted by Dunny View Post
I'd guess that ChatGPT hasn't had a lot of Amiga sources trained into it?
Sure. To be honest, if I was writing a hex dump tool I'd probably just use C standard library too.
Karlos is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
chatGPT to write Amiga C code JOB Coders. C/C++ 4 02 February 2023 15:44
Can write Amiga Bootblock PC? Retro1234 support.Other 8 10 June 2019 18:15
So, I'd like to write a new Amiga game - what do you want to see? Graham Humphrey Amiga scene 88 26 February 2012 21:50
How do I write curly braces on Amiga 600? { and } e5frog support.Other 0 15 July 2011 13:44
Amiga disk write protection Overdoc Retrogaming General Discussion 15 23 October 2001 01:02

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 07:10.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09932 seconds with 15 queries