File: epplib.c

package info (click to toggle)
mapserver 4.10.0-5%2Betch1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 11,960 kB
  • ctags: 15,165
  • sloc: ansic: 164,837; python: 5,770; java: 5,169; perl: 2,388; cpp: 1,603; makefile: 735; lex: 507; sh: 375; yacc: 317; tcl: 158; cs: 85; ruby: 53
file content (449 lines) | stat: -rwxr-xr-x 10,702 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/******************************************************************************
 *
 * Project:  MapServer
 * Purpose:  Low level EPP file access, supports drawEPP() in mapraster.c.
 * Author:   Steve Lime and the MapServer team.
 *
 ******************************************************************************
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies of this Software or works derived from this Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 *
 * $Log: epplib.c,v $
 * Revision 1.4  2006/08/22 23:11:08  hobu
 * cast the first argument of the call to swap2 on line 231 to
 * short * as the function prototype expects
 *
 * Revision 1.3  2005/06/14 16:03:33  dan
 * Updated copyright date to 2005
 *
 * Revision 1.2  2004/10/21 04:30:54  frank
 * Added standardized headers.  Added MS_CVSID().
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "epplib.h"
#include "map.h"

MS_CVSID("$Id: epplib.c,v 1.4 2006/08/22 23:11:08 hobu Exp $")

static int REVERSE; /* set to 1 on bigendian machines */

void swap2(short *x, int count)
{
  int n;
  char tmp;
  for (n=0; n<count; n++) {
    tmp=*(char *)x;
    *(char *)x=*(((char *)x)+1);
    *(((char *)x)+1)=tmp;
    x++;
  }
}

void swap4(long *x, int count)
{
  int n;
  char tmp;
  for (n=0; n<count; n++) {
    tmp=*(char *)x;
    *(char *)x=*(((char *)x)+3);
    *(((char *)x)+3)=tmp;
    tmp=*(((char *)x)+1);
    *(((char *)x)+1)=*(((char *)x)+2);
    *(((char *)x)+2)=tmp;
    x++;
  }
}

void swap8(double *x, int count)
{
  int n;
  char tmp;
  for (n=0; n<count; n++) {
    tmp=*(char *)x;
    *(char *)x=*(((char *)x)+7);
    *(((char *)x)+7)=tmp;
    tmp=*(((char *)x)+1);
    *(((char *)x)+1)=*(((char *)x)+6);
    *(((char *)x)+6)=tmp;
    tmp=*(((char *)x)+2);
    *(((char *)x)+2)=*(((char *)x)+5);
    *(((char *)x)+5)=tmp;
    tmp=*(((char *)x)+3);
    *(((char *)x)+3)=*(((char *)x)+4);
    *(((char *)x)+4)=tmp;
    x++;
  }
}

static char getrow(eppfile *epp,rowptr buff)
{
  rowptr rp;
  char *cp;
  int col,ncol,nc,n,v;
  ncol=epp->lc-epp->fc+1;
  rp=buff+1;
  for (col=0; col<ncol; col+=nc) {
    if (epp->fp-epp->fptr>4095) {
      memcpy(epp->fptr,epp->fptr+4096,256);
      epp->fp-=4096;
      nc=fread(epp->fptr+256,1,4096,epp->fil);
      /* eof check ?? */
      }
    nc=*epp->fp++;
    if (nc!=0) {
      v=*epp->fp++;
      for (n=0; n<nc; n++) *rp++=v;
    } else {
      nc=*epp->fp++;
      for (n=0; n<nc; n++) *rp++=*epp->fp++;
    }
  }
  if (epp->kind==16 && col==ncol) {
    if (REVERSE) cp=((char *)(epp->rptr+1))+2;  /* pick up high half */
    else cp=((char *)(epp->rptr+1))+3;
    for (col=0; col<ncol; col+=nc) {
      if (epp->fp-epp->fptr>4095) {
	memcpy(epp->fptr,epp->fptr+4096,256);
	epp->fp-=4096;
	nc=fread(epp->fptr+256,1,4096,epp->fil);
	/* eof check ?? */
	}
      nc=*epp->fp++;
      if (nc!=0) {
	v=*epp->fp++;
	for (n=0; n<nc; n++) {
	  *cp=v;
	  cp+=4;
	}
      } else {
	nc=*epp->fp++;
	for (n=0; n<nc; n++) {
	  *cp=*epp->fp++;
	  cp+=4;
	}
      }
    }
  }
  return (ncol==col);
}

char get_row(eppfile *EPP)
{
char STR1[80];
  if (!getrow(EPP,EPP->rptr)) {
    sprintf(STR1,"Error in reading file %s",EPP->filname);
    msSetError(MS_IMGERR,STR1,"drawEPP()");
    eppclose(EPP);
    return 0;
  }
  EPP->row_cnt++;
  return 1;
}

