/* FILE for Borland. It works, sortof. Input data must all be integers. Executing to_pc < xxxx.di will show xxxx on your screen. So far it responds to m and l commands only. Anybody who wants to improve it is welcome. */ #include #include #include #include #include #define ERRORFILE stderr /* The next thing is specific to the University installation. For your home computer, you will have to determine the location of the BGI directory, and put the right thing in here: */ #define BGIHOME "H:\\Win98\\Apps\\BC5\\BGI" typedef struct { int x_coord; int y_coord; } point; void set_clip_region(point p,point q) { ; } void comment_print(char text[]) { } void textprint_large(char text[]) { outtext(text); } void textprint_medium(char text[]) { textprint_large(text); } void textprint_small(char text[]) { textprint_large(text); } void end_drawing(void) { getchar(); getchar(); /* closegraph(); */ } void begin_drawing(void) { int g_driver, g_mode, g_error; detectgraph(&g_driver,&g_mode); if (g_driver < 0) { printf("\n\nNo graphics hardware detected!!\n\n"); exit(1); } if (g_mode==EGAHI) g_mode=EGALO; initgraph(&g_driver,&g_mode,BGIHOME); g_error = graphresult(); if (g_error!=grOk) { printf("\n\nGraphics initialization error: %s\n\n", grapherrormsg(g_error)); exit(1); } /* setaspectratio(9000,10000); */ } void set_width(int width) { int g_error; setlinestyle(SOLID_LINE,width,0); g_error = graphresult(); if (g_error<0) { closegraph(); printf("\n\nLine width error: %s\n\n", grapherrormsg(g_error)); exit(1); } } void move_to(double x, double y) { int i, j; if (x>0.0 && y>0.0 && x<1.0 && y<1.0) { i = (int) (x * getmaxx()); j = (int) (y * getmaxy()); moveto(i,j); } } void makeline(double x,double y,double u, double v) { int i, j, k, l; if (x>0.0 && y>0.0 && u>0.0 && v>0.0 && x<1.0 && y<1.0 && u<1.0 && v<1.0) { k = (int) (x * getmaxx()); l = (int) (y * getmaxy()); i = (int) (u * getmaxx()); j = (int) (v * getmaxy()); line(k,l,i,j); } } void error_1(int N,char in_filename[]) { /* fprintf(ERRORFILE,"\nSuccessfully read "); fprintf(ERRORFILE,"%d lines of data from \"%s\"\n",N,in_filename); */ } void error_w1(int N,char in_filename[]) { closegraph(); printf("\nWindow error in line %d. Window must have non-zero",N); printf(" width and height.\nProgram terminated.\n"); } void error_r0(int N,char in_filename[]) { closegraph(); printf("\nRegion error in line %d. Must give four region",N); printf(" coordinates.\nProgram terminated.\n"); } void error_w0(int N,char in_filename[]) { closegraph(); printf("\nWindow error in line %d. Must give four window",N); printf(" coordinates.\nProgram terminated.\n"); } void error_l(int N,char in_filename[]) { closegraph(); printf("\nLine error in line %d. Must give two line",N); printf(" coordinates.\nProgram terminated.\n"); } void error_l1(int N,char in_filename[]) { closegraph(); printf("\nError in line %d. You cannot draw a line",N); printf(" until you have moved at least once.\nProgram terminated.\n"); } void error_m(int N,char in_filename[]) { closegraph(); printf("\nMove error in line %d. Must give two move",N); printf(" coordinates.\nProgram terminated.\n"); } void error_W(int N,char in_filename[]) { closegraph(); printf("\nWidth error in line %d. Must give a single integral",N); printf(" coordinate.\nProgram terminated.\n"); } void main(void) { int no_input_errors = 1; int ok_to_draw_line = 0; int N,Q; char *in_filename = "standard input", out_filename[50]; char text[100]; char current_line[100]; FILE *fp_in; int fclose(); point lower_left, upper_right, p, q, ttimes(); point r, s; double x,y,u,v; fp_in = stdin; lower_left.x_coord = 0.0; lower_left.y_coord = 0.0; upper_right.x_coord = 1.0; upper_right.y_coord = 1.0; begin_drawing(); N=0; while (no_input_errors) { q = p; u = x; v = y; if (N>0) { Q = fscanf(fp_in,"\n"); } Q = fscanf(fp_in,"%[^\n]",current_line); if (Q <= 0) {error_1(N,in_filename); break;} switch(current_line[0]) { case '%'/*comment*/: /* fprintf(ERRORFILE,"Line %d treated as comment.\n",N+1); */ Q = sscanf(current_line,"%*c%[^\n]",text); comment_print(text); break; case 't'/*text, small*/: Q = sscanf(current_line,"t%[^\n]",text); textprint_small(text); break; case 'x'/*text, medium*/: Q = sscanf(current_line,"x%[^\n]",text); textprint_medium(text); break; case 'T'/*text, large*/: Q = sscanf(current_line,"T%[^\n]",text); textprint_large(text); break; case 'w'/*window*/: Q = sscanf(current_line,"w%d%d%d%d", &lower_left.x_coord,&lower_left.y_coord, &upper_right.x_coord,&upper_right.y_coord); if (Q <= 3) {error_w0(N+1,in_filename); no_input_errors=0;} if ( (lower_left.x_coord==upper_right.x_coord) || (lower_left.y_coord==upper_right.y_coord)) {error_w1(N+1,in_filename); no_input_errors=0;} break; case 'W'/*Width*/: { int width; Q = sscanf(current_line,"W%d",&width); if (Q <= 0) {error_W(N+1,in_filename); no_input_errors=0; break;} set_width(width); } break; case 'm'/*move*/: ok_to_draw_line = 1; Q = sscanf(current_line,"m%d%d",&p.x_coord,&p.y_coord); if (Q <= 1) {error_m(N+1,in_filename); no_input_errors=0; break;} x = ((double)(p.x_coord-lower_left.x_coord)) / ((double)(upper_right.x_coord-lower_left.x_coord)); y= ((double)(p.y_coord-lower_left.y_coord)) / ((double)(upper_right.y_coord-lower_left.y_coord)); y = 1.0 - y; move_to(x,y); break; case 'l'/*line*/: if (!ok_to_draw_line){error_l1(N+1,in_filename); no_input_errors=0; break;} Q = sscanf(current_line,"l%d%d",&p.x_coord,&p.y_coord); if (Q <= 1) {error_l(N+1,in_filename); no_input_errors=0; break;} x = ((double)(p.x_coord-lower_left.x_coord)) / ((double)(upper_right.x_coord-lower_left.x_coord)); y= ((double)(p.y_coord-lower_left.y_coord)) / ((double)(upper_right.y_coord-lower_left.y_coord)); y = 1.0 - y; makeline(x,y,u,v); break; default: fprintf(ERRORFILE,"Data of unknown type in line "); fprintf(ERRORFILE,"%d; ignored up to newline.\n",N+1); break; } N++; } fclose(fp_in); end_drawing(); }