Exemplo n.º 1
0
int cleanse_string(char *buf)
{
    char *stop, *p, *p2;

    p = buf;

    /*
     * get rid of any SPACEs at beginning while ( !isspace(*buf) && *buf !=
     * (char) NULL) buf++; if (*buf == (char) NULL) return -1;
     */

    /* find where this string terminates */
    if (*buf != DQUOTE) {	/* if no DQUOTEs, */
	stop = G_index(buf, SPACE);	/* then SPACE separates */
	if (stop == (char *)NULL)
	    return strlen(buf);
	else
	    return (int)(stop - buf);
    }
    else {			/* otherwise string is in DQUOTEs */
	/* but we must skip over escaped */
	/* (BSLASHed) DQUOTEs */
	if (*p == DQUOTE) {
	    while (*p != (char)NULL) {	/* get rid of first DQUOTE */
		*p = *(p + 1);
		p++;
	    }
	    p = buf;
	    stop = G_index(p + 1, DQUOTE);
	    while (*(stop - 1) == BSLASH)
		stop = G_index(++stop, DQUOTE);
	}
    }
    /* remove backslashes between buf and stop */
    p = buf;
    while ((p = G_index(p, BSLASH)) != (char *)NULL && p <= stop) {
	p2 = p + 1;
	if (*p2 != (char)NULL && (*p2 == DQUOTE || *p2 == BSLASH)) {
	    while (*p != (char)NULL) {
		*p = *(p + 1);
		p++;
	    }
	    stop--;
	}
	p = p2;
    }
    return (int)(stop - buf);
}
Exemplo n.º 2
0
int read_head(FILE * dascii, struct Map_info *Map)
{
    char buff[1024];
    char *ptr;

    for (;;) {
	if (0 == G_getl2(buff, sizeof(buff) - 1, dascii))
	    return (0);

	/* Last line of header */
	if (strncmp(buff, "VERTI:", 6) == 0)
	    return (0);

	if (!(ptr = G_index(buff, ':')))
	    G_fatal_error(_("Unexpected data in vector head:\n[%s]"), buff);

	ptr++;			/* Search for the start of text */
	while (*ptr == ' ')
	    ptr++;

	if (strncmp(buff, "ORGANIZATION:", 12) == 0)
	    Vect_set_organization(Map, ptr);
	else if (strncmp(buff, "DIGIT DATE:", 11) == 0)
	    Vect_set_date(Map, ptr);
	else if (strncmp(buff, "DIGIT NAME:", 11) == 0)
	    Vect_set_person(Map, ptr);
	else if (strncmp(buff, "MAP NAME:", 9) == 0)
	    Vect_set_map_name(Map, ptr);
	else if (strncmp(buff, "MAP DATE:", 9) == 0)
	    Vect_set_map_date(Map, ptr);
	else if (strncmp(buff, "MAP SCALE:", 10) == 0)
	    Vect_set_scale(Map, atoi(ptr));
	else if (strncmp(buff, "OTHER INFO:", 11) == 0)
	    Vect_set_comment(Map, ptr);
	else if (strncmp(buff, "ZONE:", 5) == 0 ||
		 strncmp(buff, "UTM ZONE:", 9) == 0)
	    Vect_set_zone(Map, atoi(ptr));
	else if (strncmp(buff, "WEST EDGE:", 10) == 0) {
	}
	else if (strncmp(buff, "EAST EDGE:", 10) == 0) {
	}
	else if (strncmp(buff, "SOUTH EDGE:", 11) == 0) {
	}
	else if (strncmp(buff, "NORTH EDGE:", 11) == 0) {
	}
	else if (strncmp(buff, "MAP THRESH:", 11) == 0)
	    Vect_set_thresh(Map, atof(ptr));
	else {
	    G_warning(_("Unknown keyword <%s> in vector head"), buff);
	}
    }
    /* NOTREACHED */
}
Exemplo n.º 3
0
static int askfile(void)
{
    char file[GNAME_MAX];

    while (1) {
	Curses_prompt_gets("Enter file to hold report: ", file);
	G_strip(file);
	if (*file == 0)
	    return -1;
	if (G_index(file, '/'))
	    strcpy(buf, file);
	else
	    sprintf(buf, "%s/%s", G_home(), file);
	if (access(buf, 0) != 0)
	    return 1;
	sprintf(buf, "** %s already exists. choose another file", file);
	Beep();
	Curses_write_window(PROMPT_WINDOW, 2, 1, buf);
    }

    return 0;
}
Exemplo n.º 4
0
int G_site_put_new(FILE * fptr, Site * s, int has_cat)

