]> www.wagner.pp.ru Git - oss/fgis.git/blob - epu/window.c
First checked in version
[oss/fgis.git] / epu / window.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <ctype.h>
6 #include "epp.h"
7 #include "eppl_ut.h"
8 EPP *old,*new;
9 int recalc=0;
10 int copy_cell(int col,int row,int value)
11 { return epp_get(old,col,row);
12 }
13 void help(int exitcode)
14 { printf("Usage window [-a%%] input_file fr lr fc lc [output_file]\n"
15          "coordinates are given in rows/cols unless -a option specified\n");
16  exit(exitcode);
17 }
18 void error(char *msg)
19 { fprintf(stderr,"%s\n",msg);
20   exit(1);
21 }
22 int get_coord_alt(char *param,int (*convert)(EPP *,double))
23 { double tmp;
24   char *endptr;
25   tmp=strtod(param,&endptr);
26   if (*endptr) error("Invalid coordinate");
27   return convert(old,tmp);
28 }
29 int get_coord_int(char *param,int (*convert)(EPP *,double))
30 { int tmp;
31   char *endptr;
32   tmp=strtol(param,&endptr,0);
33   if (*endptr) error("Invalid coordinate");
34   return tmp;
35 }
36
37 int main(int argc,char **argv)
38 {char outname[1024]="window.out.epp";
39  int (*get_coord)(char *,int (*)(EPP *,double))=get_coord_int;
40  int x1,y1,x2,y2;
41  int c,index,nonopt=1,verbose=0,result;
42   for(index=1;index<argc;index++)
43   if (argv[index][0]=='-'&&!(isdigit(c=argv[index][1])))
44   switch(c)
45   {case 'a':recalc=1;get_coord=get_coord_alt;break;
46    case '%':verbose=1;break;
47    case 'o':strcpy(outname,default_ext(argv[++index],".epp"));break;
48    case '-':if (!strcmp(argv[index],"--version")) 
49              show_version("window","$Revision: 1.1 $");
50             else if (!strcmp(argv[index],"--help")) help(0);
51    case 'h':
52    default: help(c!='h');
53   } 
54  else
55  switch (nonopt++)
56  {case 1:
57  if (!(old=open_epp(argv[index]))) error("Cannot open input file");
58  break;
59  case 2: 
60 y1=get_coord(argv[index],epp_row);break;
61  case 3:
62  y2=get_coord(argv[index],epp_row);break;
63  case 4:
64  x1=get_coord(argv[index],epp_col);break;
65  case 5:
66  x2=get_coord(argv[index],epp_col);break;
67  default:error("Extra parameters in command line");
68  }
69  if (nonopt!=6) help(1);
70  if (!recalc) y2++;
71  if (y1>=y2) error("Negative number of rows"); 
72  if (!recalc) x2++;
73  if (x1>=x2) error("Negative number of columns");
74  Create16bit=old->kind==16;
75  if(!(new=creat_epp(outname,x1,y1,x2-1,y2-1,
76                alt_x(old,x1),alt_y(old,y1),alt_x(old,x2),alt_y(old,y2),
77                100,0,old->offsite))) error("Cannot create output file");
78  install_progress_indicator(verbose?show_percent:check_int); 
79  result=clear_progress(for_each_cell(new,copy_cell));
80  close_epp(old);
81
82  close_epp(new);
83  if (result) unlink(outname);
84  return -result;
85 }