Exemplo n.º 1
0
/*
 * Read a grid in ASCII format, set defaults for isolines
 */
int readgrid(int gridno, char *fname)
{
    FILE *fp;
    char buf[256];
    int i, j, itmp, type, nn, nnodes;
    extern int doadcirc;

    if ((fp = fopen(fname, "r")) == NULL) {
	sprintf(buf, "In readgrid, unable to open file %s\n", fname);
	errwin(buf);
	return 0;
    }
    Free_grid(gridno);

    fgets(buf, 255, fp);
    strcpy(grid[gridno].alphid, buf);
    i = strlen(grid[gridno].alphid);
    grid[gridno].alphid[i - 1] = '\0';
    fgets(buf, 255, fp);
    convertchar(buf);
    sscanf(buf, "%d %d", &grid[gridno].nmel, &grid[gridno].nmnp);
    if (!Allocate_grid(gridno, grid[gridno].nmel, grid[gridno].nmnp)) {
	errwin("Can't allocate memory for grid");
	Free_grid(gridno);
	fclose(fp);
	return 0;
    }
    for (i = 0; i < grid[gridno].nmnp; i++) {
	if (fgets(buf, 255, fp) == NULL) {
	    errwin("Error reading table of nodes");
	    Free_grid(gridno);
	    fclose(fp);
	    return 0;
	}
	convertchar(buf);
	sscanf(buf, "%d %lf %lf %lf", &itmp, &grid[gridno].xord[i], &grid[gridno].yord[i], &grid[gridno].depth[i]);
    }
    for (i = 0; i < grid[gridno].nmel; i++) {
	if (fgets(buf, 255, fp) == NULL) {
	    errwin("Error reading table of elements");
	    Free_grid(gridno);
	    fclose(fp);
	    return 0;
	}
	convertchar(buf);
	sscanf(buf, "%d %d", &itmp, &type);
	grid[gridno].icon[i].type = type;
	switch (type) {
	case 3:
	    sscanf(buf, "%d %d %d %d %d", &itmp, &itmp,
		   &grid[gridno].icon[i].nl[0],
		   &grid[gridno].icon[i].nl[1],
		   &grid[gridno].icon[i].nl[2]);
	    for (j = 0; j < 3; j++) {
		grid[gridno].icon[i].nl[j]--;
		nn = grid[gridno].icon[i].nl[j];
		grid[gridno].ntype[nn] = CORNER;
	    }
	    grid[gridno].icon[i].nn = 3;
	    grid[gridno].icon[i].ngeom = 3;
	    break;
	case 4:
	    sscanf(buf, "%d %d %d %d %d %d", &itmp, &itmp,
		   &grid[gridno].icon[i].nl[0],
		   &grid[gridno].icon[i].nl[1],
		   &grid[gridno].icon[i].nl[2],
		   &grid[gridno].icon[i].nl[3]);
	    for (j = 0; j < 4; j++) {
		grid[gridno].icon[i].nl[j]--;
		nn = grid[gridno].icon[i].nl[j];
		grid[gridno].ntype[nn] = CORNER;
	    }
	    grid[gridno].icon[i].nn = 4;
	    grid[gridno].icon[i].ngeom = 4;
	    break;
	case 6:
	    sscanf(buf, "%d %d %d %d %d %d %d %d", &itmp, &itmp,
		   &grid[gridno].icon[i].nl[0],
		   &grid[gridno].icon[i].nl[1],
		   &grid[gridno].icon[i].nl[2],
		   &grid[gridno].icon[i].nl[3],
		   &grid[gridno].icon[i].nl[4],
		   &grid[gridno].icon[i].nl[5]);
	    for (j = 0; j < 6; j++) {
		grid[gridno].icon[i].nl[j]--;
		nn = grid[gridno].icon[i].nl[j];
		grid[gridno].ntype[nn] = (j > 2) ? MIDDLE : CORNER;
	    }
	    grid[gridno].icon[i].nn = 6;
	    grid[gridno].icon[i].ngeom = 3;
	    break;
	}
    }
    grid[gridno].gridtype = 1;	/* set gridtype to linear. */
    fclose(fp);
    set_grid_limits(gridno);
    default_isolines(&grid[gridno].ip);
    autoscale_isolines(grid[gridno].dmin, grid[gridno].dmax, &grid[gridno].ip);
    mindist = (grid[gridno].xmax - grid[gridno].xmin) * 0.01;
    return 1;
}
Exemplo n.º 2
0
/* parse a tag and all its sub tags */
static int ParseNode(struct xmlNode* Node, char **s)
{
  char szText[MAX_XML_TEXT_LEN];
  char attr[MAX_XML_NAME_LEN];
  int j ;

  /* look for opening < */
  if (!skipspace(s))
    return FALSE ;

  if (**s != '<')
    return FALSE ;
  (*s)++ ;
  
  if (!skipspace(s))
    return FALSE ;

  /* get the tag name and store it */
  GetName(attr,s);
  Node->elementType = strdup(attr) ;
  if (!Node->elementType)
    return FALSE ;

  /* is it immediately followed by /? 
   * if so we are going to define it as a null tag, I don't know
   * if this is XML kosher or not
   */
  if (!skipspace(s))
    return FALSE ;

  if (**s == '/') {
    (*s)++ ;
    if (!skipspace(s))
        return FALSE ;

    /* we always have Node->textData point to something */
    Node->textData = strdup("") ;
    if (!Node->textData)
        return FALSE ;
    return *(*s)++ == '>' ;
  }

  /* are there any attributes? */
  if(**s!='>')
  {
    /* while there are attributes */
    while(**s)
    {
      char attr[MAX_XML_NAME_LEN];
      j = 0 ;

      /* get the attribute name */
      GetName(attr, s) ;
      if (!**s)
        return FALSE ;

      if (!skipspace(s))
         return FALSE ;

      /* need an equal sign */
      if (**s != '=')
        return FALSE ;
      (*s)++ ;

      if (!skipspace(s))
         return FALSE ;

      /* need an opening quote */
      if (**s != '"')
        return FALSE ;
      (*s)++ ;

      /* now read the attribute value */
      while(**s && **s!='"'&&j<MAX_XML_TEXT_LEN) {
        szText[j] = convertchar(s);
        if (szText[j++] == 0)
            return FALSE ;
      }
      szText[j] = '\0';
      if (**s)
        (*s)++;

      /* insert it into the node */
      if (!InsertAttr(Node,attr,szText))
        return FALSE ;

      if (!skipspace(s))
         return FALSE ;

      /* auto close out the tag? */
      if(**s=='/')
      {
        
        (*s)++ ;
        if (!skipspace(s))
           return FALSE ;
    
        /* we always have Node->textData point to something */

        Node->textData = strdup("") ;
        if (!Node->textData)
            return 0 ;

        /* make sure we really have the end of the tag */
        return (*(*s)++ == '>') ;
      }

      /* other wise if it is the end of a normal opening tag
       * break out of the attribute loop and go get text
       */
      if(**s=='>')  {
        (*s)++ ;
        break;
      }
    }
  } else
    (*s)++ ;

  j = 0 ;
  /* now start processing child nodes */
  while(**s)
  {
    struct xmlNode *child ;
    struct xmlNode **link  = &Node->children;
  
    /* get any text available now
     */
    while(**s && **s!='<'&&j<MAX_XML_TEXT_LEN) {
      szText[j] = convertchar(s) ;
      if (szText[j++] == 0)
        return FALSE ;
    }
    szText[j] = '\0';

    /* MUST have an opening < at this point */
    if (**s != '<')
        return FALSE ;
    (*s)++ ;

    /* is this the beginning of a closing tag? */
    if(**s=='/') 
    {
      /* if so get its name */
      (*s)++ ;
      GetName(attr,s) ;

      if (!skipspace(s))
        return FALSE ;

      /* make sure we end with > */
      if (*(*s)++ != '>')
        return FALSE ;

      /* save text, textdata always points to something even if it is null text */
      Node->textData = stripdup(szText) ;
      if (!Node->textData)
        return FALSE ;

      /* and only return TRUE if it matched the opening tag for this node */
      return !strcmp(Node->elementType,attr) ;
    }

    /* otherwise back up to < as we are going to call this routine
     * recursively to get the node data */
    while (*(--*s) != '<') ;

    /* make a new child */
    child = calloc(sizeof(struct xmlNode),1) ;
    if (!child)
        return FALSE ;

    /* find the end of the node's children and insert the new child */
    while (*link)
        link = &((*link)->next) ;
    *link = child ;

    /* now parse the child */
    if (!ParseNode(child,s))
        return FALSE ;

  }
  return FALSE ;
}
Exemplo n.º 3
0
/*
 * read a finite difference grid
 */
