Blame json-glib/json-glib-validate.c

Packit 4c4d6b
/* json-glib-validate - Checks JSON data for errors
Packit 4c4d6b
 * 
Packit 4c4d6b
 * This file is part of JSON-GLib
Packit 4c4d6b
 *
Packit 4c4d6b
 * Copyright © 2013  Emmanuele Bassi
Packit 4c4d6b
 *
Packit 4c4d6b
 * This library is free software; you can redistribute it and/or
Packit 4c4d6b
 * modify it under the terms of the GNU Lesser General Public
Packit 4c4d6b
 * License as published by the Free Software Foundation; either
Packit 4c4d6b
 * version 2.1 of the License, or (at your option) any later version.
Packit 4c4d6b
 *
Packit 4c4d6b
 * This library is distributed in the hope that it will be useful,
Packit 4c4d6b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4c4d6b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4c4d6b
 * Lesser General Public License for more details.
Packit 4c4d6b
 *
Packit 4c4d6b
 * You should have received a copy of the GNU Lesser General Public
Packit 4c4d6b
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
Packit 4c4d6b
 *
Packit 4c4d6b
 * Author:
Packit 4c4d6b
 *   Emmanuele Bassi  <ebassi@gnome.org>
Packit 4c4d6b
 */
Packit 4c4d6b
Packit 4c4d6b
#include "config.h"
Packit 4c4d6b
Packit 4c4d6b
#ifdef HAVE_UNISTD_H
Packit 4c4d6b
#include <unistd.h>
Packit 4c4d6b
#endif
Packit 4c4d6b
#include <stdlib.h>
Packit 4c4d6b
#include <stdio.h>
Packit 4c4d6b
#include <locale.h>
Packit 4c4d6b
#include <errno.h>
Packit 4c4d6b
Packit 4c4d6b
#include <glib.h>
Packit 4c4d6b
#include <glib/gi18n.h>
Packit 4c4d6b
#include <json-glib/json-glib.h>
Packit 4c4d6b
Packit 4c4d6b
static char **files = NULL;
Packit 4c4d6b
Packit 4c4d6b
static GOptionEntry entries[] = {
Packit 4c4d6b
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL },
Packit 4c4d6b
  { NULL },
Packit 4c4d6b
};
Packit 4c4d6b
Packit 4c4d6b
static gboolean
Packit 4c4d6b
validate (JsonParser *parser,
Packit 4c4d6b
          GFile      *file)
Packit 4c4d6b
{
Packit 4c4d6b
  GInputStream *in;
Packit 4c4d6b
  GError *error;
Packit 4c4d6b
  gboolean res = TRUE;
Packit 4c4d6b
  gboolean parse_res;
Packit 4c4d6b
  gboolean close_res;
Packit 4c4d6b
Packit 4c4d6b
  error = NULL;
Packit 4c4d6b
Packit 4c4d6b
  in = (GInputStream *) g_file_read (file, NULL, &error);
Packit 4c4d6b
  if (in == NULL)
Packit 4c4d6b
    {
Packit 4c4d6b
      /* Translators: the first %s is the program name, the second one
Packit 4c4d6b
       * is the URI of the file, the third is the error message.
Packit 4c4d6b
       */
Packit 4c4d6b
      g_printerr (_("%s: %s: error opening file: %s\n"),
Packit 4c4d6b
                  g_get_prgname (), g_file_get_uri (file), error->message);
Packit 4c4d6b
      g_error_free (error);
Packit 4c4d6b
      return FALSE;
Packit 4c4d6b
    }
Packit 4c4d6b
Packit 4c4d6b
  parse_res = json_parser_load_from_stream (parser, in, NULL, &error);
Packit 4c4d6b
  if (!parse_res)
Packit 4c4d6b
    {
Packit 4c4d6b
      /* Translators: the first %s is the program name, the second one
Packit 4c4d6b
       * is the URI of the file, the third is the error message.
Packit 4c4d6b
       */
Packit 4c4d6b
      g_printerr (_("%s: %s: error parsing file: %s\n"),
Packit 4c4d6b
                  g_get_prgname (), g_file_get_uri (file), error->message);
Packit 4c4d6b
      g_clear_error (&error);
Packit 4c4d6b
      res = FALSE;
Packit 4c4d6b
    }
Packit 4c4d6b
Packit 4c4d6b
  close_res = g_input_stream_close (in, NULL, &error);
Packit 4c4d6b
  if (!close_res)
Packit 4c4d6b
    {
Packit 4c4d6b
      /* Translators: the first %s is the program name, the second one
Packit 4c4d6b
       * is the URI of the file, the third is the error message.
Packit 4c4d6b
       */
Packit 4c4d6b
      g_printerr (_("%s: %s: error closing: %s\n"),
Packit 4c4d6b
                  g_get_prgname (), g_file_get_uri (file), error->message);
Packit 4c4d6b
      g_clear_error (&error);
Packit 4c4d6b
      res = FALSE;
Packit 4c4d6b
    }
Packit 4c4d6b
Packit 4c4d6b
  return res;
Packit 4c4d6b
}
Packit 4c4d6b
Packit 4c4d6b
int
Packit 4c4d6b
main (int   argc,
Packit 4c4d6b
      char *argv[])