/* Writes a site to file open on fptr. */
{
    char ebuf[MAX_SITE_STRING], nbuf[MAX_SITE_STRING];
    char xbuf[MAX_SITE_STRING], buf[MAX_SITE_LEN];
    static int format_double();
    int fmt, i, j, k;
    int G_format_northing(), G_format_easting(), G_projection();

    fmt = G_projection();

    G_format_northing(s->north, nbuf, fmt);
    G_format_easting(s->east, ebuf, fmt);
    sprintf(buf, "%s|%s|", ebuf, nbuf);
    for (i = 0; i < s->dim_alloc; ++i) {
	format_double(s->dim[i], nbuf);
	sprintf(xbuf, "%s|", nbuf);
	strcat(buf, xbuf);
    }

    if (has_cat) {
	switch (s->cattype) {
	case CELL_TYPE:
	    sprintf(xbuf, "#%d ", s->ccat);
	    strcat(buf, xbuf);
	    break;
	case FCELL_TYPE:
	    sprintf(xbuf, "#%g ", s->fcat);
	    strcat(buf, xbuf);
	    break;
	case DCELL_TYPE:
	    sprintf(xbuf, "#%g ", s->dcat);
	    strcat(buf, xbuf);
	    break;
	}
    }
    else {			/* no cat there, so data in plain x,y,z format will be imported   12/99 MN */

	/* we create a #cat entry in site_list from the current site number 11/99 */
	sprintf(xbuf, "#%d ", loop);
	loop++;
	strcat(buf, xbuf);
    }

    /* now import attributes */
    for (i = 0; i < s->dbl_alloc; ++i) {
	format_double(s->dbl_att[i], nbuf);
	sprintf(xbuf, "%%%s ", nbuf);
	strcat(buf, xbuf);
    }

    for (i = 0; i < s->str_alloc; ++i) {
	if (strlen(s->str_att[i]) != 0) {
	    /* escape double quotes */
	    j = k = 0;
	    if (G_index(s->str_att[i], DQUOTE) != (char *)NULL) {
		while (!isnull(s->str_att[i][j])) {
		    if (isquote(s->str_att[i][j])) {
			xbuf[k++] = BSLASH;
			xbuf[k++] = DQUOTE;
		    }
		    else if (isbslash(s->str_att[i][j])) {
			xbuf[k++] = BSLASH;
			xbuf[k++] = BSLASH;
		    }
		    else
			xbuf[k++] = s->str_att[i][j];
		    j++;
		}
		xbuf[k] = (char)NULL;
	    }
	    else
		strcpy(xbuf, s->str_att[i]);

	    strcpy(s->str_att[i], xbuf);

	    if (G_index(s->str_att[i], SPACE) != (char *)NULL)
		sprintf(xbuf, "@\"%s\" ", s->str_att[i]);
	    else
		sprintf(xbuf, "@%s ", s->str_att[i]);

	    strcat(buf, xbuf);
	}
    }
    fprintf(fptr, "%s\n", buf);
    return 0;
}
Exemplo n.º 5
0
int G_oldsite_describe(FILE * ptr, int *dims, int *cat, int *strs, int *dbls)

