Exemplo n.º 1
0
MVMnum64 MVM_coerce_s_n(MVMThreadContext *tc, MVMString *s) {
    MVMCodepointIter ci;
    MVMCodepoint cp;
    MVMnum64  n = 123;
    MVM_string_ci_init(tc, &ci, s, 0, 0);

    if (get_cp(tc, &ci, &cp)) return 0;

    skip_whitespace(tc, &ci, &cp);

    // Do we have only whitespace
    if (!MVM_string_ci_has_more(tc, &ci) && cp == END_OF_NUM) {
        return 0;
    }

    n = parse_real(tc, &ci, &cp, s);

    skip_whitespace(tc, &ci, &cp);

    if (MVM_string_ci_has_more(tc, &ci) || cp != END_OF_NUM) {
        parse_error(tc, s, "trailing characters");
    }

    return n;
}
Exemplo n.º 2
0
double real_()
{
    double result=0;

    check(parse_real(result));

    return result;
}
Exemplo n.º 3
0
static struct AstNode *parse_literal_real(struct DomNode *dom)
{
    double value;

    if (!dom_node_is_atom(dom)) {
        return NULL;
    }

    if (!parse_real(dom->atom, &value)) {
        return NULL;
    }

    return ast_make_literal_atomic_real(value);
}
Exemplo n.º 4
0
/* int parseline(int num, double to[0..num-1])
*   stores the values at the next line to the array provided
*   Return values:
*    0: OK
*    1: some error
*/
int parseline(int num, double *to/*0..num-1*/)
{char *str; int i,ret;
    str=line;
    for(i=0;i<num;i++,to++){
        ret=parse_real(str,to);
        if(ret<=0){// error
            report(R_fatal,"Error parsing data line in file %s\n%s\n",filename,line);
            return 1;
        }
        str+=ret;
    }
    if(*str){ // extra characters at the end
        report(R_fatal,"Extra characters at the end of data line in file %s\n%s\n",filename,line);
        return 1;
    }
    return 0;
}
Exemplo n.º 5
0
/**
 * \brief Evaluates the value of the tree obtained from the parsed expression.
 * \param result (out) The result of the evaluation.
 * \param tree The expression to evaluate.
 * \return true if the expression has evaluated.
 */
bool bf::arithmetic_parser::evaluate_tree
( double& result, tree_node const& tree ) const
{
  switch (tree.value.id().to_long())
    {
    case arithmetic_grammar::id_real:
      return parse_real
        ( result, std::string( tree.value.begin(), tree.value.end() ) );
      
    case arithmetic_grammar::id_expression:
    case arithmetic_grammar::id_term:
    case arithmetic_grammar::id_factor:
      return evaluate_operator
        ( result, *tree.value.begin(), *tree.children.begin(),
          *(tree.children.begin() + 1) );

    default:
      CLAW_FAIL( "unknown tree identifier" );
    }
 
  return false;
} // arithmetic_parser::evaluate_tree()
Exemplo n.º 6
0
/**
 * @brief ggs_parse_double
 *
 * Translate input word into a double.
 *
 * @param value double.
 * @param word Word.
 * @return true if parsing succeed.
 */
static bool ggs_parse_double(double *value, const char *word) {
	errno = 0;
	parse_real(word, value);
	return errno == 0;
}
Exemplo n.º 7
0
extern void
args_parse(args_info *args, int argc, char **argv)
{
	// Initialize those parts of *args that we need later.
	args->files_name = NULL;
	args->files_file = NULL;
	args->files_delim = '\0';

	// Check how we were called.
	{
		// Remove the leading path name, if any.
		const char *name = strrchr(argv[0], '/');
		if (name == NULL)
			name = argv[0];
		else
			++name;

		// NOTE: It's possible that name[0] is now '\0' if argv[0]
		// is weird, but it doesn't matter here.

		// Look for full command names instead of substrings like
		// "un", "cat", and "lz" to reduce possibility of false
		// positives when the programs have been renamed.
		if (strstr(name, "xzcat") != NULL) {
			opt_mode = MODE_DECOMPRESS;
			opt_stdout = true;
		} else if (strstr(name, "unxz") != NULL) {
			opt_mode = MODE_DECOMPRESS;
		} else if (strstr(name, "lzcat") != NULL) {
			opt_format = FORMAT_LZMA;
			opt_mode = MODE_DECOMPRESS;
			opt_stdout = true;
		} else if (strstr(name, "unlzma") != NULL) {
			opt_format = FORMAT_LZMA;
			opt_mode = MODE_DECOMPRESS;
		} else if (strstr(name, "lzma") != NULL) {
			opt_format = FORMAT_LZMA;
		}
	}

	// First the flags from the environment
	parse_environment(args, argv[0], "XZ_DEFAULTS");
	parse_environment(args, argv[0], "XZ_OPT");

	// Then from the command line
	parse_real(args, argc, argv);

	// Never remove the source file when the destination is not on disk.
	// In test mode the data is written nowhere, but setting opt_stdout
	// will make the rest of the code behave well.
	if (opt_stdout || opt_mode == MODE_TEST) {
		opt_keep_original = true;
		opt_stdout = true;
	}

	// When compressing, if no --format flag was used, or it
	// was --format=auto, we compress to the .xz format.
	if (opt_mode == MODE_COMPRESS && opt_format == FORMAT_AUTO)
		opt_format = FORMAT_XZ;

	// Compression settings need to be validated (options themselves and
	// their memory usage) when compressing to any file format. It has to
	// be done also when uncompressing raw data, since for raw decoding
	// the options given on the command line are used to know what kind
	// of raw data we are supposed to decode.
	if (opt_mode == MODE_COMPRESS || opt_format == FORMAT_RAW)
		coder_set_compression_settings();

	// If no filenames are given, use stdin.
	if (argv[optind] == NULL && args->files_name == NULL) {
		// We don't modify or free() the "-" constant. The caller
		// modifies this so don't make the struct itself const.
		static char *names_stdin[2] = { (char *)"-", NULL };
		args->arg_names = names_stdin;
		args->arg_count = 1;
	} else {
		// We got at least one filename from the command line, or
		// --files or --files0 was specified.
		args->arg_names = argv + optind;
		args->arg_count = argc - optind;
	}

	return;
}
Exemplo n.º 8
0
static void
parse_environment(args_info *args, char *argv0, const char *varname)
{
	char *env = getenv(varname);
	if (env == NULL)
		return;

	// We modify the string, so make a copy of it.
	env = xstrdup(env);

	// Calculate the number of arguments in env. argc stats at one
	// to include space for the program name.
	int argc = 1;
	bool prev_was_space = true;
	for (size_t i = 0; env[i] != '\0'; ++i) {
		// NOTE: Cast to unsigned char is needed so that correct
		// value gets passed to isspace(), which expects
		// unsigned char cast to int. Casting to int is done
		// automatically due to integer promotion, but we need to
		// force char to unsigned char manually. Otherwise 8-bit
		// characters would get promoted to wrong value if
		// char is signed.
		if (isspace((unsigned char)env[i])) {
			prev_was_space = true;
		} else if (prev_was_space) {
			prev_was_space = false;

			// Keep argc small enough to fit into a singed int
			// and to keep it usable for memory allocation.
			if (++argc == my_min(
					INT_MAX, SIZE_MAX / sizeof(char *)))
				message_fatal(_("The environment variable "
						"%s contains too many "
						"arguments"), varname);
		}
	}

	// Allocate memory to hold pointers to the arguments. Add one to get
	// space for the terminating NULL (if some systems happen to need it).
	char **argv = xmalloc(((size_t)(argc) + 1) * sizeof(char *));
	argv[0] = argv0;
	argv[argc] = NULL;

	// Go through the string again. Split the arguments using '\0'
	// characters and add pointers to the resulting strings to argv.
	argc = 1;
	prev_was_space = true;
	for (size_t i = 0; env[i] != '\0'; ++i) {
		if (isspace((unsigned char)env[i])) {
			prev_was_space = true;
			env[i] = '\0';
		} else if (prev_was_space) {
			prev_was_space = false;
			argv[argc++] = env + i;
		}
	}

	// Parse the argument list we got from the environment. All non-option
	// arguments i.e. filenames are ignored.
	parse_real(args, argc, argv);

	// Reset the state of the getopt_long() so that we can parse the
	// command line options too. There are two incompatible ways to
	// do it.
#ifdef HAVE_OPTRESET
	// BSD
	optind = 1;
	optreset = 1;
#else
	// GNU, Solaris
	optind = 0;
#endif

	// We don't need the argument list from environment anymore.
	free(argv);
	free(env);

	return;
}