]> www.wagner.pp.ru Git - oss/fgis.git/blob - dll/fgisEppCalc.c
First checked in version
[oss/fgis.git] / dll / fgisEppCalc.c
1 #include <stdlib.h>
2 #include <epp.h>
3 #include <reclass.h>
4 #include "fgisInt.h"
5 #include "fgis.h"
6 /*
7  * fgisEppCalc.c 
8  *
9  * Functions to deal with raster objects, which are not directly interfaces 
10  * TCL
11  */
12
13 /* Some global variables are defined here */
14 /* Default reclass - property of reclass command 
15    Something wrong with it. It should be allocated once, not upon each
16    intiialization of package
17  */
18 RECLASS def_reclass;
19 /* Points to list of all open rasters. This is good, becouse it would 
20    correctly handle access to open rasters from multiple interptreters
21  */
22 XEPP *first_raster=NULL;
23 RASTER_OBJECT firstobject=NULL;
24 /*
25  * Returns maximal value of raster, using current reclass
26  *
27  */
28 int Fgis_RasterMax(RASTER_OBJECT handle)
29 {
30     EPP *e=epp(handle);
31     int max,i;
32     if(handle->reclass==def_reclass) 
33         max=e->max;
34     else { 
35         max=0;
36         for(i=e->min;i<=e->max;i++) 
37             if (i!=e->offsite&&handle->reclass[i]>max) 
38                 max=handle->reclass[i];
39     }
40     return max;
41 }
42
43 /*
44  * Returns minimal value of raster, taking into account current reclass
45  *
46  */
47 int Fgis_RasterMin(RASTER_OBJECT handle)
48 {
49     EPP *e=epp(handle);
50     int min,i;
51     if(handle->reclass==def_reclass) 
52         min=e->min;
53     else { 
54         min=65535;
55         for(i=e->min;i<=e->min;i++) 
56         if (i!=e->offsite&&handle->reclass[i]<min) 
57             min=handle->reclass[i];
58     }
59     return min;
60 }
61 /*
62  * Reopens file in new mode. Returns 0 on success, non-zero on failure.
63  * If fails, file remains open in old mode.
64  */
65
66
67 /*
68  * Creates new XEPP object from open EPP* object.
69  * 
70  */
71
72 XEPP* Fgis_NewXEPP(EPP *file,char *filename) 
73 {XEPP* xptr;
74
75     xptr=malloc(sizeof(XEPP));
76     xptr->e=file;
77     xptr->linkcount=1;
78     xptr->filename=stralloc(filename); 
79     xptr->next=first_raster;
80     xptr->editable=1;
81     first_raster=xptr;
82     return xptr;                 
83 }       
84 /* 
85  * Opens existing epp file. If file already opened, returns pointer
86  * to it
87  */
88
89 XEPP* Fgis_OpenXEPP(char *filename,EPP *(*openfunc)(char *))
90 {
91  EPP* epp_ptr;
92  XEPP * xptr=first_raster;
93  RASTER_OBJECT obj;
94  int reclass_size;
95     while (xptr) {
96         if (!strcmp(xptr->filename,filename)) break;
97         xptr=xptr->next;
98     }
99     
100     if (!xptr) {
101         /* îÅÔ ÔÁËÏÇÏ × ÔÁÂÌÉÃÅ, ÏÔËÒÙ×ÁÅÍ ÎÏ×ÙÊ*/
102         epp_ptr=openfunc(filename);
103         if (!epp_ptr) {
104            return NULL;
105         }
106         xptr=Fgis_NewXEPP(epp_ptr,filename);
107         xptr->editable=(openfunc==load_epp); 
108
109      } else { 
110         (xptr->linkcount)++;
111         /* if it was open in read-only mode and now we want it to 
112            be read-write */  
113         if (!xptr->editable&&openfunc==load_epp) {
114             /* close it */
115             close_epp(xptr->e);
116             /* reopen for read-write */
117             xptr->e=load_epp(filename);
118             /* if failed, restore it back in readonly mode and return NULL*/
119             if (!xptr->e) {
120               xptr->e=open_epp(filename);
121               xptr->linkcount--;
122               return NULL;
123             }
124             /* fix reclass tables for all objects, which refers to this file*/
125             reclass_size=1<<(xptr->e->kind);
126             for (obj=firstobject;obj!=NULL;obj=obj->next) {
127                if (obj->file==xptr) {
128                  obj->reclass=realloc(obj->reclass,reclass_size*sizeof(short));
129                }
130             }  
131                
132         }
133      }
134     return xptr;
135
136 }
137
138 /*
139  * Gets rid of XEPP object (if only reference - deletes, otherwise
140  * decrements link count)
141  */ 
142
143 void Fgis_CloseXEPP(XEPP* file)
144 {   
145     file->linkcount--;
146     if(!file->linkcount) {
147     /* Remove it from list */
148         if (first_raster==file) {
149             first_raster=file->next;
150         } else {
151             XEPP* tmp=first_raster;
152             while (tmp->next!=file)
153                 tmp=tmp->next;
154             tmp->next=file->next;
155         }
156         /* Close eppfile */
157         close_epp(file->e);
158         free(file->filename);
159         free(file);
160     }
161 }