<commit_before>/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

#include <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pool.h"
#include "repo.h"
#include "repo_repomdxml.h"
#include "common_write.h"

static void
usage(const char *err)
{
  if (err)
    fprintf(stderr, "\n** Error:\n  %s\n", err);
  fprintf(stderr, "\nUsage:\n"
          "repomdxml2solv [-a][-h][-k][-n <attrname>]\n"
	  "  reads a 'repomd.xml' file from <stdin> and writes a .solv file to <stdout>\n"
	  "  -h : print help & exit\n"
	 );
   exit(0);
}

int
main(int argc, char **argv)
{
  int flags = 0;
  
  Pool *pool = pool_create();
  Repo *repo = repo_create(pool, "<stdin>");

  argv++;
  argc--;
  while (argc--)
    {
      const char *s = argv[0];
      if (*s++ == '-')
        while (*s)
          switch (*s++)
	    {
	      case 'h': usage(NULL); break;
	      default : break;
	    }
      argv++;
    }

  repo_add_repomdxml(repo, stdin, flags);
  tool_write(repo, 0, 0);
  pool_free(pool);
  exit(0);<commit_msg>- add query mode<commit_after>/*
 * Copyright (c) 2007-2009, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

#include <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "pool.h"
#include "repo.h"
#include "repo_repomdxml.h"
#include "common_write.h"

static void
usage(int status)
{
  fprintf(stderr, "\nUsage:\n"
          "repomdxml2solv [-q query]\n"
	  "  reads a 'repomd.xml' file from <stdin> and writes a .solv file to <stdout>\n"
	  "  -q : query a repomd data entry\n"
	  "  -h : print help & exit\n"
	 );
   exit(status);
}

static void
doquery(Pool *pool, Repo *repo, const char *query)
{
  Id id, type = 0;
  char qbuf[256];
  const char *qp;
  Dataiterator di;

  qp = strchr(query, ':');
  if (qp)
    {
      type = strn2id(pool, query, qp - query, 0);
      if (!type)
	exit(0);
      qp++;
    }
  else
    qp = query;
  snprintf(qbuf, sizeof(qbuf), "repository:repomd:%s", qp);
  id = str2id(pool, qbuf, 0);
  if (!id)
    exit(0);
  dataiterator_init(&di, pool, repo, SOLVID_META, id, 0, 0);
  dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
  while (dataiterator_step(&di))
    {
      if (type)
	{
	  dataiterator_setpos_parent(&di);
	  if (pool_lookup_id(pool, SOLVID_POS, REPOSITORY_REPOMD_TYPE) != type)
	    continue;
	}
      switch (di.key->type)
	{
	case REPOKEY_TYPE_ID:
	case REPOKEY_TYPE_CONSTANTID:
	  printf("%s\n", id2str(pool, di.kv.id));
	  break;
	case REPOKEY_TYPE_STR:
	  printf("%s\n", di.kv.str);
	  break;
	case REPOKEY_TYPE_NUM:
	  printf("%d\n", di.kv.num);
	  break;
	case REPOKEY_TYPE_SHA1:
	  printf("sha1:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
	  break;
	case REPOKEY_TYPE_SHA256:
	  printf("sha256:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
	  break;
	default:
	  break;
	}
    }
  dataiterator_free(&di);
}

int
main(int argc, char **argv)
{
  int c, flags = 0;
  const char *query = 0;
  
  Pool *pool = pool_create();
  Repo *repo = repo_create(pool, "<stdin>");

  while ((c = getopt (argc, argv, "hq:")) >= 0)
    {
      switch(c)
        {
        case 'h':
          usage(0);
          break;
        case 'q':
	  query = optarg;
          break;
	default:
          usage(1);
          break;
        }
    }
  repo_add_repomdxml(repo, stdin, flags);
  if (query)
    doquery(pool, repo, query);
  else
    tool_write(repo, 0, 0);
  pool_free(pool);
  exit(0);
{'fraction_non_alphanumeric': 0.1674731182795699, 'fraction_numerical': 0.013709677419354839, 'mean_word_length': 2.888192267502612, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6682197', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1490, 'n_words': 357}