8 long int linecount=0,pointcount=0;
9 char line_feed[3]="\n";
10 void putreal(FILE *f,double x)
12 if (fabs(x)>1000000000.0||fabs(x)<0.01)
16 fprintf(f,"%13.10f",x);
19 fprintf(f,"%13.8f",x);
20 else if(fabs(x)<1000000.0)
21 fprintf(f,"%13.5f",x);
23 fprintf(f,"%13.2f",x);
25 void putpoint(FILE *f,DGT *dgt,POINT p)
26 { putreal(f,real_x(dgt,p.x));
28 putreal(f,real_y(dgt,p.y));
32 void putline(DGT* dgt)
34 fprintf(lines,"%ld%s",dgt_id(dgt),line_feed);
35 for(i=0;i<dgt_line_len(dgt);i++)
36 putpoint(lines,dgt,dgt_node(dgt,i));
37 fprintf(lines,"END%s",line_feed);
41 void putlabel(DGT *dgt)
42 { fprintf(points,"%ld ",dgt_id(dgt));
43 putpoint(points,dgt,dgt_point(dgt));
46 void close_gen(const char *name,FILE *f,int count)
48 {fprintf(f,"END%s",line_feed);
50 } else { fclose(f); unlink(name);}
52 void do_nothing(char c)
55 void print_count(char c)
56 { fprintf(stderr,"Exported %ld lines,%ld labels%c",linecount,pointcount,c);
59 void (*show_done)(char c)=do_nothing;
61 {printf("Usage:\n\t dgt2gen [-o filename] [-r] [-v] file\n"
62 "\t -o filename gives template for output file names (suffixess\n\t\tgen and gpn would be appended"
63 "\t -r forces MS-DOS like linefeeds (\\r\\n instead of just \\n\n"
64 "\t -v reports progress after each item converted\n");
68 void write_item(DGT *dgt)
69 { if (dgt_is_line(dgt)) putline(dgt); else putlabel(dgt);
72 int main (int argc,char **argv)
73 { char pointname[1024]="",linename[1024]="";
74 struct option longopt[]={
75 {"output-file",1,0,'o'},
78 {"dos-linefeeds",0,0,'r'},
84 while((c=getopt_long(argc,argv,"o:rv",longopt,&index))!=-1)
86 { case 2: show_version("dgt2gen","$Revision: 1.1 $");
87 case 'r': strcpy(line_feed,"\r\n");break;
88 case 'v': show_done=print_count;break;
89 case 'o': strcpy(pointname,force_ext(optarg,".gpn"));
90 strcpy(linename,force_ext(optarg,".gen"));
94 default: if (c!=1) fprintf(stderr,"Invalid options\n");
98 if (argc==optind) {fprintf(stderr,"No file name supplied\n");
102 if (!(d=open_dgt(default_ext(argv[optind],".dgt"))))
103 {fprintf(stderr,"Cannot open file:%s\n",default_ext(argv[optind],".dgt"));
107 { strcpy(linename,force_ext(argv[optind],".gen"));
108 strcpy(pointname,force_ext(argv[optind],".gpn"));
110 if(!(lines=fopen(linename,"wb")))
111 {fprintf(stderr,"Cannot create file:%s\n",linename);
114 if(!(points=fopen(pointname,"wb")))
115 { fprintf(stderr,"Cannot create file:%s\n",pointname);
119 for_each_item(d,write_item);
121 close_gen(linename,lines,linecount);
122 close_gen(pointname,points,pointcount);