Packit 4c4d6b
{
Packit 4c4d6b
  GOptionContext *context = NULL;
Packit 4c4d6b
  GError *error = NULL;
Packit 4c4d6b
  const char *description;
Packit 4c4d6b
  const char *summary;
Packit 4c4d6b
  gchar *param;
Packit 4c4d6b
  JsonParser *parser;
Packit 4c4d6b
  gboolean res;
Packit 4c4d6b
  int i;
Packit 4c4d6b
Packit 4c4d6b
  setlocale (LC_ALL, "");
Packit 4c4d6b
Packit 4c4d6b
  bindtextdomain (GETTEXT_PACKAGE, JSON_LOCALEDIR);
Packit 4c4d6b
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
Packit 4c4d6b
  textdomain (GETTEXT_PACKAGE);
Packit 4c4d6b
Packit 4c4d6b
  param = g_strdup_printf (("%s..."), _("FILE"));
Packit 4c4d6b
  /* Translators: this message will appear after the usage string */
Packit 4c4d6b
  /* and before the list of options.                              */
Packit 4c4d6b
  summary = _("Validate JSON files.");
Packit 4c4d6b
  description = _("json-glib-validate validates JSON data at the given URI.");
Packit 4c4d6b
Packit 4c4d6b
  context = g_option_context_new (param);
Packit 4c4d6b
  g_option_context_set_summary (context, summary);
Packit 4c4d6b
  g_option_context_set_description (context, description);
Packit 4c4d6b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit 4c4d6b
  g_option_context_parse (context, &argc, &argv, &error);
Packit 4c4d6b
  g_option_context_free (context);
Packit 4c4d6b
Packit 4c4d6b
  g_free (param);
Packit 4c4d6b
Packit 4c4d6b
  if (error != NULL)
Packit 4c4d6b
    {
Packit 4c4d6b
      /* Translators: the %s is the program name. This error message
Packit 4c4d6b
       * means the user is calling json-glib-validate without any
Packit 4c4d6b
       * argument.
Packit 4c4d6b
       */
Packit 4c4d6b
      g_printerr (_("Error parsing commandline options: %s\n"), error->message);
Packit 4c4d6b
      g_printerr ("\n");
Packit 4c4d6b
      g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
Packit 4c4d6b
      g_printerr ("\n");
Packit 4c4d6b
      g_error_free (error);
Packit 4c4d6b
      return 1;
Packit 4c4d6b
    }
Packit 4c4d6b
Packit 4c4d6b
  if (files == NULL)
Packit 4c4d6b
    {
Packit 4c4d6b
      /* Translators: the %s is the program name. This error message
Packit 4c4d6b
       * means the user is calling json-glib-validate without any
Packit 4c4d6b
       * argument.
Packit 4c4d6b
       */
Packit 4c4d6b
      g_printerr (_("%s: missing files"), g_get_prgname ());
Packit 4c4d6b
      g_printerr ("\n");
Packit 4c4d6b
      g_printerr (_("Try “%s --help” for more information."), g_get_prgname ());
Packit 4c4d6b
      g_printerr ("\n");
Packit 4c4d6b
      return 1;
Packit 4c4d6b
    }
Packit 4c4d6b
Packit 4c4d6b
  parser = json_parser_new ();
Packit 4c4d6b
  res = TRUE;
Packit 4c4d6b
  i = 0;
Packit 4c4d6b
Packit 4c4d6b
  do
Packit 4c4d6b
    {
Packit 4c4d6b
      GFile *file = g_file_new_for_commandline_arg (files[i]);
Packit 4c4d6b
Packit 4c4d6b
      res = validate (parser, file) && res;
Packit 4c4d6b
      g_object_unref (file);
Packit 4c4d6b
    }
Packit 4c4d6b
  while (files[++i] != NULL);
Packit 4c4d6b
Packit 4c4d6b
  g_object_unref (parser);
Packit 4c4d6b
Packit 4c4d6b
  return res ? EXIT_SUCCESS : EXIT_FAILURE;
Packit 4c4d6b
}