home *** CD-ROM | disk | FTP | other *** search
- //• append.c -- by Mark^Zimmermann, 19871019-....
-
- //• This little effort lets you append chosen text files together ...
- //• pick the first file, and then subsequent choices from the
- //• StdFiles dialog are tacked onto the end of it ... designed
- //• to help save time and space relative to DivJoin, etc. programs
- //• which copy all files as they are joined together.
-
- //• Written in Lightspeed C ...
- //• Please send improvements and suggestions to me:
- //• science(at)NEMS.ARPA;
- //• [75066,2044] on CompuServe;
- //• Mark^Zimmermann, 9511 Gwyndale Dr., Silver Spring, MD 20910, USA;
- //• telephone 301-565-2166.
-
- //• Thank you! ^z
-
- //• prototypes for my functions ... I do love prototypes!
-
- //#include <StandardFile.h>
-
- void main(void);
- void Append_File(short refNum0, Str255 *fnXp, short vRefX);
- int GetFileZ(Str255 *fnp, short *vRefp);
- void pStrCopy(char *p1, char *p2);
- void giveUp(void);
-
- //• set up buffer of size 25,600 bytes (= 512 * 50) for copying through;
- //• ZBUFSIZ must be an integer, < 32768
-
- #define ZBUFSIZ 25600
-
- char buf[ZBUFSIZ];
-
- //• set up a little window to give info to the user...very static
-
- WindowRecord w_record;
- WindowPtr info_window;
- Str255 string;
-
- //• main routine to initialize everything under the sun (is all this
- //• really necessary?) and then get the names of the files to join ... if
- //• they open ok, start joining them, and quit when the user chooses
- //• "Cancel"....
-
- void main()
- {
- Str255 fn0, fnX;
- short vRef0, vRefX, refNum0, i;
-
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (0L);
- InitCursor ();
- MaxApplZone ();
-
- info_window = GetNewWindow (128, &w_record, (WindowPtr) - 1L);
- ShowWindow (info_window);
- SetPort (info_window);
- TextFont (0);
- for (i = 1; i <= 3; ++i)
- {
- MoveTo (4, 15 * i - 5);
- GetIndString ((StringPtr) string, 128, i);
- DrawString ((StringPtr) string);
- }
-
- if (GetFileZ (&fn0, &vRef0))
- {
- if (FSOpen ((StringPtr) fn0, vRef0, &refNum0) != noErr)
- giveUp ();
- while (GetFileZ (&fnX, &vRefX))
- Append_File (refNum0, &fnX, vRefX);
- FSClose (refNum0);
- }
- }
-
- //• Routine to append the contents of file X to file 0.
-
- void Append_File(short refNum0, Str255 *fnXp, short vRefX)
- {
- short refNumX, err;
- long count;
-
- if (FSOpen ((StringPtr) fnXp, vRefX, &refNumX) != noErr)
- giveUp ();
- if (SetFPos (refNumX, fsFromStart, 0L) != noErr)
- giveUp ();
- if (SetFPos (refNum0, fsFromLEOF, 0L) != noErr)
- giveUp ();
- for (;;)
- {
- count = ZBUFSIZ;
- err = FSRead (refNumX, &count, buf);
- if (err != noErr && err != eofErr)
- giveUp ();
- if (count == 0)
- break;
- if (FSWrite (refNum0, &count, buf) != noErr)
- giveUp ();
- }
- FSClose (refNumX);
- return;
- }
-
- //• following variables and routine do the standard files dialog
- //• to get the name of the file use ... cribbed from the MiniEdit
- //• example that comes with LSC....
-
- static Point SFGwhere = { 90, 82 };
- static SFReply reply;
-
- int GetFileZ (Str255 *fnp, short *vRefp)
- {
- SFTypeList myTypes;
-
- myTypes[0]='TEXT';
- SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply);
- if (reply.good)
- {
- pStrCopy( (char *)reply.fName, (char *)fnp);
- *vRefp = reply.vRefNum;
- return(1);
- }
- else return(0);
- }
-
- //• routine to copy a pascal string from one place to another.... used in
- //• above Standard Files routine....
-
- void pStrCopy(char *p1, char *p2)
- {
- short len;
-
- len = *p2++ = *p1++;
- while (--len >= 0)
- *p2++ = *p1++;
- return;
- }
-
- //• routine to give up with a beep if an error occurs during opening the
- //• file or fixing it....
-
- void giveUp ()
- {
- SysBeep (10);
- ExitToShell ();
- }
-