Length of NSWindow's stringWithSavedFrame result?

  • In my NSDocument-based app I'm saving and restoring the window frame
    to/from an xattribute using
    NSWindow's stringWithSavedFrame and setFrameFromString: methods.

    During doc save in MyDocument.m I save the frame in an override of
    setFileURL:
    - (void)setFileURL:(NSURL *)absoluteURL
    {
        [super setFileURL:absoluteURL];
        // Save the window frame to the file's extended attributes
        NSString *frameNSString = [[tableView window]
    stringWithSavedFrame];
        // Only do the save if window frame exists.
        // i.e., if we're called during document save, not during
    document init
        if ( [frameNSString length] ) {
            const char *frameCString = [frameNSString UTF8String];
            int result = setxattr( [[absoluteURL path]
    fileSystemRepresentation],
                                                [JBMainWindowXattrName
    UTF8String],
                                                frameCString,
    strlen(frameCString) + 1,
                                                0, 0 );
        }
    }

    Upon doc load I restore the frame in an override of
    windowControllerDidLoadNib:
    - (void)windowControllerDidLoadNib:(NSWindowController *) aController
    {
        [super windowControllerDidLoadNib:aController];
        // For existing files, get the window frame from the file's
    extended attributes.
        NSURL * theURL = [self fileURL];
        if ( theURL ) {    // If we're loading an existing file, rather than
    a new one
            char frameCString [50];
            ssize_t bytesRetrieved = getxattr( [[theURL path]
    fileSystemRepresentation],

    [JBMainWindowXattrName UTF8String],

    frameCString, 50,

    0, 0 );
            if ( bytesRetrieved > 0 ) {
                [[tableView window] setFrameFromString:[NSString
    stringWithUTF8String:frameCString]];
            }
        }
    }

    This all works great. There's only one problem: the size of the string
    returned by stringWithSavedFrame.

    On my single-monitor system, the returned string looks like this:
    552 789 1312 260 0 0 1920 1178
    i.e. eight integers, four representing the window frame and four
    representing the screen frame.
    This takes about 30 bytes. I'm suspicious, though, that the 50 bytes
    I've allowed for the
    string returned by getxattr in the second method above might not be
    enough on a multi-monitor
    system. Can anyone tell us what the string returned by
    stringWithSavedFrame looks like on a
    multi-monitor system?

    ----------------------------------------------------------
    Vielen Dank to Uli Kusterer for the idea to use an xattribute for
    this, and for publishing his
    UKXATTRMETADATASTORE wrapper class.
  • On 08/03/2011, at 4:38 PM, John Bartleson wrote:

    > I'm suspicious, though, that the 50 bytes I've allowed for the
    > string returned by getxattr in the second method above might not be enough on a multi-monitor
    > system.

    You could just make it bigger - it's a very transient piece of memory on the stack (or, if that's a problem, just malloc and free a chunk). Since getxattr allows you to pass in the max size of the buffer, it's also safe from a buffer overflow attack.

    --Graham
  • Thanks, Graham.

    On Mar 8, 2011, at 12:39 AM, Graham Cox wrote:

    > You could just make it bigger - it's a very transient piece of
    > memory on the stack (or, if that's a problem, just malloc and free a
    > chunk). Since getxattr allows you to pass in the max size of the
    > buffer, it's also safe from a buffer overflow attack.

    Another solution was suggested offline:

    On Mar 7, 2011, at 10:14 PM, Scott Ribe wrote:
    > From the xattr man page:
    > When value is set to NULL, getxattr() returns current size of the
    > named attribute. This facility can be used to determine the size of
    > a buffer sufficiently large to hold the data currently associated
    > with the attribute.
    > --
    > Scott Ribe
    > <scott_ribe...>
    > http://www.elevated-dev.com/
    > (303) 722-0567 voice

    My response:

    > Thanks, Scott. Obviously I should RTFM 8-)
    >
    > I coded it to get the attribute size as you've described, allocate a
    > large enough buffer, and call getxattr() again for the data. It
    > works great!

    >
    > I found, though, that strangely I don't have any documentation of
    > the getxattr() behaviour you've described.
    > 1) I was using the book "Advanced Mac OS X Programming", by
    > Dalrymple and Hillegass. It has a section on xattributes, but
    > doesn't mention
    > the ability to set value to NULL in getxattr().
    > 2) I tried to get the xattr man page, but typing 'man xattr' and
    > 'man getxattr()' into the terminal didn't return anything.
    > a) I thought man pages were only for commands. Can we get man
    > pages for C functions too?
    > b) I'm still on OS X 10.5. Maybe that's why I don't get anything?

    ------------------------------------------------
    In further discussion with Scott, it looks like the xattribute docs
    aren't in the 10.5 man pages
previous month march 2011 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today