Example #1
0
static gboolean
po_file_is_empty (po_file_t file)
{
  const gchar *const *domains = po_file_domains (file);

  for (; *domains != NULL; domains++)
    {
      po_message_iterator_t iter = po_message_iterator (file, *domains);
      if (po_next_message (iter) != NULL)
        {
          po_message_iterator_free (iter);
          return FALSE;
        }
      po_message_iterator_free (iter);
    }
  return TRUE;
}
Example #2
0
static gboolean
_gtr_po_load (GtrPo * po, GFile * location, GError ** error)
{
  struct po_xerror_handler handler;
  po_message_iterator_t iter;
  po_message_t message;
  const gchar *msgid;
  gchar *filename;
  GtrPoPrivate *priv = gtr_po_get_instance_private (po);

  /*
   * Initialize the handler error.
   */
  handler.xerror = &on_gettext_po_xerror;
  handler.xerror2 = &on_gettext_po_xerror2;

  if (message_error != NULL)
    {
      g_free (message_error);
      message_error = NULL;
    }

  filename = g_file_get_path (location);

  if (priv->gettext_po_file)
    po_file_free (priv->gettext_po_file);

  if (priv->header)
    {
      g_object_unref (priv->header);
      priv->header = NULL;
    }

  if (priv->iter)
    {
      po_message_iterator_free (priv->iter);
      priv->iter = NULL;
    }

  priv->gettext_po_file = po_file_read (filename, &handler);
  g_free (filename);

  if (po_file_is_empty (priv->gettext_po_file))
    {
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_FILE_EMPTY, _("The file is empty"));
      return FALSE;
    }

  if (!priv->gettext_po_file)
    {
      g_set_error (error,
                   GTR_PO_ERROR,
                   GTR_PO_ERROR_FILENAME,
                   _("Failed opening file ā€œ%sā€: %s"),
                   filename, g_strerror (errno));
      g_free (filename);
      return FALSE;
    }

  iter = po_message_iterator (priv->gettext_po_file, NULL);
  message = po_next_message (iter);
  msgid = po_message_msgid (message);

  if (*msgid == '\0')
    priv->header = gtr_header_new (iter, message);
  else
    {
      po_message_iterator_free (iter);
      iter = po_message_iterator (priv->gettext_po_file, NULL);

      message = po_message_create ();
      po_message_set_msgid (message, "");
      po_message_set_msgstr (message, "");
      po_message_insert (iter, message);

      priv->header = gtr_header_new (iter, message);
    }

  priv->iter = iter;

  return TRUE;
}
Example #3
0
int main(int argc, char **argv)
{
	po_file_t po;
	po_message_iterator_t it;
	const char * const * domains;

	if(!argv[1])
	{
		fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
		return 0;
	}

	if(argv[2])
	{
		if(!strncmp(argv[2], "ask", 3))
		{
			solution_mode=1;
		} else if(!strncmp(argv[2], "fix", 3)) {
			solution_mode=2;
		} else {
			fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
			return 0;
		}
	}

	po = po_file_read(argv[1], &po_xerror_handler);

	if(!po)
	{
		fprintf(stderr,"Couldn't read the input po file\n");
		return 0;
	}

	domains = po_file_domains(po);

	if(!domains)
	{
		fprintf(stderr,"Couldn't find the message domains in the po file\n");
		return 0;
	}

	while(*domains)
	{
		it = po_message_iterator(po,*domains);
		process_messages(it);
		po_message_iterator_free(it);
		domains++;
	}

	if(total_errors == 0 && total_warnings == 0)
	{
		fprintf(stderr,"No errors found\n");
	} else if(solution_mode==0) {
		fprintf(stderr,"%d warnings, %d errors\n",total_warnings,total_errors);
	} else {
		fprintf(stderr,"%d warnings, %d errors, %d fixes, %d removed, %d ignored\n",total_warnings,total_errors, total_fix, total_remove, total_ignore);
		if(total_fix || total_remove)
		{
			int s = 0;
			printf("Commit changes to file? (y)es, (n)o:");
			while(s != 'y' && s != 'n') s = getchar();
			if(s == 'y')
			{
				if(po_file_write(po, argv[1], &po_xerror_handler)) printf("Changes committed.");
				else printf("Error writing file.");
			} else {
				printf("Changes discarded.");
			}
		}
	}

	po_file_free(po);

	return 0;
}