Classic resources - code for editing

Discussion in 'Mac Programming' started by idelovski, Aug 3, 2011.

  1. idelovski, Aug 3, 2011
    Last edited by a moderator: Aug 3, 2011

    idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #1
    Hi,

    I need some example code that is saving/replacing resources in the classic Mac OS resource format files. All I need to do is edit a DITL resource and store it back into the resource file.

    I have this Carbon project that is still alive and it has maybe 400 DITLs in it and moving these things into Cocoa would be a nightmare. The thing is, in my Carbon application I am already parsing DITL format and creating controls on my own with Carbon APIs, not the old Dialog manager, so parsing DITLs would not be a problem. I have found the format for DITL resource in one of the original Inside Macintosh I-V books.

    Yesterday I realized that in Lion even Carbon apps look kinda cool and I would really hate to kill it now, but I need to be able to edit DITL resources even on Intel Macs somehow. So I don't really need complete solution for all that, only some snippets of Carbon code that are writing to the resource files.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    If you're already parsing DITL resources, then factor that code into a command-line tool, and have it produce a textual output. Edit the textual output.

    Then write a tool (it could be in any language: awk, perl, java, etc.) that parses the textual data and writes the DITL format.

    Or have it parse the textual data and rewrite it in a format that you write a new in-program parser for. Then you can revise your program to read that new format instead of DITL resources, eliminating the dependency on the DITL resource-format.

    Or just choose a textual format produced by the main tool (the one that uses your DITL-parsing code) that's easy to write an in-program parser for. For example, if you chose JSON as the textual format, there are many parsers in many languages that can read JSON and produce a tree of objects, arrays, strings, numbers, etc.
     
  3. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #3
    I'll probably make a GUI editor.

    And this part is exactly what I need. Writing / changing resource files. I don't want to spend too much time in Carbon these days and the last time I was looking at functions that add/change resources in resource files it all seemed rather tricky so I think that maybe an example that puts a new resource or changes an existing resource might be helpful for me and get me started with the project.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    I'd have to look it up, but I don't recall the DITL format being all that complicated to write. IIRC, it's a rect followed by a type-code, and maybe some variable-length stuff if there's a string involved. That kind of thing is pretty simply to produce, even with putc() and fwrite().

    Long long ago, I wrote DITL readers and writers in Forth and in C (for MPW). I doubt that I could find the code again, but it was much simpler to write than to read and parse.

    You don't have to write the new code in Carbon. You can use Objective-C if you want. The Carbon functions work the same. And remember all the types that are "toll-free bridged": NSArray, NSDictionary, NSData, etc. Simply casting to the right pointer type lets you use it as a CF reference or an object ptr.

    I'd write the code piece-meal. Make it produce the simplest type of DITL item first. Then make it produce another type of DITL item. Connect those together to produce the actual DITL list (which is a counted list, IIRC). Then add more producers for more DITL-item types. If you test it with known data at every stage, you'll always have something that works, even if on a subset of data.
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    <http://www.mathemaesthetics.com/DemoPage.html>
     
  6. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #6
    The link you posted, leads to Resorcerer 2.4.1 Demo (X), PPC version. It runs only with Rosetta I guess.
     
  7. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    I'm sorry I didn't understand you as having said you wanted something that works under Lion. I interpreted your post as lamenting having no way of editing resources.

    I would think, as a loooonnng time developer myself, that you'd have some way of running older versions of Mac OS to do testing against!

    The simplest solution (assuming only one Mac) would seem to me is an external drive with an earlier version of Mac OS installed to do your editing on.
     
  8. idelovski, Aug 4, 2011
    Last edited: Aug 4, 2011

    idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #8
    Ah, no.

    I have Macs capable of running even Mac OS 9, as most people having legacy Carbon projects do. Obviously, I can edit those resource files there. But the thing is, it all becomes too big of a hassle.

    I need to run OS 9, OS X on PPC and on Intel, I need Tiger and I need Snow Leopard and I do have an application for Windows so I need to be able to run even that. Now, here and there, I need to be able to change resource files when I'm not in my office/at home. It's not every day, but once or twice a month I really do. And I hate working with so many different computers almost every day.

    It would be nice if I could use MacBook with Snow and MacBook Air with Lion to work on my Carbon projects when my iBook G3 and PowerBook G4 eventually die.
     
  9. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #9
    You actually gave me a good idea here, so thanks, I'll most likely start a Cocoa project of a DITL resource editor.

    The things that bothered me most were functions I needed to change existing resources. I think I managed to grasp the general ideas described in Resource manager. I was sure I'd need function SetResourceSize(), but now I think that all I have to do is much simpler:

    1. Use Get1Resource to get the handle;
    2. Change its content directly - I don't need to make a copy;
    3. Use SetHandleSize() if I need to add some extra bytes or trim it;
    4. Call ChangedResource() and leave the rest to the Resource manager;
    5. Eventually call UpdateResFile() if I'm not closing the file right after changing it...

    ... and that would be all. Seems much simpler than I thought before, but if I'm missing something I would appreciate any suggestions.
     
  10. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
    What about using SheepShaver to run OS 9 for ResEdit? Then use VMware or Parallels to run OS X 10.6 Server for Xcode 3.2.6 for PPC builds. Might be a little crazy but I think it'd let you do all development on one machine.
     
  11. idelovski, Aug 5, 2011
    Last edited: Aug 5, 2011

    idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #11
    Wow,

    SheepShaver is amazing. ResEdit works, now I only need to move CodeWarrior in there somehow and I think I'll have everything. It's not that I haven't heard about it before, but someone told me that it doesn't work well so I never even tried it myself. Now when I think about it, the person in question probably tried to run Quark or Photoshop with it.

    I'm playing with it at home on my 2010 MacMini in SL. At first, cursor was jumpy, but when forced to run as 32bit process, everything is just perfect. Almost too good to be true.

    Thank you so much.

    P.S. I wonder how Apple does their own ResEdit jobs? Just did "lsof | grep Safari" and got this line:

    Safari 14414 ... /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Resources/Extras2.rsrc

    ;)
     
  12. Lane Roathe macrumors newbie

    Joined:
    Sep 15, 2011
  13. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #13
    Thanks for the link. Looks cool, even though it does not have anything to edit DITL resources and the project doesn't seem to have changed much (if any) since 2003. But I'll probably learn a lot of interesting stuff just by looking at the cocoa/carbon code in it.
     

Share This Page