コード例 #1
0
// we just read a <; read until we find a >.
// Given <tag [attr=val attr=val] [/]>:
// - copy tag (or tag/) to buf
// - copy "attr=val attr=val" to attr_buf
//
// Return either
// XML_PARSE_TAG
// XML_PARSE_COMMENT
// XML_PARSE_EOF
// XML_PARSE_CDATA
//
int XML_PARSER::scan_tag(
    wxChar* buf, int _tag_len, wxChar* attr_buf, int attr_len
) {
    wxInt32 c;
    wxChar* buf_start = buf;
    bool found_space = false;
    int tag_len = _tag_len;

    for (int i=0; ; i++) {
        c = f->_getc();
        if ((char)c == EOF) return XML_PARSE_EOF;
        if (c == wxT('>')) {
            *buf = 0;
            if (attr_buf) *attr_buf = 0;
            return XML_PARSE_TAG;
        }
        if (_istspace(c)) {
            if (found_space && attr_buf) {
                if (--attr_len > 0) {
                    *attr_buf++ = c;
                }
            }
            found_space = true;
        } else if (c == wxT('/')) {
            if (--tag_len > 0) {
                *buf++ = c;
            }
        } else {
            if (found_space) {
                if (attr_buf) {
                    if (--attr_len > 0) {
                        *attr_buf++ = c;
                    }
                }
            } else {
                if (--tag_len > 0) {
                    *buf++ = c;
                }
            }
        }

        // check for comment start
        //
        if (i==2 && !_tcsncmp(buf_start, wxT("!--"), 3)) {
            return scan_comment();
        }
        if (i==7 && !_tcsncmp(buf_start, wxT("![CDATA["), 8)) {
            return scan_cdata(buf_start, tag_len);
        }
    }
    return 0;
}
コード例 #2
0
ファイル: Scanner.cpp プロジェクト: boxershorts7/School
void Scanner::scan() {
	char token = file.get();
	while (token != EOF && error == -1) {
		switch (token) {
		case ',':
			scan_punctuation(token, Token::COMMA);
			break;
		case '.':
			scan_punctuation(token, Token::PERIOD);
			break;
		case '?':
			scan_punctuation(token, Token::Q_MARK);
			break;
		case '(':
			scan_punctuation(token, Token::LEFT_PAREN);
			break;
		case ')':
			scan_punctuation(token, Token::RIGHT_PAREN);
			break;
		case ':':
			scan_colon();
			break;
		case '#':
			scan_comment();
			break;
		case '\'':
			scan_string();
			break;
		case '\n':
			line++;
			break;
		default:
			scan_id(token);
			break;
		}
		token = file.get();
	}

	if (error == -1) {
		line++;
		add_token("", Token::END);
	}
}
コード例 #3
0
ファイル: e.c プロジェクト: 8l/ted
void disp_page()
{
int row;
if (batch) return;
clrscr_w();
if (c_comment) {
/* if there are more than 300 lines in a comment, this will fail */
	scan_comment(cursor_row-300,cursor_row+page_len);
}
for(row=page_row;row<page_row+page_len && row < Lines; row++) {
	if (LangType>=0)
		disp_row(row);
	else
		disp_row_raw(row);
}
if (row-page_row < page_len) {
	set_att(0);
	disp_eof(row-page_row);
}
flush_buffer();
draw_scr();
set_att(0);
}
コード例 #4
0
ファイル: glptsp.c プロジェクト: BohanHsu/developer
TSP *tsp_read_data(char *fname)
{     struct dsa _dsa, *dsa = &_dsa;
      TSP *tsp = NULL;
      dsa->fname = fname;
      xprintf("tsp_read_data: reading TSP data from `%s'...\n",
         dsa->fname);
      dsa->fp = fopen(dsa->fname, "r");
      if (dsa->fp == NULL)
      {  xprintf("tsp_read_data: unable to open `%s' - %s\n",
            dsa->fname, strerror(errno));
         goto fail;
      }
      tsp = xmalloc(sizeof(TSP));
      tsp->name = NULL;
      tsp->type = TSP_UNDEF;
      tsp->comment = NULL;
      tsp->dimension = 0;
      tsp->edge_weight_type = TSP_UNDEF;
      tsp->edge_weight_format = TSP_UNDEF;
      tsp->display_data_type = TSP_UNDEF;
      tsp->node_x_coord = NULL;
      tsp->node_y_coord = NULL;
      tsp->dply_x_coord = NULL;
      tsp->dply_y_coord = NULL;
      tsp->tour = NULL;
      tsp->edge_weight = NULL;
      dsa->seqn = 1;
      if (get_char(dsa)) goto fail;
loop: if (scan_keyword(dsa)) goto fail;
      if (strcmp(dsa->token, "NAME") == 0)
      {  if (tsp->name != NULL)
         {  xprintf("%s:%d: NAME entry multiply defined\n", dsa->fname,
               dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_token(dsa, 0)) goto fail;
         if (strlen(dsa->token) == 0)
         {  xprintf("%s:%d: NAME entry incomplete\n", dsa->fname,
               dsa->seqn);
            goto fail;
         }
         tsp->name = xmalloc(strlen(dsa->token) + 1);
         strcpy(tsp->name, dsa->token);
         xprintf("tsp_read_data: NAME: %s\n", tsp->name);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "TYPE") == 0)
      {  if (tsp->type != TSP_UNDEF)
         {  xprintf("%s:%d: TYPE entry multiply defined\n", dsa->fname,
               dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_keyword(dsa)) goto fail;
         if (strcmp(dsa->token, "TSP") == 0)
            tsp->type = TSP_TSP;
         else if (strcmp(dsa->token, "ATSP") == 0)
            tsp->type = TSP_ATSP;
         else if (strcmp(dsa->token, "TOUR") == 0)
            tsp->type = TSP_TOUR;
         else
         {  xprintf("%s:%d: data type `%s' not recognized\n",
               dsa->fname, dsa->seqn, dsa->token);
            goto fail;
         }
         xprintf("tsp_read_data: TYPE: %s\n", dsa->token);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "COMMENT") == 0)
      {  if (tsp->comment != NULL)
         {  xprintf("%s:%d: COMMENT entry multiply defined\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_comment(dsa)) goto fail;
         tsp->comment = xmalloc(strlen(dsa->token) + 1);
         strcpy(tsp->comment, dsa->token);
         xprintf("tsp_read_data: COMMENT: %s\n", tsp->comment);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "DIMENSION") == 0)
      {  if (tsp->dimension != 0)
         {  xprintf("%s:%d: DIMENSION entry multiply defined\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_integer(dsa, 0, &tsp->dimension)) goto fail;
         if (tsp->dimension < 1)
         {  xprintf("%s:%d: invalid dimension\n", dsa->fname,
               dsa->seqn);
            goto fail;
         }
         xprintf("tsp_read_data: DIMENSION: %d\n", tsp->dimension);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "EDGE_WEIGHT_TYPE") == 0)
      {  if (tsp->edge_weight_type != TSP_UNDEF)
         {  xprintf("%s:%d: EDGE_WEIGHT_TYPE entry multiply defined\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_keyword(dsa)) goto fail;
         if (strcmp(dsa->token, "GEO") == 0)
            tsp->edge_weight_type = TSP_GEO;
         else if (strcmp(dsa->token, "EUC_2D") == 0)
            tsp->edge_weight_type = TSP_EUC_2D;
         else if (strcmp(dsa->token, "ATT") == 0)
            tsp->edge_weight_type = TSP_ATT;
         else if (strcmp(dsa->token, "EXPLICIT") == 0)
            tsp->edge_weight_type = TSP_EXPLICIT;
         else if (strcmp(dsa->token, "CEIL_2D") == 0)
            tsp->edge_weight_type = TSP_CEIL_2D;
         else
         {  xprintf("%s:%d: edge weight type `%s' not recognized\n",
               dsa->fname, dsa->seqn, dsa->token);
            goto fail;
         }
         xprintf("tsp_read_data: EDGE_WEIGHT_TYPE: %s\n", dsa->token);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "EDGE_WEIGHT_FORMAT") == 0)
      {  if (tsp->edge_weight_format != TSP_UNDEF)
         {  xprintf(
               "%s:%d: EDGE_WEIGHT_FORMAT entry multiply defined\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_keyword(dsa)) goto fail;
         if (strcmp(dsa->token, "UPPER_ROW") == 0)
            tsp->edge_weight_format = TSP_UPPER_ROW;
         else if (strcmp(dsa->token, "FULL_MATRIX") == 0)
            tsp->edge_weight_format = TSP_FULL_MATRIX;
         else if (strcmp(dsa->token, "FUNCTION") == 0)
            tsp->edge_weight_format = TSP_FUNCTION;
         else if (strcmp(dsa->token, "LOWER_DIAG_ROW") == 0)
            tsp->edge_weight_format = TSP_LOWER_DIAG_ROW;
         else
         {  xprintf("%s:%d: edge weight format `%s' not recognized\n",
               dsa->fname, dsa->seqn, dsa->token);
            goto fail;
         }
         xprintf("tsp_read_data: EDGE_WEIGHT_FORMAT: %s\n", dsa->token);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "DISPLAY_DATA_TYPE") == 0)
      {  if (tsp->display_data_type != TSP_UNDEF)
         {  xprintf("%s:%d: DISPLAY_DATA_TYPE entry multiply defined\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_colon(dsa)) goto fail;
         if (scan_keyword(dsa)) goto fail;
         if (strcmp(dsa->token, "COORD_DISPLAY") == 0)
            tsp->display_data_type = TSP_COORD_DISPLAY;
         else if (strcmp(dsa->token, "TWOD_DISPLAY") == 0)
            tsp->display_data_type = TSP_TWOD_DISPLAY;
         else
         {  xprintf("%s:%d: display data type `%s' not recognized\n",
               dsa->fname, dsa->seqn, dsa->token);
            goto fail;
         }
         xprintf("tsp_read_data: DISPLAY_DATA_TYPE: %s\n", dsa->token);
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "NODE_COORD_SECTION") == 0)
      {  int n = tsp->dimension, k, node;
         if (n == 0)
         {  xprintf("%s:%d: DIMENSION entry not specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (tsp->node_x_coord != NULL)
         {  xprintf("%s:%d: NODE_COORD_SECTION multiply specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_newline(dsa)) goto fail;
         tsp->node_x_coord = xcalloc(1+n, sizeof(double));
         tsp->node_y_coord = xcalloc(1+n, sizeof(double));
         for (node = 1; node <= n; node++)
            tsp->node_x_coord[node] = tsp->node_y_coord[node] = DBL_MAX;
         for (k = 1; k <= n; k++)
         {  if (scan_integer(dsa, 0, &node)) goto fail;
            if (!(1 <= node && node <= n))
            {  xprintf("%s:%d: invalid node number %d\n", dsa->fname,
                  dsa->seqn, node);
               goto fail;
            }
            if (tsp->node_x_coord[node] != DBL_MAX)
            {  xprintf("%s:%d: node number %d multiply specified\n",
                  dsa->fname, dsa->seqn, node);
               goto fail;
            }
            if (scan_number(dsa, 0, &tsp->node_x_coord[node]))
               goto fail;
            if (scan_number(dsa, 0, &tsp->node_y_coord[node]))
               goto fail;
            if (check_newline(dsa)) goto fail;
         }
      }
      else if (strcmp(dsa->token, "DISPLAY_DATA_SECTION") == 0)
      {  int n = tsp->dimension, k, node;
         if (n == 0)
         {  xprintf("%s:%d: DIMENSION entry not specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (tsp->dply_x_coord != NULL)
         {  xprintf("%s:%d: DISPLAY_DATA_SECTION multiply specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_newline(dsa)) goto fail;
         tsp->dply_x_coord = xcalloc(1+n, sizeof(double));
         tsp->dply_y_coord = xcalloc(1+n, sizeof(double));
         for (node = 1; node <= n; node++)
            tsp->dply_x_coord[node] = tsp->dply_y_coord[node] = DBL_MAX;
         for (k = 1; k <= n; k++)
         {  if (scan_integer(dsa, 0, &node)) goto fail;
            if (!(1 <= node && node <= n))
            {  xprintf("%s:%d: invalid node number %d\n", dsa->fname,
                  dsa->seqn, node);
               goto fail;
            }
            if (tsp->dply_x_coord[node] != DBL_MAX)
            {  xprintf("%s:%d: node number %d multiply specified\n",
                  dsa->fname, dsa->seqn, node);
               goto fail;
            }
            if (scan_number(dsa, 0, &tsp->dply_x_coord[node]))
               goto fail;
            if (scan_number(dsa, 0, &tsp->dply_y_coord[node]))
               goto fail;
            if (check_newline(dsa)) goto fail;
         }
      }
      else if (strcmp(dsa->token, "TOUR_SECTION") == 0)
      {  int n = tsp->dimension, k, node;
         if (n == 0)
         {  xprintf("%s:%d: DIMENSION entry not specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (tsp->tour != NULL)
         {  xprintf("%s:%d: TOUR_SECTION multiply specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_newline(dsa)) goto fail;
         tsp->tour = xcalloc(1+n, sizeof(int));
         for (k = 1; k <= n; k++)
         {  if (scan_integer(dsa, 1, &node)) goto fail;
            if (!(1 <= node && node <= n))
            {  xprintf("%s:%d: invalid node number %d\n", dsa->fname,
                  dsa->seqn, node);
               goto fail;
            }
            tsp->tour[k] = node;
         }
         if (scan_integer(dsa, 1, &node)) goto fail;
         if (node != -1)
         {  xprintf("%s:%d: extra node(s) detected\n", dsa->fname,
               dsa->seqn);
            goto fail;
         }
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "EDGE_WEIGHT_SECTION") == 0)
      {  int n = tsp->dimension, i, j, temp;
         if (n == 0)
         {  xprintf("%s:%d: DIMENSION entry not specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (tsp->edge_weight_format == TSP_UNDEF)
         {  xprintf("%s:%d: EDGE_WEIGHT_FORMAT entry not specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (tsp->edge_weight != NULL)
         {  xprintf("%s:%d: EDGE_WEIGHT_SECTION multiply specified\n",
               dsa->fname, dsa->seqn);
            goto fail;
         }
         if (check_newline(dsa)) goto fail;
         tsp->edge_weight = xcalloc(1+n*n, sizeof(int));
         switch (tsp->edge_weight_format)
         {  case TSP_FULL_MATRIX:
               for (i = 1; i <= n; i++)
               {  for (j = 1; j <= n; j++)
                  {  if (scan_integer(dsa, 1, &temp)) goto fail;
                     tsp->edge_weight[(i - 1) * n + j] = temp;
                  }
               }
               break;
            case TSP_UPPER_ROW:
               for (i = 1; i <= n; i++)
               {  tsp->edge_weight[(i - 1) * n + i] = 0;
                  for (j = i + 1; j <= n; j++)
                  {  if (scan_integer(dsa, 1, &temp)) goto fail;
                     tsp->edge_weight[(i - 1) * n + j] = temp;
                     tsp->edge_weight[(j - 1) * n + i] = temp;
                  }
               }
               break;
            case TSP_LOWER_DIAG_ROW:
               for (i = 1; i <= n; i++)
               {  for (j = 1; j <= i; j++)
                  {  if (scan_integer(dsa, 1, &temp)) goto fail;
                     tsp->edge_weight[(i - 1) * n + j] = temp;
                     tsp->edge_weight[(j - 1) * n + i] = temp;
                  }
               }
               break;
            default:
               goto fail;
         }
         if (check_newline(dsa)) goto fail;
      }
      else if (strcmp(dsa->token, "EOF") == 0)
      {  if (check_newline(dsa)) goto fail;
         goto done;
      }
      else
      {  xprintf("%s:%d: keyword `%s' not recognized\n", dsa->fname,
            dsa->seqn, dsa->token);
         goto fail;
      }
      goto loop;
done: xprintf("tsp_read_data: %d lines were read\n", dsa->seqn-1);
      fclose(dsa->fp);
      return tsp;
fail: if (tsp != NULL)
      {  if (tsp->name != NULL) xfree(tsp->name);
         if (tsp->comment != NULL) xfree(tsp->comment);
         if (tsp->node_x_coord != NULL) xfree(tsp->node_x_coord);
         if (tsp->node_y_coord != NULL) xfree(tsp->node_y_coord);
         if (tsp->dply_x_coord != NULL) xfree(tsp->dply_x_coord);
         if (tsp->dply_y_coord != NULL) xfree(tsp->dply_y_coord);
         if (tsp->tour != NULL) xfree(tsp->tour);
         if (tsp->edge_weight != NULL) xfree(tsp->edge_weight);
         xfree(tsp);
      }
      if (dsa->fp != NULL) fclose(dsa->fp);
      return NULL;
}