home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 1992 August / info-mac-1992.iso / Source / C / Dragonsmith / Dragonsmith 1.0b2 / Utilities / HandleUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-14  |  1.4 KB  |  75 lines  |  [TEXT/KAHL]

  1. /*
  2.     HandleUtils.c
  3.     
  4.     Created    28 Mar 1992    NeedHandle only
  5.     
  6.     Modified    14 Apr 1992    Added more functions
  7.     
  8. */
  9.  
  10. #include    "HandleUtils.h"
  11.  
  12. void ResizeHandle (Handle h, long newSize)
  13. {
  14.     char        hState;
  15.     
  16.     hState = HGetState (h);
  17.     HUnlock (h);
  18.     SetHandleSize (h, newSize);
  19.     HSetState (h, hState);
  20. }
  21.  
  22. char *GrowByAndPoint (Handle h, char *p, long growBy, long *length)
  23. {
  24.     /* Given a handle to a block and a pointer into that block, grow the block by a
  25.         specified amount and return 1) a pointer to the byte at the same offset from
  26.         the beginning of the block as was the input pointer; and 2) the length of the
  27.         grown block
  28.     */
  29.     
  30.     long        ofs = StripAddress (p) - StripAddress (*h);
  31.     long        oldLen = GetHandleSize (h), newLen;
  32.     
  33.     *length = newLen = oldLen + growBy;
  34.     ResizeHandle (h, newLen);
  35.     return *h + ofs;
  36. }
  37.  
  38. Handle NeedHandle (Handle h, long size)
  39. {
  40.     if (h == NULL)
  41.         h = NewHandle (size);
  42.     else if (*h == NULL)
  43.         ReallocHandle (h, size);
  44.     return h;
  45. }
  46.  
  47. Handle AnyHandle (long size)
  48. {
  49.     Handle    h = NewHandle (size);
  50.     OSErr    err;
  51.     
  52.     return h ? h : TempNewHandle (size, &err);
  53. }
  54.  
  55. OSErr HandleToScrap (Handle h, ResType type)
  56. {
  57.     long        err;
  58.     char        hState;
  59.     
  60.     err = ZeroScrap ();
  61.     if (err == 0L) {
  62.         hState = SmartHLock (h);
  63.         err = PutScrap (GetHandleSize (h), 'TEXT', *h);
  64.         HSetState (h, hState);
  65.     }
  66.     return (short) err;
  67. }
  68.  
  69. char SmartHLock (Handle h)
  70. {
  71.     char        hState = HGetState (h);
  72.     
  73.     HLock (h);
  74.     return hState;
  75. }