<commit_before>/**
 * output.c -- output to the standard output stream
 * Copyright (C) 2009-2013 by <NAME> <<EMAIL>>
 * GoAccess - An Ncurses apache weblog analyzer & interactive viewer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * A copy of the GNU General Public License is attached to this
 * source distribution for its full text.
 *
 * Visit http://goaccess.prosoftcorp.com for new releases.
 */

#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#include <ctype.h>
#include <glib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>

#include "output.h"

#include "commons.h"
#include "error.h"
#include "xmalloc.h"
#include "ui.h"
#include "gdashboard.h"
#include "util.h"

/* sanitize output with html entities for special chars */
static void
clean_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '\'':
          fprintf (fp, "&#39;");
          break;
       case '"':
          fprintf (fp, "&#34;");
          break;
       case '&':
          fprintf (fp, "&amp;");
          break;
       case '<':
          fprintf (fp, "&lt;");
          break;
       case '>':
          fprintf (fp, "&gt;");
          break;
       case ' ':
          fprintf (fp, "&nbsp;");
          break;
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

static void
escape_cvs_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '"':
          fprintf (fp, "\\\"");
          break;
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

static void
escape_json_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '"':
          fprintf (fp, "\\\"");
          break;
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

/* *INDENT-OFF* */
static void
print_html_header (FILE * fp, char *now)
{
   fprintf (fp, "<!DOCTYPE html>\n");
   fprintf (fp, "<html lang=\"en\"><head>\n");<commit_msg>added necessary escape for json<commit_after>/**
 * output.c -- output to the standard output stream
 * Copyright (C) 2009-2013 by <NAME> <<EMAIL>>
 * GoAccess - An Ncurses apache weblog analyzer & interactive viewer
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * A copy of the GNU General Public License is attached to this
 * source distribution for its full text.
 *
 * Visit http://goaccess.prosoftcorp.com for new releases.
 */

#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64

#include <ctype.h>
#include <glib.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <inttypes.h>

#include "output.h"

#include "commons.h"
#include "error.h"
#include "xmalloc.h"
#include "ui.h"
#include "gdashboard.h"
#include "util.h"

/* sanitize output with html entities for special chars */
static void
clean_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '\'':
          fprintf (fp, "&#39;");
          break;
       case '"':
          fprintf (fp, "&#34;");
          break;
       case '&':
          fprintf (fp, "&amp;");
          break;
       case '<':
          fprintf (fp, "&lt;");
          break;
       case '>':
          fprintf (fp, "&gt;");
          break;
       case ' ':
          fprintf (fp, "&nbsp;");
          break;
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

static void
escape_cvs_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '"':
          fprintf (fp, "\\\"");
          break;
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

static void
escape_json_output (FILE * fp, char *s)
{
   while (*s) {
      switch (*s) {
       case '"':
          fprintf (fp, "\\\"");
          break;
       case '\'':
          fprintf (fp, "\\\'");
          break;
       case '\\':
          fprintf (fp, "\\\\");
          break;
        //TODO: escape \t \b \r \n and charcters less than U0020 if something breaks it again
        //TODO: escape \\x?? to \\u00?? if necessary
       default:
          fputc (*s, fp);
          break;
      }
      s++;
   }
}

/* *INDENT-OFF* */
static void
print_html_header (FILE * fp, char *now)
{
   fprintf (fp, "<!DOCTYPE html>\n");
   fprintf (fp, "<html lang=\"en\"><head>\n");
{'fraction_non_alphanumeric': 0.1428833792470156, 'fraction_numerical': 0.0073461891643709825, 'mean_word_length': 2.1773628938156357, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '4831477', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1743, 'n_words': 590}