Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2003 Pekka Riikonen
+ Copyright (C) 2003 - 2004 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
bool silc_map_load_ppm(SilcMap map, const char *filename)
{
- FILE *fp;
+ int fd;
char type[3];
- int ret, retval = TRUE;
+ unsigned char header[80];
+ int ret, retval = TRUE, i;
SILC_LOG_DEBUG(("Load PPM '%s'", filename));
- fp = fopen(filename, "r");
- if (!fp) {
- fprintf(stderr, "fopen: %s: %s\n", strerror(errno), filename);
+ fd = open(filename, O_RDONLY, 0600);
+ if (fd < 0) {
+ fprintf(stderr, "open: %s: %s\n", strerror(errno), filename);
+ return FALSE;
+ }
+
+ /* Read file header */
+ memset(header, 0, sizeof(header));
+ ret = read(fd, (void *)header, sizeof(header) - 1);
+ if (ret < 0) {
+ fprintf(stderr, "read: %s: %s\n", strerror(errno), filename);
return FALSE;
}
/* Read width and height */
- ret = fscanf(fp, "%s %ld %ld %ld ",
- type, &map->width, &map->height, &map->maxcolor);
+ ret = sscanf(header, "%2s %ld %ld %ld\n", type,
+ (unsigned long *)&map->width,
+ (unsigned long *)&map->height,
+ (unsigned long *)&map->maxcolor);
if (ret < 4) {
fprintf(stderr, "Invalid PPM file");
retval = FALSE;
goto out;
}
+ for (i = sizeof(header) - 1; i >= 0; i--)
+ if (header[i] == '\n' || header[i] == ' ')
+ break;
+ lseek(fd, i + 1, SEEK_SET);
+
/* Read the picture */
- map->bitsilc_map_size = map->width * 3 * map->height;
- map->bitmap = silc_malloc(map->bitsilc_map_size);
- ret = fread(map->bitmap, map->bitsilc_map_size, 1, fp);
+ map->bitmap_size = map->width * 3 * map->height;
+ map->bitmap = silc_malloc(map->bitmap_size);
+ ret = read(fd, map->bitmap, map->bitmap_size);
if (ret < 0) {
- fprintf(stderr, "fread: %s\n", strerror(errno));
+ fprintf(stderr, "read: %s\n", strerror(errno));
retval = FALSE;
goto out;
}
out:
- fclose(fp);
+ close(fd);
return retval;
}
bool silc_map_write_ppm(SilcMap map, const char *filename)
{
- FILE *fp;
+ int fd;
int retval = TRUE;
- int i, k;
+ char header[80];
SILC_LOG_DEBUG(("Write PPM '%s'", filename));
- fp = fopen(filename, "w+");
- if (!fp) {
- fprintf(stderr, "fopen: %s: %s\n", strerror(errno), filename);
+ fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0600);
+ if (fd < 0) {
+ fprintf(stderr, "open: %s: %s\n", strerror(errno), filename);
return FALSE;
}
/* Write the header */
- fprintf(fp, "P6 %ld %ld %ld\n", map->width, map->height, map->maxcolor);
+ memset(header, 0, sizeof(header));
+ snprintf(header, sizeof(header) - 1, "P6 %ld %ld %ld\n",
+ (unsigned long)map->width,
+ (unsigned long)map->height,
+ (unsigned long)map->maxcolor);
+ write(fd, header, strlen(header));
/* Write the bitmap */
- for (i = 0; i < map->height; i++)
- for (k = 0; k < map->width; k++) {
- putc(map->bitmap[i * (map->width * 3) + (k * 3) ], fp); /* R */
- putc(map->bitmap[i * (map->width * 3) + (k * 3) + 1], fp); /* G */
- putc(map->bitmap[i * (map->width * 3) + (k * 3) + 2], fp); /* B */
- }
+ write(fd, map->bitmap, map->bitmap_size);
+ close(fd);
- fclose(fp);
return retval;
}
/* Sanity checks */
if (height > map->height - y) {
- fprintf(stderr, "Requesting too much height: %ld\n", height);
+ fprintf(stderr, "Requesting too much height: %ld\n",
+ (unsigned long)height);
return FALSE;
}
if (width > map->width - x) {
- fprintf(stderr, "Requesting too much width: %ld\n", width);
+ fprintf(stderr, "Requesting too much width: %ld\n",
+ (unsigned long)width);
return FALSE;
}
(*ret_map)->width = width;
(*ret_map)->height = height;
(*ret_map)->maxcolor = map->maxcolor;
- (*ret_map)->bitsilc_map_size = (width * 3) * height;
- (*ret_map)->bitmap = silc_malloc((*ret_map)->bitsilc_map_size);
+ (*ret_map)->bitmap_size = (width * 3) * height;
+ (*ret_map)->bitmap = silc_malloc((*ret_map)->bitmap_size);
/* Copy the requested area */
for (i = 0; i < height; i++) {