1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Simple C++ / Carbon app

Discussion in 'Mac Programming' started by toddburch, Jan 29, 2008.

  1. macrumors 6502a

    #1
    I have an app I've written that will read any file in binary mode and write it to a second (text) file in hex dump format. It's all command line stuff.

    I would like to change this to write the output to a windowed application that I could scroll. I suspect there would be minimal changes needed to do this.

    Anyone got a pointer? I'd be happy to share it when finished.

    (Phase 2 might be allowing a text entry box where I can paste the input file name, or a "browse" button that opens a file-open dialog.)

    Thanks, Todd
     
  2. macrumors newbie

    #2
    In OSX is there a difference between binary and text files? I thought that was a DOS/OS9 thing.
     
  3. macrumors 6502a

    #3
    I don't know if Mac OS X considers them different or not, but it certainly matter hows a file is processed. File associations (or whatever it's called on a Mac) make them different. The contents of a file certainly would make them different. Which C or C++ I/O function is used makes them different.

    It's my understand that "text" implies a certain format, like being full of displayable characters and newline characters and will generally display in editors as a human might expect.

    Binary files, on the other hand, could have any available character encoding and be chock full of non-displayable characters, and "newline" characters that don't necessarily mean "new line".

    Todd
     
  4. Moderator emeritus

    kainjow

    #4
    Describe "minimal" :)

    If your app is a CLI, then you will need to create a new Carbon app and put in all the fun event handling code, which is fortunately already provided for you in the Xcode's Carbon Application project template. But you'd still have to add your textbox code and such. And AFAIK there aren't many, if any, Carbon devs on this forum to help you.

    What you want to do can be done with one line of Cocoa code. I can whip up a project if you want.
     
  5. macrumors 6502a

    #5
    That is certainly gracious of you to offer.

    I picked Carbon because I could use C and/or C++. I don't know Objective-C yet (but I have bought both the Obj-C book and the Cocoa book, for what that's worth!).

    After posting, I did try a C++ Carbon Standard Application template. I wasn't sure where to go after that. There was a Window, and I could drag and drop text controls and/or a scrollbar to it, but without knowing the event model, that doesn't do me a lot of good.

    Let me fuss with it some more. Tomorrow or the next day I may come back and take you up on your offer though! I feel like I have some more due diligence to do.

    Thanks!
     
  6. macrumors member

    #6
    Hex Fiend is an open source hex editor -- browsing through the source may be instructive. http://www.ridiculousfish.com/hexfiend/.

    Cocoa can call C++. Thus the non-graphical core of your application can remain implemented in C++, but with a Cocoa GUI. This is handy for Cocoa newbies like me ;)
     
  7. macrumors 68000

    #7

    Text files are binary files. The only difference is that the binary digits are mapped to ASCII characters and then displayed as such.

    For example:

    Code:
    #include <stdio.h>
    
    main()
    {
    
      int a = 54; // 54 is the ASCII character code for "6"
      printf("%c\n", a); // format to ASCII and print
    
    }
    
    Anyway, I thought it was cool.
     
  8. macrumors member

    #8
    Files are quite simply sequences of zero or more bytes. The distinction between text and binary format relates to the translation of line-endings on read and write. Whilst Unix does not do (and has never done) this translation, Windows does (and always has).

    Thus, when we talk about "text" or "binary" files, we are essentially describing whether the file is human-readable. (Example Library/Safari/Bookmarks.plist is binary, whereas Library/PubSub/Clients.plist is text -- at least on my 10.5.1 installation.)
     
  9. macrumors 6502a

    #9
    Good idea. My purpose is a bit special, and I feel it will do me more good to learn from scratch than to start butchering someone else's code.

    Here's my special purpose. (Reminds me of the movie "The Jerk")

    I have EBCDIC files I want to edit on the Mac. They can be quite large - +- 500MB:

    • I want to have a toggle for the eyecatcher area to display in either ASCII or EBCDIC. (in case I want to look at a binary file is that not EBCDIC).
    • I want scrolling to be blazingly fast for these large file. What I think I'll do is merely scroll the offset column on the left, and when scrolling is done, seek into the file to get that data. This way, I'm not reading tons of data just to get through it for those time I want to get to the bottom of the data quickly.
      Code:
      Offset   Rec. Off. Data................................  Eyecatcher......
      00000000 000000    dddddddd dddddddd dddddddd ddddddddd *----------------*
      00000010 000010    dddddddd dddddddd dddddddd ddddddddd *----------------*
      ...
      000000D0 0000D0    dddddddd dddddddd dddd               *---------*
      
      
      000000DA 000000    dddddddd dddddddd dddddddd ddddddddd *----------------*
      000000EA 000010    dddddddd dddddddd dddddddd ddddddddd *----------------*
      
      
    • I want an option for carving the hex view up into fixed length records. So, let's say each record is 0xDA long. At offset 0x00, the first record starts. At offset 0xDA the second records starts, at 0x01B4 the 3rd record starts, etc. I want a visual break between each record.
    • I want a "global offset" on the far left column (showing the offset in the file from byte 0
    • I want a "record" offset, so for each record that is 0xDA long, I want an offset of 0x00 through 0xD9

    Todd
     
  10. macrumors member

    #10
    I hear you, I would do the same!

    That said, Hex Fiend was specifically designed to deal with stupidly large files, and so it may be that you can re-use that part. Good luck with your project :)
     
  11. Moderator emeritus

    kainjow

    #11
    Hex Fiend does work really well with huge files. However, HexEdit (an older Carbon app), works very well also, and it too is open source.
     
  12. Moderator emeritus

    kainjow

    #12
    I forgot about this thread but remembered it today, so here's a Cocoa project I whipped up that demonstrates generating a C-style string from a C function, and then converting it so it can be placed into a text view, in Cocoa. I used a NIB because it's much easier but that really is the only confusing part about it IMO.
     

    Attached Files:

  13. macrumors 6502a

    #13
    [size=+4]COOL![/size] I'll be studying this tonight. (The wife can go out to dinner by HERSELF!!)

    Thank you, thank you, thank you! Todd
     
  14. macrumors newbie

    #14
    Moving from C to C++

    I have been reading this page with big interest since I moved to XCode to translate my Pascal programs to C++ / Carbon (I know all very old stuff ;-)

    when I tried the posted TextOut application it runned all right; so I tried to move forward and added a button to add text to the TextView window. Runs perfectly when I added it to the .c file provided - but when I tried to copy all the code to a .cpp file instead of a .c file I got the following error:

    --------

    "_pAddMyLines", referenced from:
    -[Button_Action convert:] in Button_Action.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    -------

    please note that I added the following code to the original .c file:

    -------

    char* pAddMyLines()
    {
    return "CPP Text in the window!! \n";
    }

    -----

    and I created a new Carbon Control called "Button_Action" which basically contains the same lines as the Controller.m file provided.

    Hope I explain it clearly enough? anyone who can explain why the same code doesn't work in a CPP file compared to a C file?

    thanks in advance

    Tom
     

Share This Page