up to a test client
authorTero Marttila <terom@fixme.fi>
Mon, 28 Dec 2009 19:54:41 +0200
changeset 2 05de54150a4c
parent 1 f3cde3db1fef
child 3 da7c6dcafb43
up to a test client
Makefile
src/lib/cache.c
src/lib/cache.h
src/lib/image.c
src/util/main.c
--- a/Makefile	Sun Dec 27 23:14:10 2009 +0200
+++ b/Makefile	Mon Dec 28 19:54:41 2009 +0200
@@ -13,11 +13,15 @@
 DIST_NAME = 78949E-as2
 DIST_RESOURCES = README "Learning Diary.pdf"
 
-all: depend lib/libpngtile.so
+all: depend lib/libpngtile.so bin/util
 
 lib/libpngtile.so : \
 	build/obj/lib/image.o build/obj/lib/cache.o
 
+bin/util: \
+	lib/libpngtile.so \
+	build/obj/shared/log.o
+
 SRC_PATHS = $(wildcard src/*/*.c)
 SRC_NAMES = $(patsubst src/%,%,$(SRC_PATHS))
 SRC_DIRS = $(dir $(SRC_NAMES))
--- a/src/lib/cache.c	Sun Dec 27 23:14:10 2009 +0200
+++ b/src/lib/cache.c	Mon Dec 28 19:54:41 2009 +0200
@@ -6,6 +6,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/mman.h>
+#include <errno.h>
 
 
 
@@ -50,10 +51,25 @@
     return 0;
 }
 
-int pt_cache_stale (struct pt_cache *cache, const char *orig_path)
+int pt_cache_stale (struct pt_cache *cache, const char *img_path)
 {
-    // TODO: stat + mtime
-    return false;
+    struct stat st_img, st_cache;
+    
+    // test original file
+    if (stat(img_path, &st_img) < 0)
+        return -1;
+    
+    // test cache file
+    if (stat(cache->path, &st_cache) < 0) {
+        // always stale if it doesn't exist yet
+        if (errno == ENOENT)
+            return 1;
+        else
+            return -1;
+    }
+
+    // compare mtime
+    return (st_img.st_mtime > st_cache.st_mtime);
 }
 
 /**
@@ -161,6 +177,12 @@
  */
 static int pt_cache_open_create (struct pt_cache *cache, struct pt_cache_header *header)
 {
+    // no access
+    if (!(cache->mode & PT_IMG_WRITE)) {
+        errno = EPERM;
+        return -1;
+    }
+
     // open
     if (pt_cache_open_fd(cache, &cache->fd))
         return -1;
--- a/src/lib/cache.h	Sun Dec 27 23:14:10 2009 +0200
+++ b/src/lib/cache.h	Mon Dec 28 19:54:41 2009 +0200
@@ -60,7 +60,7 @@
 /**
  * Verify if the cached data has become stale compared to the given original file.
  */
-int pt_cache_stale (struct pt_cache *cache, const char *orig_path);
+int pt_cache_stale (struct pt_cache *cache, const char *img_path);
 
 /**
  * Update the cache data from the given PNG image.
--- a/src/lib/image.c	Sun Dec 27 23:14:10 2009 +0200
+++ b/src/lib/image.c	Mon Dec 28 19:54:41 2009 +0200
@@ -39,7 +39,7 @@
     FILE *fp;
     
     // open
-    if (fopen(img->path, "rb") < 0)
+    if ((fp = fopen(img->path, "rb")) < 0)
         return -1;
 
     // ok
@@ -137,13 +137,29 @@
  */
 static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len)
 {
-    // TODO: impl
+    char *ext;
+
+    // XXX: be more careful about buf len
+    
+    // copy filename
+    strncpy(buf, image->path, len);
+
+    // find .ext
+    if ((ext = strrchr(buf, '.')) == NULL)
+        return -1;
+
+    // change to .cache
+    strncpy(ext, ".cache", (buf + len) - ext);
+
+    // hmmk
+    return 0;
 }
 
 int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode)
 {
     struct pt_image *image;
     char cache_path[_POSIX_PATH_MAX];
+    int stale;
 
     // XXX: verify that the path exists and looks like a PNG file
 
@@ -161,8 +177,10 @@
     
     // update if not fresh
     // XXX: check cache_mode
-    // XXX: error handling
-    if (pt_cache_stale(image->cache, image->path))
+    if ((stale = pt_cache_stale(image->cache, image->path)) < 0)
+        goto error;
+
+    if (stale)
         pt_image_update_cache(image);
     
     // ok, ready for access
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/util/main.c	Mon Dec 28 19:54:41 2009 +0200
@@ -0,0 +1,104 @@
+#include "lib/pngtile.h"
+#include "shared/log.h"
+
+#include <getopt.h>
+#include <stdio.h>
+#include <stdbool.h>
+
+/**
+ * Command-line options
+ */
+static const struct option options[] = {
+    { "help",       false,  NULL,   'h' },
+    { "quiet",      false,  NULL,   'q' },
+    { "verbose",    false,  NULL,   'v' },
+    { "debug",      false,  NULL,   'D' },
+    { 0,            0,      0,      0   }
+};
+
+/**
+ * Print usage/help info on stderr
+ */
+void help (const char *argv0)
+{
+    fprintf(stderr, "Usage: %s [options] <image> [...]\n", argv0);
+    fprintf(stderr,
+        "XXX: Process some image files.\n"
+        "\n"
+        "\t-h, --help           show this help and exit\n"
+        "\t-q, --quiet          supress informational output\n"
+        "\t-v, --verbose        display more informational output\n"
+        "\t-D, --debug          equivalent to -v\n"
+    );
+}
+
+int main (int argc, char **argv)
+{
+    int opt;
+    
+    // parse arguments
+    while ((opt = getopt_long(argc, argv, "hqvD", options, NULL)) != -1) {
+        switch (opt) {
+            case 'h':
+                // display help
+                help(argv[0]);
+                
+                return EXIT_SUCCESS;
+
+            case 'q':
+                // supress excess log output
+                set_log_level(LOG_WARN);
+
+                break;
+
+            case 'v':
+            case 'D':
+                // display additional output
+                set_log_level(LOG_DEBUG);
+
+                break;
+
+            case '?':
+                // useage error
+                help(argv[0]);
+
+                return EXIT_FAILURE;
+
+            default:
+                // getopt???
+                FATAL("getopt_long returned unknown code %d", opt);
+        }
+    }
+    
+    // end-of-arguments?
+    if (!argv[optind])
+        EXIT_WARN(EXIT_FAILURE, "No images given");
+    
+
+
+    struct pt_ctx *ctx = NULL;
+    struct pt_image *image = NULL;;
+
+    log_debug("Processing %d images...", argc - optind);
+
+    for (int i = optind; i < argc; i++) {
+        const char *img_path = argv[i];
+
+        log_debug("Load image from: %s", img_path);
+
+        // open
+        if (pt_image_open(&image, ctx, img_path, PT_IMG_READ | PT_IMG_WRITE)) {
+            log_errno("pt_image_open: %s", img_path);
+
+            continue;
+
+        } else {
+            log_info("Opened image at: %s", img_path);
+
+        }
+    }
+
+    // XXX: done
+    return 0;
+}
+