PDA

View Full Version : Simple C++ / Carbon app




toddburch
Jan 29, 2008, 10:49 AM
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



lin2mac
Jan 29, 2008, 12:30 PM
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.


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

toddburch
Jan 29, 2008, 01:21 PM
In OSX is there a difference between binary and text files? I thought that was a DOS/OS9 thing.

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

kainjow
Jan 29, 2008, 01:41 PM
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.

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.

toddburch
Jan 29, 2008, 02:11 PM
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!

fimac
Jan 29, 2008, 02:13 PM
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.

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 ;)

SC68Cal
Jan 29, 2008, 02:15 PM
In OSX is there a difference between binary and text files? I thought that was a DOS/OS9 thing.


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:


#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.

fimac
Jan 29, 2008, 02:33 PM
Text files are binary files. The only difference is that the binary digits are mapped to ASCII characters and then displayed as such.

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.)

toddburch
Jan 29, 2008, 02:40 PM
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 ;)

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.

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

fimac
Jan 29, 2008, 02:47 PM
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.

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 :)

kainjow
Jan 29, 2008, 02:51 PM
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 :)

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.

kainjow
Feb 8, 2008, 02:08 PM
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!

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.

toddburch
Feb 8, 2008, 02:12 PM
COOL! I'll be studying this tonight. (The wife can go out to dinner by HERSELF!!)

Thank you, thank you, thank you! Todd

Tom1983
Feb 9, 2009, 02:46 AM
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