diff options
Diffstat (limited to 'app/bin/bdf2xtp.c')
-rw-r--r-- | app/bin/bdf2xtp.c | 620 |
1 files changed, 326 insertions, 294 deletions
diff --git a/app/bin/bdf2xtp.c b/app/bin/bdf2xtp.c index f19ceb8..20cea11 100644 --- a/app/bin/bdf2xtp.c +++ b/app/bin/bdf2xtp.c @@ -17,30 +17,30 @@ #include <stdlib.h> char helpStr[] = -"Bdf2xtp translates .bdf files (which are source files for Winrail track\n" -"libraries) to .xtp files (which are XTrkCad parameter files).\n" -"Bdf2xtp is a MS-DOS command and must be in run in a DOS box under MS-Windows.\n" -"\n" -"Usage: bdf2xtp OPTIONS SOURCE.BDF TARGET.XTP\n" -"\n" -"OPTIONS:\n" -" -c CONTENTS description of contents\n" -" -k COLOR color of non-track segments\n" -" -s SCALE scale of turnouts (ie. HO HOn3 N O S ... )\n" -" -v verbose - include .bdf source as comments in .xtp file\n" -"\n" -"For example:\n" -" bdf2xtp -c \"Faller HO Structures\" -k ff0000 -s HO fallerh0.bdf fallerh0.xtp\n" -"\n" -"Turnouts are composed of rails (which are Black) and lines. Structures are\n" -"composed of only lines. By default lines are Purple but you change this with\n" -"the -k optioon. The color is specified as a 6 digit hexidecimal value, where\n" -"the first 2 digits are the Red value, the middle 2 digits are the Green value\n" -"and the last 2 digits are the Blue value\n" -" ff0000 Red\n" -" 00ff00 Green\n" -" 00ffff Yellow\n" -; + "Bdf2xtp translates .bdf files (which are source files for Winrail track\n" + "libraries) to .xtp files (which are XTrkCad parameter files).\n" + "Bdf2xtp is a MS-DOS command and must be in run in a DOS box under MS-Windows.\n" + "\n" + "Usage: bdf2xtp OPTIONS SOURCE.BDF TARGET.XTP\n" + "\n" + "OPTIONS:\n" + " -c CONTENTS description of contents\n" + " -k COLOR color of non-track segments\n" + " -s SCALE scale of turnouts (ie. HO HOn3 N O S ... )\n" + " -v verbose - include .bdf source as comments in .xtp file\n" + "\n" + "For example:\n" + " bdf2xtp -c \"Faller HO Structures\" -k ff0000 -s HO fallerh0.bdf fallerh0.xtp\n" + "\n" + "Turnouts are composed of rails (which are Black) and lines. Structures are\n" + "composed of only lines. By default lines are Purple but you change this with\n" + "the -k optioon. The color is specified as a 6 digit hexidecimal value, where\n" + "the first 2 digits are the Red value, the middle 2 digits are the Green value\n" + "and the last 2 digits are the Blue value\n" + " ff0000 Red\n" + " 00ff00 Green\n" + " 00ffff Yellow\n" + ; /* NOTES: BDF files have a number of constructors for different types of turnouts @@ -89,9 +89,9 @@ Then the list of segments is written out to the output file. #define MAXSEG (40) /* Maximum number of segments in an object */ typedef struct { /* a co-ordinate */ - double x; - double y; - } coOrd; + double x; + double y; +} coOrd; FILE * fin; /* input file */ FILE * fout; /* output file */ @@ -107,8 +107,8 @@ long color = 0x00FF00FF;/* default color */ double normalizeAngle( double angle ) /* make sure <angle> is >= 0.0 and < 360.0 */ { - while (angle<0) angle += 360.0; - while (angle>=360) angle -= 360.0; + while (angle<0) { angle += 360.0; } + while (angle>=360) { angle -= 360.0; } return angle; } @@ -143,8 +143,8 @@ double findAngle( coOrd p0, coOrd p1 ) { double dx = p1.x-p0.x, dy = p1.y-p0.y; if (small(dx) && small(dy)) { - if (dy >=0.0) return 0.0; - else return 180.0; + if (dy >=0.0) { return 0.0; } + else { return 180.0; } } return R2D(atan2( dx,dy )); } @@ -152,156 +152,157 @@ double findAngle( coOrd p0, coOrd p1 ) /* Where do we expect each input line? */ typedef enum { - CLS_NULL, - CLS_START, - CLS_END, - CLS_BODY - } class_e; + CLS_NULL, + CLS_START, + CLS_END, + CLS_BODY +} class_e; /* Type of input line */ typedef enum { - ACT_UNKNOWN, - ACT_DONE, - ACT_STRAIGHT, - ACT_CURVE, - ACT_TURNOUT_LEFT, - ACT_TURNOUT_RIGHT, - ACT_CURVEDTURNOUT_LEFT, - ACT_CURVEDTURNOUT_RIGHT, - ACT_THREEWAYTURNOUT, - ACT_CROSSING_LEFT, - ACT_CROSSING_RIGHT, - ACT_DOUBLESLIP_LEFT, - ACT_DOUBLESLIP_RIGHT, - ACT_CROSSING_SYMMETRIC, - ACT_DOUBLESLIP_SYMMETRIC, - ACT_TURNTABLE, - ACT_ENDTURNTABLE, - ACT_TRANSFERTABLE, - ACT_ENDTRANSFERTABLE, - ACT_TRACK, - ACT_STRUCTURE, - ACT_ENDSTRUCTURE, - - ACT_FILL_POINT, - ACT_LINE, - ACT_CURVEDLINE, - ACT_CIRCLE, - ACT_DESCRIPTIONPOS, - ACT_ARTICLENOPOS, - ACT_CONNECTINGPOINT, - ACT_STRAIGHTTRACK, - ACT_CURVEDTRACK, - ACT_STRAIGHT_BODY, - ACT_CURVE_BODY, - ACT_PRICE - } action_e; + ACT_UNKNOWN, + ACT_DONE, + ACT_STRAIGHT, + ACT_CURVE, + ACT_TURNOUT_LEFT, + ACT_TURNOUT_RIGHT, + ACT_CURVEDTURNOUT_LEFT, + ACT_CURVEDTURNOUT_RIGHT, + ACT_THREEWAYTURNOUT, + ACT_CROSSING_LEFT, + ACT_CROSSING_RIGHT, + ACT_DOUBLESLIP_LEFT, + ACT_DOUBLESLIP_RIGHT, + ACT_CROSSING_SYMMETRIC, + ACT_DOUBLESLIP_SYMMETRIC, + ACT_TURNTABLE, + ACT_ENDTURNTABLE, + ACT_TRANSFERTABLE, + ACT_ENDTRANSFERTABLE, + ACT_TRACK, + ACT_STRUCTURE, + ACT_ENDSTRUCTURE, + + ACT_FILL_POINT, + ACT_LINE, + ACT_CURVEDLINE, + ACT_CIRCLE, + ACT_DESCRIPTIONPOS, + ACT_ARTICLENOPOS, + ACT_CONNECTINGPOINT, + ACT_STRAIGHTTRACK, + ACT_CURVEDTRACK, + ACT_STRAIGHT_BODY, + ACT_CURVE_BODY, + ACT_PRICE +} action_e; /* input line description */ typedef struct { - char * name; /* first token on line */ - class_e class; /* where do we expect this? */ - action_e action;/* what type of line is it */ - char *args; /* what else is on the line */ - } tokenDesc_t; + char * name; /* first token on line */ + class_e class; /* where do we expect this? */ + action_e action;/* what type of line is it */ + char *args; /* what else is on the line */ +} tokenDesc_t; /* first token on each line tells what kind of line it is */ tokenDesc_t tokens[] = { - { "Straight", CLS_START, ACT_STRAIGHT, "SSNN" }, - { "EndStraight", CLS_END, ACT_DONE, NULL }, - { "Curve", CLS_START, ACT_CURVE, "SSNNN" }, - { "EndCurve", CLS_END, ACT_DONE, NULL }, - { "Turnout_Left", CLS_START, ACT_TURNOUT_LEFT, "SSN" }, - { "Turnout_Right", CLS_START, ACT_TURNOUT_RIGHT, "SSN" }, - { "EndTurnout", CLS_END, ACT_DONE, NULL }, - { "CurvedTurnout_Left", CLS_START, ACT_CURVEDTURNOUT_LEFT, "SSN" }, - { "CurvedTurnout_Right", CLS_START, ACT_CURVEDTURNOUT_RIGHT, "SSN" }, - { "ThreeWayTurnout", CLS_START, ACT_THREEWAYTURNOUT, "SSN" }, - { "Crossing_Left", CLS_START, ACT_CROSSING_LEFT, "SSNNNN" }, - { "Crossing_Right", CLS_START, ACT_CROSSING_RIGHT, "SSNNNN" }, - { "DoubleSlip_Left", CLS_START, ACT_DOUBLESLIP_LEFT, "SSNNNNN" }, - { "DoubleSlip_Right", CLS_START, ACT_DOUBLESLIP_RIGHT, "SSNNNNN" }, - { "Crossing_Symetric", CLS_START, ACT_CROSSING_SYMMETRIC, "SSNNN" }, - { "DoubleSlip_Symetric", CLS_START, ACT_DOUBLESLIP_SYMMETRIC, "SSNNNN" }, - { "EndCrossing", CLS_END, ACT_DONE, NULL }, - { "Turntable", CLS_START, ACT_TURNTABLE, "SSNNNN" }, - { "EndTurntable", CLS_END, ACT_ENDTURNTABLE, NULL }, - { "TravellingPlatform", CLS_START, ACT_TRANSFERTABLE, "SSNNNNN" }, - { "EndTravellingPlatform", CLS_END, ACT_ENDTRANSFERTABLE, NULL }, - { "Track", CLS_START, ACT_TRACK, "SSN" }, - { "EndTrack", CLS_END, ACT_DONE, NULL }, - { "Structure", CLS_START, ACT_STRUCTURE, "SS" }, - { "EndStructure", CLS_END, ACT_ENDSTRUCTURE, NULL }, - - { "FillPoint", CLS_BODY, ACT_FILL_POINT, "NNI" }, - { "Line", CLS_BODY, ACT_LINE, "NNNN" }, - { "CurvedLine", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, - { "CurveLine", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, - { "Circle", CLS_BODY, ACT_CIRCLE, "NNN" }, - { "DescriptionPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, - { "ArticleNoPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, - { "ConnectingPoint", CLS_BODY, ACT_CONNECTINGPOINT, "NNN" }, - { "StraightTrack", CLS_BODY, ACT_STRAIGHTTRACK, "NNNN" }, - { "CurvedTrack", CLS_BODY, ACT_CURVEDTRACK, "NNNNN" }, - { "Straight", CLS_BODY, ACT_STRAIGHT_BODY, "N" }, - { "Curve", CLS_BODY, ACT_CURVE_BODY, "NNN" }, - { "Price", CLS_BODY, ACT_PRICE, "N" }, - - { "Gerade", CLS_START, ACT_STRAIGHT, "SSNN" }, - { "EndGerade", CLS_END, ACT_DONE, NULL }, - { "Bogen", CLS_START, ACT_CURVE, "SSNNN" }, - { "EndBogen", CLS_END, ACT_DONE, NULL }, - { "Weiche_links", CLS_START, ACT_TURNOUT_LEFT, "SSN" }, - { "Weiche_Rechts", CLS_START, ACT_TURNOUT_RIGHT, "SSN" }, - { "EndWeiche", CLS_END, ACT_DONE, NULL }, - { "Bogenweiche_Links", CLS_START, ACT_CURVEDTURNOUT_LEFT, "SSN" }, - { "Bogenweiche_Rechts", CLS_START, ACT_CURVEDTURNOUT_RIGHT, "SSN" }, - { "Dreiwegweiche", CLS_START, ACT_THREEWAYTURNOUT, "SSN" }, - { "Kreuzung_Links", CLS_START, ACT_CROSSING_LEFT, "SSNNNN" }, - { "Kreuzung_Rechts", CLS_START, ACT_CROSSING_RIGHT, "SSNNNN" }, - { "DKW_Links", CLS_START, ACT_DOUBLESLIP_LEFT, "SSNNNNN" }, - { "DKW_Rechts", CLS_START, ACT_DOUBLESLIP_RIGHT, "SSNNNNN" }, - { "Kreuzung_Symmetrisch", CLS_START, ACT_CROSSING_SYMMETRIC, "SSNNN" }, - { "DKW_Symmetrisch", CLS_START, ACT_DOUBLESLIP_SYMMETRIC, "SSNNNN" }, - { "EndKreuzung", CLS_END, ACT_DONE, NULL }, - { "Drehscheibe", CLS_START, ACT_TURNTABLE, "SSNNNN" }, - { "EndDrehscheibe", CLS_END, ACT_ENDTURNTABLE, NULL }, - { "Schiebebuehne", CLS_START, ACT_TRANSFERTABLE, "SSNNNNN" }, - { "EndSchiebebuehne", CLS_END, ACT_ENDTRANSFERTABLE, NULL }, - { "Schiene", CLS_START, ACT_TRACK, "SSN" }, - { "EndSchiene", CLS_END, ACT_DONE, NULL }, - { "Haus", CLS_START, ACT_STRUCTURE, "SS" }, - { "EndHaus", CLS_END, ACT_ENDSTRUCTURE, NULL }, - - { "FuellPunkt", CLS_BODY, ACT_FILL_POINT, "NNI" }, - { "Linie", CLS_BODY, ACT_LINE, "NNNN" }, - { "Bogenlinie", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, - { "Kreislinie", CLS_BODY, ACT_CIRCLE, "NNN" }, - { "BezeichnungsPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, - { "ArtikelNrPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, - { "Anschlusspunkt", CLS_BODY, ACT_CONNECTINGPOINT, "NNN" }, - { "GeradesGleis", CLS_BODY, ACT_STRAIGHTTRACK, "NNNN" }, - { "BogenGleis", CLS_BODY, ACT_CURVEDTRACK, "NNNNN" }, - { "Gerade", CLS_BODY, ACT_STRAIGHT_BODY, "N" }, - { "Bogen", CLS_BODY, ACT_CURVE_BODY, "NNN" }, - { "Preis", CLS_BODY, ACT_PRICE, "N" } }; + { "Straight", CLS_START, ACT_STRAIGHT, "SSNN" }, + { "EndStraight", CLS_END, ACT_DONE, NULL }, + { "Curve", CLS_START, ACT_CURVE, "SSNNN" }, + { "EndCurve", CLS_END, ACT_DONE, NULL }, + { "Turnout_Left", CLS_START, ACT_TURNOUT_LEFT, "SSN" }, + { "Turnout_Right", CLS_START, ACT_TURNOUT_RIGHT, "SSN" }, + { "EndTurnout", CLS_END, ACT_DONE, NULL }, + { "CurvedTurnout_Left", CLS_START, ACT_CURVEDTURNOUT_LEFT, "SSN" }, + { "CurvedTurnout_Right", CLS_START, ACT_CURVEDTURNOUT_RIGHT, "SSN" }, + { "ThreeWayTurnout", CLS_START, ACT_THREEWAYTURNOUT, "SSN" }, + { "Crossing_Left", CLS_START, ACT_CROSSING_LEFT, "SSNNNN" }, + { "Crossing_Right", CLS_START, ACT_CROSSING_RIGHT, "SSNNNN" }, + { "DoubleSlip_Left", CLS_START, ACT_DOUBLESLIP_LEFT, "SSNNNNN" }, + { "DoubleSlip_Right", CLS_START, ACT_DOUBLESLIP_RIGHT, "SSNNNNN" }, + { "Crossing_Symetric", CLS_START, ACT_CROSSING_SYMMETRIC, "SSNNN" }, + { "DoubleSlip_Symetric", CLS_START, ACT_DOUBLESLIP_SYMMETRIC, "SSNNNN" }, + { "EndCrossing", CLS_END, ACT_DONE, NULL }, + { "Turntable", CLS_START, ACT_TURNTABLE, "SSNNNN" }, + { "EndTurntable", CLS_END, ACT_ENDTURNTABLE, NULL }, + { "TravellingPlatform", CLS_START, ACT_TRANSFERTABLE, "SSNNNNN" }, + { "EndTravellingPlatform", CLS_END, ACT_ENDTRANSFERTABLE, NULL }, + { "Track", CLS_START, ACT_TRACK, "SSN" }, + { "EndTrack", CLS_END, ACT_DONE, NULL }, + { "Structure", CLS_START, ACT_STRUCTURE, "SS" }, + { "EndStructure", CLS_END, ACT_ENDSTRUCTURE, NULL }, + + { "FillPoint", CLS_BODY, ACT_FILL_POINT, "NNI" }, + { "Line", CLS_BODY, ACT_LINE, "NNNN" }, + { "CurvedLine", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, + { "CurveLine", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, + { "Circle", CLS_BODY, ACT_CIRCLE, "NNN" }, + { "DescriptionPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, + { "ArticleNoPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, + { "ConnectingPoint", CLS_BODY, ACT_CONNECTINGPOINT, "NNN" }, + { "StraightTrack", CLS_BODY, ACT_STRAIGHTTRACK, "NNNN" }, + { "CurvedTrack", CLS_BODY, ACT_CURVEDTRACK, "NNNNN" }, + { "Straight", CLS_BODY, ACT_STRAIGHT_BODY, "N" }, + { "Curve", CLS_BODY, ACT_CURVE_BODY, "NNN" }, + { "Price", CLS_BODY, ACT_PRICE, "N" }, + + { "Gerade", CLS_START, ACT_STRAIGHT, "SSNN" }, + { "EndGerade", CLS_END, ACT_DONE, NULL }, + { "Bogen", CLS_START, ACT_CURVE, "SSNNN" }, + { "EndBogen", CLS_END, ACT_DONE, NULL }, + { "Weiche_links", CLS_START, ACT_TURNOUT_LEFT, "SSN" }, + { "Weiche_Rechts", CLS_START, ACT_TURNOUT_RIGHT, "SSN" }, + { "EndWeiche", CLS_END, ACT_DONE, NULL }, + { "Bogenweiche_Links", CLS_START, ACT_CURVEDTURNOUT_LEFT, "SSN" }, + { "Bogenweiche_Rechts", CLS_START, ACT_CURVEDTURNOUT_RIGHT, "SSN" }, + { "Dreiwegweiche", CLS_START, ACT_THREEWAYTURNOUT, "SSN" }, + { "Kreuzung_Links", CLS_START, ACT_CROSSING_LEFT, "SSNNNN" }, + { "Kreuzung_Rechts", CLS_START, ACT_CROSSING_RIGHT, "SSNNNN" }, + { "DKW_Links", CLS_START, ACT_DOUBLESLIP_LEFT, "SSNNNNN" }, + { "DKW_Rechts", CLS_START, ACT_DOUBLESLIP_RIGHT, "SSNNNNN" }, + { "Kreuzung_Symmetrisch", CLS_START, ACT_CROSSING_SYMMETRIC, "SSNNN" }, + { "DKW_Symmetrisch", CLS_START, ACT_DOUBLESLIP_SYMMETRIC, "SSNNNN" }, + { "EndKreuzung", CLS_END, ACT_DONE, NULL }, + { "Drehscheibe", CLS_START, ACT_TURNTABLE, "SSNNNN" }, + { "EndDrehscheibe", CLS_END, ACT_ENDTURNTABLE, NULL }, + { "Schiebebuehne", CLS_START, ACT_TRANSFERTABLE, "SSNNNNN" }, + { "EndSchiebebuehne", CLS_END, ACT_ENDTRANSFERTABLE, NULL }, + { "Schiene", CLS_START, ACT_TRACK, "SSN" }, + { "EndSchiene", CLS_END, ACT_DONE, NULL }, + { "Haus", CLS_START, ACT_STRUCTURE, "SS" }, + { "EndHaus", CLS_END, ACT_ENDSTRUCTURE, NULL }, + + { "FuellPunkt", CLS_BODY, ACT_FILL_POINT, "NNI" }, + { "Linie", CLS_BODY, ACT_LINE, "NNNN" }, + { "Bogenlinie", CLS_BODY, ACT_CURVEDLINE, "NNNNN" }, + { "Kreislinie", CLS_BODY, ACT_CIRCLE, "NNN" }, + { "BezeichnungsPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, + { "ArtikelNrPos", CLS_BODY, ACT_DESCRIPTIONPOS, "NN" }, + { "Anschlusspunkt", CLS_BODY, ACT_CONNECTINGPOINT, "NNN" }, + { "GeradesGleis", CLS_BODY, ACT_STRAIGHTTRACK, "NNNN" }, + { "BogenGleis", CLS_BODY, ACT_CURVEDTRACK, "NNNNN" }, + { "Gerade", CLS_BODY, ACT_STRAIGHT_BODY, "N" }, + { "Bogen", CLS_BODY, ACT_CURVE_BODY, "NNN" }, + { "Preis", CLS_BODY, ACT_PRICE, "N" } +}; /* argument description */ typedef union { - char * string; - double number; - long integer; - } arg_t; + char * string; + double number; + long integer; +} arg_t; /* description of a curve */ typedef struct { - char type; - coOrd pos[2]; - double radius, a0, a1; - coOrd center; - } line_t; + char type; + coOrd pos[2]; + double radius, a0, a1; + coOrd center; +} line_t; /* state info for the current object */ int curAction; @@ -314,30 +315,30 @@ int right = 0; /* A XTrkCad End-Point */ typedef struct { - int busy; - coOrd pos; - double a; - } endPoint_t; + int busy; + coOrd pos; + double a; +} endPoint_t; endPoint_t endPoints[MAXSEG]; endPoint_t *endPoint_p; /* the segments */ typedef struct { - double radius; - coOrd pos[2]; - int mark; - endPoint_t * ep[2]; - } segs_t; + double radius; + coOrd pos[2]; + int mark; + endPoint_t * ep[2]; +} segs_t; segs_t segs[MAXSEG]; segs_t *seg_p; /* the segment paths */ typedef struct { - int index; - int count; - int segs[MAXSEG]; - } paths_t; + int index; + int count; + int segs[MAXSEG]; +} paths_t; paths_t paths[MAXSEG]; paths_t *paths_p; @@ -345,16 +346,18 @@ int curPath[MAXSEG]; int curPathInx; char * pathNames[] = { - "Normal", - "Reverse" }; + "Normal", + "Reverse" +}; int isclose( coOrd a, coOrd b ) { if ( fabs(a.x-b.x) < 0.1 && - fabs(a.y-b.y) < 0.1 ) + fabs(a.y-b.y) < 0.1 ) { return 1; - else + } else { return 0; + } } @@ -373,22 +376,25 @@ void searchSegs( segs_t * sp, int ep ) curPath[curPathInx] = (ep==0?-((sp-segs)+1):((sp-segs)+1)); if (sp->ep[ep] != NULL) { inx = abs(curPath[0]); - if ( (sp-segs)+1 < inx ) + if ( (sp-segs)+1 < inx ) { return; + } paths_p->index = 0; paths_p->count = curPathInx+1; - for (inx=0;inx<=curPathInx;inx++) + for (inx=0; inx<=curPathInx; inx++) { paths_p->segs[inx] = curPath[inx]; + } paths_p++; return; } curPathInx++; for ( sp1 = segs; sp1<seg_p; sp1++ ) { if (!sp1->mark) { - if ( isclose( sp->pos[ep], sp1->pos[0] ) ) + if ( isclose( sp->pos[ep], sp1->pos[0] ) ) { searchSegs( sp1, 1 ); - else if ( isclose( sp->pos[ep], sp1->pos[1] ) ) + } else if ( isclose( sp->pos[ep], sp1->pos[1] ) ) { searchSegs( sp1, 0 ); + } } } curPathInx--; @@ -424,8 +430,9 @@ void computePaths( void ) } } for ( sp = segs; sp<seg_p; sp++ ) { - for ( sp1 = segs; sp1<seg_p; sp1++ ) + for ( sp1 = segs; sp1<seg_p; sp1++ ) { sp1->mark = 0; + } curPathInx = 0; if ( sp->ep[0] ) { searchSegs( sp, 1 ); @@ -436,10 +443,11 @@ void computePaths( void ) pathIndex = 0; pathCount = paths_p-paths; while (pathCount>0) { - if (pathIndex < 2) + if (pathIndex < 2) { fprintf( fout, "\tP \"%s\"", pathNames[pathIndex] ); - else + } else { fprintf( fout, "\tP \"%d\"", pathIndex+1 ); + } pathIndex++; firstPath = 1; memset( bitmap, 0, sizeof bitmap ); @@ -447,8 +455,9 @@ void computePaths( void ) ep->busy = 0; } for (pp = paths; pp < paths_p; pp++) { - if (pp->count == 0) + if (pp->count == 0) { continue; + } segNo = pp->segs[0]; epNo = (segNo>0?0:1); ep = segs[abs(segNo)-1].ep[epNo]; @@ -458,12 +467,13 @@ void computePaths( void ) if ( (ep && ep->busy) || (ep2 && ep2->busy) ) { goto nextPath; } - if (ep) ep->busy = 1; - if (ep2) ep2->busy = 1; + if (ep) { ep->busy = 1; } + if (ep2) { ep2->busy = 1; } for (inx=0; inx<pp->count; inx++) { segNo = abs(pp->segs[inx]); - if (bitmap[segNo]) + if (bitmap[segNo]) { goto nextPath; + } } if (!firstPath) { fprintf( fout, " 0"); @@ -478,7 +488,7 @@ void computePaths( void ) pp->count = 0; pathCount--; nextPath: - ; + ; } fprintf( fout, "\n" ); } @@ -492,7 +502,8 @@ void translate( coOrd *res, coOrd orig, double a, double d ) } -static void computeCurve( coOrd pos0, coOrd pos1, double radius, coOrd * center, double * a0, double * a1 ) +static void computeCurve( coOrd pos0, coOrd pos1, double radius, coOrd * center, + double * a0, double * a1 ) /* translate between curves described by 2 end-points and a radius to a curve described by a center, radius and angles. */ @@ -502,8 +513,9 @@ static void computeCurve( coOrd pos0, coOrd pos1, double radius, coOrd * center, d = findDistance( pos0, pos1 )/2.0; a = findAngle( pos0, pos1 ); s = fabs(d/radius); - if (s > 1.0) + if (s > 1.0) { s = 1.0; + } aa = R2D(asin( s )); if (radius > 0) { aaa = a + (90.0 - aa); @@ -520,10 +532,11 @@ static void computeCurve( coOrd pos0, coOrd pos1, double radius, coOrd * center, double X( double v ) { - if ( -0.000001 < v && v < 0.000001 ) + if ( -0.000001 < v && v < 0.000001 ) { return 0.0; - else + } else { return v; + } } @@ -542,27 +555,27 @@ void generateTurnout( void ) computePaths(); for (ep=endPoints; ep<endPoint_p; ep++) fprintf( fout, "\tE %0.6f %0.6f %0.6f\n", - X(ep->pos.x), X(ep->pos.y), X(ep->a) ); + X(ep->pos.x), X(ep->pos.y), X(ep->a) ); for (lp=lines; lp<line_p; lp++) { switch (lp->type) { case 'L': fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); + X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); break; case 'A': fprintf( fout, "\tA %ld 0 %0.6f %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); + X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); break; } } for (sp=segs; sp<seg_p; sp++) if (sp->radius == 0.0) { fprintf( fout, "\tS 0 0 %0.6f %0.6f %0.6f %0.6f\n", - X(sp->pos[0].x), X(sp->pos[0].y), X(sp->pos[1].x), X(sp->pos[1].y) ); + X(sp->pos[0].x), X(sp->pos[0].y), X(sp->pos[1].x), X(sp->pos[1].y) ); } else { computeCurve( sp->pos[0], sp->pos[1], sp->radius, ¢er, &a0, &a1 ); fprintf( fout, "\tC 0 0 %0.6f %0.6f %0.6f %0.6f %0.6f\n", - X(sp->radius), X(center.x), X(center.y), X(a0), X(a1) ); + X(sp->radius), X(center.x), X(center.y), X(a0), X(a1) ); } fprintf( fout, "\t%s\n", END_SEGS ); } @@ -572,23 +585,25 @@ void reset( tokenDesc_t * tp, arg_t *args ) /* Start of a new turnout or structure */ { int inx; - curAction = tp->action; - line_p = lines; - seg_p = segs; - endPoint_p = endPoints; - partNo = strdup( args[0].string ); - name = strdup( args[1].string ); - for (inx=2; tp->args[inx]; inx++) - params[inx-2] = args[inx].number; + curAction = tp->action; + line_p = lines; + seg_p = segs; + endPoint_p = endPoints; + partNo = strdup( args[0].string ); + name = strdup( args[1].string ); + for (inx=2; tp->args[inx]; inx++) { + params[inx-2] = args[inx].number; + } } double getDim( double value ) /* convert to inches from tenths of a an inch or millimeters. */ { - if (inch) + if (inch) { return value/10.0; - else + } else { return value/25.4; + } } @@ -597,8 +612,9 @@ char * getLine( void ) { char * cp; while (1) { - if (fgets(line, sizeof line, fin) == NULL) + if (fgets(line, sizeof line, fin) == NULL) { return NULL; + } lineCount++; lineLen = strlen(line); if (lineLen > 0 && line[lineLen-1] == '\n') { @@ -619,10 +635,12 @@ char * getLine( void ) cp++; lineLen--; } - if (lineLen <= 0) + if (lineLen <= 0) { continue; - if (verbose) + } + if (verbose) { fprintf( fout, "# %s\n", line ); + } return cp; } } @@ -633,8 +651,9 @@ void flushInput( void ) { char *cp; while (cp=getLine()) { - if (strncasecmp( cp, "End", 3 ) == 0 ) + if (strncasecmp( cp, "End", 3 ) == 0 ) { break; + } } inBody = 0; } @@ -711,8 +730,9 @@ void process( tokenDesc_t * tp, arg_t *args ) endPoint_p->pos.x = 0.0; endPoint_p->a = 270.0; endPoint_p++; - if ((cp=getLine())==NULL) + if ((cp=getLine())==NULL) { return; + } if ((rc=sscanf( line, "%lf %lf", &radius, &angle ) ) != 2) { fprintf( stderr, "syntax error: %d: %s\n", lineCount, line ); flushInput(); @@ -728,8 +748,9 @@ void process( tokenDesc_t * tp, arg_t *args ) seg_p->radius = -radius; endPoint_p++; seg_p++; - if ((cp=getLine())==NULL) + if ((cp=getLine())==NULL) { return; + } if ((rc=sscanf( line, "%lf %lf", &radius2, &angle ) ) != 2) { fprintf( stderr, "syntax error: %d: %s\n", lineCount, line ); flushInput(); @@ -789,7 +810,7 @@ void process( tokenDesc_t * tp, arg_t *args ) seg_p++; length /= 2.0; if (tp->action == ACT_CROSSING_SYMMETRIC || - tp->action == ACT_DOUBLESLIP_SYMMETRIC) { + tp->action == ACT_DOUBLESLIP_SYMMETRIC) { length2 = length; } else { length2 = getDim( args[5].number )/2.0; @@ -805,7 +826,7 @@ void process( tokenDesc_t * tp, arg_t *args ) endPoint_p++; seg_p++; if (tp->action == ACT_CROSSING_RIGHT || - tp->action == ACT_DOUBLESLIP_RIGHT ) { + tp->action == ACT_DOUBLESLIP_RIGHT ) { endPoint_p[-1].pos.y = -endPoint_p[-1].pos.y; endPoint_p[-2].pos.y = -endPoint_p[-2].pos.y; seg_p[-1].pos[0].y = -seg_p[-1].pos[0].y; @@ -817,8 +838,9 @@ void process( tokenDesc_t * tp, arg_t *args ) case ACT_TURNTABLE: reset( tp, args ); - if ((cp=getLine())==NULL) + if ((cp=getLine())==NULL) { return; + } if ((rc=sscanf( line, "%lf %s", &angle, bits ) ) != 2) { fprintf( stderr, "syntax error: %d: %s\n", lineCount, line ); flushInput(); @@ -828,8 +850,9 @@ void process( tokenDesc_t * tp, arg_t *args ) count = 360.0/angle; angle = 0; length = strlen( bits ); - if (length < count) + if (length < count) { count = length; + } length = getDim( args[3].number ); length2 = getDim( args[5].number ); endNo = 1; @@ -843,24 +866,24 @@ void process( tokenDesc_t * tp, arg_t *args ) angle = normalizeAngle( 90.0 - inx * ( 360.0 / count ) ); if (bits[inx]!='0') fprintf( fout, "\tE %0.6f %0.6f %0.6f\n", - X(length * sin(D2R(angle))), - X(length * cos(D2R(angle))), - X(angle) ); + X(length * sin(D2R(angle))), + X(length * cos(D2R(angle))), + X(angle) ); } for ( inx=0; inx<count; inx++ ) { angle = normalizeAngle( 90.0 - inx * ( 360.0 / count ) ); if (bits[inx]!='0') fprintf( fout, "\tS 0 0 %0.6f %0.6f %0.6f %0.6f\n", - X(length * sin(D2R(angle))), - X(length * cos(D2R(angle))), - X(length2 * sin(D2R(angle))), - X(length2 * cos(D2R(angle))) ); + X(length * sin(D2R(angle))), + X(length * cos(D2R(angle))), + X(length2 * sin(D2R(angle))), + X(length2 * cos(D2R(angle))) ); } fprintf( fout, "\tA %ld 0 %0.6f 0.000000 0.000000 0.000000 360.000000\n", - color, length2 ); + color, length2 ); if (length != length2) fprintf( fout, "\tA %ld 0 %0.6f 0.000000 0.000000 0.000000 360.000000\n", - color, length ); + color, length ); break; case ACT_ENDTURNTABLE: @@ -868,11 +891,11 @@ void process( tokenDesc_t * tp, arg_t *args ) switch (lp->type) { case 'L': fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); + X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); break; case 'A': fprintf( fout, "\tA %ld 0 %0.6f %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); + X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); break; } } @@ -886,15 +909,18 @@ void process( tokenDesc_t * tp, arg_t *args ) width2 = getDim(args[5].number); length = getDim( args[6].number); fprintf( fout, "\tL %ld 0 0.0000000 0.000000 0.000000 %0.6f\n", color, length ); - fprintf( fout, "\tL %ld 0 0.0000000 %0.6f %0.6f %0.6f\n", color, length, width, length ); - fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f 0.000000\n", color, width, length, width ); + fprintf( fout, "\tL %ld 0 0.0000000 %0.6f %0.6f %0.6f\n", color, length, width, + length ); + fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f 0.000000\n", color, width, length, + width ); fprintf( fout, "\tL %ld 0 %0.6f 0.0000000 0.000000 0.000000\n", color, width ); fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f %0.6f\n", color, - (width-width2)/2.0, 0.0, (width-width2)/2.0, length ); + (width-width2)/2.0, 0.0, (width-width2)/2.0, length ); fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f %0.6f\n", color, - width-(width-width2)/2.0, 0.0, width-(width-width2)/2.0, length ); - if ((cp=getLine())==NULL) + width-(width-width2)/2.0, 0.0, width-(width-width2)/2.0, length ); + if ((cp=getLine())==NULL) { return; + } if ((rc=sscanf( line, "%lf %lf %s", &length2, &offset, bits ) ) != 3) { fprintf( stderr, "syntax error: %d: %s\n", lineCount, line ); flushInput(); @@ -905,14 +931,15 @@ void process( tokenDesc_t * tp, arg_t *args ) for (inx=0; bits[inx]; inx++) { if (bits[inx]=='1') { fprintf( fout, "\tE 0.000000 %0.6f 270.0\n", - offset ); + offset ); fprintf( fout, "\tS 0 0 0.000000 %0.6f %0.6f %0.6f\n", - offset, (width-width2)/2.0, offset ); + offset, (width-width2)/2.0, offset ); } offset += length2; } - if ((cp=getLine())==NULL) + if ((cp=getLine())==NULL) { return; + } if ((rc=sscanf( line, "%lf %lf %s", &length2, &offset, bits ) ) != 3) { fprintf( stderr, "syntax error: %d: %s\n", lineCount, line ); flushInput(); @@ -923,9 +950,9 @@ void process( tokenDesc_t * tp, arg_t *args ) for (inx=0; bits[inx]; inx++) { if (bits[inx]=='1') { fprintf( fout, "\tE %0.6f %0.6f 90.0\n", - width, offset ); + width, offset ); fprintf( fout, "\tS 0 0 %0.6f %0.6f %0.6f %0.6f\n", - width-(width-width2)/2.0, offset, width, offset ); + width-(width-width2)/2.0, offset, width, offset ); } offset += length2; } @@ -949,11 +976,11 @@ void process( tokenDesc_t * tp, arg_t *args ) switch (lp->type) { case 'L': fprintf( fout, "\tL %ld 0 %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); + X(lp->pos[0].x), X(lp->pos[0].y), X(lp->pos[1].x), X(lp->pos[1].y) ); break; case 'A': fprintf( fout, "\tA %ld 0 %0.6f %0.6f %0.6f %0.6f %0.6f\n", color, - X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); + X(lp->radius), X(lp->center.x), X(lp->center.y), X(lp->a0), X(lp->a1) ); break; } } @@ -981,7 +1008,8 @@ void process( tokenDesc_t * tp, arg_t *args ) angle = args[3].number/2.0 + args[4].number; pos1.x = pos0.x + length2*cos(D2R(angle)); pos1.y = pos0.y + length2*sin(D2R(angle)); - computeCurve( pos0, pos1, line_p->radius, &line_p->center, &line_p->a0, &line_p->a1 ); + computeCurve( pos0, pos1, line_p->radius, &line_p->center, &line_p->a0, + &line_p->a1 ); line_p++; break; @@ -1096,71 +1124,74 @@ void parse( void ) inch++; continue; } - for ( tp=tokens; tp<&tokens[COUNT( tokens )]; tp++ ){ + for ( tp=tokens; tp<&tokens[COUNT( tokens )]; tp++ ) { tlen = strlen(tp->name); - if ( strncasecmp( cp, tp->name, tlen) != 0 ) + if ( strncasecmp( cp, tp->name, tlen) != 0 ) { continue; - if ( cp[tlen] != '\0' && cp[tlen] != ' ' && cp[tlen] != ',' ) + } + if ( cp[tlen] != '\0' && cp[tlen] != ' ' && cp[tlen] != ',' ) { continue; + } if ( (inBody) == (tp->class==CLS_START) ) { continue; } cp += tlen+1; if (tp->args) - for ( inx=0, sp=strings; tp->args[inx]; inx++ ) { - if (*cp == '\0') { - fprintf( stderr, "%d: unexpected end of line\n", lineCount ); - goto nextLine; - } - switch( tp->args[inx] ) { - case 'S': - args[inx].string = sp; - while (isspace((unsigned char)*cp)) cp++; - if (*cp != '"') { - fprintf( stderr, "%d: expected a \": %s\n", lineCount, cp ); + for ( inx=0, sp=strings; tp->args[inx]; inx++ ) { + if (*cp == '\0') { + fprintf( stderr, "%d: unexpected end of line\n", lineCount ); goto nextLine; } - cp++; - while ( *cp ) { - if ( *cp != '"' ) { - *sp++ = *cp++; - } else if ( cp[1] == '"' ) { - *sp++ = '"'; - *sp++ = '"'; - cp += 2; - } else { - cp++; - *sp++ = '\0'; - break; + switch( tp->args[inx] ) { + case 'S': + args[inx].string = sp; + while (isspace((unsigned char)*cp)) { cp++; } + if (*cp != '"') { + fprintf( stderr, "%d: expected a \": %s\n", lineCount, cp ); + goto nextLine; } - } - break; + cp++; + while ( *cp ) { + if ( *cp != '"' ) { + *sp++ = *cp++; + } else if ( cp[1] == '"' ) { + *sp++ = '"'; + *sp++ = '"'; + cp += 2; + } else { + cp++; + *sp++ = '\0'; + break; + } + } + break; - case 'N': - args[inx].number = strtod( cp, &cpp ); - if (cpp == cp) { - fprintf( stderr, "%d: expected a number: %s\n", lineCount, cp ); - goto nextLine; - } - cp = cpp; - break; + case 'N': + args[inx].number = strtod( cp, &cpp ); + if (cpp == cp) { + fprintf( stderr, "%d: expected a number: %s\n", lineCount, cp ); + goto nextLine; + } + cp = cpp; + break; + + case 'I': + args[inx].integer = strtol( cp, &cpp, 10 ); + if (cpp == cp) { + fprintf( stderr, "%d: expected an integer: %s\n", lineCount, cp ); + goto nextLine; + } + cp = cpp; + break; - case 'I': - args[inx].integer = strtol( cp, &cpp, 10 ); - if (cpp == cp) { - fprintf( stderr, "%d: expected an integer: %s\n", lineCount, cp ); - goto nextLine; } - cp = cpp; - break; - } - } process( tp, args ); - if (tp->class == CLS_START) + if (tp->class == CLS_START) { inBody = 1; - else if (tp->class == CLS_END) + } else if (tp->class == CLS_END) { inBody = 0; + } tp = NULL; break; } @@ -1222,7 +1253,8 @@ int main ( int argc, char * argv[] ) perror(*argv); exit(1); } - if (contents) + if (contents) { fprintf( fout, "CONTENTS %s\n", contents ); + } parse(); } |