<commit_before>#include "parser.h"
#include <stdint.h>
#include <string.h>

#ifndef _MSC_VER
	#include <unistd.h>
	//#include <dirent.h>
#else
	#define F_OK 0

	extern int access(const char *path, int mode);
#endif


const char *const default_output = "export.txt";


void ws_init(workspace *const ws)
{
	ws->files_num = 0;
	ws->dirs_num = 0;
	ws->flags_num = 0;

	strcpy(ws->output, default_output);
	ws->was_error = 0;
}

void ws_add_error(workspace *const ws)
{
	if (ws == NULL)
	{
		return;
	}

	ws->was_error = 1;
}

workspace ws_parse_args(const int argc, const char *const *const argv)
{
	workspace ws;
	ws_init(&ws);

	if (argv == NULL)
	{
		ws_add_error(&ws);
		return ws;
	}

	for (int i = 1; i < argc; i++)
	{
		if (argv[i] == NULL)
		{
			ws_add_error(&ws);
			return ws;
		}

		if (argv[i][0] != '-')
		{
			if (ws_add_file(&ws, argv[i]) == -1)
			{
				return ws;
			}
			continue;
		}

		if (argv[i][1] == 'o' && argv[i][2] == '\0')
		{
			if (i + 1 < argc || ws_set_output(&ws, argv[++i]) == -1)
			{
				ws_add_error(&ws);
				return ws;
			}
			continue;
		}

		if (ws_add_flag(&ws, argv[i]) == -1)
		{
			return ws;
		}
	}

	return ws;
}


workspace ws_create()
{
	workspace ws;
	ws_init(&ws);
	return ws;
}


int ws_add_file(workspace *const ws, const char *const path)
{
	if (!ws_is_correct(ws) || path == NULL || access(path, F_OK) == -1)
	{
		ws_add_error(ws);
		return -1;
	}

	strcpy(ws->files[ws->files_num++], path);
	return 0;
}

int ws_add_files(workspace *const ws, const char *const *const paths, const size_t num)
{
	if (paths == NULL)
	{
		ws_add_error(ws);
		return -1;
	}

	for (size_t i = 0; i < num; i++)
	{
		if (ws_add_file(ws, paths[i]) == -1)
		{
			return -1;
		}
	}
	return 0;
}


int ws_add_dir(workspace *const ws, const char *const path)
{
	if (!ws_is_correct(ws) || path == NULL || access(path, F_OK) == -1)
	{
		ws_add_error(ws);
		return -1;
	}

	/*DIR* dir = opendir(path);
	if (!dir)
	{
		ws_add_error(ws);
		return -1;
	}
	closedir(dir);*/

	strcpy(ws->dirs[ws->dirs_num++], path);
	return 0;
}

int ws_add_dirs(workspace *const ws, const char *const *const paths, const size_t num)
{
	if (paths == NULL)
	{<commit_msg>Реализована функция приведения пути к единому формату<commit_after>#include "parser.h"
#include <stdint.h>
#include <string.h>

#ifndef _MSC_VER
	#include <unistd.h>
#else
	#define F_OK 0

	extern int access(const char *path, int mode);
#endif


const char *const default_output = "export.txt";


void ws_init(workspace *const ws)
{
	ws->files_num = 0;
	ws->dirs_num = 0;
	ws->flags_num = 0;

	strcpy(ws->output, default_output);
	ws->was_error = 0;
}

void ws_add_error(workspace *const ws)
{
	if (ws == NULL)
	{
		return;
	}

	ws->was_error = 1;
}

workspace ws_parse_args(const int argc, const char *const *const argv)
{
	workspace ws;
	ws_init(&ws);

	if (argv == NULL)
	{
		ws_add_error(&ws);
		return ws;
	}

	for (int i = 1; i < argc; i++)
	{
		if (argv[i] == NULL)
		{
			ws_add_error(&ws);
			return ws;
		}

		if (argv[i][0] != '-')
		{
			if (ws_add_file(&ws, argv[i]) == -1)
			{
				return ws;
			}
			continue;
		}

		if (argv[i][1] == 'o' && argv[i][2] == '\0')
		{
			if (i + 1 < argc || ws_set_output(&ws, argv[++i]) == -1)
			{
				ws_add_error(&ws);
				return ws;
			}
			continue;
		}

		if (ws_add_flag(&ws, argv[i]) == -1)
		{
			return ws;
		}
	}

	return ws;
}


workspace ws_create()
{
	workspace ws;
	ws_init(&ws);
	return ws;
}


void ws_unix_path(const char *const path, char *const buffer)
{
	size_t i = 0;
	size_t j = 0;
	size_t last = 0;

	while (path[i] != '\0')
	{
		buffer[j++] = path[i] == '\\' ? '/' : path[i];

		if (buffer[j - 1] == '/')
		{
			if (j - last == 2 && buffer[last] == '.')
			{
				j = last;
			}
			else if (last != 0 && j - last == 3 && buffer[last] == '.' && buffer[last + 1] == '.'
				&& !(last > 3 && buffer[last - 2] == '.' && buffer[last - 3] == '.' && buffer[last - 4] == '/'
					|| last == 3 && buffer[last - 2] == '.' && buffer[last - 3] == '.'))
			{
				j = last - 1;
				while (j > 0 && buffer[j - 1] != '/')
				{
					j--;
				}
			}
			
			last = j;
		}

		i++;
	}

	buffer[j] = '\0';
}

int ws_add_file(workspace *const ws, const char *const path)
{
	if (!ws_is_correct(ws) || path == NULL)
	{
		ws_add_error(ws);
		return -1;
	}

	ws_unix_path(path, ws->files[ws->files_num]);
	if (access(ws->files[ws->files_num], F_OK) == -1)
	{
		ws_add_error(ws);
		return -1;
	}

	ws->files_num++;
	return 0;
}

int ws_add_files(workspace *const ws, const char *const *const paths, const size_t num)
{
	if (paths == NULL)
	{
		ws_add_error(ws);
		return -1;
	}

	for (size_t i = 0; i < num; i++)
	{
		if (ws_add_file(ws, paths[i]) == -1)
		{
			return -1;
		}
	}
	return 0;
}


int ws_add_dir(workspace *const ws, const char *const path)
{
	if (!ws_is_correct(ws) || path == NULL)
	{
		ws_add_error(ws);
		return -1;
	}

	ws_unix_path(path, ws->dirs[ws->dirs_num]);
	if (access(ws->dirs[ws->dirs_num], F_OK) == -1)
	{
		ws_add_error(ws);
		return -1;
	}
	
	ws->dirs_num++;
	return 0;

}

int ws_add_dirs(workspace *const ws, const char *const *const paths, const size_t num)
{
	if (paths == NULL)
	{
{'fraction_non_alphanumeric': 0.19450399532254922, 'fraction_numerical': 0.01461703371662444, 'mean_word_length': 2.9416282642089095, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 38, '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': '70168', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2288, 'n_words': 526}