Added SILC Thread Queue API
[crypto.git] / apps / silcmap / silcmap_bitmap.c
index dd1c7a6a0e641d03ad1919eac5d11a2fefe0d6dd..6440f88ddcc8e6208d3263f2ad5ce07428cca756 100644 (file)
@@ -4,7 +4,7 @@
 
   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, "%s %ld %ld %ld\n", type,
+              (unsigned long *)&map->width,
+              (unsigned long *)&map->height,
+              (unsigned long *)&map->maxcolor);
   if (ret < 4) {
-    fprintf(stderr, "fscanf: %s\n", strerror(errno));
+    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, "fscanf: %s\n", strerror(errno));
+    fprintf(stderr, "read: %s\n", strerror(errno));
     retval = FALSE;
     goto out;
   }
 
  out:
-  fclose(fp);
+  close(fd);
   return retval;
 }
 
@@ -70,30 +86,30 @@ bool silc_map_load_ppm(SilcMap map, const char *filename)
 
 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;
 }
 
@@ -111,11 +127,13 @@ bool silc_map_cut(SilcMap map, SilcInt32 x, SilcInt32 y,
 
   /* 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;
   }
 
@@ -128,8 +146,8 @@ bool silc_map_cut(SilcMap map, SilcInt32 x, SilcInt32 y,
   (*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++) {
@@ -376,6 +394,11 @@ double silc_map_parse_pos(char *pos)
     return 0;
   }
 
+  if (d < 0) {
+    m = (m < 0 ? m : -m);
+    s = (s < 0 ? s : -s);
+  }
+
   return ((d < 0 ? -1 : d > 0 ? 1 : 0) *
          abs(d) + (m / 60) + (s / 3600));
 }
@@ -455,7 +478,7 @@ bool silc_map_load_font(SilcMap map, const char *filename)
   /* Load the file */
   fp = fopen(filename, "r");
   if (!fp) {
-    fprintf(stderr, "fopen: %s\n", strerror(errno));
+    fprintf(stderr, "fopen: %s: %s\n", strerror(errno), filename);
     return FALSE;
   }