char eppreset(eppfile *EPP)
{  /*Open_file*/
  int nrd;
  char STR1[102];

  { union { long i; char c[4]; } u; u.i=1; REVERSE=(u.c[0]==0); }
  EPP->fil=fopen(EPP->filname,"rb");
  if (EPP->fil==NULL) {
    sprintf(STR1,"Can't open %s",EPP->filname);
    msSetError(MS_IMGERR,STR1,"drawEPP()");
    return 0;
  }
  nrd=fread(&EPP->fr,1,128,EPP->fil);
      /*fr is start of data*/
  if (REVERSE) {        /* fix up header on big-endian machines */
    swap2(&EPP->fr,4);
    swap8(&EPP->fry,4);
    swap2((short *)&EPP->kind,4);
    swap8(&EPP->sfactor,1);
    swap4(&EPP->access_ptr,1);
    swap2((short *)&EPP->minval,2);
  }
  if (EPP->kind!=8 && (EPP->kind!=16 || nrd!=128)) {
    sprintf(STR1,"%s is not an EPPL file.",EPP->filname);
    msSetError(MS_IMGERR,STR1,"drawEPP()");
    fclose(EPP->fil);
    return 0;
  }
  EPP->row_cnt=0;
  if (EPP->kind==8 && (EPP->minval > 256 || EPP->maxval > 256)) {
    /*old EPPL file; try to fix up*/
    EPP->minval=0;
    EPP->maxval=255;
  }
  EPP->fptr =(unsigned char *)malloc(sizeof(file_buffer));
  nrd=fread(&EPP->fptr[384],1,3968,EPP->fil);
      /*so we read even 4096 byte chunks*/
  EPP->fp=EPP->fptr+384;
  if (nrd==3968)
    EPP->lastbyte=4096;
  else
    EPP->lastbyte=nrd + 384;
  EPP->rptr =(rowptr)malloc((EPP->lc-EPP->fc+3) * sizeof(*EPP->rptr));
  EPP->access_table=NULL;
  return 1;
}

static char epprewind(eppfile *EPP)
{
  if (!eppclose(EPP))
    return 0;
  return eppreset(EPP);
}

char position(eppfile *EPP,int row)
{
  int r,nread;
  long z,now;

  if (EPP->access_table==NULL) {  /*first time through*/
    EPP->access_table=(unsigned short *)malloc((EPP->lr-EPP->fr+2)*2+2);
	/*2 bytes of extra space*/
    EPP->saved=EPP->access_table;
    now=ftell(EPP->fil);
    fseek(EPP->fil,EPP->access_ptr*128,0);
    nread=fread(EPP->access_table,1,(EPP->lr-EPP->fr+1)*2,EPP->fil);
    if (nread!=(EPP->lr-EPP->fr+1)*2)
	  /*if not right amount,assume bad*/
	    EPP->access_ptr=0;
    if (REVERSE) swap2((short*)EPP->access_table,EPP->lr-EPP->fr+1);
    fseek(EPP->fil,now,0);
  }
  if (EPP->access_ptr==0) {  /*some kind of bad table,just count*/
    if (row<EPP->row_cnt+EPP->fr) {
      if (!epprewind(EPP)) return 0;
    }
    for (r=EPP->fr+EPP->row_cnt; r<row; r++) {
      if (!get_row(EPP)) return 0;
    }
    return 1;
  }
  z=128;   /*header*/
  for (r=0; r<row-EPP->fr; r++)
    z+=EPP->access_table[r];
  if (feof(EPP->fil) && EPP->lastbyte!=4096)
    now=ftell(EPP->fil)-EPP->lastbyte+256;
  else
    now=ftell(EPP->fil)-4096;
  if ((unsigned long)(z-now)<3840) {
    EPP->fp=EPP->fptr+z-now+256;
    return 1;
  }
  EPP->fp=EPP->fptr+(z & 127)+256;
  fseek(EPP->fil,z & 0xffffff80,0);
  nread=fread(&EPP->fptr[256],1,4096,EPP->fil);
  if (nread==4096)
    EPP->lastbyte=4096;
  else
    EPP->lastbyte=nread+256;
  return 1;
}


char eppclose(eppfile *EPP)
{
  char badfile,eightbit;

  badfile=0;
  eightbit=0;
  if (EPP->access_table!=NULL) free(EPP->saved);
  if (EPP->rptr!=NULL) free(EPP->rptr);
  free(EPP->fptr);
  fclose(EPP->fil);
  return 1;
}

