All pastes #406166 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-03-22 04:56 UTC
rendered paste body
#include <mowgli.h>

mowgli_error_context_t *mec;

void process_data(const char *filename)
{
	mowgli_error_context_push(mec, "While processing file '%s'", filename);
	char buf[65535];
	FILE *f;
	int line_number = 0;

	if (filename == NULL)
		mowgli_error_context_display_with_error(mec, ":\n   * ", "no filename provided");
	else
	{
		mowgli_error_context_push(mec, "While reading file '%s'", filename);

		if ((f = fopen(filename, "rb")) == NULL)
			mowgli_error_context_display_with_error(mec, ":\n   * ", strerror(errno));

		mowgli_error_context_pop(mec);
	}

	while(fgets(buf, 65535, f) != NULL)
	{
		line_number++;

		mowgli_error_context_push(mec, "While parsing line %d of file '%s'",
			line_number, filename);

		if (strncasecmp(buf, "#e ", 3) == 0)
			mowgli_error_context_display_with_error(mec, ":\n   * ", buf + 3);

		if (strncasecmp(buf, "#w ", 3) == 0)
		{
			mowgli_error_context_display(mec, ":\n   * ");
			printf("Warning: %s\n", buf + 3);

			mowgli_error_context_pop(mec);
			continue;
		}

		if (strncasecmp(buf, "#i ", 3) == 0)
		{
			buf[strlen(buf) - 1] = '\0';

			mowgli_error_context_push(mec, "While including file '%s' from line %d of file '%s'",
				buf + 3, line_number, filename);

			process_data(buf + 3);

			mowgli_error_context_pop(mec);
			mowgli_error_context_pop(mec);

			continue;
		}

		if (strncasecmp(buf, "#", 1) == 0)
			mowgli_error_context_display_with_error(mec, ":\n   * ", "Unknown directive.");

		mowgli_error_context_pop(mec);
		printf("%s", buf);
	}

	mowgli_error_context_pop(mec);
}

int main(int argc, char *argv[])
{
	mowgli_init();

	mec = mowgli_error_context_create();
	mowgli_error_context_push(mec, "In program %s", argv[0]);

	process_data(argv[1]);

	mowgli_error_context_destroy(mec);

	return 0;
}