home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Helpers / Tzu Release 4 / Tool Development / Uncomment.cp < prev    next >
Encoding:
Text File  |  1994-10-15  |  1.6 KB  |  66 lines  |  [TEXT/MMCC]

  1.  
  2. // * Comment Tzu
  3. // * ©1994 Chris K. Thomas.  All Rights Reserved.
  4. // * 
  5. // * 100 11 Oct 94 ckt
  6. // * 101 14 Oct 94 ckt - rewrote to decomment more intelligently
  7. // *                         & wrote DeleteText
  8.  
  9. //future mods - check to see if there are comments after
  10. //a series of tabs after the return
  11.  
  12. #include "TzuTools.h"
  13.  
  14. pascal void main(Handle text);
  15.  
  16. void DeleteText(Handle hText,long offsetToText,long textLen);
  17.  
  18. // * Main entry point
  19. // text - handle to text to be modified
  20. pascal void main(Handle text)
  21. {
  22.     EnterCodeResource();
  23.     
  24.     const char *commentText    = "//";
  25.     long         sizeOfText    = GetHandleSize(text);
  26.     Ptr            localText    = *text;
  27.     
  28.     // delete “//” before text, since there isn't a return there usually
  29.     if(localText[0] == '/' && localText[1] == '/')
  30.         DeleteText(text,0,2);
  31.     
  32.     // replace returns + “//” with return
  33.     for(long i=0; i<(sizeOfText-1); i++)
  34.     {
  35.         localText = *text;
  36.         if(localText[i] == '\r' && localText[i+1] == '/' && localText[i+2] == '/')
  37.         {
  38.             DeleteText(text,i+1,2);
  39.             i+=1;
  40.         }
  41.     }
  42.     
  43.     ExitCodeResource();
  44. }
  45.  
  46. //Delete will probably be a callback in the
  47. //robustified TzuTool interface
  48. void DeleteText(Handle hText,long offsetToText,long textLen)
  49. {
  50.     long    hTextLen    = GetHandleSize(hText);
  51.     char    hTextState    = HGetState(hText);
  52.     Ptr        localText    = NULL;
  53.     
  54.     // SetHandleSize might need to move the memory to resize it
  55.     // which it can't do if the handle is locked
  56.     HUnlock(hText);
  57.     if(MemError()!=noErr) return;
  58.     
  59.     HLockHi(hText);
  60.     localText = *hText;
  61.     
  62.     BlockMoveData(&localText[offsetToText+textLen],&localText[offsetToText],hTextLen - (offsetToText+textLen));    
  63.     SetHandleSize(hText,hTextLen - textLen);
  64.     
  65.     HSetState(hText,hTextState);
  66. }