Пример #1
0
static int
scan_number_assignment(struct misc_file_buffer* file, struct scan_token* token,
		      int line)
{
	int rc;

	rc = scan_number(file, &token->content.number.number, line);
	if (rc)
		return rc;
	skip_blanks(file);
	if (misc_get_char(file, 0) != '=') {
		error_reason("Line %d: number expected as keyword", line);
		return -1;
	}
	file->pos++;
	skip_blanks(file);
	rc = scan_value_string(file, &token->content.number.value, line);
	if (rc)
		return rc;
	if (skip_trailing_blanks(file)) {
		error_reason("Line %d: unexpected characters at end of line",
			     line);
		return -1;
	}
	token->id = scan_id_number_assignment;
	token->line = line;
	return 0;
}
Пример #2
0
std::string in_numeric_range(cmdline_param const & param, char const * value,
    void const * range_info) {

    const char * const MSG = "Error with {1}=\"{2}\": expected a value between "
        "{3=min} and {4=max} (inclusive), formatted as {5}.";
    precondition(range_info != nullptr);

    numeric_range_info const & nri =
        *reinterpret_cast<numeric_range_info const *>(range_info);
    number_info info;
    auto ptr_to_null_char = strchr(value, 0);
    auto end = scan_number(value, ptr_to_null_char, nri.allowed_formats, info);
    auto valid = (end == ptr_to_null_char);
    if (valid) {
        if (info.format == numeric_formats::floating_point_only) {
            if (!std::isnan(nri.min) && info.floating_point < nri.min) {
                valid = false;
            } else if (!std::isnan(nri.max) && info.floating_point > nri.max) {
                valid = false;
            }
        } else {
            int64_t n = static_cast<int64_t>(info.whole_number);
            if (info.negative) {
                n *= -1;
            }
            valid = n >= nri.min && n <= nri.max;
        }
    }
    return valid ? "" :
        interp(MSG, {param.get_preferred_name(), value, nri.min, nri.max,
            get_names_for_numeric_formats(nri.allowed_formats)});
}
Пример #3
0
/* Scan a line of the specified /proc/devices FILE buffer and advance the
 * current file position pointer respectively. If the current line matches
 * the correct pattern, fill in the corresponding data into ENTRY and return 0.
 * Return non-zero otherwise. */
static int
scan_dev_entry(struct file_buffer* file, struct proc_dev_entry* entry,
	       int blockdev)
{
	int rc;
	size_t dev_major;
	char* name;

	/* Scan for: (\s*)(\d+)(\s+)(\S+)(\.*)$ */
	skip_whitespaces(file);
	rc = scan_number(file, &dev_major);
	if (rc)
		return rc;
	rc = skip_whitespaces(file);
	if (rc)
		return rc;
	rc = scan_name(file, &name);
	if (rc)
		return rc;
	skip_line(file);
	entry->device = makedev(dev_major, 0);
	entry->name = name;
	entry->blockdev = blockdev;
	return 0;
}
Пример #4
0
int get_prefix_1(inet_prefix *dst, char *arg, int family)
{
	int err;
	unsigned plen;
	char *slash;

	memset(dst, 0, sizeof(*dst));

	if (strcmp(arg, "default") == 0 || strcmp(arg, "any") == 0) {
		dst->family = family;
		dst->bytelen = 0;
		dst->bitlen = 0;
		return 0;
	}

	slash = strchr(arg, '/');
	if (slash)
		*slash = 0;
	err = get_addr_1(dst, arg, family);
	if (err == 0) {
		dst->bitlen = (dst->family == AF_INET6 ? 128 : 32);
		if (slash) {
			if (scan_number(slash+1, &plen) || plen > dst->bitlen) {
				err = -1;
				goto done;
			}
			dst->bitlen = plen;
		}
	}
done:
	if (slash)
		*slash = '/';
	return err;
}
Пример #5
0
/******************************************************************
			scan()
See prlex.h for return values other than characters (i.e. > 256)
*******************************************************************/
int scan()
{
	ini_scan();
	getachar();
	if(Ch == EOF)
		return(EOF);
	switch(Ctype[Ch])
	{

	case DI:
		MY_ASSERT(isdigit(Ch)); /* double check */
	case SI:
		MY_ASSERT(isdigit(Ch) || Ch == '-' || Ch == '+');
		return(scan_number(Ch));

	case QU:
		MY_ASSERT(Ch == '"');
		scan_string();
		return(TOKEN_STRING);

	case BR:
		MY_ASSERT(Ch == ')' || Ch == '(');
		return(Ch);

#ifdef CLIPS_SYNTAX
	case QE:
		scan_identifier(?);
		return(TOKEN_VAR);
	case AL:
	case AU:
	case OT:
		scan_identifier(Ch);
		return(TOKEN_ATOM);
#else
	case AL:
		MY_ASSERT(islower(Ch));
		scan_identifier(Ch);
		return(TOKEN_ATOM);

	case US:
		MY_ASSERT(Ch == '_');

	case AU:
		scan_identifier(Ch);
		return(TOKEN_VAR);
#endif
	case CC:
		return(SCAN_ERR);
#ifdef  CHARACTER
	case AP:
		return(scan_character());
#endif
	default:
		return(Ch);
	}
}
Пример #6
0
char *read_cnumber(struct token *tok, const char *s, const char *e, tok_message_queue *mq) {
	struct scan_number sn;
	
	tok->type = scan_number(&sn, s, e);
	if (tok->type == TOK_INTEGER)
		read_integer(&tok->integer, &sn, mq);
	else
		read_floating(&tok->floating, &sn, mq);
	
	return (char*)sn.end;
}
Пример #7
0
/* Scan a line of the specified /proc/partitions FILE buffer and advance the
 * current file position pointer respectively. If the current line matches
 * the correct pattern, fill in the corresponding data into ENTRY and return 0.
 * Return non-zero otherwise. */
