home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / Append 1.0.3 / Append.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  3.4 KB  |  153 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2.  
  3.     Append
  4.     version 1.0.3
  5.     
  6.     This snippet lets you append chosen text files together. Just pick
  7.     the first file, and then subsequent choices from the StdFile dialog
  8.     are tacked onto the end of it.
  9.  
  10.     Written by: Mark Zimmermann
  11.     Ported to CW by: Ken Long
  12.     Updated for CW9 by: Paul Celestin
  13.     
  14.     950711 - 1.0.1 - ported to CW
  15.     951201 - 1.0.2 - updated for CW7
  16.     960707 - 1.0.3 - updated for CW9
  17.  
  18. ---------------------------------------------------------------------- */
  19.  
  20. // prototypes
  21.  
  22. void    main(void);
  23. void    AppendFile(short refNum0, Str255 *fnXp, short vRefX);
  24. int        GetTheFiles(Str255 *fnp, short *vRefp);
  25. void    pStrCopy(char *p1, char *p2);
  26. void    GiveUp(void);
  27.  
  28. //• set up theBufferfer of size 25,600 bytes (= 512 * 50) for copying through;
  29. //• ktheBufferferSize must be an integer, < 32768
  30.  
  31. #define ktheBufferferSize  25600
  32.  
  33. char    theBuffer[ktheBufferferSize];
  34.  
  35. //• set up a little window to give info to the user...very static
  36.  
  37. WindowRecord myWinRecord;
  38. WindowPtr infoWindow;
  39. Str255 myString;
  40.  
  41. //• main routine to initialize everything under the sun (is all this
  42. //• really necessary?) and then get the names of the files to join ... if
  43. //• they open ok, start joining them, and quit when the user chooses
  44. //• "Cancel"....
  45.  
  46. void main() 
  47. {
  48.     Str255 fn0, fnX;
  49.     short vRef0, vRefX, refNum0, i;
  50.  
  51.     InitGraf (&qd.thePort);
  52.     InitFonts ();
  53.     FlushEvents (everyEvent, 0);
  54.     InitWindows ();
  55.     InitMenus ();
  56.     TEInit ();
  57.     InitDialogs (0L);
  58.     InitCursor ();
  59.     MaxApplZone ();
  60.     
  61.     infoWindow = GetNewWindow (128, &myWinRecord, (WindowPtr) - 1L);
  62.     ShowWindow (infoWindow);
  63.     SetPort (infoWindow);
  64.     TextFont (0);
  65.     for (i = 1; i <= 3; ++i)
  66.     {
  67.         MoveTo (4, 15 * i - 5);
  68.         GetIndString ((StringPtr) myString, 128, i);
  69.         DrawString ((StringPtr) myString);
  70.     }
  71.  
  72.     if (GetTheFiles (&fn0, &vRef0))
  73.     {
  74.         if (FSOpen ((StringPtr) fn0, vRef0, &refNum0) != noErr)
  75.             GiveUp ();
  76.         while (GetTheFiles (&fnX, &vRefX))
  77.             AppendFile (refNum0, &fnX, vRefX);
  78.         FSClose (refNum0);
  79.     }
  80. }
  81.  
  82. //• Routine to append the contents of file X to file 0.
  83.  
  84. void AppendFile(short refNum0, Str255 *fnXp, short vRefX)
  85. {
  86.     short refNumX, err;
  87.     long count;
  88.     
  89.     if (FSOpen ((StringPtr) fnXp, vRefX, &refNumX) != noErr)
  90.         GiveUp ();
  91.     if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
  92.         GiveUp ();
  93.     if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
  94.         GiveUp ();
  95.     for (;;)
  96.     {
  97.         count = ktheBufferferSize;
  98.         err = FSRead (refNumX, &count, theBuffer);
  99.         if (err != noErr && err != eofErr)
  100.             GiveUp ();
  101.         if (count == 0)
  102.             break;
  103.         if (FSWrite (refNum0, &count, theBuffer) != noErr)
  104.             GiveUp ();
  105.     }
  106.     FSClose (refNumX);
  107.     return;
  108. }
  109.  
  110. //• following variables and routine do the standard files dialog
  111. //• to get the name of the file use ... cribbed from the MiniEdit
  112. //• example that comes with LSC....
  113.  
  114. static Point SFGwhere = { 90, 82 };
  115. static SFReply reply;
  116.  
  117. int GetTheFiles (Str255 *fnp, short *vRefp)
  118. {
  119.     SFTypeList myTypes;
  120.  
  121.     myTypes[0]='TEXT';
  122.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  123.     if (reply.good)
  124.     {
  125.         pStrCopy( (char *)reply.fName, (char *)fnp);
  126.         *vRefp = reply.vRefNum;
  127.         return(1);
  128.     }
  129.     else return(0);
  130. }
  131.  
  132. //• routine to copy a pascal myString from one place to another.... used in
  133. //• above Standard Files routine....
  134.  
  135. void pStrCopy(char *p1, char *p2)
  136. {
  137.     short len;
  138.     
  139.     len = *p2++ = *p1++;
  140.     while (--len >= 0)
  141.         *p2++ = *p1++;
  142.     return;
  143. }
  144.  
  145. //• routine to give up with a beep if an error occurs during opening the
  146. //• file or fixing it....
  147.  
  148. void GiveUp ()
  149. {
  150.     SysBeep (10);
  151.     ExitToShell ();
  152. }
  153.