1 /*
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2025 The FreeBSD Foundation
5 *
6 * Portions of this software were developed by
7 * Tuukka Pasanen <tuukka.pasanen@ilmi.fi> under sponsorship from
8 * the FreeBSD Foundation
9 */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include "util.h"
14 #include "serialize.h"
15 #include "software.h"
16 #include "core.h"
17 #include "simplelicensing.h"
18
19 /*
20 * !doc
21 *
22 * .. c:function:: spdxtool_simplelicensing_license_expression_t *spdxtool_simplelicensing_licenseExpression_new(pkgconf_client_t *client, char *license)
23 *
24 * Create new /SimpleLicensing/SimpleLicensingText struct
25 *
26 * :param pkgconf_client_t *client: The pkgconf client being accessed.
27 * :param char *license: SPDX name of license
28 * :return: NULL if some problem occurs and SimpleLicensingText struct if not
29 */
30 spdxtool_simplelicensing_license_expression_t *
spdxtool_simplelicensing_licenseExpression_new(pkgconf_client_t * client,const char * license)31 spdxtool_simplelicensing_licenseExpression_new(pkgconf_client_t *client, const char *license)
32 {
33 if (!client || !license)
34 return NULL;
35
36 spdxtool_simplelicensing_license_expression_t *expression = calloc(1, sizeof(spdxtool_simplelicensing_license_expression_t));
37 if (!expression)
38 goto err;
39
40 expression->type = "simplelicensing_LicenseExpression";
41 expression->license_expression = strdup(license);
42 expression->spdx_id = spdxtool_util_get_spdx_id_string(client, expression->type, license);
43
44 if (!expression->license_expression || !expression->spdx_id)
45 goto err;
46
47 return expression;
48
49 err:
50 pkgconf_error(client, "spdxtool_simplelicensing_licenseExpression_new: out of memory");
51 spdxtool_simplelicensing_licenseExpression_free(expression);
52 return NULL;
53 }
54
55 /*
56 * !doc
57 *
58 * .. c:function:: void spdxtool_simplelicensing_licenseExpression_free(spdxtool_simplelicensing_license_expression_t *expression)
59 *
60 * Free /SimpleLicensing/SimpleLicensingText struct
61 *
62 * :param spdxtool_simplelicensing_license_expression_t *expression: SimpleLicensingText struct to be freed.
63 * :return: nothing
64 */
65 void
spdxtool_simplelicensing_licenseExpression_free(spdxtool_simplelicensing_license_expression_t * expression)66 spdxtool_simplelicensing_licenseExpression_free(spdxtool_simplelicensing_license_expression_t *expression)
67 {
68 if(!expression)
69 return;
70
71 free(expression->spdx_id);
72 free(expression->license_expression);
73
74 free(expression);
75 }
76
77 /*
78 * !doc
79 *
80 * .. c:function:: spdxtool_serialize_value_t *spdxtool_simplelicensing_licenseExpression_to_object(const char *creation_info, const spdxtool_simplelicensing_license_expression_t *expression)
81 *
82 * Serialize /SimpleLicensing/LicenseExpression struct to a JSON value tree.
83 *
84 * :param const char *creation_info: The creationInfo ID string to embed in the object.
85 * :param const spdxtool_simplelicensing_license_expression_t *expression: LicenseExpression struct to be serialized.
86 * :return: spdxtool_serialize_value_t * representing the LicenseExpression object.
87 */
88 spdxtool_serialize_value_t *
spdxtool_simplelicensing_licenseExpression_to_object(pkgconf_client_t * client,const char * creation_info,const spdxtool_simplelicensing_license_expression_t * expression)89 spdxtool_simplelicensing_licenseExpression_to_object(pkgconf_client_t *client, const char *creation_info, const spdxtool_simplelicensing_license_expression_t *expression)
90 {
91 spdxtool_serialize_value_t *ret = NULL;
92 spdxtool_serialize_object_list_t *object_list = spdxtool_serialize_object_list_new();
93 if (!object_list)
94 goto err;
95
96 if (!(spdxtool_serialize_object_add_string(object_list, "type", "simplelicensing_LicenseExpression") &&
97 spdxtool_serialize_object_add_string(object_list, "creationInfo", creation_info) &&
98 spdxtool_serialize_object_add_string(object_list, "spdxId", expression->spdx_id) &&
99 spdxtool_serialize_object_add_string(object_list, "simplelicensing_licenseExpression", expression->license_expression)))
100 {
101 goto err;
102 }
103
104 ret = spdxtool_serialize_value_object(object_list);
105 object_list = NULL;
106
107 err:
108 if (!ret)
109 pkgconf_error(client, "spdxtool_simplelicensing_licenseExpression_to_object: out of memory");
110
111 spdxtool_serialize_object_list_free(object_list);
112 return ret;
113 }
114