c-md/srcs/map/io.c.md
lohhiiccc 80f7a1b9b6 feat(build): add build instructions in README and convert sources to .c.md format
- Add detailed build and bootstrap instructions to README.md.
 - Convert all source and header files from .c/.h to .c.md/.h.md.
 - Add bootstrap.sh script for automated building across version history.
 - Update Makefile and sources.mk to reflect new markdown-based source organization.
2026-01-12 14:54:49 +01:00

948 B

#include <stdio.h>

#include "map.h"
#include "internal/map_internal.h"

int8_t
write_header(FILE *f, const char *source, const char *target)
{
	if (0 > fprintf(f, "C-MD MAP v1\n"))
		return (1);
	if (0 > fprintf(f, "source: %s\n", source))
		return (1);
	if (0 > fprintf(f, "target: %s\n", target))
		return (1);
	if (0 > fprintf(f, "---\n"))
		return (1);
	return (0);
}

int8_t
write_ranges(FILE *f, t_map *map)
{
	size_t		i;
	t_range		*r;

	i = 0;
	while (i < map->count)
	{
		r = &map->ranges[i];
		if (0 > fprintf(f, "%u-%u:%u-%u\n",
			r->src_start, r->src_end, r->dst_start, r->dst_end))
			return (1);
		i++;
	}
	return (0);
}

int8_t
map_write(t_map *map, const char *path, const char *source, const char *target)
{
	FILE	*f;
	int8_t	ret;

	f = fopen(path, "w");
	if (NULL == f)
	{
		perror(path);
		return (1);
	}
	ret = write_header(f, source, target);
	if (0 == ret)
		ret = write_ranges(f, map);
	fclose(f);
	return (ret);
}