/*! \brief Save category index to binary file (cidx) \param Map pointer to Map_info structure \return 0 on success \return 1 on error */ int Vect_cidx_save(struct Map_info *Map) { struct Plus_head *plus; char *path; struct gvfile fp; G_debug(2, "Vect_cidx_save()"); check_status(Map); plus = &(Map->plus); dig_file_init(&fp); path = Vect__get_path(Map); fp.file = G_fopen_new(path, GV_CIDX_ELEMENT); G_free(path); if (fp.file == NULL) { G_warning(_("Unable to create category index file for vector map <%s>"), Vect_get_name(Map)); return 1; } /* set portable info */ dig_init_portable(&(plus->cidx_port), dig__byte_order_out()); if (0 > dig_write_cidx(&fp, plus)) { G_warning(_("Error writing out category index file")); return 1; } fclose(fp.file); return 0; }
/*! \brief Save topology file for vector map \param Map pointer to Map_info structure \return 1 on success \return 0 on error */ int Vect_save_topo(struct Map_info *Map) { struct Plus_head *plus; char path[GPATH_MAX]; struct gvfile fp; G_debug(1, "Vect_save_topo()"); /* write out all the accumulated info to the plus file */ plus = &(Map->plus); dig_file_init(&fp); Vect__get_path(path, Map); fp.file = G_fopen_new(path, GV_TOPO_ELEMENT); if (fp.file == NULL) { G_warning(_("Unable to create topo file for vector map <%s>"), Map->name); return 0; } /* set portable info */ dig_init_portable(&(plus->port), dig__byte_order_out()); if (0 > dig_write_plus_file(&fp, plus)) { G_warning(_("Error writing out topo file")); return 0; } fclose(fp.file); return 1; }
/*! \brief Writes head information to text file (GV_HEAD_ELEMENT) \param Map pointer to Map_info structure \return 0 on success \return -1 on error */ int Vect__write_head(const struct Map_info *Map) { char *path; FILE *head_fp; path = Vect__get_path(Map); head_fp = G_fopen_new(path, GV_HEAD_ELEMENT); G_free(path); if (head_fp == NULL) { G_warning(_("Unable to create header file for vector map <%s>"), Vect_get_full_name(Map)); return -1; } fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map)); fprintf(head_fp, "DIGIT DATE: %s\n", Vect_get_date(Map)); fprintf(head_fp, "DIGIT NAME: %s\n", Vect_get_person(Map)); fprintf(head_fp, "MAP NAME: %s\n", Vect_get_map_name(Map)); fprintf(head_fp, "MAP DATE: %s\n", Vect_get_map_date(Map)); fprintf(head_fp, "MAP SCALE: %d\n", Vect_get_scale(Map)); fprintf(head_fp, "OTHER INFO: %s\n", Vect_get_comment(Map)); if (Vect_get_proj(Map) > 0) fprintf(head_fp, "PROJ: %d\n", Vect_get_proj(Map)); fprintf(head_fp, "ZONE: %d\n", Vect_get_zone(Map)); fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map)); fclose(head_fp); return 0; }
/*! \brief Write dblinks to file \param Map pointer to Map_info structure \return 0 on success \return -1 on error */ int Vect_write_dblinks(struct Map_info *Map) { int i; FILE *fd; char *path, buf[1024]; struct dblinks *dbl; if (Map->format != GV_FORMAT_NATIVE) /* nothing to write for non-native formats */ return 0; G_debug(1, "Vect_write_dblinks(): map = %s, mapset = %s", Map->name, Map->mapset); dbl = Map->dblnk; path = Vect__get_path(Map); fd = G_fopen_new(path, GV_DBLN_ELEMENT); G_free(path); if (fd == NULL) { /* This may be correct, no tables defined */ G_warning(_("Unable to create database definition file for vector map <%s>"), Vect_get_name(Map)); return -1; } for (i = 0; i < dbl->n_fields; i++) { if (dbl->field[i].name != NULL) sprintf(buf, "%d/%s", dbl->field[i].number, dbl->field[i].name); else sprintf(buf, "%d", dbl->field[i].number); fprintf(fd, "%s|%s|%s|%s|%s\n", buf, dbl->field[i].table, dbl->field[i].key, dbl->field[i].database, dbl->field[i].driver); G_debug(1, "%s|%s|%s|%s|%s", buf, dbl->field[i].table, dbl->field[i].key, dbl->field[i].database, dbl->field[i].driver); } fclose(fd); G_debug(1, "Dblinks written"); return 0; }
/*! \brief Read category index from cidx file if exists \param Map pointer to Map_info structure \param head_only read only header of the file \return 0 on success \return 1 if file does not exist \return -1 error, file exists but cannot be read */ int Vect_cidx_open(struct Map_info *Map, int head_only) { int ret; char file_path[GPATH_MAX], *path; struct gvfile fp; struct Plus_head *Plus; G_debug(2, "Vect_cidx_open(): name = %s mapset= %s", Map->name, Map->mapset); Plus = &(Map->plus); path = Vect__get_path(Map); G_file_name(file_path, path, GV_CIDX_ELEMENT, Map->mapset); if (access(file_path, F_OK) != 0) { /* does not exist */ G_free(path); return 1; } dig_file_init(&fp); fp.file = G_fopen_old(path, GV_CIDX_ELEMENT, Map->mapset); G_free(path); if (fp.file == NULL) { /* category index file is not available */ G_warning(_("Unable to open category index file for vector map <%s>"), Vect_get_full_name(Map)); return -1; } /* load category index to memory */ ret = dig_read_cidx(&fp, Plus, head_only); fclose(fp.file); if (ret == 1) { G_debug(3, "Cannot read cidx"); return -1; } return 0; }
/*! \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only \param Map pointer to Map_info structure \return 0 on success \return -1 on error */ int Vect__read_head(struct Map_info *Map) { FILE *head_fp; char buff[2000]; char *path, *ptr; /* Reset / init */ Vect__init_head(Map); G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset); path = Vect__get_path(Map); head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset); G_free(path); if (head_fp == NULL) { G_warning(_("Unable to open header file of vector <%s>"), Vect_get_full_name(Map)); return -1; } while (G_getl2(buff, 2000, head_fp)) { if (!(ptr = strchr(buff, ':'))) { G_warning(_("Corrupted row in head: %s"), buff); continue; } ptr++; /* Search for the start of text */ while (*ptr == ' ') ptr++; if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0) Vect_set_organization(Map, ptr); else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0) Vect_set_date(Map, ptr); else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0) Vect_set_person(Map, ptr); else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0) Vect_set_map_name(Map, ptr); else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0) Vect_set_map_date(Map, ptr); else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0) Vect_set_scale(Map, atoi(ptr)); else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0) Vect_set_comment(Map, ptr); else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0) Vect_set_proj(Map, atoi(ptr)); else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 || strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0) Vect_set_zone(Map, atoi(ptr)); else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) { } else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) { } else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) { } else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) { } else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0) Vect_set_thresh(Map, atof(ptr)); else G_warning(_("Unknown keyword '%s' in vector head"), buff); } fclose(head_fp); return 0; }
static int read_dblinks_nat(struct Map_info *Map) { FILE *fd; char file[1024], buf[2001]; char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname; int fld; char *c, *path; int row, rule; struct dblinks *dbl; char **tokens; int ntok, i; dbl = Map->dblnk; /* Read dblink for native format */ path = Vect__get_path(Map); fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset); G_free(path); if (fd == NULL) { /* This may be correct, no tables defined */ G_debug(1, "Cannot open vector database definition file"); return -1; } row = 0; rule = 0; while (G_getl2(buf, 2000, fd)) { row++; G_chop(buf); G_debug(1, "dbln: %s", buf); c = (char *)strchr(buf, '#'); if (c != NULL) *c = '\0'; if (strlen(buf) == 0) continue; #ifdef NOT_ABLE_TO_READ_GRASS_6 int ndef; ndef = sscanf(buf, "%s|%s|%s|%s|%s", fldstr, tab, col, db, drv); if (ndef < 2 || (ndef < 5 && rule < 1)) { G_warning(_("Error in rule on row %d in <%s>"), row, file); continue; } #else tokens = G_tokenize(buf, " |"); ntok = G_number_of_tokens(tokens); if (ntok < 2 || (ntok < 5 && rule < 1)) { G_warning(_("Error in rule on row %d in <%s>"), row, file); continue; } strcpy(fldstr, tokens[0]); strcpy(tab, tokens[1]); if (ntok > 2) { strcpy(col, tokens[2]); if (ntok > 3) { strcpy(db, tokens[3]); /* allow for spaces in path names */ for (i=4; i < ntok-1; i++) { strcat(db, " "); strcat(db, tokens[i]); } strcpy(drv, tokens[ntok-1]); } } G_free_tokens(tokens); #endif /* get field and field name */ fldname = strchr(fldstr, '/'); if (fldname != NULL) { /* field has name */ fldname[0] = 0; fldname++; } fld = atoi(fldstr); Vect_add_dblink(dbl, fld, fldname, tab, col, db, drv); G_debug(1, "field = %d name = %s, table = %s, key = %s, database = %s, driver = %s", fld, fldname, tab, col, db, drv); rule++; } fclose(fd); G_debug(1, "Dblinks read"); return rule; }