<commit_before>#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../inc/macros.h"
#include "../inc/parse.h"

/**
 * @file parse.c
 *
 * Contains implementations of the functions defined in parse.h plus some static helper functions.
 */

/**
 * The size of the line buffer used in `parseLogEntry()`.
 */
#define PARSER_LINEBUFFER_SIZE 1024

/**
 * Converts a string to a HTTP method constant.
 * The constants defined in Logresult->h are used.
 *
 * @see Logresult->h
 *
 * @param str A string that represents a HTTP method. If `str` is `NULL`, `HTTP_UNKNOWN` is returned.
 * @return the constant for the  HTTP method, `str` represents.
 */
static unsigned short toHttpMethod(const char *str)
{
	if (str == NULL) return HTTP_UNKNOWN;
	else if (!strcasecmp(str, "CONNECT")) return HTTP_CONNECT;
	else if (!strcasecmp(str, "DELETE")) return HTTP_DELETE;
	else if (!strcasecmp(str, "GET")) return HTTP_GET;
	else if (!strcasecmp(str, "HEAD")) return HTTP_HEAD;
	else if (!strcasecmp(str, "OPTIONS")) return HTTP_OPTIONS;
	else if (!strcasecmp(str, "POST")) return HTTP_POST;
	else if (!strcasecmp(str, "PUT")) return HTTP_PUT;
	else if (!strcasecmp(str, "TRACE")) return HTTP_TRACE;
	else return HTTP_UNKNOWN;
}

static unsigned char toMonth(char *str)
{
	if (str == NULL)
		return 0;

	for (int i = 0; i < 3; i++)
		str[i] = (char)tolower(str[i]);

	if (!strcmp(str, "jan")) return 1;
	else if (!strcmp(str, "feb")) return 2;
	else if (!strcmp(str, "mar")) return 3;
	else if (!strcmp(str, "apr")) return 4;
	else if (!strcmp(str, "may")) return 5;
	else if (!strcmp(str, "jun")) return 6;
	else if (!strcmp(str, "jul")) return 7;
	else if (!strcmp(str, "aug")) return 8;
	else if (!strcmp(str, "sep")) return 9;
	else if (!strcmp(str, "oct")) return 10;
	else if (!strcmp(str, "nov")) return 11;
	else if (!strcmp(str, "dec")) return 12;
	else return 0;
}

// TODO: nochma durchdenken, was passiert, wenn der string kuerzer is, als gedacht
int parseLogEntry(FILE *stream, struct LogEntry *result)
{
	if (stream == NULL)
		return -1;
	if (result == NULL)
		return -1;

	// line buffer
	char buffer[PARSER_LINEBUFFER_SIZE];
	// pointer to the current position in buffer
	char *cur = buffer;<commit_msg>Added TODO in parse.c.<commit_after>#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "../inc/macros.h"
#include "../inc/parse.h"

/**
 * @file parse.c
 *
 * Contains implementations of the functions defined in parse.h plus some static helper functions.
 */

/**
 * The size of the line buffer used in `parseLogEntry()`.
 */
#define PARSER_LINEBUFFER_SIZE 1024

/**
 * Converts a string to a HTTP method constant.
 * The constants defined in Logresult->h are used.
 *
 * @see Logresult->h
 *
 * @param str A string that represents a HTTP method. If `str` is `NULL`, `HTTP_UNKNOWN` is returned.
 * @return the constant for the  HTTP method, `str` represents.
 */
static unsigned short toHttpMethod(const char *str)
{
	if (str == NULL) return HTTP_UNKNOWN;
	else if (!strcasecmp(str, "CONNECT")) return HTTP_CONNECT;
	else if (!strcasecmp(str, "DELETE")) return HTTP_DELETE;
	else if (!strcasecmp(str, "GET")) return HTTP_GET;
	else if (!strcasecmp(str, "HEAD")) return HTTP_HEAD;
	else if (!strcasecmp(str, "OPTIONS")) return HTTP_OPTIONS;
	else if (!strcasecmp(str, "POST")) return HTTP_POST;
	else if (!strcasecmp(str, "PUT")) return HTTP_PUT;
	else if (!strcasecmp(str, "TRACE")) return HTTP_TRACE;
	else return HTTP_UNKNOWN;
}

static unsigned char toMonth(char *str)
{
	if (str == NULL)
		return 0;

	for (int i = 0; i < 3; i++)
		str[i] = (char)tolower(str[i]);

	if (!strcmp(str, "jan")) return 1;
	else if (!strcmp(str, "feb")) return 2;
	else if (!strcmp(str, "mar")) return 3;
	else if (!strcmp(str, "apr")) return 4;
	else if (!strcmp(str, "may")) return 5;
	else if (!strcmp(str, "jun")) return 6;
	else if (!strcmp(str, "jul")) return 7;
	else if (!strcmp(str, "aug")) return 8;
	else if (!strcmp(str, "sep")) return 9;
	else if (!strcmp(str, "oct")) return 10;
	else if (!strcmp(str, "nov")) return 11;
	else if (!strcmp(str, "dec")) return 12;
	else return 0;
}

// TODO: die datums-zahlen nicht groesser als y=9999, m=99, d=99 zulassen, oder vlt sogar m=12, d=31
// TODO: nochma durchdenken, was passiert, wenn der string kuerzer is, als gedacht
int parseLogEntry(FILE *stream, struct LogEntry *result)
{
	if (stream == NULL)
		return -1;
	if (result == NULL)
		return -1;

	// line buffer
	char buffer[PARSER_LINEBUFFER_SIZE];
	// pointer to the current position in buffer
	char *cur = buffer;
{'fraction_non_alphanumeric': 0.1549935149156939, 'fraction_numerical': 0.01340250756593169, 'mean_word_length': 4.424384525205158, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '3979935', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1626, 'n_words': 577}