int readpomgrid(int gridno, char *fname)
{
    FILE *fp;
    char buf[256];
    int i, j, itmp, type, nn, nnodes;
    int cnt, nx, ny;
    int idx, idy;
    extern int doadcirc;

    if ((fp = fopen(fname, "r")) == NULL) {
	sprintf(buf, "In readgrid, unable to open file %s\n", fname);
	errwin(buf);
	return 0;
    }
    Free_grid(gridno);

    fgets(buf, 255, fp);
    strcpy(grid[gridno].alphid, buf);
    i = strlen(grid[gridno].alphid);
    grid[gridno].alphid[i - 1] = '\0';
    fgets(buf, 255, fp);
    convertchar(buf);
    sscanf(buf, "%d %d", &nx, &ny);
    grid[gridno].nmnp = nx * ny;
    grid[gridno].nmel = (nx - 1) * (ny - 1);
    if (!Allocate_grid(gridno, grid[gridno].nmel, grid[gridno].nmnp)) {
	errwin("Can't allocate memory for grid");
	Free_grid(gridno);
	fclose(fp);
	return 0;
    }
    cnt = 0;
    grid[gridno].fdgrid = 1;
    grid[gridno].nx = nx;
    grid[gridno].ny = ny;
    for (j = 0; j < ny; j++) {
	for (i = 0; i < nx; i++) {
	    if (fgets(buf, 255, fp) == NULL) {
		errwin("Error reading table of nodes");
		Free_grid(gridno);
		fclose(fp);
		return 0;
	    }
	    convertchar(buf);
	    sscanf(buf, "%d %d %lf %lf %lf", &itmp, &itmp, &grid[gridno].xord[cnt], &grid[gridno].yord[cnt], &grid[gridno].depth[cnt]);
	    cnt++;
	}
    }
    cnt = 0;
    for (j = 0; j < ny - 1; j++) {
	for (i = 0; i < nx - 1; i++) {
	    grid[gridno].icon[cnt].type = 4;
	    grid[gridno].icon[cnt].wetdry = 0;
	    grid[gridno].icon[cnt].nl[0] = j * nx + i;
	    grid[gridno].icon[cnt].nl[1] = j * nx + i + 1;
	    grid[gridno].icon[cnt].nl[2] = (j + 1) * nx + i + 1;
	    grid[gridno].icon[cnt].nl[3] = (j + 1) * nx + i;
	    grid[gridno].icon[cnt].nn = 4;
	    grid[gridno].icon[cnt].ngeom = 4;
	    cnt++;
	}
    }
/*
 * read dry nodes
 */
    while (fgets(buf, 255, fp) != NULL) {
	cnt = sscanf(buf, "%d %d", &i, &j);
	if (cnt == 2) {
	    cnt = (j - 1) * (nx - 1) + i - 1;
	    grid[gridno].icon[cnt].wetdry = 1;
	}
    }
    grid[gridno].gridtype = 1;	/* set gridtype to linear. */
    fclose(fp);
    set_grid_limits(gridno);
    default_isolines(&grid[gridno].ip);
    autoscale_isolines(grid[gridno].dmin, grid[gridno].dmax, &grid[gridno].ip);
    mindist = (grid[gridno].xmax - grid[gridno].xmin) * 0.01;
    return 1;
}