/*! \brief Create misc element in the current mapset. \param dir directory path \param name element name \return 0 ? \return ? */ int G__make_mapset_element_misc(const char *dir, const char *name) { char buf[GNAME_MAX * 2 + 1]; sprintf(buf, "%s/%s", dir, name); return G__make_mapset_element(buf); }
/*! \brief Lowest level open routine. Opens the file <i>name</i> in <i>element</i> ("cell", etc.) in mapset <i>mapset</i> according to the i/o <i>mode</i>. - mode = 0 (read) will look for <i>name</i> in <i>mapset</i> and open the file for read only the file must exist - mode = 1 (write) will create an empty file <i>name</i> in the current mapset and open the file for write only <i>mapset</i> ignored - mode = 2 (read and write) will open a file in the current mapset for reading and writing creating a new file if necessary <i>mapset</i> ignored \param element database element name \param name map file name \param mapset mapset containing map <i>name</i> \param mode r/w mode 0=read, 1=write, 2=read/write \return open file descriptor (int) \return -1 could not open */ static int G__open(const char *element, const char *name, const char *mapset, int mode) { char path[GPATH_MAX]; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; G__check_gisinit(); /* READ */ if (mode == 0) { if (G_name_is_fully_qualified(name, xname, xmapset)) { if (*mapset && strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"), mapset, xmapset); return -1; } name = xname; mapset = xmapset; } else if (!mapset || !*mapset) mapset = G_find_file2(element, name, mapset); if (!mapset) return -1; G_file_name(path, element, name, mapset); return open(path, 0); } /* WRITE */ if (mode == 1 || mode == 2) { mapset = G_mapset(); if (G_name_is_fully_qualified(name, xname, xmapset)) { if (strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"), xmapset, mapset); return -1; } name = xname; } if (*name && G_legal_filename(name) == -1) return -1; G_file_name(path, element, name, mapset); if (mode == 1 || access(path, 0) != 0) { G__make_mapset_element(element); close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666)); } return open(path, mode); } return -1; }
int I_list_cameras(int full) { char *element; char buf[1024]; char title[50]; FILE *ls, *temp; int any; if (tempfile == NULL) tempfile = G_tempfile(); element = "camera"; G__make_mapset_element(element); temp = fopen(tempfile, "w"); if (temp == NULL) G_fatal_error("can't open any temp files"); fprintf(temp, "Available cameras\n"); fprintf(temp, "---------------------------------\n"); any = 0; strcpy(buf, "cd "); G_file_name(buf + strlen(buf), element, "", G_mapset()); strcat(buf, ";ls"); if (!full) strcat(buf, " -C"); if (ls = popen(buf, "r")) { while (G_getl(buf, sizeof buf, ls)) { any = 1; fprintf(temp, "%s", buf); if (full) { I_get_cam_title(buf, title, sizeof title); if (*title) fprintf(temp, " (%s)", title); fprintf(temp, "\n"); } else fprintf(temp, "\n"); } pclose(ls); } if (!any) fprintf(temp, "no camera files available\n"); fprintf(temp, "---------------------------------\n"); fclose(temp); sprintf(buf, "$GRASS_PAGER %s", tempfile); G_system(buf); unlink(tempfile); fprintf(stderr, "hit RETURN to continue -->"); G_gets(buf); return 0; }
/*! * \brief Populates element with a path string. * * \param[out] element element name */ void G__temp_element(char *element) { const char *machine; strcpy(element, ".tmp"); machine = G__machine_name(); if (machine != NULL && *machine != 0) { strcat(element, "/"); strcat(element, machine); } G__make_mapset_element(element); }
int I_list_elev(int full) { char *element; char buf[1024]; FILE *ls, *temp; int any; if (tempfile == NULL) tempfile = G_tempfile(); element = "cell"; G__make_mapset_element(element); temp = fopen(tempfile, "w"); if (temp == NULL) G_fatal_error("can't open any temp files"); fprintf(temp, "Available raster maps:\n"); fprintf(temp, "---------------------------------\n"); any = 0; strcpy(buf, "cd "); G__file_name(buf + strlen(buf), element, " ", " "); strcat(buf, ";ls"); strcat(buf, " -C"); if ((ls = popen(buf, "r"))) { while (G_getl(buf, sizeof buf, ls)) { any = 1; fprintf(temp, "%s", buf); fprintf(temp, "\n"); } pclose(ls); } if (!any) fprintf(temp, "no raster maps available\n"); fprintf(temp, "---------------------------------\n"); fclose(temp); sprintf(buf, "$GRASS_PAGER %s", tempfile); G_system(buf); unlink(tempfile); fprintf(stderr, "hit RETURN to continue -->"); G_gets(buf); /******/ G_list_element("cell", "cell", G_mapset(), NULL); return 0; }
/*! \brief Sets up database connection settings using GRASS default from dbmi.h \todo DB_OK on success, DB_* error code on fail \return returns DB_OK */ int db_set_default_connection(void) { dbConnection connection; char buf[GPATH_MAX]; G_debug(1, "Creating new default DB params with db_set_default_connection()"); /* is this really needed ? */ db_get_connection(&connection); if (strcmp(DB_DEFAULT_DRIVER, "dbf") == 0) { /* Set default values and create dbf db dir */ connection.driverName = "dbf"; connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/"; db_set_connection(&connection); sprintf(buf, "%s/%s/dbf", G_location_path(), G_mapset()); G__make_mapset_element("dbf"); } else if (strcmp(DB_DEFAULT_DRIVER, "sqlite") == 0) { /* Set default values and create dbf db dir */ connection.driverName = "sqlite"; /* * TODO: Use one DB for entire mapset (LFS problems?) * or per-map DBs in $MASPET/vector/mapname/sqlite.db (how to set that here?) * or $MAPSET/sqlite/mapname.sql as with dbf? */ connection.databaseName = "$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite.db"; db_set_connection(&connection); } else G_fatal_error(_("Programmer error")); return DB_OK; }
static int close_new(int fd, int ok) { struct fileinfo *fcb = &G__.fileinfo[fd]; int stat; struct Categories cats; struct History hist; char path[GPATH_MAX]; CELL cell_min, cell_max; int row, i, open_mode; if (ok) { switch (fcb->open_mode) { case OPEN_NEW_COMPRESSED: G_debug(1, "close %s compressed", fcb->name); break; case OPEN_NEW_UNCOMPRESSED: G_debug(1, "close %s uncompressed", fcb->name); break; case OPEN_NEW_RANDOM: G_debug(1, "close %s random", fcb->name); break; } if (fcb->open_mode != OPEN_NEW_RANDOM && fcb->cur_row < fcb->cellhd.rows) { G_zero_raster_buf(fcb->data, fcb->map_type); for (row = fcb->cur_row; row < fcb->cellhd.rows; row++) G_put_raster_row(fd, fcb->data, fcb->map_type); G_free(fcb->data); fcb->data = NULL; } /* create path : full null file name */ G__make_mapset_element_misc("cell_misc", fcb->name); G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name, G_mapset()); remove(path); if (fcb->null_cur_row > 0) { /* if temporary NULL file exists, write it into cell_misc/name/null */ int null_fd; null_fd = G__open_null_write(fd); if (null_fd <= 0) return -1; if (null_fd < 1) return -1; /* first finish writing null file */ /* write out the rows stored in memory */ for (row = fcb->min_null_row; row < fcb->null_cur_row; row++) G__write_null_bits(null_fd, fcb->NULL_ROWS[row - fcb->min_null_row], row, fcb->cellhd.cols, fd); /* write missing rows */ if (fcb->open_mode != OPEN_NEW_RANDOM && fcb->null_cur_row < fcb->cellhd.rows) { G__init_null_bits(fcb->null_work_buf, fcb->cellhd.cols); for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++) G__write_null_bits(null_fd, fcb->null_work_buf, row, fcb->cellhd.cols, fd); } close(null_fd); if (rename(fcb->null_temp_name, path)) { G_warning(_("closecell: can't move %s\nto null file %s"), fcb->null_temp_name, path); stat = -1; } else { remove(fcb->null_temp_name); } } else { remove(fcb->null_temp_name); remove(path); } /* null_cur_row > 0 */ if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */ fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR); G__write_row_ptrs(fd); } if (fcb->map_type != CELL_TYPE) { /* floating point map */ int cell_fd; if (G__write_fp_format(fd) != 0) { G_warning(_("Error writing floating point format file for map %s"), fcb->name); stat = -1; } /* now write 0-length cell file */ G__make_mapset_element("cell"); cell_fd = creat(G__file_name(path, "cell", fcb->name, fcb->mapset), 0666); close(cell_fd); strcpy(CELL_DIR, "fcell"); } else { /* remove fcell/name file */ G__file_name(path, "fcell", fcb->name, fcb->mapset); remove(path); /* remove cell_misc/name/f_format */ G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset); remove(path); strcpy(CELL_DIR, "cell"); close(fd); } } /* ok */ /* NOW CLOSE THE FILE DESCRIPTOR */ close(fd); /* remember open_mode */ open_mode = fcb->open_mode; fcb->open_mode = -1; if (fcb->data != NULL) G_free(fcb->data); if (fcb->null_temp_name != NULL) { G_free(fcb->null_temp_name); fcb->null_temp_name = NULL; } /* if the cell file was written to a temporary file * move this temporary file into the cell file * if the move fails, tell the user, but go ahead and create * the support files */ stat = 1; if (ok && (fcb->temp_name != NULL)) { G__file_name(path, CELL_DIR, fcb->name, fcb->mapset); remove(path); if (rename(fcb->temp_name, path)) { G_warning(_("closecell: can't move %s\nto cell file %s"), fcb->temp_name, path); stat = -1; } else { remove(fcb->temp_name); } } if (fcb->temp_name != NULL) { G_free(fcb->temp_name); } if (ok) { /* remove color table */ G_remove_colors(fcb->name, ""); /* create a history file */ G_short_history(fcb->name, "raster", &hist); G_write_history(fcb->name, &hist); /* write the range */ if (fcb->map_type == CELL_TYPE) { G_write_range(fcb->name, &fcb->range); G__remove_fp_range(fcb->name); } /*NOTE: int range for floating point maps is not written out */ else { /* if(fcb->map_type != CELL_TYPE) */ G_write_fp_range(fcb->name, &fcb->fp_range); G_construct_default_range(&fcb->range); /* this range will be used to add default rule to quant structure */ } if (fcb->map_type != CELL_TYPE) fcb->cellhd.format = -1; else /* CELL map */ fcb->cellhd.format = fcb->nbytes - 1; /* write header file */ G_put_cellhd(fcb->name, &fcb->cellhd); /* if map is floating point write the quant rules, otherwise remove f_quant */ if (fcb->map_type != CELL_TYPE) { /* DEFAULT RANGE QUANT G_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max); if(!G_is_d_null_value(&dcell_min) && !G_is_d_null_value(&dcell_max)) { G_get_range_min_max(&fcb->range, &cell_min, &cell_max); G_quant_add_rule(&fcb->quant, dcell_min, dcell_max, cell_min, cell_max); } */ G_quant_round(&fcb->quant); if (G_write_quant(fcb->name, fcb->mapset, &fcb->quant) < 0) G_warning(_("unable to write quant file!")); } else { /* remove cell_misc/name/f_quant */ G__file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name, fcb->mapset); remove(path); } /* create empty cats file */ G_get_range_min_max(&fcb->range, &cell_min, &cell_max); if (G_is_c_null_value(&cell_max)) cell_max = 0; G_init_cats(cell_max, (char *)NULL, &cats); G_write_cats(fcb->name, &cats); G_free_cats(&cats); /* write the histogram */ /* only works for integer maps */ if ((fcb->map_type == CELL_TYPE) && (fcb->want_histogram)) { G_write_histogram_cs(fcb->name, &fcb->statf); G_free_cell_stats(&fcb->statf); } else { G_remove_histogram(fcb->name); } } /* OK */ G_free(fcb->name); G_free(fcb->mapset); for (i = 0; i < NULL_ROWS_INMEM; i++) G_free(fcb->NULL_ROWS[i]); G_free(fcb->null_work_buf); if (fcb->map_type != CELL_TYPE) G_quant_free(&fcb->quant); return stat; }
/*! \brief Copy a map including attribute tables Old vector is deleted \param in input vector map name \param mapset mapset name \param out output vector map name \return -1 error \return 0 success */ int Vect_copy(const char *in, const char *mapset, const char *out) { int i, n, ret, type; struct Map_info In, Out; struct field_info *Fi, *Fin; char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX]; const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT, GV_HEAD_ELEMENT, GV_HIST_ELEMENT, GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT, NULL }; const char *inmapset; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; dbDriver *driver; G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out); /* check for [A-Za-z][A-Za-z0-9_]* in name */ if (Vect_legal_filename(out) < 0) G_fatal_error(_("Vector map name is not SQL compliant")); inmapset = G_find_vector2(in, mapset); if (!inmapset) { G_warning(_("Unable to find vector map <%s> in <%s>"), in, mapset); return -1; } mapset = inmapset; /* remove mapset from fully qualified name, confuses G_file_name() */ if (G_name_is_fully_qualified(in, xname, xmapset)) { in = xname; } /* Delete old vector if it exists */ if (G_find_vector2(out, G_mapset())) { G_warning(_("Vector map <%s> already exists and will be overwritten"), out); ret = Vect_delete(out); if (ret != 0) { G_warning(_("Unable to delete vector map <%s>"), out); return -1; } } /* Copy the directory */ G__make_mapset_element(GV_DIRECTORY); sprintf(buf, "%s/%s", GV_DIRECTORY, out); G__make_mapset_element(buf); i = 0; while (files[i]) { sprintf(buf, "%s/%s", in, files[i]); G_file_name(old_path, GV_DIRECTORY, buf, mapset); sprintf(buf, "%s/%s", out, files[i]); G_file_name(new_path, GV_DIRECTORY, buf, G_mapset()); if (access(old_path, F_OK) == 0) { /* file exists? */ G_debug(2, "copy %s to %s", old_path, new_path); if (copy_file(old_path, new_path)) { G_warning(_("Unable to copy vector map <%s> to <%s>"), old_path, new_path); } } i++; } G_file_name(old_path, GV_DIRECTORY, in, mapset); G_file_name(new_path, GV_DIRECTORY, out, G_mapset()); /* Open input */ Vect_set_open_level(1); Vect_open_old_head(&In, in, mapset); if (In.format != GV_FORMAT_NATIVE) { /* Done */ Vect_close(&In); return 0; } /* Open output */ Vect_set_open_level(1); Vect_open_update_head(&Out, out, G_mapset()); /* Copy tables */ n = Vect_get_num_dblinks(&In); type = GV_1TABLE; if (n > 1) type = GV_MTABLE; for (i = 0; i < n; i++) { Fi = Vect_get_dblink(&In, i); if (Fi == NULL) { G_warning(_("Database connection not defined for layer %d"), In.dblnk->field[i].number); Vect_close(&In); Vect_close(&Out); return -1; } Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type); G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'", Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database, Fin->table); Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key, Fin->database, Fin->driver); ret = db_copy_table(Fi->driver, Fi->database, Fi->table, Fin->driver, Vect_subst_var(Fin->database, &Out), Fin->table); if (ret == DB_FAILED) { G_warning(_("Unable to copy table <%s>"), Fin->table); Vect_close(&In); Vect_close(&Out); return -1; } driver = db_start_driver_open_database(Fin->driver, Vect_subst_var(Fin->database, &Out)); if (driver == NULL) { G_warning(_("Unable to open database <%s> by driver <%s>"), Fin->database, Fin->driver); } else { if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK) G_warning(_("Unable to create index for table <%s>, key <%s>"), Fi->table, Fi->key); db_close_database_shutdown_driver(driver); } } Vect_close(&In); Vect_close(&Out); return 0; }
int G__open ( char *element, char *name, char *mapset, int mode) { char path[1024]; char xname[512], xmapset[512], *dummy; int G_fd; G__check_gisinit(); /* READ */ if (mode == 0) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, mapset) != 0) { fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n", mapset,xmapset); return -1; } name = xname; } if ((dummy = G_find_file (element, name, mapset)) == NULL) return -1; G_free (dummy); G__file_name (path, element, name, mapset); G_fd = open (path, 0); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } /* WRITE */ if (mode == 1 || mode == 2) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, G_mapset()) != 0) { fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n", xmapset,G_mapset()); return -1; } name = xname; } if (G_legal_filename(name) == -1) return -1; G__file_name (path, element, name, G_mapset()); if(mode == 1 || access(path,0) != 0) { G__make_mapset_element (element); close (creat (path, 0666)); } G_fd = open (path, mode); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } return -1; }