/** * Create a context. * * Creates and returns a new `dpl_ctx_t` object, or NULL on error. * * Possible errors include memory allocation failure, failure to find * the profile, and failure to parse the profile. @note If this function * fails there is no way for the caller to discover what went wrong. * * The droplet directory is used as the base directory for finding several * important files, notably the profile file. It is the first non-NULL pathname of: * @arg the parameter @a droplet_dir, or * @arg the environmental variable `DPLDIR`, or * @arg `"~/.droplet/"`. * * A profile file is read to set up options for the context. The profile * file is named `<name>.profile` where `<name>` is the profile's name, * and is located in the droplet directory. The profile's name is the * first non-NULL name of: * @arg the @a profile_name parameter, or * @arg the environment variable `DPLPROFILE`, or * @arg `"default"`. * * Thus, if both parameters are passed `NULL` the profile will be read * from the file `~/.droplet/default.profile`. * * See @ref profile "Profile File" for details of the profile file format. * * @param droplet_dir pathname of directory containing Droplet state, or `NULL` * @param profile_name name of the profile to use, or `NULL` * @return a new context, or NULL on error */ dpl_ctx_t * dpl_ctx_new(const char *droplet_dir, const char *profile_name) { dpl_ctx_t *ctx; int ret; ctx = dpl_ctx_alloc(); if (NULL == ctx) { DPL_LOG(NULL, DPL_ERROR, "No memory for droplet context creation."); return NULL; } ret = dpl_profile_load(ctx, droplet_dir, profile_name); if (DPL_SUCCESS != ret) { dpl_ctx_free(ctx); return NULL; } dpl_ctx_post_load(ctx); return ctx; }
/** * Creates a new context with the profile specified in a dict. * * Creates a new profile and sets up the the profile information from a * dict containing profile variable settings as if they had been read in * from a profile file, without reading a file. This function is provided * for applications which need to use the Droplet library without a * profile file. * * Note that to create a context without needing a droplet directory at * all, applications should set the profile variable @c pricing_dir to * an empty string. * * See @ref profile "Profile File" for details of the profile variables. * The droplet directory is set by a special variable in the dict called * @c droplet_dir, and the profile name by a special variable called @c * profile_name. * * @param profile a dict containing profile variables * @return a new context or NULL on error */ dpl_ctx_t * dpl_ctx_new_from_dict(const dpl_dict_t *profile) { dpl_ctx_t *ctx; int ret; ctx = dpl_ctx_alloc(); if (NULL == ctx) return NULL; ret = dpl_profile_set_from_dict(ctx, profile); if (DPL_SUCCESS != ret) { dpl_ctx_free(ctx); return NULL; } dpl_ctx_post_load(ctx); return ctx; }
/** * Create a context. * * Creates and returns a new `dpl_ctx_t` object, or NULL on error. * * Possible errors include memory allocation failure, failure to find * the profile, and failure to parse the profile. @note If this function * fails there is no way for the caller to discover what went wrong. * * The droplet directory is used as the base directory for finding several * important files, notably the profile file. It is the first non-NULL pathname of: * @arg the parameter @a droplet_dir, or * @arg the environmental variable `DPLDIR`, or * @arg `"~/.droplet/"`. * * A profile file is read to set up options for the context. The profile * file is named `<name>.profile` where `<name>` is the profile's name, * and is located in the droplet directory. The profile's name is the * first non-NULL name of: * @arg the @a profile_name parameter, or * @arg the environment variable `DPLPROFILE`, or * @arg `"default"`. * * Thus, if both parameters are passed `NULL` the profile will be read * from the file `~/.droplet/default.profile`. * * See @ref profile "Profile File" for details of the profile file format. * * @param droplet_dir pathname of directory containing Droplet state, or `NULL` * @param profile_name name of the profile to use, or `NULL` * @return a new context, or NULL on error */ dpl_ctx_t * dpl_ctx_new(const char *droplet_dir, const char *profile_name) { dpl_ctx_t *ctx; int ret; ctx = dpl_ctx_alloc(); if (NULL == ctx) return NULL; ret = dpl_profile_load(ctx, droplet_dir, profile_name); if (DPL_SUCCESS != ret) { dpl_ctx_free(ctx); return NULL; } dpl_ctx_post_load(ctx); return ctx; }