/*-
 * Tries to guess the format of a sites list (the dimensionality,
 * the presence/type of a category, and the number of string and decimal
 * attributes) by reading the first record in the file.
 * Reads ptr and returns 0 on success,
 *                      -1 on EOF,
 *                      -2 for other error.
 */
{
    char sbuf[MAX_SITE_LEN], *buf;
    char ebuf[128], nbuf[128];
    int err;
    int itmp;
    float ftmp;

    if (ftell(ptr) != 0L) {
	fprintf(stderr,
		"\nPROGRAMMER ERROR: G_oldsite_describe() must be called\n");
	fprintf(stderr, "        immediately after G_fopen_sites_old()\n");
	return -2;
    }

    *dims = *strs = *dbls = 0;
    *cat = -1;
    buf = sbuf;

    if ((buf = fgets(sbuf, 1024, ptr)) == (char *)NULL) {
	rewind(ptr);
	return EOF;
    }
    /* skip over comment & header lines */
    while ((*buf == '#' || !isdigit(*buf)) && *buf != '-' && *buf != '+')
	if ((buf = fgets(sbuf, 1024, ptr)) == (char *)NULL) {
	    rewind(ptr);
	    return EOF;
	}

    if (buf[strlen(buf) - 1] == '\n')
	buf[strlen(buf) - 1] = '\0';

    if ((err = sscanf(buf, "%[^|]|%[^|]|%*[^\n]", ebuf, nbuf)) < 2) {
	fprintf(stderr, "ERROR: ebuf %s nbuf %s\n", ebuf, nbuf);
	rewind(ptr);
	return -2;
    }
    *dims = 2;

    /* move pointer past easting and northing fields */
    while (!ispipe(*buf) && !isnull(*buf))
	buf++;
    if (!isnull(*buf) && !isnull(*(buf + 1)))
	buf++;
    else {
	rewind(ptr);
	return -2;
    }
    while (!ispipe(*buf) && !isnull(*buf))
	buf++;
    if (!isnull(*buf) && !isnull(*(buf + 1)))
	buf++;
    else {
	rewind(ptr);
	return 0;
    }

    /* check for remaining dimensional fields */
    while (G_index(buf, PIPE) != (char *)NULL) {
	(*dims)++;
	while (!ispipe(*buf) && !isnull(*buf))
	    buf++;
	if (isnull(*buf) || isnull(*(buf + 1))) {
	    rewind(ptr);
	    return 0;
	}
	if (!isnull(*(buf + 1)))
	    buf++;
	else {
	    rewind(ptr);
	    return -2;
	}
    }

    /* no more dimensions-now we parse attribute fields */
    while (!isnull(*buf)) {
	switch (*buf) {
	case '#':		/* category field */
	    sscanf(buf, "#%s ", ebuf);
	    if (G_strstr(ebuf, ".") == NULL && sscanf(ebuf, "%d", &itmp) == 1)
		*cat = CELL_TYPE;
	    else if (G_strstr(ebuf, ".") != NULL &&
		     sscanf(ebuf, "%f", &ftmp) == 1)
		*cat = FCELL_TYPE;
	    else
		*cat = -1;

	    /* move to beginning of next attribute */
	    while (!isspace(*buf) && !isnull(*buf))
		buf++;
	    if (isnull(*buf) || isnull(*(buf + 1))) {
		rewind(ptr);
		return 0;
	    }
	    else
		buf++;
	    break;
	case '%':		/* decimal attribute */
	    (*dbls)++;
	    /* move to beginning of next attribute */
	    while (!isspace(*buf) && !isnull(*buf))
		buf++;
	    if (isnull(*buf) || isnull(*(buf + 1))) {
		rewind(ptr);
		return 0;
	    }
	    else
		buf++;
	    break;
	case '@':		/* string attribute */
	    if (isnull(*buf) || isnull(*(buf + 1))) {
		rewind(ptr);
		return 0;
	    }
	    else
		buf++;
	default:		/* defaults to string attribute */
	    /* allow both prefixed and unprefixed strings */
	    if ((err = cleanse_string(buf)) > 0) {
		(*strs)++;
		buf += err;
	    }

	    /* move to beginning of next attribute */
	    while (!isspace(*buf) && !isnull(*buf))
		buf++;
	    if (isnull(*buf) || isnull(*(buf + 1))) {
		rewind(ptr);
		return 0;
	    }
	    else
		buf++;
	    break;
	}
    }

    rewind(ptr);
    return 0;
}
Exemplo n.º 6
0
int G__oldsite_get(FILE * ptr, Site * s, int fmt)

/*-
 * Reads ptr and returns 0 on success,
 *                      -1 on EOF,
 *                      -2 on other fatal error or insufficient data,
 *                       1 on format mismatch (extra data)
 */
{
    char sbuf[MAX_SITE_LEN], *buf, *last, *p1, *p2;
    char ebuf[128], nbuf[128];
    int n = 0, d = 0, c = 0, dim = 0, err = 0, tmp;

    buf = sbuf;

    if ((buf = fgets(sbuf, 1024, ptr)) == (char *)NULL)
	return EOF;

    while ((*buf == '#' || !isdigit(*buf)) && *buf != '-' && *buf != '+')
	if ((buf = fgets(sbuf, 1024, ptr)) == (char *)NULL)
	    return EOF;

    if (buf[strlen(buf) - 1] == '\n')
	buf[strlen(buf) - 1] = '\0';

    if (sscanf(buf, "%[^|]|%[^|]|%*[^\n]", ebuf, nbuf) < 2) {
	fprintf(stderr, "ERROR: ebuf %s nbuf %s\n", ebuf, nbuf);
	return -2;
    }

    if (!G_scan_northing(nbuf, &(s->north), fmt) ||
	!G_scan_easting(ebuf, &(s->east), fmt)) {
	fprintf(stderr, "ERROR: ebuf %s nbuf %s\n", ebuf, nbuf);
	return -2;
    }

    /* move pointer past easting and northing fields */
    if (NULL == (buf = G_index(buf, PIPE)))
	return -2;
    if (NULL == (buf = G_index(buf + 1, PIPE)))
	return -2;

    /* check for remaining dimensional fields */
    do {
	buf++;
	if (isnull(*buf))
	    return (FOUND_ALL(s, n, dim, c, d) ? 0 : -2);
	last = buf;
	if (dim < s->dim_alloc) {	/* should be more dims to read */
	    if (sscanf(buf, "%lf|", &(s->dim[dim++])) < 1)
		return -2;	/* no more dims, though expected */
	}
	else if (NULL != (p1 = G_index(buf, PIPE))) {
	    if (NULL == (p2 = G_index(buf, DQUOTE)))
		err = 1;	/* more dims, though none expected */
	    else if (strlen(p1) > strlen(p2))
		err = 1;	/* more dims, though none expected */
	}
    } while ((buf = G_index(buf, PIPE)) != NULL);
    buf = last;

    /* no more dimensions-now we parse attribute fields */
    while (!isnull(*buf)) {
	switch (*buf) {
	case '#':		/* category field */
	    if (n == 0) {
		switch (s->cattype) {
		case CELL_TYPE:
		    if (sscanf(buf, "#%d", &s->ccat) == 1)
			n++;
		    break;
		case FCELL_TYPE:
		    if (sscanf(buf, "#%f", &s->fcat) == 1)
			n++;
		    break;
		case DCELL_TYPE:
		    if (sscanf(buf, "#%lf", &s->dcat) == 1)
			n++;
		    break;
		default:
		    err = 1;	/* has cat, none expected */
		    break;
		}
	    }
	    else {
		err = 1;	/* extra cat */
	    }

	    /* move to beginning of next attribute */
	    if ((buf = next_att(buf)) == (char *)NULL)
		return (FOUND_ALL(s, n, dim, c, d) ? err : -2);
	    break;

	case '%':		/* decimal attribute */
	    if (d < s->dbl_alloc) {
		p1 = ++buf;
		errno = 0;
		s->dbl_att[d++] = strtod(buf, &p1);
		if (p1 == buf || errno == ERANGE) {
		    /* replace with:
		     * s->dbl_att[d - 1] = NAN
		     * when we add NULL attribute support
		     */
		    return -2;
		}
		/* err = 0; Make sure this is zeroed */
	    }
	    else {
		err = 1;	/* extra decimal */
	    }

	    if ((buf = next_att(buf)) == (char *)NULL) {
		return (FOUND_ALL(s, n, dim, c, d)) ? err : -2;
	    }
	    break;
	case '@':		/* string attribute */
	    if (isnull(*buf) || isnull(*(buf + 1)))
		return (FOUND_ALL(s, n, dim, c, d) ? err : -2);
	    else
		buf++;
	default:		/* defaults to string attribute */
	    /* allow both prefixed and unprefixed strings */
	    if (c < s->str_alloc) {
		if ((tmp = cleanse_string(buf)) > 0) {
		    G_strncpy(s->str_att[c++], buf, tmp);
		    buf += tmp;
		}
		else
		    return (FOUND_ALL(s, n, dim, c, d) ? err : -2);
	    }
	    if ((buf = next_att(buf)) == (char *)NULL) {
		return (FOUND_ALL(s, n, dim, c, d) ? err : -2);
	    }
	    break;
	}
    }

    return (FOUND_ALL(s, n, dim, c, d) ? err : -2);
}
Exemplo n.º 7
0
char *G_site_format(const Site * s, const char *fs, int id)
/* sprintf analog to G_site_put with the addition of a field separator fs 
   and option of printing site attribute identifiers
 */
{
    char ebuf[MAX_SITE_STRING], nbuf[MAX_SITE_STRING];
    char xbuf[MAX_SITE_STRING];
    const char *nfs;
    char *buf;
    int fmt, i, j, k;

    buf = (char *)G_malloc(MAX_SITE_LEN * sizeof(char));

    fmt = G_projection();

    G_format_northing(s->north, nbuf, fmt);
    G_format_easting(s->east, ebuf, fmt);

    nfs = (char *)((fs == (char *)NULL) ? "|" : fs);

    sprintf(buf, "%s%s%s", ebuf, nfs, nbuf);

    for (i = 0; i < s->dim_alloc; ++i) {
	format_double(s->dim[i], nbuf);
	sprintf(xbuf, "%s%s", nfs, nbuf);
	G_strcat(buf, xbuf);
    }

    nfs = (fs == NULL) ? " " : fs;

    switch (s->cattype) {
    case CELL_TYPE:
	sprintf(xbuf, "%s%s%d ", nfs, ((id == 0) ? "" : "#"), (int)s->ccat);
	G_strcat(buf, xbuf);
	break;
    case FCELL_TYPE:
    case DCELL_TYPE:
	sprintf(xbuf, "%s%s%g ", nfs, ((id == 0) ? "" : "#"), (float)s->fcat);
	G_strcat(buf, xbuf);
	break;
    }

    for (i = 0; i < s->dbl_alloc; ++i) {
	format_double(s->dbl_att[i], nbuf);
	sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "%"), nbuf);
	G_strcat(buf, xbuf);
    }

    for (i = 0; i < s->str_alloc; ++i) {
	if (strlen(s->str_att[i]) != 0) {
	    /* escape double quotes */
	    j = k = 0;

	    /* do not uncomment this code because sites file was created
	     * as we want. So it's enough to print them out as it is.
	     *
	     if (G_index (s->str_att[i], DQUOTE) != (char *) NULL)
	     {
	     while (!isnull(s->str_att[i][j]))
	     {
	     if (isquote(s->str_att[i][j]))
	     {
	     xbuf[k++] = BSLASH;
	     xbuf[k++] = DQUOTE;
	     }
	     else
	     xbuf[k++] = s->str_att[i][j];
	     j++;
	     }
	     xbuf[k] = (char) NULL;
	     }
	     else
	     */

	    G_strcpy(xbuf, s->str_att[i]);

	    G_strcpy(s->str_att[i], xbuf);

	    if (G_index(s->str_att[i], SPACE) != (char *)NULL)
		sprintf(xbuf, "%s%s\"%s\"", nfs, ((id == 0) ? "" : "@"),
			s->str_att[i]);
	    else
		sprintf(xbuf, "%s%s%s", nfs, ((id == 0) ? "" : "@"),
			s->str_att[i]);
	    G_strcat(buf, xbuf);
	}
    }
    return buf;
}