| 1 |
/* font-manager-json.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2022 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-json.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-json |
| 25 |
* @short_description: JSON related utility functions |
| 26 |
* @title: JSON Utility Functions |
| 27 |
* @include: font-manager-json.h |
| 28 |
* |
| 29 |
* Helper functions to make working with JSON data easier. |
| 30 |
*/ |
| 31 |
|
| 32 |
/** |
| 33 |
* font_manager_write_json_file: |
| 34 |
* @root: #JsonNode to use as root |
| 35 |
* @filepath: filepath to write @root to |
| 36 |
* @pretty: whether the generated string should be pretty printed |
| 37 |
* |
| 38 |
* Returns: %TRUE if file was written successfully |
| 39 |
*/ |
| 40 |
gboolean |
| 41 |
font_manager_write_json_file (JsonNode *root, |
| 42 |
const gchar *filepath, |
| 43 |
gboolean pretty) |
| 44 |
{ |
| 45 |
g_return_val_if_fail(root != NULL && filepath != NULL, FALSE); |
| 46 |
|
| 47 |
g_autoptr(JsonGenerator) generator = json_generator_new(); |
| 48 |
json_generator_set_root(generator, root); |
| 49 |
json_generator_set_pretty(generator, pretty); |
| 50 |
json_generator_set_indent(generator, 4); |
| 51 |
return json_generator_to_file(generator, filepath, NULL); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* font_manager_load_json_file: |
| 56 |
* @filepath: filepath to a valid json file |
| 57 |
* |
| 58 |
* Returns: (transfer full) (nullable): #JsonNode or %NULL if file failed to load |
| 59 |
*/ |
| 60 |
JsonNode * |
| 61 |
font_manager_load_json_file (const gchar *filepath) |
| 62 |
{ |
| 63 |
g_return_val_if_fail(filepath != NULL, NULL); |
| 64 |
|
| 65 |
g_autoptr(JsonParser) parser = json_parser_new(); |
| 66 |
JsonNode *root = NULL; |
| 67 |
if (json_parser_load_from_file(parser, filepath, NULL)) |
| 68 |
root = json_parser_get_root(parser); |
| 69 |
return root ? json_node_copy(root) : NULL; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* font_manager_compare_json_int_member: |
| 74 |
* @member_name: name of member to compare |
| 75 |
* @a: #JsonObject |
| 76 |
* @b: #JsonObject |
| 77 |
* |
| 78 |
* Returns: An integer less than, equal to, or greater than zero |
| 79 |
* if member from a is <, == or > than member from b |
| 80 |
*/ |
| 81 |
gint |
| 82 |
font_manager_compare_json_int_member (const gchar *member_name, |
| 83 |
JsonObject *a, |
| 84 |
JsonObject *b) |
| 85 |
{ |
| 86 |
g_return_val_if_fail(member_name != NULL, 0); |
| 87 |
g_return_val_if_fail(a != NULL && b != NULL, 0); |
| 88 |
g_return_val_if_fail(json_object_has_member(a, member_name), 0); |
| 89 |
g_return_val_if_fail(json_object_has_member(b, member_name), 0); |
| 90 |
gint int_a = json_object_get_int_member(a, member_name); |
| 91 |
gint int_b = json_object_get_int_member(b, member_name); |
| 92 |
return int_a == int_b ? 0 : int_a - int_b; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* font_manager_compare_json_string_member: |
| 97 |
* @member_name: name of member to compare |
| 98 |
* @a: #JsonObject |
| 99 |
* @b: #JsonObject |
| 100 |
* |
| 101 |
* Returns: An integer less than, equal to, or greater than zero |
| 102 |
* if member from a is <, == or > than member from b |
| 103 |
*/ |
| 104 |
gint |
| 105 |
font_manager_compare_json_string_member (const gchar *member_name, |
| 106 |
JsonObject *a, |
| 107 |
JsonObject *b) |
| 108 |
{ |
| 109 |
g_return_val_if_fail(member_name != NULL, 0); |
| 110 |
g_return_val_if_fail(a != NULL && b != NULL, 0); |
| 111 |
g_return_val_if_fail(json_object_has_member(a, member_name), 0); |
| 112 |
g_return_val_if_fail(json_object_has_member(b, member_name), 0); |
| 113 |
const gchar *str_a = json_object_get_string_member(a, member_name); |
| 114 |
const gchar *str_b = json_object_get_string_member(b, member_name); |
| 115 |
g_return_val_if_fail(str_a != NULL && str_b != NULL, 0); |
| 116 |
return font_manager_natural_sort(str_a, str_b); |
| 117 |
} |
| 118 |
|
| 119 |
/* Order matters */ |
| 120 |
static const gchar *STYLE_PROPS[3] = { |
| 121 |
"width", |
| 122 |
"weight", |
| 123 |
"slant" |
| 124 |
}; |
| 125 |
|
| 126 |
/** |
| 127 |
* font_manager_compare_json_font_node: |
| 128 |
* @node_a: #JsonNode containing a font description |
| 129 |
* @node_b: #JsonNode containing a font description |
| 130 |
* |
| 131 |
* Returns: An integer less than, equal to, or greater than zero, |
| 132 |
* if font a is <, == or > than font b |
| 133 |
*/ |
| 134 |
gint |
| 135 |
font_manager_compare_json_font_node (JsonNode *node_a, |
| 136 |
JsonNode *node_b) |
| 137 |
{ |
| 138 |
g_return_val_if_fail(JSON_NODE_HOLDS_OBJECT(node_a), 0); |
| 139 |
g_return_val_if_fail(JSON_NODE_HOLDS_OBJECT(node_b), 0); |
| 140 |
JsonObject *a = json_node_get_object(node_a); |
| 141 |
JsonObject *b = json_node_get_object(node_b); |
| 142 |
g_return_val_if_fail(a != NULL && b != NULL, 0); |
| 143 |
gint i, result = 0; |
| 144 |
/* Attempt to sort based on style properties */ |
| 145 |
for (i = 0; i < (gint) G_N_ELEMENTS(STYLE_PROPS); i++) { |
| 146 |
result = font_manager_compare_json_int_member(STYLE_PROPS[i], a, b); |
| 147 |
if (result != 0) |
| 148 |
return result; |
| 149 |
} |
| 150 |
/* All else being equal sort alphabetically based on style */ |
| 151 |
return font_manager_compare_json_string_member("style", a, b); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* font_manager_str_list_to_json_array: |
| 156 |
* @slist: (element-type utf8): a #GList containing only strings |
| 157 |
* |
| 158 |
* Returns: (transfer full): A newly created #JsonArray |
| 159 |
*/ |
| 160 |
JsonArray * |
| 161 |
font_manager_str_list_to_json_array (GList *slist) |
| 162 |
{ |
| 163 |
GList *iter; |
| 164 |
JsonArray *result = json_array_new(); |
| 165 |
for (iter = slist; iter != NULL; iter = iter->next) |
| 166 |
json_array_add_string_element(result, iter->data); |
| 167 |
return result; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* font_manager_print_json_array: |
| 172 |
* @json_arr: a #JsonArray |
| 173 |
* @pretty: whether the output should be prettyfied for printing |
| 174 |
* |
| 175 |
* Convenience function which simply wraps json_to_string to allow direct |
| 176 |
* conversion of a #JsonArray to string. Equivalent to creating a #JsonNode, |
| 177 |
* then setting the given array inside the node and calling json_to_string |
| 178 |
* on the node. |
| 179 |
* |
| 180 |
* Returns: (transfer full): A newly allocated buffer holding a JSON data stream. |
| 181 |
* The returned string should be freed with g_free() when no longer needed. |
| 182 |
*/ |
| 183 |
gchar * |
| 184 |
font_manager_print_json_array (JsonArray *json_arr, |
| 185 |
gboolean pretty) |
| 186 |
{ |
| 187 |
g_return_val_if_fail(json_arr != NULL, NULL); |
| 188 |
g_autoptr(JsonNode) n = json_node_new(JSON_NODE_ARRAY); |
| 189 |
json_node_set_array(n, json_arr); |
| 190 |
gchar *res = (gchar *) json_to_string(n, pretty); |
| 191 |
json_node_set_array(n, NULL); |
| 192 |
return res; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* font_manager_print_json_object: |
| 197 |
* @json_obj: a #JsonObject |
| 198 |
* @pretty: whether the output should be prettyfied for printing |
| 199 |
* |
| 200 |
* Convenience function which simply wraps json_to_string to allow direct |
| 201 |
* conversion of a #JsonObject to string. Equivalent to creating a #JsonNode, |
| 202 |
* then setting the given object inside the node and calling json_to_string |
| 203 |
* on the node. |
| 204 |
* |
| 205 |
* Returns: (transfer full): A newly allocated buffer holding a JSON data stream. |
| 206 |
* The returned string should be freed with g_free() when no longer needed. |
| 207 |
*/ |
| 208 |
gchar * |
| 209 |
font_manager_print_json_object (JsonObject *json_obj, |
| 210 |
gboolean pretty) |
| 211 |
{ |
| 212 |
g_return_val_if_fail(json_obj != NULL, NULL); |
| 213 |
g_autoptr(JsonNode) n = json_node_new(JSON_NODE_OBJECT); |
| 214 |
json_node_set_object(n, json_obj); |
| 215 |
gchar *res = (gchar *) json_to_string(n, pretty); |
| 216 |
json_node_set_object(n, NULL); |
| 217 |
return res; |
| 218 |
} |
| 219 |
|