]> www.wagner.pp.ru Git - oss/fgis.git/blob - dll/fgisInit.c
*** empty log message ***
[oss/fgis.git] / dll / fgisInit.c
1 /******************************************************************************
2  * $Id: fgisInit.c,v 1.3 2003-01-08 09:50:05 dron Exp $
3  *
4  * Project:  fGIS core engine
5  * Purpose:  initialization procedure for Fgis package
6  *
7  ******************************************************************************
8  *
9  * Copyright (C) 1997, Victor Wagner <vitus@ice.ru>
10  * Copyright (C) 2003, Andrey Kiselev <dron@remotesensing.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25  * USA.
26  *****************************************************************************
27  */
28
29 #include <string.h>
30 #include <math.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33   
34 #include <tcl.h>
35 #include <tk.h>
36 #include "fgis.h"
37
38 int check_for_rc(Tcl_Interp *interp,char *home,char *path);
39 /* Global variables */
40 \f
41 /* 
42  * Initialization procedure for Environmental Planning Tcl extension
43  * Return value TCL_OK if success
44  * Side effects:
45  * defines commands raster vector palette fgisPlanchet_mapx fgisPlanchet_mapy
46  * fgisPlanchet_scrx fgisPlanchet_scry fgisPlanchet_fit defaultpalette
47  * defines default (1:1) reclass table
48  * defines fGIS_HOME variable to directory, containing fgis.rc
49  *     Directories are searched in following order
50  *     FGIS_CONFDIR as defined in Makefile
51  *     ~/lib/fgis
52  * executes fgis.rc
53  */
54 EXPORT(int, Fgis_Init)(Tcl_Interp *interp)
55 {
56   char *fGIS_HOME;
57   char buffer[1024];
58 /* Defining commands */ 
59    
60 /* Palette objects */
61     if (!Tcl_CreateCommand(interp,"palette",Fgis_Palette,NULL,
62             Fgis_DefDeleteProc)) 
63         return TCL_ERROR;
64     if (!Tcl_CreateCommand(interp,"defaultpalette",Fgis_PaletteObj,
65        (ClientData)default_palette,Fgis_DeletePalette))
66        return TCL_ERROR;
67 /* Pattern objects */
68     if (!Tcl_CreateCommand(interp,"patterns",Fgis_CreatePatterns,
69           NULL,Fgis_DefDeleteProc))
70         return TCL_ERROR;
71 /* Raster objects */
72     if (!Tcl_CreateCommand(interp,"raster",Fgis_Raster,
73             NULL,Fgis_DeleteRaster)) 
74         return TCL_ERROR;
75 /* Vector objects */
76     if (!Tcl_CreateCommand(interp,"vector",Fgis_Vector,NULL,Fgis_DefDeleteProc))
77        return TCL_ERROR;
78   /* Tk dependent fgis commands. Should eventually go into separate package*/
79   /* Object Drawing */
80    if (!Tcl_CreateCommand(interp,"fgisRasterColorImage",Fgis_RasterImage,
81        (ClientData)1,Fgis_DefDeleteProc)) return TCL_ERROR;
82    if (!Tcl_CreateCommand(interp,"fgisRasterBWImage",Fgis_RasterImage,
83       NULL,Fgis_DefDeleteProc)) return TCL_ERROR;
84   /* planchet widget subcommands, written on C */    
85     if (!Tcl_CreateCommand(interp,"fgisPlanchet_mapx",Fgis_MapX,NULL,
86        Fgis_DefDeleteProc))
87         return TCL_ERROR;
88     if (!Tcl_CreateCommand(interp,"fgisPlanchet_mapy",Fgis_MapY,NULL,
89             Fgis_DefDeleteProc))
90
91         return TCL_ERROR;
92     if (!Tcl_CreateCommand(interp,"fgisPlanchet_scrx",Fgis_ScrX,NULL,
93             Fgis_DefDeleteProc))
94         return TCL_ERROR;
95     if (!Tcl_CreateCommand(interp,"fgisPlanchet_scry",Fgis_ScrY,NULL,
96         Fgis_DefDeleteProc))
97         return TCL_ERROR;
98     if (!Tcl_CreateCommand(interp,"fgisPlanchet_fit",Fgis_Fit,NULL,
99         Fgis_DefDeleteProc))
100         return TCL_ERROR;
101     if (!Tcl_CreateCommand(interp,"projection",Fgis_Projection,NULL,
102         Fgis_DefDeleteProc))
103         return TCL_ERROR;
104      
105     /* Searching for fgis.rc and executing it */
106     /* First, check if fGIS_HOME already set */
107     if ((fGIS_HOME=Tcl_GetVar(interp,"fGIS_HOME",TCL_GLOBAL_ONLY))==NULL) {
108          fGIS_HOME=buffer;
109          /* Check if compilied in default is valid */ 
110          if (!check_for_rc(interp,fGIS_HOME,FGIS_CONFDIR)&&
111 #ifdef __unix__
112              !check_for_rc(interp,fGIS_HOME,"/usr/local/lib/fgis")&&
113              !check_for_rc(interp,fGIS_HOME,"/usr/lib/fgis")&&
114              !check_for_rc(interp,fGIS_HOME,"~/fgis")
115 #else 
116              !check_for_rc(interp,fGIS_HOME,"C:/FGIS") &&
117              !check_for_rc(interp,fGIS_HOME,"C:/Program Files/fGIS") &&
118              !check_for_rc(interp,fGIS_HOME,"C:/TCL/LIB/fGIS") 
119 #endif
120             ) {
121                  Tcl_SetResult(interp,"Cannot determine fGIS home directory",
122                          TCL_STATIC); 
123                  return TCL_ERROR;
124          };
125         Tcl_SetVar(interp,"fGIS_HOME",fGIS_HOME,TCL_GLOBAL_ONLY);
126     }
127        
128     if (Tcl_EvalFile(interp,FGIS_CONFDIR "/fgis.rc")!=TCL_OK) {
129             Tcl_SetResult(interp,"Error in initialization file",TCL_STATIC); 
130             return TCL_ERROR;
131     }
132     /* Providing package for Tcl versions, which supports it */
133 #if (TCL_MAJOR_VERSION>7)||((TCL_MAJOR_VERSION==7)&&(TCL_MINOR_VERSION>=5))
134     Tcl_PkgProvide(interp,"Fgis","0.1");
135 #endif
136     return TCL_OK;
137 }
138
139 int check_for_rc (Tcl_Interp *interp,char *home,char *path)
140 { char buffer[1024];
141   int result;
142   sprintf(buffer,"file readable [file join {%s} fgis.rc]",path);
143   Tcl_Eval(interp,buffer);
144   if (Tcl_GetInt(interp,interp->result,&result)==TCL_OK&& result) {
145       strcpy(home,path);
146       return 1;
147   }
148   return 0;
149 }