Newsgroups: comp.sys.apple2.programmer Path: news.weeg.uiowa.edu!news.uiowa.edu!uunet!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!wupost!waikato!comp.vuw.ac.nz!actrix.gen.nz!dempson From: dempson@swell.actrix.gen.nz (David Empson) Subject: Re: Using getDirEntry... Organization: Actrix Information Exchange Date: Sat, 12 Feb 1994 16:35:38 GMT Message-ID: References: Sender: dempson@actrix.gen.nz (David Empson) Lines: 107 In article caw@dcs.ed.ac.uk (Calum Wilkie) writes: > As my first program using the gsos calls, I thought I would try and > list the directory of the current path... but whenever I call > getDirEntry with the base and displacement equal to 1, I get a > bufferToSmall error ($4F). GetDirEntry returns two pieces of information in result buffers: name (the filename) and optionList. A buffTooSmall error could be referring to either of these buffers. You should check that each buffer is set up correctly: it should a buffer length word at the beginning, containing the total number of bytes in the buffer (including the length word), followed by space for the filename. If you allow for 31 characters in the filename buffer, it shouldn't need to be enlarged (for existing file systems, at least). The optionList result buffer must be able to hold at least two bytes of data, and will have to be resized to cope with the option list for a file with a resource fork (or on an HFS or AppleShare volume). Here is some C code to call GetDirEntry correctly. This is based on code in one of my utility programs, which builds up a list of all the files in a directory. This version doesn't actually do anything with the directory information. Error handling is rudimentary - the ChkToolErr function causes the program to terminate if there was a toolbox or GS/OS error. This should be regarded as an example. If you aren't intending to keep the filename and option list for very long, then you should use the same handles each time, rather than allocating new ones (as I'm doing here). As you process the directory, the handles would grow as needed to fit the largest filename and option list (you could probably get away with a smaller filename handle as the default, and let the program enlarge it if necessary). In my original program, I assume a maximum filename length and option list length of 255, and use static ResultBuf255 structures for the name and option list. I don't know whether this will compile as written - I did it on the fly in the message editor. It will probably need type-casting on several of the calls involving handles (especially if you're using prototyped toolbox header files, as supplied with ORCA/C 2.x). #define defNameSize 36 /* 32 chars plus two length words */ #define defOptListSize 6 void ScanDirectory(GSString255Ptr dirPath) { ResultBuf255Hndl name, optionList; static OpenRecGS openPB = {3, 0, NULL, readEnable}; static RefNumRecGS closePB = {1, 0}; static DirEntryRecGS dirEntryPB = {17, 0, 0, 1, 1}; word nameSize, optListSize; openPB.pathname = dirPath; Open(&openPB); /* Open the directory */ ChkToolErr(); dirEntryPB.refNum = closePB.refNum = openPB.refNum; for (;;) { nameSize = defNameSize; optListSize = defOptListSize; name = NewHandle((long)nameSize, myID, 0, 0L); ChkToolErr(); optionList = NewHandle((long)optListSize, myID, 0, 0L); ChkToolErr(); do { HLock(name); HLock(optionList); (**name).bufSize = nameSize; (**optionList).bufSize = optListSize; dirEntryPB.name = *name; dirEntryPB.optionList = *optionList; GetDirEntryGS(&dirEntryPB); if (_toolErr == buffTooSmall) { if ((**name).bufString.length + 4 > (**name).bufSize) { nameSize = (**name).bufString.length + 4; HUnlock(name); SetHandleSize((long)nameSize, name); ChkToolErr(); } if ((**optionList).bufString.length + 4 > (**optionList).bufSize) { optListSize = (**optionList).bufString.length + 4; HUnlock(optionList); SetHandleSize((long)optListSize, optionList); ChkToolErr(); } _toolErr = buffTooSmall; /* Force loop */ } } while (_toolErr == buffTooSmall); if (_toolErr == endOfDir) break; /* We're done */ ChkToolErr(); /* Other errors from GetDirEntry */ /* Add any needed code in here to process the entry */ DisposeHandle(name); DisposeHandle(optionList); } CloseGS(&closePB); } -- David Empson dempson@swell.actrix.gen.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand