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

/* K&R C (<NAME> & <NAME>)
 Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to
 handle quoted strings and character constants properly. C comments don't nest.
*/

int FileExists(const char* path) 
{
	if (!path) {
		return 0;
	}
	FILE* file = fopen(path, "r");
	if (!file) {
		return 0;
	}
	return 1;
}

int main(int argc, const char** argv) {

	if (argc < 3) {
		fprintf(stderr, "Usage: ./removeComments source destination.\n");
		return 1;
	}

	FILE* from = fopen(argv[1], "r");
	if (!from) {
		fprintf(stderr, "Error opening file \"%s\"", argv[1]);
		return 2;
	}

	if (FileExists(argv[2])) {
		fprintf(stderr, "Destination file with the same name already exists in the directory.\n");
		return 3;
	}

	FILE* to = fopen(argv[2], "w");
	if (!to) {
		fprintf(stderr, "Error creating a destination file.\n");
	}

	// parsing logic wip

	fclose(to);
	fclose(from);	

	return 0;
}<commit_msg>added long line parsing with dynamic buffer<commit_after>#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

/* K&R C (<NAME> & <NAME>)
 Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to
 handle quoted strings and character constants properly. C comments don't nest.
*/

#define BUFFER_INIT_CAPACITY 128

int FileExists(const char* path) 
{
	if (!path) {
		return 0;
	}
	FILE* file = fopen(path, "r");
	if (!file) {
		return 0;
	}
	return 1;
}

int main(int argc, const char** argv) {

	if (argc < 3) {
		fprintf(stderr, "Usage: ./removeComments source destination.\n");
		return 1;
	}

	FILE* from = fopen(argv[1], "r");
	if (!from) {
		fprintf(stderr, "Error opening file \"%s\"", argv[1]);
		return 2;
	}

	if (FileExists(argv[2])) {
		fprintf(stderr, "Destination file with the same name already exists in the directory.\n");
		return 3;
	}

	FILE* to = fopen(argv[2], "w");
	if (!to) {
		fprintf(stderr, "Error creating a destination file.\n");
	}
	
	const char* COMMENT_START_TAG = "/*";
	const char* COMMENT_END_TAG = "*/";

	const size_t COMMENT_START_TAG_LEN = strlen(COMMENT_START_TAG);
	const size_t COMMENT_END_TAG_LEN = strlen(COMMENT_END_TAG);

	size_t bufferCapacity = BUFFER_INIT_CAPACITY;
	char* buffer = (char*)malloc(sizeof(char) * BUFFER_INIT_CAPACITY);
	if (!buffer) {
		errno = ENOMEM;
		return 1;
	}

	size_t bufferCount = 0;
	int isEOF = 0;
	int inComment = 0;

	char c = 0;

	while (!isEOF)
	{
		bufferCount = 0;

		while (1) {		
			c = fgetc(from);
			if (c == EOF) {
				isEOF = 1;
				break;
			}
			if (bufferCount >= bufferCapacity - 1) 
			{
				// a safeguard in case someone accidentally changes bufferCapacity to 0
				bufferCapacity = (bufferCapacity > 0) ? bufferCapacity * 2 : BUFFER_INIT_CAPACITY;
				buffer = (char*)realloc(buffer, bufferCapacity);
				if (!buffer) {
					errno = ENOMEM;
					return 1;
				}
			}
			buffer[bufferCount] = c;
			++bufferCount;
			
			if (c == '\n') break;
		}
		buffer[bufferCount] = 0;
		
		// parsing logic here
	}

	free(buffer);
	buffer = NULL;

	return 0;
}
{'fraction_non_alphanumeric': 0.14846470402025957, 'fraction_numerical': 0.014878125989237101, 'mean_word_length': 3.463276836158192, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '4726158', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1147, 'n_words': 349}