Пример #1
0
/**
 * \brief Finds the data of a config file by a path relative to the data directory root.
 * \param self Paths.
 * \param path Path relative to the data directory root.
 * \param config Nonzero for a config file, zero for a data file.
 * \return Absolute path or NULL.
 */
char* lipth_paths_find_path (
	const LIPthPaths* self,
	const char*       path,
	int               config)
{
	char* path1;

	/* Try the config/save path. */
	if (config)
		path1 = lisys_path_concat (self->module_config, path, NULL);
	else
		path1 = lisys_path_concat (self->module_data_save, path, NULL);
	if (path1 == NULL)
		return NULL;
	if (lisys_filesystem_access (path1, LISYS_ACCESS_READ))
		return path1;
	lisys_free (path1);

	/* Try the data path. */
	path1 = lisys_path_concat (self->module_data, path, NULL);
	if (lisys_filesystem_access (path1, LISYS_ACCESS_READ))
		   return path1;
	lisys_free (path1);

	return NULL;
}
Пример #2
0
/**
 * \brief Gets the path to an SQL database.
 *
 * Calling this function will create the save directory if it doesn't exist
 * yet. If the creation fails or the function runs out of memory, NULL is
 * returned and the error message is set.
 *
 * \param self Paths object.
 * \param name File name.
 * \return Newly allocated absolute path or NULL.
 */
char* lipth_paths_get_sql (
	const LIPthPaths* self,
	const char*       name)
{
	char* path;

	/* Format the path. */
	path = lisys_path_concat (self->module_state, name, NULL);
	if (path == NULL)
		return NULL;

	/* Check if the save directory exists. */
	if (lisys_filesystem_access (self->module_state, LISYS_ACCESS_EXISTS))
	{
		if (!lisys_filesystem_access (self->module_state, LISYS_ACCESS_WRITE))
		{
			lisys_error_set (EINVAL, "save path `%s' is not writable", path);
			lisys_free (path);
			return NULL;
		}
		return path;
	}

	/* Create the save directory. */
	if (!lisys_filesystem_makepath (self->module_state))
	{
		lisys_free (path);
		return NULL;
	}

	return path;
}
Пример #3
0
/**
 * \brief Gets the global data directory.
 *
 * Follows the XDG Base Directory Specification:
 * http://www.freedesktop.org/Standards/basedir-spec
 *
 * \param path Relative path being searched for.
 * \return New string or NULL.
 */
char* lisys_paths_get_data_global (
	const char* path)
{
#ifdef __WIN32__
	return NULL;
#else
	int last;
	char* dup;
	char* ptr;
	char* ret;
	char* start;
	const char* dirs;

	/* Get the list of global data directories. */
	dirs = getenv ("XDG_DATA_DIRS");
	if (dirs == NULL || dirs[0] == '\0')
		return NULL;
	dup = lisys_string_dup  (dirs);
	if (dup == NULL)
		return NULL;

	/* Loop through all directories. */
	ptr = start = dup;
	ret = NULL;
	last = 0;
	while (1)
	{
		/* Search for the delimiter. */
		last = (*ptr == '\0');
		if (*ptr != ':' && !last)
		{
			ptr++;
			continue;
		}
		*ptr = '\0';

		/* Test if the path is valid. */
		ret = lisys_path_concat (start, path, NULL);
		if (ret != NULL)
		{
			if (lisys_filesystem_access (ret, LISYS_ACCESS_READ))
				break;
			lisys_free (ret);
			ret = NULL;
		}

		/* Check if more candidates exist. */
		if (last)
			break;
		ptr++;
		start = ptr;
	}

	/* Return the result or NULL. */
	lisys_free (dup);
	return ret;
#endif
}
Пример #4
0
static int private_create_save_path (
	LIPthPaths* self,
	const char* path)
{
	/* Check if the save directory exists. */
	if (lisys_filesystem_access (path, LISYS_ACCESS_EXISTS))
	{
		if (!lisys_filesystem_access (path, LISYS_ACCESS_WRITE))
		{
			lisys_error_set (EINVAL, "save path `%s' is not writable", path);
			return 0;
		}
		return 1;
	}

	/* Create the save directory. */
	if (!lisys_filesystem_makepath (path))
		return 0;

	return 1;
}
Пример #5
0
/**
 * \brief Gets the path to a sound file.
 *
 * \param self Paths object.
 * \param name File name.
 * \return Full path or NULL.
 */
char* lipth_paths_get_sound (
	const LIPthPaths* self,
	const char*       name)
{
	char* path;

	/* Try the override path. */
	path = lisys_path_concat (self->override_data, "sounds", name, NULL);
	if (path == NULL)
		return NULL;
	if (lisys_filesystem_access (path, LISYS_ACCESS_READ))
		return path;
	lisys_free (path);

	/* Try the real path. */
	return lisys_path_concat (self->module_data, "sounds", name, NULL);
}