1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* convert-emoji.c: A tool to create an Emoji data GVariant
 * Copyright 2017, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/* Build with gcc -o convert-emoji convert-emoji.c `pkg-config --cflags --libs json-glib-1.0`
 */

#include <json-glib/json-glib.h>
#include <string.h>

gboolean
parse_code (GVariantBuilder *b,
            const char      *code,
            GString         *name_key)
{
  g_auto(GStrv) strv = NULL;
  int j;

  strv = g_strsplit (code, " ", -1);
  for (j = 0; strv[j]; j++)
    {
      guint32 u;
      char *end;

      u = (guint32) g_ascii_strtoull (strv[j], &end, 16);
      if (*end != '\0')
        {
          g_error ("failed to parse code: %s\n", strv[j]);
          return FALSE;
        }
      if (0x1f3fb <= u && u <= 0x1f3ff)
        g_variant_builder_add (b, "u", 0);
      else
        {
          g_variant_builder_add (b, "u", u);
          if (j > 0)
            g_string_append_c (name_key, '-');
          g_string_append_printf (name_key, "%x", u);
        }
    }

  return TRUE;
}

int
main (int argc, char *argv[])
{
  JsonParser *parser;
  JsonNode *root;
  JsonArray *array;
  JsonObject *ro;
  JsonNode *node;<--- Shadowed declaration
  const char *name;<--- Shadowed declaration
  JsonObjectIter iter;
  GError *error = NULL;
  guint length, i;
  GVariantBuilder builder;
  GVariant *v;
  GString *s;<--- Unused variable: s
  GBytes *bytes;
  GHashTable *names;
  GString *name_key;

  if (argc != 4)
    {
      g_print ("Usage: emoji-convert INPUT INPUT1 OUTPUT\n");
      return 1;
    }

  parser = json_parser_new ();

  if (!json_parser_load_from_file (parser, argv[2], &error))
    {
      g_error ("%s", error->message);
      return 1;
    }

  root = json_parser_get_root (parser);
  ro = json_node_get_object (root);
  json_object_iter_init (&iter, ro);

  names = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
  name_key = g_string_new ("");

  while (json_object_iter_next (&iter, &name, &node))
    {
      JsonObject *obj = json_node_get_object (node);
      const char *unicode;
      const char *shortname;

      unicode = json_object_get_string_member (obj, "unicode");
      shortname = json_object_get_string_member (obj, "shortname");

      g_hash_table_insert (names, g_strdup (unicode), g_strdup (shortname));
    }

  g_object_unref (parser);

  parser = json_parser_new ();

  if (!json_parser_load_from_file (parser, argv[1], &error))
    {
      g_error ("%s", error->message);
      return 1;
    }

  root = json_parser_get_root (parser);
  array = json_node_get_array (root);
  length = json_array_get_length (array);

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(auss)"));
  i = 0;
  while (i < length)
    {
      JsonNode *node = json_array_get_element (array, i);<--- Shadow variable
      JsonObject *obj = json_node_get_object (node);
      GVariantBuilder b1;
      const char *name;<--- Shadow variable
      const char *shortname;
      char *code;
      int j;<--- Unused variable: j
      gboolean skip;<--- Unused variable: skip
      gboolean has_variations;

      i++;

      g_variant_builder_init (&b1, G_VARIANT_TYPE ("au"));

      name = json_object_get_string_member (obj, "name");
      code = g_strdup (json_object_get_string_member (obj, "code"));

      has_variations = FALSE;
      while (i < length)
        {
          JsonNode *node2 = json_array_get_element (array, i);
          JsonObject *obj2 = json_node_get_object (node2);
          const char *name2;
          const char *code2;

          name2 = json_object_get_string_member (obj2, "name");
          code2 = json_object_get_string_member (obj2, "code");

          if (!strstr (name2, "skin tone") || !g_str_has_prefix (name2, name))
            break;

          if (!has_variations)
            {
              has_variations = TRUE;
              g_free (code);
              code = g_strdup (code2);
            }
          i++;
        }

      g_string_set_size (name_key, 0);
      if (!parse_code (&b1, code, name_key))
        return 1;

      shortname = g_hash_table_lookup (names, name_key->str);

      g_variant_builder_add (&builder, "(auss)", &b1, name, shortname ? shortname : "");
    }

  v = g_variant_builder_end (&builder);
  bytes = g_variant_get_data_as_bytes (v);
  if (!g_file_set_contents (argv[3], g_bytes_get_data (bytes, NULL), g_bytes_get_size (bytes), &error))
    {
      g_error ("%s", error->message);
      return 1;
    }

  return 0;
}