static int
scan_part_entry(struct file_buffer* file, struct proc_part_entry* entry)
{
	int rc;
	size_t dev_major;
	size_t dev_minor;
	size_t blockcount;
	char* name;

	/* Scan for: (\s*)(\d+)(\s+)(\d+)(\s+)(\d+)(\s+)(\S+)(\.*)$ */
	skip_whitespaces(file);
	rc = scan_number(file, &dev_major);
	if (rc)
		return rc;
	rc = skip_whitespaces(file);
	if (rc)
		return rc;
	rc = scan_number(file, &dev_minor);
	if (rc)
		return rc;
	rc = skip_whitespaces(file);
	if (rc)
		return rc;
	rc = scan_number(file, &blockcount);
	if (rc)
		return rc;
	rc = skip_whitespaces(file);
	if (rc)
		return rc;
	rc = scan_name(file, &name);
	if (rc)
		return rc;
	skip_line(file);
	entry->device = makedev(dev_major, dev_minor);
	entry->blockcount = blockcount;
	entry->name = name;
	return 0;
}
Пример #8
0
static _TKN_TYPE_ scan_Dgt(_NIL_TYPE_)
 { return scan_number(); }
Пример #9
0
int
main (int argc, char *argv[])
{
  const unsigned char *p, *eof;
  int i;
  const char *error_string;
  struct stat sb;

  Marpa_Config marpa_configuration;

  Marpa_Grammar g;
  Marpa_Recognizer r;
  /* Longest rule is 4 symbols */
  Marpa_Symbol_ID rhs[4];

  int fd = open (argv[1], O_RDONLY);
  //initialize a stat for getting the filesize
  if (fstat (fd, &sb) == -1)
    {
      perror ("fstat");
      return 1;
    }
  //do the actual mmap, and keep pointer to the first element
  p = (unsigned char *) mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  //something went wrong
  if (p == MAP_FAILED)
    {
      perror ("mmap");
      return 1;
    }

  marpa_c_init (&marpa_configuration);
  g = marpa_g_new (&marpa_configuration);
  if (!g)
    {
      Marpa_Error_Code errcode =
        marpa_c_error (&marpa_configuration, &error_string);
      printf ("marpa_g_new returned %d: %s", errcode, error_string);
      exit (1);
    }

  ((S_begin_array = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_begin_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_end_array = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_end_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_name_separator = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_value_separator = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_member = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_value = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_false = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_null = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_true = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_array = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_number = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_string = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_object_contents = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_array_contents = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);

  rhs[0] = S_false;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_null;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_true;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_object;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_array;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_number;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_string;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);

  rhs[0] = S_begin_array;
  rhs[1] = S_array_contents;
  rhs[2] = S_end_array;
  (marpa_g_rule_new (g, S_array, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  rhs[0] = S_begin_object;
  rhs[1] = S_object_contents;
  rhs[2] = S_end_object;
  (marpa_g_rule_new (g, S_object, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  (marpa_g_sequence_new
   (g, S_array_contents, S_value, S_value_separator, 0,
    MARPA_PROPER_SEPARATION) >= 0) || fail ("marpa_g_sequence_new", g);
  (marpa_g_sequence_new
   (g, S_object_contents, S_member, S_value_separator, 0,
    MARPA_PROPER_SEPARATION) >= 0) || fail ("marpa_g_sequence_new", g);

  rhs[0] = S_string;
  rhs[1] = S_name_separator;
  rhs[2] = S_value;
  (marpa_g_rule_new (g, S_member, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  if (0)
    {
      (marpa_g_symbol_is_terminal_set (g, S_value_separator, 1) >= 0) ||
        fail ("marpa_g_symbol_is_terminal", g);
    }

  (marpa_g_start_symbol_set (g, S_value) >= 0)
    || fail ("marpa_g_start_symbol_set", g);
  if (marpa_g_precompute (g) < 0)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  r = marpa_r_new (g);
  if (!r)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  if (!marpa_r_start_input (r))
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }

  i = 0;
  eof = p + sb.st_size;
  while (p + i < eof)
    {
      Marpa_Symbol_ID token;
      const int start_of_token = i;

      switch (p[i])
        {
        case '-':
        case '+':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
          i = scan_number (p + i, eof) - p;
          token = S_number;
          break;
        case '"':
          i = scan_string (p + i, eof) - p;
          token = S_string;
          break;
        case '[':
          token = S_begin_array;
          i++;
          break;
        case ']':
          token = S_end_array;
          i++;
          break;
        case '{':
          token = S_begin_object;
          i++;
          break;
        case '}':
          token = S_end_object;
          i++;
          break;
        case ',':
          token = S_value_separator;
          i++;
          break;
        case ':':
          token = S_name_separator;
          i++;
          break;
        case 'n':
          i = scan_constant ("null", (p + i), eof) - p;
          token = S_null;
          break;
        case ' ':
        case 0x09:
        case 0x0A:
        case 0x0D:
          i++;
          goto NEXT_TOKEN;
        default:
          marpa_g_error (g, &error_string);
          printf ("lexer failed at char %d: '%c'", i, p[i]);
          exit (1);
        }
      /* Token value of zero is not allowed, so we add one */
      if (0)
        fprintf (stderr, "reading token %ld, %s\n",
                 (long) token, symbol_name (token));
      int status = marpa_r_alternative (r, token, start_of_token + 1, 1);
      if (status != MARPA_ERR_NONE)
        {
          Marpa_Symbol_ID expected[20];
          int count_of_expected = marpa_r_terminals_expected (r, expected);
          int i;
          for (i = 0; i < count_of_expected; i++)
            {
              fprintf (stderr, "expecting symbol %ld, %s\n",
                       (long) i, symbol_name (expected[i]));
            }
          marpa_g_error (g, &error_string);
          fprintf (stderr,
                   "marpa_alternative(%p,%ld,%s,%ld,1) returned %d: %s", r,
                   (long) token, symbol_name (token),
                   (long) (start_of_token + 1), status, error_string);
          exit (1);
        }
      status = marpa_r_earleme_complete (r);
      if (status < 0)
        {
          marpa_g_error (g, &error_string);
          printf ("marpa_earleme_complete returned %d: %s", status,
                  error_string);
          exit (1);
        }
    NEXT_TOKEN:;
    }

  {
    Marpa_Bocage bocage;
    Marpa_Order order;
    Marpa_Tree tree;
    bocage = marpa_b_new (r, -1);
    if (!bocage)
      {
        int errcode = marpa_g_error (g, &error_string);
        printf ("marpa_bocage_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    order = marpa_o_new (bocage);
    if (!order)
      {
        int errcode = marpa_g_error (g, &error_string);
        printf ("marpa_order_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    tree = marpa_t_new (order);
    if (!tree)
      {
        Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
        printf ("marpa_t_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    {
      Marpa_Value value = NULL;
      int column = 0;
      int tree_status;
      tree_status = marpa_t_next (tree);
      if (tree_status <= -1)
        {
          Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
          printf ("marpa_t_next returned %d: %s", errcode, error_string);
          exit (1);
        }

      value = marpa_v_new (tree);
      if (!value)
        {
          Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
          printf ("marpa_v_new returned %d: %s", errcode, error_string);
          exit (1);
        }
      while (1)
        {
          Marpa_Step_Type step_type = marpa_v_step (value);
          Marpa_Symbol_ID token;
          if (step_type < 0)
            {
              Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
              printf ("marpa_v_event returned %d: %s", errcode, error_string);
              exit (1);
            }
          if (step_type == MARPA_STEP_INACTIVE)
            {
              if (0)
                printf ("No more events\n");
              break;
            }
          if (step_type != MARPA_STEP_TOKEN)
            continue;
          token = marpa_v_token (value);
          if (1)
            {
              if (column > 60)
                {
                  putchar ('\n');
                  column = 0;
                }
              if (token == S_begin_array)
                {
                  putchar ('[');
                  column++;
                  continue;
                }
              if (token == S_end_array)
                {
                  putchar (']');
                  column++;
                  continue;
                }
              if (token == S_begin_object)
                {
                  putchar ('{');
                  column++;
                  continue;
                }
              if (token == S_end_object)
                {
                  putchar ('}');
                  column++;
                  continue;
                }
              if (token == S_name_separator)
                {
                  putchar (':');
                  column++;
                  continue;
                }
              if (token == S_value_separator)
                {
                  putchar (',');
                  column++;
                  continue;
                }
              if (token == S_null)
                {
                  fputs ("undef", stdout);
                  column += 5;
                  continue;
                }
              if (token == S_true)
                {
                  putchar ('1');
                  column++;
                  continue;
                }
              if (token == S_false)
                {
                  putchar ('0');
                  column++;
                  continue;
                }
              if (token == S_number)
                {
                  /* We added one to avoid zero
                   * Now we must subtract it
                   */
                  int i;
                  const int start_of_number = marpa_v_token_value (value) - 1;
                  const int end_of_number =
                    scan_number (p + start_of_number, eof) - p;
                  column += 2 + (end_of_number - start_of_number);

                  /* We output numbers as Perl strings */
                  putchar ('"');
                  for (i = start_of_number; i < end_of_number; i++)
                    {
                      putchar (p[i]);
                    }
                  putchar ('"');
                  continue;
                }
              if (token == S_string)
                {
                  /* We added one to avoid zero
                   * Now we must subtract it, but we also
                   * add one for the initial double quote
                   */
                  int i;
                  const int start_of_string = marpa_v_token_value (value);
                  /* Subtract one for the final double quote */
                  const int end_of_string =
                    (scan_string (p + start_of_string, eof) - p) - 1;

                  /* We add back the inital and final double quotes,
                   * and increment the column accordingly.
                   */
                  column += 2;
                  putchar ('"');
                  i = start_of_string;
                  while (i < end_of_string)
                    {
                      const unsigned char ch0 = p[i++];
                      if (ch0 == '\\')
                        {
                          const unsigned char ch1 = p[i++];
                          switch (ch1)
                            {
                            case '\\':
                            case '/':
                            case '"':
                            case 'b':
                            case 'f':
                            case 'n':
                            case 'r':
                            case 't':
                              /* explicit non-hex JSON escapes are the same
                               * as the Perl escapes */
                              column += 2;
                              putchar ('\\');
                              putchar (ch1);
                              continue;
                            case 'u':
                              {
                                int digit;
                                putchar ('x');
                                putchar ('{');
                                for (digit = 0; digit < 4; digit++)
                                  {
                                    const unsigned char hex_ch = p[i + digit];
                                    if ((hex_ch >= 'a' && hex_ch <= 'f')
                                        || (hex_ch >= 'A' && hex_ch <= 'F')
                                        || (hex_ch >= '0' && hex_ch <= '9'))
                                      {
                                        printf
                                          ("illegal char in JSON hex number at location %d (0x%x): '%c' ",
                                           i, hex_ch, hex_ch);
                                        exit (1);
                                      }
                                    putchar (hex_ch);
                                  }
                                putchar ('}');
                                column += 7;
                                i += 4;
                              }
                              continue;
                            default:
                              printf
                                ("illegal escaped char in JSON input (0x%x):'%c' ",
                                 i, p[i]);
                              exit (1);
                            }
                        }

                      /* An unescaped JSON char, one that does not need Perl escaping */
                      if (ch0 == '_' || (ch0 >= '0' && ch0 <= '9')
                          || (ch0 >= 'a' && ch0 <= 'z') || (ch0 >= 'A'
                                                            && ch0 <= 'Z'))
                        {
                          putchar (ch0);
                          column++;
                          continue;
                        }
                      /* An unescaped JSON char,
                       * but one which quotemeta would escape for Perl */
                      putchar ('\\');
                      putchar (ch0);
                      column += 2;
                      continue;
                    }
                  putchar ('"');
                  continue;
                }
              fprintf (stderr, "Unknown symbol %s at %d",
                       symbol_name (token), marpa_v_token_value (value) - 1);
              exit (1);
            }
        }
      if (column > 60)
        {
          putchar ('\n');
          column = 0;
        }
    }
  }

  return 0;
}
Пример #10
0
int scan(scan_info *info)
{
    int err = 0;
    int sym_code = SVO_NONE;
    const char **in = &(info->scanner);
    char tmp[SCAN_SIZE+1];

    while((**in != 0) && (isspace(**in))) (*in)++;

    if(isalpha(**in)){

        scan_ident(in,info->token,SCAN_SIZE);

        sym_code = token_to_id(info->token);
        if(sym_code == SVO_NONE) sym_code = SVO_IDENT;

    } else if(isdigit(**in)){

        scan_number(in,info->token,SCAN_SIZE);
        sym_code = SVO_NUMBER;
    } else{
        switch(**in){
        case '#':
            sym_code = SVO_END;
            break;
        case '>':
        case '<':
        case '=':
        case '!':

            scan_comp(in,info->token,SCAN_SIZE);
            break;

        case '"':

            err = scan_string(info,SCAN_SIZE);

            if(err != SHE_NO_ERROR){
                parse_vars_in_string(info->token,tmp,SCAN_SIZE);
                strncpy(info->token,tmp,SCAN_SIZE);
                info->token[SCAN_SIZE] = 0;
            }

            sym_code = SVO_DQ_STRING;
            break;
        case '\'':

            sym_code = SVO_SQ_STRING;
            err =scan_string(info,SCAN_SIZE);
            break;
        case 0:

            *(info->token) = 0;
            sym_code = SVO_END;
            break;
        default:
            info->token[0] = **in;
            info->token[1] = 0;
            (*in)++;
            break;

        }
        if(sym_code == SVO_NONE) sym_code = token_to_id(info->token);

    }

    info->sym_code = sym_code;

    return err;
}
Пример #11
0
int
luna_scan(luna_lexer_t *self) {
  int c;
  token(ILLEGAL);

  // deferred outdents
  if (self->outdents) return outdent(self);

  // scan
  scan:
  switch (c = next) {
    case ' ':
    case '\t': goto scan;
    case '(': return token(LPAREN);
    case ')': return token(RPAREN);
    case '{': return token(LBRACE);
    case '}': return token(RBRACE);
    case '[': return token(LBRACK);
    case ']': return token(RBRACK);
    case ',': return token(COMMA);
    case '.': return token(OP_DOT);
    case '%': return token(OP_MOD);
    case '^': return token(OP_BIT_XOR);
    case '~': return token(OP_BIT_NOT);
    case '?': return token(QMARK);
    case ':': return token(COLON);
    case '@':
      self->tok.value.as_string = "self";
      return token(ID);
    case '+':
      switch (next) {
        case '+': return token(OP_INCR);
        case '=': return token(OP_PLUS_ASSIGN);
        default: return undo, token(OP_PLUS);
      }
    case '-':
      switch (next) {
        case '-': return token(OP_DECR);
        case '=': return token(OP_MINUS_ASSIGN);
        default: return undo, token(OP_MINUS);
      }
    case '*':
      switch (next) {
        case '=': return token(OP_MUL_ASSIGN);
        case '*': return token(OP_POW);
        default: return undo, token(OP_MUL);
      }
    case '/':
      return '=' == next
        ? token(OP_DIV_ASSIGN)
        : (undo, token(OP_DIV));
    case '!':
      return '=' == next
        ? token(OP_NEQ)
        : (undo, token(OP_NOT));
    case '=':
      return '=' == next
        ? token(OP_EQ)
        : (undo, token(OP_ASSIGN));
    case '&':
      switch (next) {
        case '&':
          return '=' == next
            ? token(OP_AND_ASSIGN)
            : (undo, token(OP_AND));
        default:
          return undo, token(OP_BIT_AND);
      }
    case '|':
      switch (next) {
        case '|':
          return '=' == next
            ? token(OP_OR_ASSIGN)
            : (undo, token(OP_OR));
        default:
          return undo, token(OP_BIT_OR);
      }
    case '<':
      switch (next) {
        case '=': return token(OP_LTE);
        case '<': return token(OP_BIT_SHL);
        default: return undo, token(OP_LT);
      }
    case '>':
      switch (next) {
        case '=': return token(OP_GTE);
        case '>': return token(OP_BIT_SHR);
        default: return undo, token(OP_GT);
      }
    case '#':
      while ((c = next) != '\n' && c) ; undo;
      goto scan;
    case '\n':
      return scan_newline(self);
    case '"':
    case '\'':
      return scan_string(self, c);
    case 0:
      if (self->indents) {
        --self->indents;
        return token(OUTDENT);
      }
      token(EOS);
      return 0;
    default:
      if (isalpha(c) || '_' == c) return scan_ident(self, c);
      if (isdigit(c) || '.' == c) return scan_number(self, c);
      error("illegal character");
      return 0;
  }
}
Пример #12
0
int getToken()
{	char c;
 
       if (activeToken)
       { activeToken = FALSE;
         return ttype;
       }   // we do not need an else because the function returns in the body 
           // of the if
   
       skipSpace();   
       tokenLength = 0;
       c = getchar();
       switch (c)
       {   case '.': return DOT;
           case '+': return PLUS;
           case '-': return MINUS;
           case '/': return DIV;
           case '*': return MULT;
           case '=': return EQUAL;
           case ':': return COLON;
           case ',': return COMMA;
           case ';': return SEMICOLON;
           case '[': return LBRAC;
           case ']': return RBRAC;
           case '(': return LPAREN;
           case ')': return RPAREN;
	   	   case '{': return LBRACE;
	   	   case '}': return RBRACE;
           case '<':
                      c = getchar();
                       if (c == '=')
                          return LTEQ;
                       else
                       if (c == '>')
                          return NOTEQUAL;
                       else
                       {
                          ungetc(c,stdin);
                          return LESS;
                       }
           case '>': 
                        c = getchar();
                        if (c == '=')
                           return GTEQ;
                        else
                        {
                           ungetc(c, stdin);
                           return GREATER;
                        }
           
           default :
			if (isdigit(c))
			{	ungetc(c,stdin);
				return scan_number();
			}
			else
			if (isalpha(c))
			{	ungetc(c,stdin);
				return scan_id_or_keyword();
			}
			else
			if (c == EOF)
				return EOF;
			else
				return ERROR;
	}
}
Пример #13
0
bool tokz_get_token(Tokenizer *tokz, Token *tok)
{
    int c, c2, e;
    
    if (!(tokz->flags&TOKZ_READ_FROM_BUFFER))
    assert(tokz->file!=NULL);
    
    tok_free(tok);
    
    if(!TOK_IS_INVALID(&(tokz->ungettok))){
        *tok=tokz->ungettok;
        tokz->ungettok.type=TOK_INVALID;
        return TRUE;
    }

    while(1){
    
        e=0;
        
        do{
            c=GETCH();
        }while(c!='\n' && c!=EOF && isspace(c));
    
        tok->line=tokz->line;
    
        switch(c){
        case EOF:
            TOK_SET_OP(tok, OP_EOF);
            return TRUE;
            
        case '\n':
            INC_LINE();
            
            if(tokz->flags&TOKZ_IGNORE_NEXTLINE)
                continue;
            
            TOK_SET_OP(tok, OP_NEXTLINE);
            
            return TRUE;
            
        case '\\':
            do{
                c=GETCH();
                if(c==EOF){
                    TOK_SET_OP(tok, OP_EOF);
                    return FALSE;
                }
                if(!isspace(c) && e==0){
                    e=E_TOKZ_EOL_EXPECTED;
                    tokz_warn_error(tokz, tokz->line, e);
                    if(!(tokz->flags&TOKZ_ERROR_TOLERANT))
                        return FALSE;
                }
            }while(c!='\n');
            
            INC_LINE();
            continue;

        case '#':
            if(tokz->flags&TOKZ_READ_COMMENTS){
                e=scan_line_comment(tok, tokz);
                break;
            }else if((e=skip_line_comment(tokz))){
                break;
            }
            
            continue;
            
        case '/':
            c2=GETCH();
            
            if(c2=='='){
                TOK_SET_OP(tok, OP_AS_DIV);
                return TRUE;
            }
            
            if(c2!='*'){
                UNGETCH(c2);
                TOK_SET_OP(tok, OP_DIV);
                return TRUE;
            }
            
            if(tokz->flags&TOKZ_READ_COMMENTS){
                e=scan_c_comment(tok, tokz);
                break;
            }else if((e=skip_c_comment(tokz))){
                break;
            }
            
            continue;
            
        case '\"':
            e=scan_string(tok, tokz, TRUE);
            break;

        case '\'':
            e=scan_char(tok, tokz);
            break;

        default: 
            if(('0'<=c && c<='9') || c=='-' || c=='+'){
                e=scan_number(tok, tokz, c);
                break;
            }

             if(START_IDENT(c))
                e=scan_identifier(tok, tokz, c);
            else
                e=scan_op(tok, tokz, c);
        }
        
        if(!e)
            return TRUE;
        
        tokz_warn_error(tokz, tokz->line, e);
        return FALSE;
    }
}
Пример #14
0
/*
 * The scanner
 *
 */
int mcy_lex(void)
{
	static const WCHAR ustr_dot1[] = { '.', '\n', 0 };
	static const WCHAR ustr_dot2[] = { '.', '\r', '\n', 0 };
	static int isinit = 0;
	int ch;

	if(!isinit)
	{
		isinit++;
		set_codepage(WMC_DEFAULT_CODEPAGE);
		add_token(tok_keyword,	ustr_codepages,		tCODEPAGE,	0, NULL, 0);
		add_token(tok_keyword,	ustr_facility,		tFACILITY,	0, NULL, 1);
		add_token(tok_keyword,	ustr_facilitynames,	tFACNAMES,	0, NULL, 1);
		add_token(tok_keyword,	ustr_language,		tLANGUAGE,	0, NULL, 1);
		add_token(tok_keyword,	ustr_languagenames,	tLANNAMES,	0, NULL, 1);
		add_token(tok_keyword,	ustr_messageid,		tMSGID,		0, NULL, 1);
		add_token(tok_keyword,	ustr_messageidtypedef,	tTYPEDEF,	0, NULL, 1);
		add_token(tok_keyword,	ustr_outputbase,	tBASE,		0, NULL, 1);
		add_token(tok_keyword,	ustr_severity,		tSEVERITY,	0, NULL, 1);
		add_token(tok_keyword,	ustr_severitynames,	tSEVNAMES,	0, NULL, 1);
		add_token(tok_keyword,	ustr_symbolicname,	tSYMNAME,	0, NULL, 1);
		add_token(tok_severity,	ustr_error,		0x03,		0, NULL, 0);
		add_token(tok_severity,	ustr_warning,		0x02,		0, NULL, 0);
		add_token(tok_severity,	ustr_informational,	0x01,		0, NULL, 0);
		add_token(tok_severity,	ustr_success,		0x00,		0, NULL, 0);
		add_token(tok_facility,	ustr_application,	0xFFF,		0, NULL, 0);
		add_token(tok_facility,	ustr_system,		0x0FF,		0, NULL, 0);
		add_token(tok_language,	ustr_english,		0x409,		437, ustr_msg00001, 0);
	}

	empty_unichar_stack();

	while(1)
	{
		if(want_line)
		{
			while((ch = get_unichar()) != '\n')
			{
				if(ch == EOF)
					xyyerror("Unexpected EOF\n");
				push_unichar(ch);
			}
			newline();
			push_unichar(ch);
			push_unichar(0);
			if(!unistrcmp(ustr_dot1, get_unichar_stack()) || !unistrcmp(ustr_dot2, get_unichar_stack()))
			{
				want_line = 0;
				/* Reset the codepage to our default after each message */
				set_codepage(WMC_DEFAULT_CODEPAGE);
				return tMSGEND;
			}
			mcy_lval.str = xunistrdup(get_unichar_stack());
			return tLINE;
		}

		ch = get_unichar();

		if(ch == EOF)
			return EOF;

		if(ch == '\n')
		{
			newline();
			if(want_nl)
			{
				want_nl = 0;
				return tNL;
			}
			continue;
		}

		if(isisochar(ch))
		{
			if(want_file)
			{
				int n = 0;
				while(n < 8 && isisochar(ch))
				{
					int t = char_table[ch];
					if((t & CH_PUNCT) || !(t & CH_SHORTNAME))
						break;

					push_unichar(ch);
					n++;
					ch = get_unichar();
				}
				unget_unichar(ch);
				push_unichar(0);
				want_file = 0;
				mcy_lval.str = xunistrdup(get_unichar_stack());
				return tFILE;
			}

			if(char_table[ch] & CH_IDENT)
			{
				token_t *tok;
				while(isisochar(ch) && (char_table[ch] & (CH_IDENT|CH_NUMBER)))
				{
					push_unichar(ch);
					ch = get_unichar();
				}
				unget_unichar(ch);
				push_unichar(0);
				if(!(tok = lookup_token(get_unichar_stack())))
				{
					mcy_lval.str = xunistrdup(get_unichar_stack());
					return tIDENT;
				}
				switch(tok->type)
				{
				case tok_keyword:
					return tok->token;

				case tok_language:
					codepage = tok->codepage;
					/* Fall through */
				case tok_severity:
				case tok_facility:
					mcy_lval.tok = tok;
					return tTOKEN;

				default:
					internal_error(__FILE__, __LINE__, "Invalid token type encountered\n");
				}
			}

			if(isspace(ch))	/* Ignore space */
				continue;

			if(isdigit(ch))
				return scan_number(ch);
		}

		switch(ch)
		{
		case ':':
		case '=':
		case '+':
		case '(':
		case ')':
			return ch;
		case ';':
			while(ch != '\n' && ch != EOF)
			{
				push_unichar(ch);
				ch = get_unichar();
			}
			newline();
			push_unichar(ch);	/* Include the newline */
			push_unichar(0);
			mcy_lval.str = xunistrdup(get_unichar_stack());
			return tCOMMENT;
		default:
			xyyerror("Invalid character '%c' (0x%04x)\n", isisochar(ch) && isprint(ch) ? ch : '.', ch);
		}
	}
}
game* game_from_rle(FILE* rle_file) {
	char buffer[MAX_LINE_SIZE ]; // need to check if row is larger than expected so allocate larger buffer
	char* pos; 
	int x = -1, y = -1;
	int i; 
	int row = 0; 
	int col = 0; 
	int line = 0; 
	int num_repeat; 
	game* g = NULL;  

	while (NULL != fgets(buffer, MAX_LINE_SIZE, rle_file)) {

		++line; //Increment current line
		//printf("Inputting row: %d\n", row); 

		if (buffer[0]=='#') { //Comments, ignore the line
			continue; 
		}

		else if (buffer[0] == 'x') { //This is the special line declaring the size of the matrix
			sscanf(buffer, "x = %d, y = %d", &x, &y); 
			if (x < 0 || y < 0) {
				fprintf(stderr, "Error reading RLE file.\n"); 
				return NULL; 
			}

			printf("x=%d, y=%d\n", x, y); 
			
			g = new_game(y, x*2); //Extra space to the right 			
			continue; 
		}
		
		pos = buffer; 

		while ( (pos = scan_whitespace(pos)) && ('\0' != *pos) ) { //while we haven't hit the of the line, or gone outside bounds of 2D-array
			num_repeat = 1; //Number to repeat is 1 by default
 
			if (is_number(*pos)) {
				sscanf(pos, "%d", &num_repeat); //Get the number of times the next character repeats 
				//printf("num_repeat: %d\n", num_repeat); 
				pos = scan_number(pos); //Scan past the number
			}
			if (RLE_ENDROW == *pos) { //If we hit the end of row character, increment rows and go to next char
				for (i = 0; i < num_repeat; i++) { //Make a new row num_repeat times
					for ( ; col < x; col++) { //Implicitly the end of the row is all dead cells
						g->board[row][col] = DEAD; 
					} 
					col = 0; //Start at first column of the next row
					row++; 
				}
				pos++; 
				continue; 
			}
			if (RLE_ALIVE == *pos) {
				for (i = 0; i < num_repeat; i++) {
					g->board[row][col++] = ALIVE; 
				}
				pos++; 
			}	
			else if (RLE_DEAD == *pos) {
				for (i = 0; i < num_repeat; i++) {
					g->board[row][col++] = DEAD; 
				}
				pos++; 
			}
			else if (RLE_ENDFILE == *pos) {
				printf("RLE INPUT SUCCESSFULLY COMPLETED\n"); 
				return g; 
			}
			else {
				fprintf(stderr, "INVALID CHARACTER FOUND IN RLE FILE on line %d, col %d: %c\n", line, pos - buffer,  *pos); 
				return NULL; 
			}
			
		}
	}
	return g; 
}
Пример #16
0
token_type getToken()
{
    char c;

    if (active_token)
    {
        active_token = 0;
        return t_type;
    }
    skip_space();
    token_length = 0;
    current_token[0] = '\0';
    c = getchar();
    switch (c)
    {
        case '.': t_type = DOT;        return t_type;
        case '+': t_type = PLUS;       return t_type;
        case '-': t_type = MINUS;      return t_type;
        case '/': t_type = DIV;        return t_type;
        case '*': t_type = MULT;       return t_type;
        case '=': t_type = EQUAL;      return t_type;
        case ':': t_type = COLON;      return t_type;
        case ',': t_type = COMMA;      return t_type;
        case ';': t_type = SEMICOLON;  return t_type;
        case '[': t_type = LBRAC;      return t_type;
        case ']': t_type = RBRAC;      return t_type;
        case '(': t_type = LPAREN;     return t_type;
        case ')': t_type = RPAREN;     return t_type;
        case '<':
            c = getchar();
            if (c == '=')
            {
                t_type = LTEQ;
            }
            else if (c == '>')
            {
                t_type = NOTEQUAL;
            }
            else
            {
                ungetc(c, stdin);
                t_type = LESS;
            }
            return t_type;
       case '>':
            c = getchar();
            if (c == '=')
            {
                t_type = GTEQ;
            }
            else
            {
                ungetc(c, stdin);
                t_type = GREATER;
            }
            return t_type;
       default:
            if (isdigit(c))
            {
                ungetc(c, stdin);
                t_type = scan_number();
            }
            else if (isalpha(c)) // token is either keyword or ID
            {
                ungetc(c, stdin);
                t_type = scan_id_keyword();
            }
            else if (c == EOF)
            {
                t_type = END_OF_FILE;
            }
            else
            {
                t_type = ERROR;
            }
            return t_type;
    }
}
Пример #17
0
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;
}
Пример #18
0
int
stream_vscanf (stream_t *stream,
	const char *fmt,		/* Format string for the scanf */
	va_list argp)			/* Arguments to scanf */
#endif
{
	unsigned char ch, size;
	unsigned base;
	unsigned short len;
	int nmatch, ic;
	void *ptr;
	const char *pattern = 0;

	nmatch = 0;
	for (;;) switch (ch = FETCH_BYTE (fmt++)) {
	case '\0':
		return nmatch;
	case '%':
		ch = FETCH_BYTE (fmt++);
		if (ch == '%')
			goto def;
		if (ch != '*')
			ptr = va_arg (argp, void*);
		else {
			ptr = 0;
			ch = FETCH_BYTE (fmt++);
		}
		len = 0;
		size = REGULAR;
		while (ch >= '0' && ch <= '9') {
			len = len*10 + ch - '0';
			ch = FETCH_BYTE (fmt++);
		}
		if (len == 0)
			len = 30000;

		if (ch == 'l') {
			ch = FETCH_BYTE (fmt++);
			size = LONG;
		} else if (ch == 'h') {
			size = SHORT;
			ch = FETCH_BYTE (fmt++);
		} else if (ch == '[') {
			pattern = fmt;
			ch = FETCH_BYTE (fmt);
			if (ch == '^')
				ch = FETCH_BYTE (++fmt);
			while (ch != 0 && ch != ']')
				ch = FETCH_BYTE (++fmt);
			if (ch != 0)
				++fmt;
			ch = '[';
		}

		if (ch >= 'A' && ch <= 'Z') {
			ch = ch + 'a' - 'A';
			size = LONG;
		}

		switch (ch) {
		case 0:
			return -1;
		case 'c':
			if (len == 30000)
				len = 1;
			goto string;
		case 's':
		case '[':
string:			if (scan_string (stream, (char*) ptr, ch, len,
			    pattern) && ptr)
				++nmatch;
			break;
		case 'o':
			base = 8 | snoUnsigned;
			goto number;
		case 'x':
			base = 16 | snoUnsigned;
			goto number;
		case 'd':
			base = 10;
            goto number;
        case 'u':
            base = 10 | snoUnsigned;
number:			if (scan_number (stream, ptr, base, len, size) && ptr)
				++nmatch;
			break;
		}

		if (feof (stream))
			return nmatch ? nmatch : -1;
		break;

	case ' ':
	case '\n':
	case '\t':
		/* Skip spaces. */
		for (;;) {
			ic = peekchar (stream);
			if (ic < 0)
				break;
			if (! ISSPACE ((unsigned char) ic))
				break;
			getchar (stream);
		}
		break;

	default:
def:		ic = peekchar (stream);
		if (ic < 0)
			return -1;
		if ((unsigned char) ic != ch)
			return nmatch;
		getchar (stream);
	}