home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / cshar_ap.sit < prev    next >
Encoding:
Text File  |  1988-06-20  |  4.7 KB  |  220 lines

  1. 18-Jun-88 14:47:49-MDT,5021;000000000000
  2. Return-Path: <u-lchoqu%sunset@cs.utah.edu>
  3. Received: from cs.utah.edu by SIMTEL20.ARPA with TCP; Sat, 18 Jun 88 14:47:43 MDT
  4. Received: by cs.utah.edu (5.54/utah-2.0-cs)
  5.     id AA22714; Sat, 18 Jun 88 14:47:46 MDT
  6. Received: by sunset.utah.edu (5.54/utah-2.0-leaf)
  7.     id AA24839; Sat, 18 Jun 88 14:47:43 MDT
  8. Date: Sat, 18 Jun 88 14:47:43 MDT
  9. From: u-lchoqu%sunset@cs.utah.edu (Lee Choquette)
  10. Message-Id: <8806182047.AA24839@sunset.utah.edu>
  11. To: rthum@simtel20.arpa
  12. Subject: append.c
  13.  
  14. /* append.c -- by Mark^Zimmermann, 19871019-....
  15.  *
  16.  *    This little effort lets you append chosen text files together ...
  17.  *    pick the first file, and then subsequent choices from the
  18.  *    StdFiles dialog are tacked onto the end of it ... designed
  19.  *    to help save time and space relative to DivJoin, etc. programs
  20.  *    which copy all files as they are joined together.
  21.  *
  22.  *    Written in Lightspeed C ... 
  23.  *    Please send improvements and suggestions to me:
  24.  *    science(at)NEMS.ARPA;
  25.  *    [75066,2044] on CompuServe;
  26.  *    Mark^Zimmermann, 9511 Gwyndale Dr., Silver Spring, MD  20910, USA;
  27.  *    telephone 301-565-2166.
  28.  *
  29.  *    Thank you!  ^z
  30.  */
  31.  
  32. /* I don't know if all of these #includes are necessary.... */
  33.  
  34. #include <QuickDraw.h>
  35. #include <MacTypes.h>
  36. #include <FontMgr.h>
  37. #include <WindowMgr.h>
  38. #include <MenuMgr.h>
  39. #include <TextEdit.h>
  40. #include <DialogMgr.h>
  41. #include <EventMgr.h>
  42. #include <DeskMgr.h>
  43. #include <FileMgr.h>
  44. #include <ToolboxUtil.h>
  45. #include <ControlMgr.h>
  46. #include "StdFilePkg.h"
  47.  
  48. /* prototypes for my functions ... I do love prototypes! */
  49.  
  50. void main (void);
  51. void appendFile (int refNum0, Str255 *fnX, int vRefX);
  52. int GetFileZ (Str255 *fn, int *vRef);
  53. void pStrCopy (char *p1, char *p2);
  54. void giveUp (void);
  55.  
  56. /* set up buffer of size 25,600 bytes (= 512 * 50) for copying through;
  57.  * ZBUFSIZ must be an integer, < 32768
  58.  */
  59. #define ZBUFSIZ  25600
  60.  
  61. char buf[ZBUFSIZ];
  62.  
  63. /* set up a little window to give info to the user...very static
  64.  */
  65. WindowRecord w_record;
  66. WindowPtr info_window;
  67. Str255 string;
  68.  
  69. /* main routine to initialize everything under the sun (is all this
  70.  * really necessary?) and then get the names of the files to join ... if
  71.  * they open ok, start joining them, and quit when the user chooses
  72.  * "Cancel"....
  73.  */
  74.  
  75. void main() 
  76.   {
  77.     Str255 fn0, fnX;
  78.     int vRef0, vRefX, refNum0, i;
  79.  
  80.     InitGraf (&thePort);
  81.     InitFonts ();
  82.     FlushEvents (everyEvent, 0);
  83.     InitWindows ();
  84.     InitMenus ();
  85.     TEInit ();
  86.     InitDialogs (0L);
  87.     InitCursor ();
  88.     MaxApplZone ();
  89.     
  90.     info_window = GetNewWindow (128, &w_record, (WindowPtr) - 1L);
  91.     ShowWindow (info_window);
  92.     SetPort (info_window);
  93.     TextFont (0);
  94.     for (i = 1; i <= 3; ++i)
  95.       {
  96.         MoveTo (4, 15 * i - 5);
  97.         GetIndString (&string, 128, i);
  98.         DrawString (&string);
  99.       }
  100.  
  101.     if (GetFileZ (&fn0, &vRef0))
  102.       {
  103.         if (FSOpen (&fn0, vRef0, &refNum0) != noErr)
  104.             giveUp ();
  105.         while (GetFileZ (&fnX, &vRefX))
  106.             appendFile (refNum0, &fnX, vRefX);
  107.         FSClose (refNum0);
  108.       }
  109.   }
  110.  
  111.  
  112. /* routine to append the contents of file X to file 0....
  113.  */
  114.  
  115. void appendFile (refNum0, fnXp, vRefX)
  116.   int refNum0, vRefX;
  117.   Str255 *fnXp;
  118.   {
  119.     int refNumX, err;
  120.     long count;
  121.     
  122.     if (FSOpen (fnXp, vRefX, &refNumX) != noErr)
  123.         giveUp ();
  124.     if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
  125.         giveUp ();
  126.     if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
  127.         giveUp ();
  128.     for (;;)
  129.       {
  130.         count = ZBUFSIZ;
  131.         err = FSRead (refNumX, &count, buf);
  132.         if (err != noErr && err != eofErr)
  133.             giveUp ();
  134.         if (count == 0)
  135.             break;
  136.         if (FSWrite (refNum0, &count, buf) != noErr)
  137.             giveUp ();
  138.       }
  139.     FSClose (refNumX);
  140.     return;
  141.   }
  142.   
  143.  
  144. /* following variables and routine do the standard files dialog
  145.  * to get the name of the file use ... cribbed from the MiniEdit
  146.  * example that comes with LSC....
  147.  */
  148.  
  149. static Point SFGwhere = { 90, 82 };
  150. static SFReply reply;
  151.  
  152. int GetFileZ (fnp, vRefp)
  153.   Str255 *fnp;
  154.   int *vRefp;
  155.   {
  156.     SFTypeList myTypes;
  157.  
  158.     myTypes[0]='TEXT';
  159.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
  160.     if (reply.good)
  161.       {
  162.         pStrCopy( (char *)reply.fName, (char *)fnp);
  163.         *vRefp = reply.vRefNum;
  164.         return(1);
  165.       }
  166.     else return(0);
  167.   }
  168.  
  169.  
  170. /* routine to copy a pascal string from one place to another.... used in
  171.  * above Standard Files routine....
  172.  */
  173.  
  174. void pStrCopy( p1, p2 )
  175.   register char *p1, *p2;
  176.   {
  177.     register int len;
  178.     
  179.     len = *p2++ = *p1++;
  180.     while (--len>=0)
  181.         *p2++=*p1++;
  182.     return;
  183.   }
  184.  
  185.  
  186.  
  187. /* routine to give up with a beep if an error occurs during opening the
  188.  * file or fixing it....
  189.  */
  190.  
  191. void giveUp ()
  192.   {
  193.     SysBeep (10);
  194.     ExitToShell ();
  195.   }
  196.  
  197.  ==========
  198. append 9.rsrc
  199. ????CTLZ
  200.  
  201.  
  202. TYPE WIND
  203.   ,128
  204. Information
  205. 30 12 72 500
  206. Visible NoGoAway
  207. 1
  208. 0
  209.  
  210.  
  211. TYPE STR#
  212.   ,128
  213. 3
  214.     Greetings, Program!  Welcome to 'Append' by Mark^Zimmermann.
  215.   Pick a text file, then choose others to be appended to the end of it...
  216.      Choose 'Cancel' to quit.                                   v.1 - 19871019
  217.  
  218.  
  219. ==========
  220.