5 typedef void (*close_func)(void *f);
8 DGT* creat_dgt(char *filename,double x_left,double y_bottom,
9 double x_right,double y_top, unsigned char proj)
10 /* creates file with given limits with given name */
12 f=fopen(filename,"w+");
14 return fcreat_dgt(f,x_left,y_bottom,x_right,y_top,proj);
16 int write_header(FILE *f ,double x_left,double y_bottom,
17 double x_right,double y_top,unsigned char proj)
19 memset(&h,0,sizeof(h));
20 h.xleft=swapdouble(x_left);
21 h.ybottom=swapdouble(y_bottom);
22 h.xright=swapdouble(x_right);
23 h.ytop=swapdouble(y_top);
25 return ( fwrite(&h,1,128,f)==128);
27 int write_buf(DGT *dgt)
30 chk_mask(dgt->buffer);
31 dgt->modified=0;dgt->item_no++;
32 return fwrite(dgt->buffer,1,14+4*dgt->buffer->npoints,(FILE *)dgt->F)==
33 14+4*dgt->buffer->npoints;
36 chk_mask(dgt->buffer);
37 tmp.ID=swaplong(dgt->buffer->ID);
38 swab(&(dgt->buffer->x1),&(tmp.x1),10+4*dgt->buffer->npoints);
39 dgt->modified=0;dgt->item_no++;
40 return fwrite(&tmp,14+4*dgt->buffer->npoints,1,(FILE *)dgt->F)==
41 14+4*dgt->buffer->npoints;
44 void write_eof(DGT* dgt)
45 {short int eof_marker[7]={0,0,0,0,0,0,0};
46 if (dgt->modified) write_buf(dgt);
47 fwrite(&eof_marker,14,1,(FILE *)dgt->F);
49 DGT* fcreat_dgt(FILE *f ,double x_left,double y_bottom,
50 double x_right,double y_top,unsigned char proj)
51 /* creates dgt file with given limits in given stream */
53 if (!write_header(f,x_left,y_bottom,x_right,y_top,proj)) { map_error=ME_WRITE_ERROR; return NULL;}
54 if (!(tmp=malloc(sizeof(DGT)))) {map_error=ME_OUT_OF_MEMORY;return NULL;}
56 tmp->YBottom=y_bottom;
65 tmp->next_item=write_buf;
66 tmp->rewind=write_eof;
67 tmp->dispose=(close_func) fclose;
68 tmp->buffer=malloc(sizeof(DGT_ITEM));
71 DGT* creat_dgt_as(char *filename,DGT* pattern)
72 {return creat_dgt(filename,pattern->XLeft,pattern->YBottom,pattern->XRight,
73 pattern->YTop,pattern->projection);
75 DGT* fcreat_dgt_as(FILE *f,DGT *pattern)
76 {return fcreat_dgt(f,pattern->XLeft,pattern->YBottom,pattern->XRight,
77 pattern->YTop,pattern->projection);
79 void chk_mask(DGT_ITEM* item)
80 /*set correct values of bounding rectangle*/
84 if (!item->npoints) return;
89 for(i=0,p=item->s;i<item->npoints;i++,p++)
90 { if(p->x<x1) x1=p->x;
100 void dgt_put(DGT *dgt, DGT_ITEM* item)
101 /* inserts item in writable file*/
102 { if (!dgt->mode&MAP_OUTPUT) {map_error=ME_INVALID_MODE;}
103 if (dgt->modified) dgt->next_item(dgt);
104 memcpy(dgt->buffer,item,14+4*item->npoints);
108 void item_set_id(DGT_ITEM *item,long int newID)
109 {item->ID=newID?newID:1;
111 void dgt_set_point(DGT *dgt,int x,int y)
113 dgt->buffer->npoints=0;
114 if (x!=KEEP_OLD_VALUE) dgt->buffer->x1=x;
115 if (y!=KEEP_OLD_VALUE) dgt->buffer->y1=y;
117 int dgt_add_node(DGT* dgt,int x,int y)
118 { DGT_ITEM *buf=dgt->buffer;
119 POINT *p=dgt->buffer->s+dgt->buffer->npoints-1;
121 if (buf->npoints==MAX_LINE_LEN) return DGT_LINE_TOO_LONG;
122 if (buf->npoints==0||p->x!=x||p->y!=y)
128 } else return DGT_ZERO_SEGMENT;
130 int dgt_ins_node(DGT *dgt,int index,int x,int y)
131 { DGT_ITEM *buf=dgt->buffer;
132 if (index<0) return 3;
133 if (buf->npoints==MAX_LINE_LEN) return DGT_LINE_TOO_LONG;
134 if (index>=buf->npoints)
135 if (index==buf->npoints) return dgt_add_node(dgt,x,y);
136 else return DGT_INVALID_NODE;
137 if ((index==0||buf->s[index-1].x!=x||buf->s[index-1].y!=y)
138 &&(buf->s[index].x!=x||buf->s[index].y!=y))
139 {int i; POINT *s,*s1;
140 s=buf->s+buf->npoints;
142 for(i=buf->npoints;i>index;i--,*(s--)=*(s1--));
150 return DGT_ZERO_SEGMENT;
154 int item_mv_node(DGT_ITEM *item,int index,int x,int y)
155 { POINT *s=item->s+index,*s1;
156 if (index>=item->npoints||index<0) return DGT_INVALID_NODE;
158 if (x==KEEP_OLD_VALUE) x=s->x;
159 if (y==KEEP_OLD_VALUE) y=s->y;
160 if (index>0) {s1=s-1;if (s1->x==x&&s1->y==y) return DGT_ZERO_SEGMENT;}
161 if (index<item->npoints-1)
162 { s1=s+1;if (s1->x==x&&s1->y==y) return DGT_ZERO_SEGMENT;}
167 int dgt_rm_node(DGT *dgt,int index)
168 { DGT_ITEM *buf=dgt->buffer;int i;POINT *s,*s1,*s2;
169 if (buf->npoints==2) return DGT_LINE_TOO_SHORT;
170 if (index<0||index>=buf->npoints) return DGT_INVALID_NODE;
173 if(!index&&index<buf->npoints-1&&s1->x==s2->x&&s1->y==s2->y)
174 return DGT_ZERO_SEGMENT;
175 for(i=index+1;i<buf->npoints;i++,*(s++)=*(s1++));
179 int dgt_split(DGT *dgt,int index)
180 {DGT_ITEM new_line,*buf=dgt->buffer;
183 if (index<=0||index>=buf->npoints-1) return DGT_BAD_SPLIT_POINT;
186 for(i=index;i<buf->npoints;i++,*(s1++)=*(s++));
187 new_line.npoints=buf->npoints-index;
188 buf->npoints=index+1;
190 memcpy(buf,&new_line,14+4*new_line.npoints);
195 int dgt_cut_segment(DGT *dgt,int index)
197 DGT_ITEM new_line,*buf=dgt->buffer;
199 if (index<=1||index>=buf->npoints-1) return DGT_BAD_SPLIT_POINT;
202 for(i=index;i<buf->npoints;i++,*(s1++)=*(s++));
203 new_line.npoints=buf->npoints-index;
206 memcpy(buf,&new_line,14+4*new_line.npoints);