Esempio n. 1
0
static void list_dicts(const std::list<std::string> &dicts_dir_list, bool use_json)
{
    bool first_entry = true;
    if (!use_json)
        printf(_("Dictionary's name   Word count\n"));
    else
        fputc('[', stdout);
    std::list<std::string> order_list, disable_list;
    for_each_file(dicts_dir_list, ".ifo", order_list,
                  disable_list, [use_json, &first_entry](const std::string &filename, bool) -> void {
                      DictInfo dict_info;
                      if (dict_info.load_from_ifo_file(filename, false)) {
                          const std::string bookname = utf8_to_locale_ign_err(dict_info.bookname);
                          if (use_json) {
                              if (first_entry) {
                                  first_entry = false;
                              } else {
                                  fputc(',', stdout); // comma between entries
                              }
                              printf("{\"name\": \"%s\", \"wordcount\": \"%d\"}", json_escape_string(bookname).c_str(), dict_info.wordcount);
                          } else {
                              printf("%s    %d\n", bookname.c_str(), dict_info.wordcount);
                          }
                      }
                  });
    if (use_json)
        fputs("]\n", stdout);
}
static bool output_value(uint8_t *source, uint8_t *sourcelimit, uint8_t typecode, dynbuffer_t *dest)
{
	uint8_t subtype;

	/* switch on type */
	switch (typecode) {
	case JSONBINARY_TYPE_OBJECT:
		return output_object(source, sourcelimit, dest);
	case JSONBINARY_TYPE_ARRAY:
		return output_array(source, sourcelimit, dest);
	case JSONBINARY_TYPE_STRING:
		dynbuffer_append_byte(dest, '"');
		json_escape_string(dest, source, sourcelimit-source, true, '"');
		dynbuffer_append_byte(dest, '"');
		break;
	case JSONBINARY_TYPE_NUMBER:
		dynbuffer_append(dest, source, sourcelimit-source);
		break;
	case JSONBINARY_TYPE_SS:
		if (source>=sourcelimit) return false;
		subtype=*source;
		switch (subtype) {
		case JSONBINARY_SS_DATA_FALSE:
			dynbuffer_append(dest, "false", 5);
			break;
		case JSONBINARY_SS_DATA_TRUE:
			dynbuffer_append(dest, "true", 4);
			break;
		case JSONBINARY_SS_DATA_NULL:
			dynbuffer_append(dest, "null", 4);
			break;
		case JSONBINARY_SS_DATA_UNDEFINED:
			dynbuffer_append(dest, "undefined", 9);
			break;
		default:
			return false;
		}
		break;
	}

	return true;
}
static bool output_object(uint8_t *source, uint8_t *sourcelimit, dynbuffer_t *dest)
{
	int index=0;
   uint8_t typecode;
   uint8_t *data;
   uint32_t datalen;

	dynbuffer_append_byte(dest, '{');
	while (source<sourcelimit) {
		/* object is just a sequence of pairs.  pair==asciiz + value */
		uint8_t *label=source;
		for (;;) {
			if (source>=sourcelimit) return false;	/* short */
			if (*(source++)==0) break;
		}

		if (index>0) dynbuffer_append_byte(dest, ',');

		/* output label */
		dynbuffer_append_byte(dest, '"');
		json_escape_string(dest, label, source-label-1, true, '"');
		dynbuffer_append(dest, "\":", 2);

		/* decode type length */
		if (!extract_type_length(source, sourcelimit, &typecode, &data, &datalen)) return false;

		/* output value */
		source=data+datalen;
		if (!output_value(data, source, typecode, dest)) return false;

		index++;
	}
	dynbuffer_append_byte(dest, '}');

	return true;
}
Esempio n. 4
0
gint
main (gint argc, gchar **argv)
{
  GList      *operations;
  GList      *iter;
  gboolean first = TRUE;

  gegl_init (&argc, &argv);

  g_object_set (gegl_config (),
                "application-license", "GPL3",
                NULL);


  operations = gegl_operations ();

  g_print ("window.opdb=[\n");

  for (iter=operations;iter;iter = g_list_next (iter))
    {
      GeglOperationClass *klass = iter->data;

      const char *name = gegl_operation_class_get_key (klass, "name");
      const char *categoris = gegl_operation_class_get_key (klass, "categories");

      if (first)
        first = FALSE;
      else
        g_print (",");

      g_print ("{'op':'%s'\n", name);

      if (klass->compat_name)
        g_print (",'compat-op':'%s'\n", klass->compat_name);

      if (klass->opencl_support)
        g_print (",'opencl-support':'true'\n");

      g_print (",'parent':'%s'\n", 
          g_type_name (g_type_parent(G_OBJECT_CLASS_TYPE(klass))));

      {
        char *image = operation_to_path (name);

        if (g_file_test (image, G_FILE_TEST_EXISTS))
          g_print (",'image':'%s'\n", image);
        g_free (image);
      }

      {
        gchar *commandline = g_strdup_printf (
            "sh -c \"(cd " TOP_SRCDIR ";grep -r '\\\"%s\\\"' operations) | grep operations | grep -v '~:' | grep '\\\"name\\\"' | cut -f 1 -d ':'\"",
             name);
        gchar *output = NULL;
        
        if (g_spawn_command_line_sync (commandline, &output, NULL, NULL, NULL))
        {
          if (strlen(output))
            {
              output[strlen(output)-1] = 0;
              g_print (
      ",'source':'https://git.gnome.org/browse/gegl/tree/%s'\n", output);
            }
          g_free (output);
        }

        g_free (commandline);
      }

      if (categoris)
      {
        const gchar *ptr = categoris;
          gboolean first = TRUE;
        g_print (",'categories':[");

        while (ptr && *ptr)
          {
            gchar category[64]="";
            gint i=0;
            while (*ptr && *ptr!=':' && i<63)
              {
                category[i++]=*(ptr++);
                category[i]='\0';
              }
            if (*ptr==':')
              ptr++;
            {
              if (first)
                first = FALSE;
              else
                g_print (",");
              g_print ("'%s'", category);
            }
          }
        g_print ("]\n");
      }

      json_list_properties (G_OBJECT_CLASS_TYPE (klass), name);
      json_list_pads (G_OBJECT_CLASS_TYPE (klass), name);

      {
        guint nkeys;
        gchar **keys = gegl_operation_list_keys (name, &nkeys);

        if (keys)
          {
            for (gint i = 0; keys[i]; i++)
              {
                const gchar *value = gegl_operation_get_key (name, keys[i]);

                if (g_str_equal (keys[i], "categories") ||
                    g_str_equal (keys[i], "cl-source") ||
                    g_str_equal (keys[i], "source") ||
                    g_str_equal (keys[i], "name")
                    )
                  continue;

                g_print (",\"%s\":\"", keys[i]);
                json_escape_string (value);
                g_print ("\"\n");
              }
            g_free (keys);
          }
      }

      g_print (" }\n");
    }
  g_print ("]\n");

  return 0;
}
Esempio n. 5
0
static void
json_list_properties (GType type, const gchar *opname)
{
  GParamSpec **self;
  GParamSpec **parent;
  guint n_self;
  guint n_parent;
  gint prop_no;
  gboolean first_prop = TRUE;

  g_print (",'properties':[\n");

  if (!type)
    return;

  self = g_object_class_list_properties (
            G_OBJECT_CLASS (g_type_class_ref (type)),
            &n_self);
  parent = g_object_class_list_properties (
            /*G_OBJECT_CLASS (g_type_class_peek_parent (g_type_class_ref (type))),*/
            G_OBJECT_CLASS (g_type_class_ref (GEGL_TYPE_OPERATION)),
            &n_parent);


  for (prop_no=0;prop_no<n_self;prop_no++)
    {
      gint parent_no;
      gboolean found=FALSE;
      for (parent_no=0;parent_no<n_parent;parent_no++)
        if (self[prop_no]==parent[parent_no])
          found=TRUE;
      /* only print properties if we are an addition compared to
       * GeglOperation
       */
      if (!found)
        {
          const gchar *type_name = g_type_name (G_OBJECT_TYPE (self[prop_no]));

          if (first_prop)
          {
            first_prop = FALSE;
            g_print(" { 'name':'%s'\n", g_param_spec_get_name (self[prop_no]));
          }
          else
            g_print(",{'name':'%s'\n", g_param_spec_get_name (self[prop_no]));

          g_print("  ,'label':\"");
            json_escape_string (g_param_spec_get_nick (self[prop_no]));
          g_print ("\"\n");

          if(strstr (type_name, "Param"))
          {
            type_name = strstr (type_name, "Param");
            type_name+=5;
          }

          g_print("  ,'type':'");
          {
            for (const char *p = type_name; *p; p++)
              g_print("%c", g_ascii_tolower (*p));
          }
          g_print("'\n");

          if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (self[prop_no]), G_TYPE_DOUBLE))
            {
              gdouble default_value = G_PARAM_SPEC_DOUBLE (self[prop_no])->default_value;
              gdouble min = G_PARAM_SPEC_DOUBLE (self[prop_no])->minimum;
              gdouble max = G_PARAM_SPEC_DOUBLE (self[prop_no])->maximum;

              if (default_value<-10000000)
                g_print ("  ,'default':'-inf'\n");
              else if (default_value>10000000)
                g_print ("  ,'default':'+inf'\n");
              else
                g_print ("  ,'default':'%2.2f'\n", default_value);

              if (min<-10000000)
                g_print ("  ,'minimum':'-inf'\n");
              else
                g_print ("  ,'minimum':'%2.2f'\n", min);

              if (max>10000000)
                g_print ("  ,'maximum':'+inf'\n");
              else
                g_print ("  ,'maximum':'%2.2f'\n", max);
              
              if (GEGL_IS_PARAM_SPEC_DOUBLE (self[prop_no]))
              {
                GeglParamSpecDouble *pspec =
                              GEGL_PARAM_SPEC_DOUBLE (self[prop_no]);

                if (pspec->ui_minimum < -10000000)
                  g_print ("  ,'ui-minimum':'-inf'\n");
                else
                  g_print ("  ,'ui-minimum':'%2.2f'\n", pspec->ui_minimum);

                if (pspec->ui_maximum > 10000000)
                  g_print ("  ,'ui-maximum':'+inf'\n");
                else
                  g_print ("  ,'ui-maximum':'%2.2f'\n", pspec->ui_maximum);

                g_print ("  ,'ui-gamma':'%2.2f'\n", pspec->ui_gamma);
                g_print ("  ,'ui-step-small':'%2.2f'\n", pspec->ui_step_small);
                g_print ("  ,'ui-step-big':'%2.2f'\n", pspec->ui_step_big);
                g_print ("  ,'ui-digits':'%i'\n", pspec->ui_digits);
              }

            }
          else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (self[prop_no]), G_TYPE_INT))
            {
              gint default_value = G_PARAM_SPEC_INT (self[prop_no])->default_value;
              gint min = G_PARAM_SPEC_INT (self[prop_no])->minimum;
              gint max = G_PARAM_SPEC_INT (self[prop_no])->maximum;

              if (default_value<-10000000)
                g_print ("  ,'default':'-inf'\n");
              else if (default_value>10000000)
                g_print ("  ,'default':'+inf'\n");
              else
                g_print ("  ,'default':'%i'\n", default_value);

              if (min<-10000000)
                g_print ("  ,'minimum':'-inf'\n");
              else
                g_print ("  ,'minimum':'%i'\n", min);

              if (max>10000000)
                g_print ("  ,'maximum':'+inf'\n");
              else
                g_print ("  ,'maximum':'%i'\n", max);

              if (GEGL_IS_PARAM_SPEC_INT (self[prop_no]))
              {
                GeglParamSpecInt *pspec =
                              GEGL_PARAM_SPEC_INT (self[prop_no]);

                if (pspec->ui_minimum < -10000000)
                  g_print ("  ,'ui-minimum':'-inf'\n");
                else
                  g_print ("  ,'ui-minimum':'%i'\n", pspec->ui_minimum);

                if (pspec->ui_maximum > 10000000)
                  g_print ("  ,'ui-maximum':'+inf'\n");
                else
                  g_print ("  ,'ui-maximum':'%i'\n", pspec->ui_maximum);

                g_print ("  ,'ui-gamma':'%2.2f'\n", pspec->ui_gamma);
                g_print ("  ,'ui-step-small':'%i'\n", pspec->ui_step_small);
                g_print ("  ,'ui-step-big':'%i'\n", pspec->ui_step_big);
              }

            }
          else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (self[prop_no]), G_TYPE_BOOLEAN))
            {
              g_print ("  ,'default':'%s'\n", G_PARAM_SPEC_BOOLEAN (self[prop_no])->default_value?"True":"False");
            }
          else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (self[prop_no]), G_TYPE_STRING))
            {
              const gchar *string = G_PARAM_SPEC_STRING (self[prop_no])->default_value;

              g_print ("  ,'default':\"");
              json_escape_string (string);
              g_print ("\"\n");

            }
          else if (g_type_is_a (G_PARAM_SPEC_VALUE_TYPE (self[prop_no]), GEGL_TYPE_COLOR))
            {
              GeglColor *color = gegl_param_spec_color_get_default (self[prop_no]);
              if (color)
                {
                  gchar *string;

                  g_object_get (color, "string", &string, NULL);
                  g_print ("  ,'default':\"");
                  json_escape_string (string);
                  g_print ("\"\n");
                  g_free (string);
                }
            }
          else
            {
            }

          if (g_param_spec_get_blurb (self[prop_no]) &&
              g_param_spec_get_blurb (self[prop_no])[0]!='\0')
          {
            g_print ("  ,'description':\"");
          
            json_escape_string (g_param_spec_get_blurb (self[prop_no]));
            g_print ("\"\n");
          }

      {
        guint count;
        gchar **property_keys = gegl_operation_list_property_keys (
            opname, 
            g_param_spec_get_name (self[prop_no]),
            
            &count);

        if (property_keys)
        {
          int i;
          if (property_keys[0])
          {
            /* XXX: list is in reverse order */
            for (i = 0; property_keys[i]; i++)
            {
              g_print ("  ,'%s':'%s'\n",
                    property_keys[i],
                    gegl_operation_get_property_key (opname, 
                                      g_param_spec_get_name (self[prop_no]),
                                                property_keys[i]));
            }
          }
          g_free (property_keys);
        }
      }

          g_print(" }");
        }
    }
  if (self)
    g_free (self);
  if (parent)
    g_free (parent);
  g_print ("]");
}
Esempio n. 6
0
static int meta_data_keys_to_json(char *buffer, size_t buffer_size, /* {{{ */
                                  meta_data_t *meta, char **keys,
                                  size_t keys_num) {
  size_t offset = 0;
  int status;

  buffer[0] = 0;

#define BUFFER_ADD(...)                                                        \
  do {                                                                         \
    status = ssnprintf(buffer + offset, buffer_size - offset, __VA_ARGS__);    \
    if (status < 1)                                                            \
      return (-1);                                                             \
    else if (((size_t)status) >= (buffer_size - offset))                       \
      return (-ENOMEM);                                                        \
    else                                                                       \
      offset += ((size_t)status);                                              \
  } while (0)

  for (size_t i = 0; i < keys_num; ++i) {
    int type;
    char *key = keys[i];

    type = meta_data_type(meta, key);
    if (type == MD_TYPE_STRING) {
      char *value = NULL;
      if (meta_data_get_string(meta, key, &value) == 0) {
        char temp[512] = "";

        status = json_escape_string(temp, sizeof(temp), value);
        sfree(value);
        if (status != 0)
          return status;

        BUFFER_ADD(",\"%s\":%s", key, temp);
      }
    } else if (type == MD_TYPE_SIGNED_INT) {
      int64_t value = 0;
      if (meta_data_get_signed_int(meta, key, &value) == 0)
        BUFFER_ADD(",\"%s\":%" PRIi64, key, value);
    } else if (type == MD_TYPE_UNSIGNED_INT) {
      uint64_t value = 0;
      if (meta_data_get_unsigned_int(meta, key, &value) == 0)
        BUFFER_ADD(",\"%s\":%" PRIu64, key, value);
    } else if (type == MD_TYPE_DOUBLE) {
      double value = 0.0;
      if (meta_data_get_double(meta, key, &value) == 0)
        BUFFER_ADD(",\"%s\":%f", key, value);
    } else if (type == MD_TYPE_BOOLEAN) {
      _Bool value = 0;
      if (meta_data_get_boolean(meta, key, &value) == 0)
        BUFFER_ADD(",\"%s\":%s", key, value ? "true" : "false");
    }
  } /* for (keys) */

  if (offset == 0)
    return (ENOENT);

  buffer[0] = '{'; /* replace leading ',' */
  BUFFER_ADD("}");

#undef BUFFER_ADD

  return (0);
} /* }}} int meta_data_keys_to_json */