10 unsigned short int outclass;
11 unsigned short int classes[32];
13 typedef struct record *rec;
15 void help(int exitcode)
16 { printf("Usage: intable [-%%] [-i] [-o file] [-f file] [-O value] [-u value] files\n");
23 char skip[32]={0,0,0,0,0,0,0,0,
27 int files_count,count,limit,delta;
34 int compare(struct record *r1, struct record *r2)
36 for (i=0;i<files_count;i++)
37 { if (r1->classes[i]>r2->classes[i]) return 1;
39 if (r1->classes[i]<r2->classes[i]) return -1;
44 int search(struct record *key,int *index)
45 {int l=0,h=count-1,i,c;
48 c=compare((struct record *)(table+(1+files_count)*i),key);
52 if (!c) { *index=i;return 1;}
58 void insert(struct record *key,int index)
59 { int i;unsigned short int *l;
62 table=realloc(table,(limit+=delta)*(1+files_count)*sizeof(short int));
64 { fprintf(stderr,"Couldn't realloc table. Table limit is %d\n",limit);
69 for(i=count;i>index;i--)
70 memcpy(table+(i)*(1+files_count),table+(i-1)*(1+files_count),
71 (1+files_count)*sizeof(short int));
72 l=(unsigned short int *)(table+index*(1+files_count));
73 memcpy(l,key,(files_count+1)*sizeof(short int));
76 void setvalue(int index,int value)
78 unsigned short int *l=(unsigned short int *)(table+index*(1+files_count));
82 int add_record(FILE *f)
83 { /* ÷ÏÚ×ÒÁÝÁÅÔ 0, ÅÓÌÉ ÔÁËÁÑ ËÏÍÂÉÎÁÃÉÑ ÕÖÅ ÅÓÔØ*/
88 endptr=fgets(line,256,f);
89 if (!endptr) return 1;
91 for(i=0,j=0;i<files_count+skip_count;i++)
92 { v=strtol(endptr,&endptr,0);
93 while(*endptr==' '||*endptr=='\t') endptr++;
94 if (*endptr!=delimiter&&!(isdigit(*endptr)&&delimiter==' '))
95 { fprintf(stderr,"Invalid line %d in table. Error at char %d\n",
99 if (*endptr==delimiter)
102 { fprintf(stderr,"Not enough values in line %d:%d instead of%d\n",
103 lineno,i+1,files_count+1);
106 /* if ((v<files[i]->min||v>files[i]->max)&&v!=files[i]->offsite) */
108 { fprintf(stderr,"Illegal value %d in column %d of line %d\n",
115 v=strtol(endptr,&endptr,0);
116 while (*endptr==' '||*endptr=='\t')endptr++;
118 { fprintf(stderr,"Extra data in line %d\n",lineno);
122 { fprintf(stderr,"Illegal value %d in column %d of line %d\n",
123 v,files_count+1,lineno);
127 if (v>255) Create16bit=1;
129 { setvalue(i,v); return 0;}
131 { insert(&tmp,i); return 1;}
134 int make_cell(int x,int y,int value)
137 for(i=0,epp=files;i<files_count;tmp.classes[i++]=epp_get(*(epp++),x,y));
139 return *((unsigned short int *)(table+i*(1+files_count)));
144 int getclass(const char *s)
147 int tmp=strtol(s,&endptr,0);
148 if (*endptr||tmp<0||tmp>65535)
149 { fprintf(stderr,"Invalid class value %s\n",s);
155 int main(int argc,char **argv)
156 { struct option long_options[]={
160 {"delimiter",1,0,'d'},
161 {"input-file",1,0,'f'},
162 {"output-file",1,0,'o'},
164 {"unmatched",1,0,'u'},
166 {"ignore-dupes",0,0,'i'},
169 char outname[1024]="intable.out.epp";
171 int ignore_dupes=0,verbose=0;
174 while ((c=getopt_long(argc,argv,"%iu:d:f:o:O:129456789",
175 long_options,&index))!=-1)
177 {case 2:show_version("intable","$Revision: 1.1 $");
178 case '%':verbose=1;break;
179 case 'd':delimiter=optarg[0];break;
180 case 'f':if (!(f=fopen(optarg,"r")))
181 { fprintf(stderr,"Cannot open file %s\n",optarg);
184 case 'o':strcpy(outname,default_ext(optarg,".epp"));break;
185 case 'O':offsite=getclass(optarg);
187 case 'u':unmatched=getclass(optarg);break;
188 case 'i':ignore_dupes=1;break;
190 case '1': case '2': case '3': case '4': case '5': case '6': case '7':
195 case 3:{char *endptr;
196 i=strtol(optarg,&endptr,0);
197 if (*endptr||i<1||i>32)
198 { fprintf(stderr,"Invalid column number %s\n",optarg);
210 files_count=argc-optind;
211 table=malloc(65536*sizeof(short)*(files_count+1));
216 for(i=0,index=optind;i<files_count;i++,index++,epp++)
217 {if (!(*epp=open_epp(default_ext(argv[index],".epp"))))
218 { fprintf(stderr,"Cannot open file %s\n",default_ext(argv[index],".epp"));
221 if(i&&!is_aligned(*files,*epp))
222 { fprintf(stderr,"File %s is incompatible with %s\n",
223 argv[optind],argv[index]);
228 { if(!ignore_dupes && !add_record(f))
229 {fprintf(stderr,"Duplicated lines in table\n");
233 printf("%d lines read. %d values used\n",lineno,count);
234 if (offsite==-1) offsite=Create16bit?65535:255;
235 if (unmatched==-1) unmatched=offsite;
236 if (!(new_file=creat_epp_as(outname,*files)))
237 { fprintf(stderr,"Cannot create file %s\n",outname);
240 new_file->offsite=offsite;
241 install_progress_indicator(verbose?show_percent:check_int);
242 if ((i=clear_progress(for_each_cell(new_file,make_cell))))
244 if (!Create16bit||new_file->max>255)
248 fast_convert_to_8bit(new_file,outname);