#ifdef LEGENDS
char legfile::reset(void)
{
  char j,k;
  long v;
  char s[100],*s2;
  legend *p,*p1,*p2;

  root=NULL;
  defaultleg.level=1;
  defaultleg.npass=1;
  memcpy(defaultleg.printchar,"    ",4);
  defaultleg.next=NULL;
  if (*filname=='\0') return 0;
#ifdef __BORLANDC__ 
  _fullpath(s,filname,80);
  strcpy(filname,s);
#else
  if (*filname!='/') {
    getcwd(s,80);
    strcat(s,"/");
    strcat(s,filname);
    strcpy(filname,s);
  }
#endif
  addExt(filname,".leg",0);
  tfil=fopen(filname,"r");
  if (tfil==NULL) return 0;
  while (!feof(tfil)) {
    fgets(s,100,tfil);
    if (strchr(s,'\n')!=NULL) *strchr(s,'\n')=0;
    j=sscanf(s,"%ld",&v);
    if (v<0 || j==0) continue;
    s2=s;
    while (*s2==' ') s2++;
    while (*s2 && *s2!=' ') s2++;
    while (*s2==' ') s2++;
    p=(legend *)malloc(sizeof(legend)-255+strlen(s2));
    strcpy(p->legtext,s2);
    p->level=v;
    p->npass=0;
    memcpy(p->printchar,"    ",4);
    if (root==NULL) {
      root=p;
      root->next=NULL;
      continue;
    }
    p1=root;
    while (p1!=NULL && p1->level<v) {
      p2=p1;
      p1=p1->next;
    }
    if (p1==NULL) {   /*already there,keep first line*/
      p2->next=p;
      p->next=NULL;
      continue;
    }
    if (p1->level==v) {
      free(p);
      continue;
    }
    if (p1==root) {
      p->next=root;
      root=p;
    } else {
      p->next=p2->next;
      p2->next=p;
    }
  }
  fclose(tfil);
  return (root!=NULL);

}


legend *legfile::getptr(epplev v)
{
  legend *p;

  p=root;
  while (p!=NULL && p->level<v)
    p=p->next;
  if (p==NULL || p->level!=v) {
    defaultleg.printchar[0]=v % 78+'0';
    *defaultleg.legtext='\0';
    return (&defaultleg);
  } else
    return p;
}


char legfile::close(void)
{
  char n;
  legend *p1,*p2;

  for (n=0; n<=14; n++) if (this==open_fibs[n]) open_fibs[n]=NULL;
  p1=root;
  while (p1!=NULL) {
    p2=p1->next;
    free(p1);
    p1=p2;
  }
  return 1;
}
#endif

char clrreset(clrfile *CLR)
{
  int valu;
  int n,r,g,b,sz;
  clrTag tmp[300];
  char STR1[80];

  CLR->lastColor=0;
  strcpy(strrchr(CLR->filname,'.'),".clr");
  CLR->fil=fopen(CLR->filname,"r");
  if (CLR->fil==NULL) return 0;
  memset(tmp,0,sizeof(tmp));
  sz=sizeof(tmp)/300;

  while (!feof(CLR->fil)) {
    fgets(STR1,80,CLR->fil);
    sscanf(STR1,"%d%d%d%d",&valu,&r,&g,&b);
    n=CLR->lastColor-1;
    while (n>=0 && tmp[n].eppval>valu) {
      tmp[n+1]=tmp[n];
      n--;
    }
    if (r>=1000) r=999;
    if (g>=1000) g=999;
    if (b>=1000) b=999;
    tmp[n+1].color.red=r*32/125;
    tmp[n+1].color.green=g*32/125;
    tmp[n+1].color.blue=b*32/125;
    tmp[n+1].eppval=valu;
    CLR->lastColor++;   /*?? check overflow*/
  }
  fclose(CLR->fil);
  CLR->clrTbl=(clrTag *)malloc(CLR->lastColor*sz);
  memmove(CLR->clrTbl,tmp,CLR->lastColor*sz);
  return (CLR->lastColor!=0);
}


void clrget(clrfile *CLR,epplev n,TRGB *color)
{
  int ndx;

  ndx=0;   /**** use binary search for efficiency,later*/
  while (ndx<CLR->lastColor && CLR->clrTbl[ndx].eppval<n)
    ndx++;
  if (ndx!=CLR->lastColor && CLR->clrTbl[ndx].eppval==n)
  {   /*lousy choice of names*/
    *color=CLR->clrTbl[ndx].color;
    return;
  }
  color->red=0;   /*paint it black...*/
  color->green=0;
  color->blue=0;
}

char clrclose(clrfile *CLR)
{
  free(CLR->clrTbl);
  return 1;
}