Programming samples by Robert Elton Maas C These are excerpts from the file Cru91Ca.c (31k bytes, under development): #include stdio.h #define eofval 10 /* LineFeed not present in Macintosh files, EOF token. */ main() { int volin /* ,out,volout,numline,lptr,curline */ ; char ch,infile[64] /* ,line[10000],outfile[64] */ ; openw("My window"); /* Opens transcript window with that name */ printf("Press key then select input file:"); ch=getchar(); printf("Gotcha, stand by for dialog ..."); volin=getfile(infile); /* Standard select input file */ printf(" done."); /* volout=putfile(infile,"Output filename?",outfile); */ events(); in=open(infile,volin,1); /* these are some mac compatible open */ /* routines, you can also use fopen */ /* printf("Volume=%x FileName=%s\n",volin,infile); */ mainProg(); } mainProg() {char ch; int num,b3,b2,b1,b0; /* Simple test of character input from file omitted now, code kept as example for when I want to input data from a file. */ /* printf("Press key to see file input:"); ch=getchar(); ch=getc(in); printf(" <%c>",ch); ch=getc(in); printf("<%c>",ch); ch=getc(in); printf("<%c>",ch); ch=getc(in); printf("<%c>",ch); printf("\n"); */ /* Init storage allocation needed for data compression */ /* printf("\nPress key to initialize storage alloc:"); ch=getchar(); printf("\n"); */ initAlloc(); printf("\n"); /* ch=getchar(); */ /* Various demos&tests of storage allocation not needed now, flush soon. */ /* printf("Press key to show storage alloc:"); ch=getchar(); printf("\n"); */ /* showAlloc(); printf("\n"); */ /* printf("Press key to begin command loop:"); ch=getchar(); intLoop(); printf("\n"); */ /* printf("Press key to test integer coding:"); ch=getchar(); packInt(0,5,3,1,&num); printf("Num0531=%x",num); num = num + 36457; splitInt(num,&b3,&b2,&b1,&b0); printf("Num=%x=<%x,%x,%x,%x> ",num,b3,b2,b1,b0); */ /* Input from keyboard here, also might be needed as example. */ /* ch=getchar(); printf("%c%c%c",ch,ch,ch); ch=getchar(); printf("%c%c%c%c\n",ch,ch,ch,ch); */ topCru(); /* Under development */ printf("\n** Press key to end program:"); wait(); printf("\nGotcha! Bye now."); closew(); } topCru() {initcru(); printf("\nStarting to crunch ..."); nstop=1; while (more()) {int avl; avl=freen-(hglong+5); /* Allow cxthdr&cell hghdr&cell */ /* if (avl<40) printf("\nfreen=%d hglong=%d avl=%d",freen,hglong,avl); */ if (avl<=0) {printf("\n** freen=%d, start global purge ...",freen); if (trc802a>0) {nstop=0; if (more()==0) break; } glopur1(cxtroot,0); printf("\n** global purge done, freen=%d",freen); if (cxtnfork0) {nstop=0; if (more()==0) break; } } if (onecru()==0) break; } fincru(); } This is the whole file LineSegment.h (a joint effort of myself and my boss): #define _H_LineSegment #include class LineSegment : public CObject { public: Boolean closed; /* If last point is logically connected back to first */ long nPoints; /* Number of points actually in the line or circle, which is less than or equal to size of pointArray allocated */ Point **pointArray; public: LineSegment(void); void Dispose(void); void AddPoint(Point pt); void AddLongPoint(LongPt *pt); void CheckSize(long ptsToAdd); void AddMany(Point *dataStartP, long nPtsToAdd); void AddReversed(Point *dataStartP, long nPtsToAdd); void AddSegment(LineSegment *seg2P); void AddReversedSegment(LineSegment *seg2P); void GetPoint(long idx, Point *ptP); long NextIdx(long idx); long PrevIdx(long idx); void SetClosedFlag(Boolean cl); void DeleteLastPoint(void); }; These are excerpts from the file LineSegment.c (3k bytes, a joint effort of myself and my boss): #include #include #include "LineSegment.h" short gNPOINTS = 100; /*------------------------------------------------------------------*/ void LineSegment :: LineSegment() { nPoints = 0; closed = T; pointArray = (Point **) NewHandleClear(gNPOINTS * sizeof(Point)); } /*------------------------------------------------------------------*/ void LineSegment :: Dispose(void) { DisposHandle((Handle) pointArray); } /*------------------------------------------------------------------*/ void LineSegment :: DeleteLastPoint(void) { nPoints--; } /*------------------------------------------------------------------*/ void LineSegment :: SetClosedFlag(Boolean cl) { closed = cl; } /*------------------------------------------------------------------*/ void LineSegment :: AddPoint (Point pt) { CheckSize(1); (*pointArray)[nPoints++] = pt; } /*------------------------------------------------------------------*/ void LineSegment :: AddLongPoint (LongPt *pt) { CheckSize(1); LongToQDPt(pt,&(*pointArray)[nPoints++]); } /*------------------------------------------------------------------*/ /* If there is already enough allocated space to add all these points, do nothing. If there isn't enough space, allocate enough space to add that number of points, plus more to allow additional points without having to allocate immediately again. */ void LineSegment ::CheckSize(long ptsToAdd) { if (((nPoints + ptsToAdd) * sizeof(Point)) >= GetHandleSize((Handle) pointArray)) { SetHandleSize((Handle) pointArray, (nPoints + ptsToAdd + gNPOINTS) * sizeof(Point)); } } /*------------------------------------------------------------------*/ void LineSegment :: AddMany(Point *dataStartP, long nPtsToAdd) { CheckSize(nPtsToAdd); BlockMove(dataStartP, (*pointArray)+nPoints, (nPtsToAdd) * sizeof(Point)); nPoints += nPtsToAdd; } /*--------------------------------------------------------------------------*/ long LineSegment :: NextIdx(long idx) { return((idx < nPoints-1) ? (idx+1) : 0); } /*--------------------------------------------------------------------------*/ long LineSegment :: PrevIdx(long idx) { return((idx > 0) ? (idx-1) : (nPoints-1)); } These are excerpts from the file Contour.c (22k bytes, a joint effort of myself and my boss): #include #include #include /* R.Maas: I don't have a copy of this file yet, nor the corresponding CList.c containing object functions. */ #include "Contour.h" #include "CenterLine.h" #include "CMoBetterBitMap.h" /* Computes new vector of same length, rotated quarter-turn clockwise from original using Macintosh convention that positive vertical direction is DOWN. */ void RotateQuarterCW(Point origVec, Point *newVecP) { (*newVecP).v = origVec.h; (*newVecP).h = -origVec.v; } Boolean Contour :: FindOtherMatch(Contour *contour2, long *ix1P, long *ix2P) { long ix1,ix2; short grcnt, grcntz = floor(nPoints/gSkipHowMany); float ix1r = nPoints-5, ix1rincr = nPoints*gInvGRSQ; for (grcnt = 0; grcnt < grcntz; grcnt++) { ix1 = (long)(ix1r+0.5) % nPoints; if (FindOtherMatchAtIdx(contour2, ix1, &ix2)) { *ix1P = ix1; *ix2P = ix2; return(T); } ix1r += ix1rincr; } return(F); }