]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/dgt2gen.c
First checked in version
[oss/fgis.git] / epu / dgt2gen.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include "dgt.h"
4 #include "math.h"
5 #include "eppl_ut.h"
6 #include <getopt.h>
7 FILE *lines,*points;
8 long int linecount=0,pointcount=0;
9 char line_feed[3]="\n";
10 void putreal(FILE *f,double x)
11
12   if (fabs(x)>1000000000.0||fabs(x)<0.01)
13    fprintf(f,"%13E",x);
14   else
15   if (fabs(x)<10.0)
16    fprintf(f,"%13.10f",x);
17   else
18   if (fabs(x)<1000.0)
19    fprintf(f,"%13.8f",x);
20   else if(fabs(x)<1000000.0)
21    fprintf(f,"%13.5f",x);
22   else
23    fprintf(f,"%13.2f",x);
24 }
25 void putpoint(FILE *f,DGT *dgt,POINT p)
26 { putreal(f,real_x(dgt,p.x));
27   fputc(' ',f);
28   putreal(f,real_y(dgt,p.y));
29   fputs(line_feed,f);
30 }
31
32 void putline(DGT* dgt)
33 {int i;
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);
38  linecount++;
39 }
40
41 void putlabel(DGT *dgt)
42 { fprintf(points,"%ld ",dgt_id(dgt));
43   putpoint(points,dgt,dgt_point(dgt));
44   pointcount++;
45 }
46 void close_gen(const char *name,FILE *f,int count)
47 {if (count)
48  {fprintf(f,"END%s",line_feed);
49   fclose(f);
50  } else { fclose(f); unlink(name);}
51 }
52 void do_nothing(char c)
53 {
54 }
55 void print_count(char c)
56 { fprintf(stderr,"Exported %ld lines,%ld labels%c",linecount,pointcount,c);
57   fflush(stderr);
58 }
59 void (*show_done)(char c)=do_nothing;
60 void help(void)
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");
65
66  
67
68 void write_item(DGT *dgt)
69 { if (dgt_is_line(dgt)) putline(dgt); else putlabel(dgt);
70   show_done('\r');
71 }
72 int main (int argc,char **argv)
73 { char pointname[1024]="",linename[1024]="";
74   struct option longopt[]={
75                     {"output-file",1,0,'o'},
76                     {"help",0,0,1},
77                     {"version",0,0,2},
78                     {"dos-linefeeds",0,0,'r'},
79                     {"verbose",0,0,'v'},
80                     {NULL,0,0,0},
81                   };
82   int c,index;
83   DGT *d;
84   while((c=getopt_long(argc,argv,"o:rv",longopt,&index))!=-1)
85    switch(c)
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"));
91                break;
92      case '1':
93      case '?':
94      default: if (c!=1) fprintf(stderr,"Invalid options\n");
95               help();
96               exit(c!=1);
97    }
98    if (argc==optind) {fprintf(stderr,"No file name supplied\n");
99                       help();
100                       exit(1);
101                      }
102    if (!(d=open_dgt(default_ext(argv[optind],".dgt"))))
103    {fprintf(stderr,"Cannot open file:%s\n",default_ext(argv[optind],".dgt"));
104     exit(1);
105    }
106   if (!*linename)
107   { strcpy(linename,force_ext(argv[optind],".gen"));  
108     strcpy(pointname,force_ext(argv[optind],".gpn"));
109   }
110   if(!(lines=fopen(linename,"wb")))
111   {fprintf(stderr,"Cannot create file:%s\n",linename);
112    exit(1);
113   }
114   if(!(points=fopen(pointname,"wb")))
115   { fprintf(stderr,"Cannot create file:%s\n",pointname);
116     fclose(lines);
117     unlink(linename);
118   }
119   for_each_item(d,write_item);
120   close_dgt(d);
121   close_gen(linename,lines,linecount);
122   close_gen(pointname,points,pointcount);
123   print_count('\n');
124   return (0);
125 }   
126                      
127              
128