GRASS Programmer's Manual  6.4.2(2012)
g3dwindowio.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <sys/stat.h>
00005 #include <unistd.h>
00006 #include <string.h>
00007 #include <grass/gis.h>
00008 #include "G3d_intern.h"
00009 
00010 /*---------------------------------------------------------------------------*/
00011 
00012 static int
00013 G3d_readWriteWindow(struct Key_Value *windowKeys, int doRead, int *proj,
00014                     int *zone, double *north, double *south, double *east,
00015                     double *west, double *top, double *bottom, int *rows,
00016                     int *cols, int *depths, double *ew_res, double *ns_res,
00017                     double *tb_res)
00018 {
00019     int returnVal;
00020     int (*windowInt) (), (*windowDouble) ();
00021 
00022     if (doRead) {
00023         windowDouble = G3d_keyGetDouble;
00024         windowInt = G3d_keyGetInt;
00025     }
00026     else {
00027         windowDouble = G3d_keySetDouble;
00028         windowInt = G3d_keySetInt;
00029     }
00030 
00031     returnVal = 1;
00032     returnVal &= windowInt(windowKeys, G3D_REGION_PROJ, proj);
00033     returnVal &= windowInt(windowKeys, G3D_REGION_ZONE, zone);
00034 
00035     returnVal &= windowDouble(windowKeys, G3D_REGION_NORTH, north);
00036     returnVal &= windowDouble(windowKeys, G3D_REGION_SOUTH, south);
00037     returnVal &= windowDouble(windowKeys, G3D_REGION_EAST, east);
00038     returnVal &= windowDouble(windowKeys, G3D_REGION_WEST, west);
00039     returnVal &= windowDouble(windowKeys, G3D_REGION_TOP, top);
00040     returnVal &= windowDouble(windowKeys, G3D_REGION_BOTTOM, bottom);
00041 
00042     returnVal &= windowInt(windowKeys, G3D_REGION_ROWS, rows);
00043     returnVal &= windowInt(windowKeys, G3D_REGION_COLS, cols);
00044     returnVal &= windowInt(windowKeys, G3D_REGION_DEPTHS, depths);
00045 
00046     returnVal &= windowDouble(windowKeys, G3D_REGION_EWRES, ew_res);
00047     returnVal &= windowDouble(windowKeys, G3D_REGION_NSRES, ns_res);
00048     returnVal &= windowDouble(windowKeys, G3D_REGION_TBRES, tb_res);
00049 
00050     if (returnVal)
00051         return 1;
00052 
00053     G3d_error("G3d_readWriteWindow: error writing window");
00054     return 0;
00055 }
00056 
00057 /*
00058  * If windowName == NULL -> G3D_WINDOW_ELEMENT ("$MAPSET/WIND3")
00059  * otherwise G3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME")
00060  */
00061 static void G3d_getFullWindowPath(char *path, const char *windowName)
00062 {
00063     char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00064 
00065     if (windowName == NULL) {
00066         G__file_name(path, "", G3D_WINDOW_ELEMENT, G_mapset());
00067         return;
00068     }
00069 
00070     while (*windowName == ' ')
00071         windowName++;
00072 
00073     if ((*windowName == '/') || (*windowName == '.')) {
00074         sprintf(path, windowName);
00075         return;
00076     }
00077 
00078     if (G__name_is_fully_qualified(windowName, xname, xmapset)) {
00079         G__file_name(path, G3D_WINDOW_DATABASE, xname, xmapset);
00080         return;
00081     }
00082 
00083     G__file_name(path, G3D_WINDOW_DATABASE, windowName, G_mapset());
00084 }
00085 
00086 /*---------------------------------------------------------------------------*/
00087 /*
00088    static void
00089    G3d_getWindowLocation (path, windowName)
00090 
00091    char path[1024];
00092    char *windowName;
00093 
00094    {
00095    char xname[512], xmapset[512];
00096    char *p, *slash;
00097 
00098    if (windowName == NULL) {
00099    G__file_name (path, "", "", G_mapset ());
00100    return;
00101    }
00102 
00103    while (*windowName == ' ') windowName++;
00104 
00105    if ((*windowName != '/') && (*windowName != '.')) {
00106    if (G__name_is_fully_qualified (windowName, xname, xmapset)) 
00107    G__file_name (path, G3D_WINDOW_DATABASE, xname, xmapset);
00108    else
00109    G__file_name (path, G3D_WINDOW_DATABASE, windowName, G_mapset ());
00110    } else
00111    sprintf (path, windowName);
00112    p = path;
00113    slash = NULL;
00114    while (*p != 0) {
00115    if (*p == '/') slash = p;
00116    p++;
00117    }
00118    if (slash != NULL) *slash = 0;
00119    }
00120  */
00121 
00122 /*---------------------------------------------------------------------------*/
00123 
00124 
00139 int G3d_readWindow(G3D_Region * window, const char *windowName)
00140 {
00141     struct Cell_head win;
00142     struct Key_Value *windowKeys;
00143     char path[GPATH_MAX];
00144     int status;
00145 
00146 
00147     if (windowName == NULL) {
00148         G_get_window(&win);
00149 
00150         window->proj = win.proj;
00151         window->zone = win.zone;
00152         window->north = win.north;
00153         window->south = win.south;
00154         window->east = win.east;
00155         window->west = win.west;
00156         window->top = win.top;
00157         window->bottom = win.bottom;
00158         window->rows = win.rows3;
00159         window->cols = win.cols3;
00160         window->depths = win.depths;
00161         window->ns_res = win.ns_res3;
00162         window->ew_res = win.ew_res3;
00163         window->tb_res = win.tb_res;
00164     }
00165     else {
00166         G3d_getFullWindowPath(path, windowName);
00167 
00168         if (access(path, R_OK) != 0) {
00169             G_warning("G3d_readWindow: unable to find [%s].", path);
00170             return 0;
00171         }
00172 
00173         windowKeys = G_read_key_value_file(path, &status);
00174         if (status != 0) {
00175             G3d_error("G3d_readWindow: Unable to open %s", path);
00176             return 0;
00177         }
00178 
00179         if (!G3d_readWriteWindow(windowKeys, 1,
00180                                  &(window->proj), &(window->zone),
00181                                  &(window->north), &(window->south),
00182                                  &(window->east), &(window->west),
00183                                  &(window->top), &(window->bottom),
00184                                  &(window->rows), &(window->cols),
00185                                  &(window->depths), &(window->ew_res),
00186                                  &(window->ns_res), &(window->tb_res))) {
00187             G3d_error
00188                 ("G3d_readWindow: error extracting window key(s) of file %s",
00189                  path);
00190             return 0;
00191         }
00192 
00193         G_free_key_value(windowKeys);
00194     }
00195 
00196     return 1;
00197 }
00198 
00199 /*---------------------------------------------------------------------------*/
00200 /* modified version of G__make_mapset_element */
00201 /*
00202    static int
00203    G3d_createPath (thePath)
00204 
00205    char *thePath;
00206 
00207    {
00208    char command[1024];
00209    char *path, *p, *pOld;
00210 
00211    if (*thePath == 0) return 0;
00212 
00213    strcpy (path = command, "mkdir ");
00214    while (*path) path++;
00215    p = path;
00216  */
00217   /* now append element, one directory at a time, to path */
00218 /*
00219    while (1) {
00220    if (*thePath == '/') *p++ = *thePath++;
00221    pOld = p;
00222    while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
00223    *p = 0;
00224 
00225    if (p == pOld) return 1;
00226 
00227    if (access (path, 0) != 0) mkdir (path,0777);
00228    if (access (path, 0) != 0) system (command);
00229    if (access (path, 0) != 0) {
00230    char err[1024];
00231    sprintf (err, "can't make mapset element %s (%s)", thePath, path);
00232    G_fatal_error (err);
00233    exit(1);
00234    }
00235    }
00236    }
00237  */
00238 
00239 /*---------------------------------------------------------------------------*/
00240 
00241 
00256 /*
00257    int
00258    G3d_writeWindow (window, windowName)
00259 
00260    G3D_Region *window;
00261    char *windowName;
00262 
00263    {
00264    return 0;
00265    }
00266  */
00267 
00268 /*---------------------------------------------------------------------------*/
00269 
00270 
00281 void G3d_useWindowParams(void)
00282 {
00283     G3d_setWindowParams();
00284 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines