Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define locale for parsing float values #202

Open
MasterMind2k opened this issue May 20, 2023 · 2 comments
Open

Define locale for parsing float values #202

MasterMind2k opened this issue May 20, 2023 · 2 comments

Comments

@MasterMind2k
Copy link

Hi.

Float parsing uses system configuration and will print out and load floats as locale aware. In our usecase, we use libcyaml as confiuration schema and is unacceptable to have floats in locale format, as it prehibits compatibility of configuration values cross-machines.

@tlsa
Copy link
Owner

tlsa commented May 22, 2023

Why not just create wrappers that set and restore the locale around cyaml load/save calls?

For example something like this (untested):

bool my_yaml_loader(
		const char *path,
		my_data_t **data_out)
{
	char *old_locale, *saved_locale;
	unsigned sequence_count;
	bool ret = false;
	my_data_t *data;
	cyaml_err_t err;

	/* Get the name of the current locale.  */
	old_locale = setlocale (LC_ALL, NULL);

	/* Copy the name so it won’t be clobbered by setlocale. */
	saved_locale = strdup(old_locale);
	if (saved_locale == NULL) {
		fprintf(stderr, "Out of memory\n");
		return false;
	}

	/* Now change the locale to the one we want for loading. */
	setlocale (LC_ALL, "C");

	/* Do the load. */
	err = cyaml_load_file(path, config, schema, &data, &sequence_count);
	if (err != CYAML_OK) {
		fprintf(stderr, "Error loading %s: %s\n",
				path, cyaml_strerror(err));
		goto restore;
	}

	*data_out = data;
	ret = true;

restore:
	/* Restore the original locale. */
	setlocale (LC_ALL, saved_locale);
	free(saved_locale);

	return ret;
}

@MasterMind2k
Copy link
Author

It is a work-around. I was hoping on using float parsing library that is not dependant on locale. To remove additional complexitz of setting locale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants