<commit_before>/**
 * collection4 - action_list_graphs.c
 * Copyright (C) 2010  <NAME>
 * 
 * This program 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.1 of the License, or (at your option) any later version.
 * 
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors:
 *   <NAME> <ff at octo.it>
 **/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "action_list_graphs.h"
#include "common.h"
#include "graph.h"
#include "graph_ident.h"
#include "graph_instance.h"
#include "graph_list.h"
#include "utils_cgi.h"

#include <fcgiapp.h>
#include <fcgi_stdio.h>

#define RESULT_LIMIT 50

struct callback_data_s
{
  graph_config_t *cfg;
  int graph_index;
  int graph_limit;
  _Bool graph_more;
  int inst_index;
  int inst_limit;
  _Bool inst_more;
};
typedef struct callback_data_s callback_data_t;

static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
    graph_instance_t *inst,
    void *user_data)
{
  callback_data_t *data = user_data;
  char params[1024];
  char desc[1024];

  if (data->cfg != cfg)
  {
    data->graph_index++;
    if (data->graph_index >= data->graph_limit)
    {
      data->graph_more = 1;
      return (1);
    }

    if (data->cfg != NULL)
      printf ("  </ul></li>\n");

    memset (desc, 0, sizeof (desc));
    graph_get_title (cfg, desc, sizeof (desc));
    html_escape_buffer (desc, sizeof (desc));

    printf ("  <li class=\"graph\">%s\n"
        "  <ul class=\"instance_list\">\n", desc);

    data->cfg = cfg;
    data->inst_index = -1;
    data->inst_more = 0;
  }

  data->inst_index++;
  if (data->inst_index >= data->inst_limit)
  {
    if (!data->inst_more)
    {
      memset (params, 0, sizeof (params));
      graph_get_params (cfg, params, sizeof (params));
      html_escape_buffer (params, sizeof (params));

      printf ("    <li class=\"instance more\"><a href=\"%s"
          "?action=show_graph;%s\">More &#x2026;</a></li>\n",
          script_name (), params);

      data->inst_more = 1;
    }
    return (0);
  }

  memset (params, 0, sizeof (params));
  inst_get_params (cfg, inst, params, sizeof (params));
  html_escape_buffer (params, sizeof (params));

  memset (desc, 0, sizeof (desc));
  inst_describe (cfg, inst, desc, sizeof (desc));
  html_escape_buffer (desc, sizeof (desc));

  printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
      script_name (), params, desc);

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

static int print_graph_html (graph_config_t *cfg, /* {{{ */
    __attribute__((unused)) void *user_data)
{
  char params[1024];
  char title[1024];

  memset (title, 0, sizeof (title));
  graph_get_title (cfg, title, sizeof (title));
  html_escape_buffer (title, sizeof (title));

  memset (params, 0, sizeof (params));
  graph_get_params (cfg, params, sizeof (params));
  html_escape_buffer (params, sizeof (params));

  printf ("      <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
      "%s</a></li>\n",
      script_name (), params, title);

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

struct page_data_s
{
  const char *search_term;
};
typedef struct page_data_s page_data_t;

static int print_search_result (void *user_data) /* {{{ */
{
  page_data_t *pg_data = user_data;
  callback_data_t cb_data = { /* cfg = */ NULL,
    /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
    /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0 };

  if (pg_data->search_term != NULL)
  {
    char *search_term_html = html_escape (pg_data->search_term);
    printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
        search_term_html);
    free (search_term_html);
  }

  printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
  if (pg_data->search_term == NULL)
  {
    gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);<commit_msg>"list graphs" action: Pass the search term to the "show graph" action.<commit_after>/**
 * collection4 - action_list_graphs.c
 * Copyright (C) 2010  <NAME>
 * 
 * This program 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.1 of the License, or (at your option) any later version.
 * 
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors:
 *   <NAME> <ff at octo.it>
 **/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "action_list_graphs.h"
#include "common.h"
#include "graph.h"
#include "graph_ident.h"
#include "graph_instance.h"
#include "graph_list.h"
#include "utils_cgi.h"

#include <fcgiapp.h>
#include <fcgi_stdio.h>

#define RESULT_LIMIT 50

struct callback_data_s
{
  graph_config_t *cfg;
  int graph_index;
  int graph_limit;
  _Bool graph_more;
  int inst_index;
  int inst_limit;
  _Bool inst_more;
  const char *search_term;
};
typedef struct callback_data_s callback_data_t;

static int print_graph_inst_html (graph_config_t *cfg, /* {{{ */
    graph_instance_t *inst,
    void *user_data)
{
  callback_data_t *data = user_data;
  char params[1024];
  char desc[1024];

  if (data->cfg != cfg)
  {
    data->graph_index++;
    if (data->graph_index >= data->graph_limit)
    {
      data->graph_more = 1;
      return (1);
    }

    if (data->cfg != NULL)
      printf ("  </ul></li>\n");

    memset (desc, 0, sizeof (desc));
    graph_get_title (cfg, desc, sizeof (desc));
    html_escape_buffer (desc, sizeof (desc));

    printf ("  <li class=\"graph\">%s\n"
        "  <ul class=\"instance_list\">\n", desc);

    data->cfg = cfg;
    data->inst_index = -1;
    data->inst_more = 0;
  }

  data->inst_index++;
  if (data->inst_index >= data->inst_limit)
  {
    if (!data->inst_more)
    {
      char *search_term_html = html_escape (data->search_term);
      char param_search_term[1024];

      memset (params, 0, sizeof (params));
      graph_get_params (cfg, params, sizeof (params));
      html_escape_buffer (params, sizeof (params));

      param_search_term[0] = 0;
      if (search_term_html != NULL)
      {
        snprintf (param_search_term, sizeof (param_search_term), ";q=%s",
            search_term_html);
        param_search_term[sizeof (param_search_term) - 1] = 0;
      }

      free (search_term_html);

      printf ("    <li class=\"instance more\"><a href=\"%s"
          "?action=show_graph;%s%s\">More &#x2026;</a></li>\n",
          script_name (), params, param_search_term);

      data->inst_more = 1;
    }
    return (0);
  }

  memset (params, 0, sizeof (params));
  inst_get_params (cfg, inst, params, sizeof (params));
  html_escape_buffer (params, sizeof (params));

  memset (desc, 0, sizeof (desc));
  inst_describe (cfg, inst, desc, sizeof (desc));
  html_escape_buffer (desc, sizeof (desc));

  printf ("    <li class=\"instance\"><a href=\"%s?action=show_instance;%s\">%s</a></li>\n",
      script_name (), params, desc);

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

static int print_graph_html (graph_config_t *cfg, /* {{{ */
    __attribute__((unused)) void *user_data)
{
  char params[1024];
  char title[1024];

  memset (title, 0, sizeof (title));
  graph_get_title (cfg, title, sizeof (title));
  html_escape_buffer (title, sizeof (title));

  memset (params, 0, sizeof (params));
  graph_get_params (cfg, params, sizeof (params));
  html_escape_buffer (params, sizeof (params));

  printf ("      <li class=\"graph\"><a href=\"%s?action=show_graph;%s\">"
      "%s</a></li>\n",
      script_name (), params, title);

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

struct page_data_s
{
  const char *search_term;
};
typedef struct page_data_s page_data_t;

static int print_search_result (void *user_data) /* {{{ */
{
  page_data_t *pg_data = user_data;
  callback_data_t cb_data = { /* cfg = */ NULL,
    /* graph_index = */ -1, /* graph_limit = */ 20, /* graph_more = */ 0,
    /* inst_index = */  -1, /* inst_limit = */   5, /* inst more = */  0,
    /* search_term = */ pg_data->search_term };

  if (pg_data->search_term != NULL)
  {
    char *search_term_html = html_escape (pg_data->search_term);
    printf ("    <h2>Search results for &quot;%s&quot;</h2>\n",
        search_term_html);
    free (search_term_html);
  }

  printf ("    <ul id=\"search-output\" class=\"graph_list\">\n");
  if (pg_data->search_term == NULL)
  {
    gl_graph_get_all (print_graph_html, /* user_data = */ &cb_data);
{'fraction_non_alphanumeric': 0.15132342085837816, 'fraction_numerical': 0.014130549404196984, 'mean_word_length': 3.40297121634169, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3361711', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3220, 'n_words': 881}