| 1 |
/* convert-emoji.c: A tool to create an Emoji data GVariant |
| 2 |
* Copyright 2017, Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
/* Build with gcc -o convert-emoji convert-emoji.c `pkg-config --cflags --libs json-glib-1.0` |
| 19 |
*/ |
| 20 |
|
| 21 |
/* Reads data from the json files in emojibase, expecting |
| 22 |
* language-specific data.raw.json as input |
| 23 |
*/ |
| 24 |
|
| 25 |
/* The format of the generated data is: a(ausasu). |
| 26 |
* Each member of the array has the following fields: |
| 27 |
* au - sequence of unicode codepoints. If the |
| 28 |
* sequence contains a 0, it marks the point |
| 29 |
* where skin tone modifiers should be inserted |
| 30 |
* s - name, e.g. "man worker" |
| 31 |
* as - keywords, e.g. "man", "worker" |
| 32 |
* u - the group that this item belongs to: |
| 33 |
* 0: smileys-emotion |
| 34 |
* 1: people-body |
| 35 |
* 2: component |
| 36 |
* 3: animals-nature |
| 37 |
* 4: food-drink |
| 38 |
* 5: travel-places |
| 39 |
* 6: activities |
| 40 |
* 7: objects |
| 41 |
* 8: symbols |
| 42 |
* 9: flags |
| 43 |
*/ |
| 44 |
#include <json-glib/json-glib.h> |
| 45 |
#include <string.h> |
| 46 |
|
| 47 |
gboolean |
| 48 |
parse_code (GVariantBuilder *b, |
| 49 |
const char *code) |
| 50 |
{ |
| 51 |
g_auto(GStrv) strv = NULL; |
| 52 |
int j; |
| 53 |
|
| 54 |
strv = g_strsplit (code, "-", -1); |
| 55 |
for (j = 0; strv[j]; j++) |
| 56 |
{ |
| 57 |
guint32 u; |
| 58 |
char *end; |
| 59 |
|
| 60 |
u = (guint32) g_ascii_strtoull (strv[j], &end, 16); |
| 61 |
if (*end != '\0') |
| 62 |
{ |
| 63 |
g_error ("failed to parse code: %s\n", strv[j]); |
| 64 |
return FALSE; |
| 65 |
} |
| 66 |
if (0x1f3fb <= u && u <= 0x1f3ff) |
| 67 |
g_variant_builder_add (b, "u", 0); |
| 68 |
else |
| 69 |
g_variant_builder_add (b, "u", u); |
| 70 |
} |
| 71 |
|
| 72 |
return TRUE; |
| 73 |
} |
| 74 |
|
| 75 |
int |
| 76 |
main (int argc, char *argv[]) |
| 77 |
{ |
| 78 |
JsonParser *parser; |
| 79 |
JsonNode *root; |
| 80 |
JsonObject *ro; |
| 81 |
JsonArray *array; |
| 82 |
JsonNode *node; |
| 83 |
const char *unicode; |
| 84 |
JsonObjectIter iter; |
| 85 |
GError *error = NULL; |
| 86 |
guint length, i; |
| 87 |
GVariantBuilder builder; |
| 88 |
GVariant *v; |
| 89 |
GString *s; |
| 90 |
GHashTable *names; |
| 91 |
GString *name_key; |
| 92 |
|
| 93 |
if (argc != 3) |
| 94 |
{ |
| 95 |
g_print ("Usage: emoji-convert INPUT OUTPUT\n"); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
parser = json_parser_new (); |
| 100 |
|
| 101 |
if (!json_parser_load_from_file (parser, argv[1], &error)) |
| 102 |
{ |
| 103 |
g_error ("%s", error->message); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
|
| 107 |
root = json_parser_get_root (parser); |
| 108 |
array = json_node_get_array (root); |
| 109 |
length = json_array_get_length (array); |
| 110 |
|
| 111 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ausasu)")); |
| 112 |
for (i = 0; i < length; i++) |
| 113 |
{ |
| 114 |
JsonObject *obj = json_array_get_object_element (array, i); |
| 115 |
GVariantBuilder b1; |
| 116 |
GVariantBuilder b2; |
| 117 |
guint group; |
| 118 |
const char *name; |
| 119 |
char *code; |
| 120 |
|
| 121 |
if (!json_object_has_member (obj, "group")) |
| 122 |
continue; |
| 123 |
|
| 124 |
group = json_object_get_int_member (obj, "group"); |
| 125 |
name = json_object_get_string_member (obj, "label"); |
| 126 |
|
| 127 |
if (json_object_has_member (obj, "skins")) |
| 128 |
{ |
| 129 |
JsonArray *a2 = json_object_get_array_member (obj, "skins"); |
| 130 |
JsonNode *n2 = json_array_get_element (a2, 0); |
| 131 |
JsonObject *o2 = json_node_get_object (n2); |
| 132 |
code = g_strdup (json_object_get_string_member (o2, "hexcode")); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
code = g_strdup (json_object_get_string_member (obj, "hexcode")); |
| 137 |
} |
| 138 |
|
| 139 |
g_variant_builder_init (&b1, G_VARIANT_TYPE ("au")); |
| 140 |
|
| 141 |
if (!parse_code (&b1, code)) |
| 142 |
return 1; |
| 143 |
|
| 144 |
g_variant_builder_init (&b2, G_VARIANT_TYPE ("as")); |
| 145 |
if (json_object_has_member (obj, "tags")) |
| 146 |
{ |
| 147 |
JsonArray *tags = json_object_get_array_member (obj, "tags"); |
| 148 |
for (int j = 0; j < json_array_get_length (tags); j++) |
| 149 |
g_variant_builder_add (&b2, "s", json_array_get_string_element (tags, j)); |
| 150 |
} |
| 151 |
|
| 152 |
g_variant_builder_add (&builder, "(ausasu)", &b1, name, &b2, group); |
| 153 |
} |
| 154 |
|
| 155 |
v = g_variant_builder_end (&builder); |
| 156 |
if (g_str_has_suffix (argv[2], ".json")) |
| 157 |
{ |
| 158 |
JsonNode *node; |
| 159 |
char *out; |
| 160 |
|
| 161 |
node = json_gvariant_serialize (v); |
| 162 |
out = json_to_string (node, TRUE); |
| 163 |
if (!g_file_set_contents (argv[2], out, -1, &error)) |
| 164 |
{ |
| 165 |
g_error ("%s", error->message); |
| 166 |
return 1; |
| 167 |
} |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
GBytes *bytes; |
| 172 |
|
| 173 |
bytes = g_variant_get_data_as_bytes (v); |
| 174 |
if (!g_file_set_contents (argv[2], g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &error)) |
| 175 |
{ |
| 176 |
g_error ("%s", error->message); |
| 177 |
return 1; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |