src/lib/image.c
changeset 2 05de54150a4c
parent 1 f3cde3db1fef
child 3 da7c6dcafb43
equal deleted inserted replaced
1:f3cde3db1fef 2:05de54150a4c
    37 static int pt_image_open_file (struct pt_image *img, FILE **file_ptr)
    37 static int pt_image_open_file (struct pt_image *img, FILE **file_ptr)
    38 {
    38 {
    39     FILE *fp;
    39     FILE *fp;
    40     
    40     
    41     // open
    41     // open
    42     if (fopen(img->path, "rb") < 0)
    42     if ((fp = fopen(img->path, "rb")) < 0)
    43         return -1;
    43         return -1;
    44 
    44 
    45     // ok
    45     // ok
    46     *file_ptr = fp;
    46     *file_ptr = fp;
    47 
    47 
   135  * Build a filesystem path representing the appropriate path for this image's cache entry, and store it in the given
   135  * Build a filesystem path representing the appropriate path for this image's cache entry, and store it in the given
   136  * buffer.
   136  * buffer.
   137  */
   137  */
   138 static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len)
   138 static int pt_image_cache_path (struct pt_image *image, char *buf, size_t len)
   139 {
   139 {
   140     // TODO: impl
   140     char *ext;
       
   141 
       
   142     // XXX: be more careful about buf len
       
   143     
       
   144     // copy filename
       
   145     strncpy(buf, image->path, len);
       
   146 
       
   147     // find .ext
       
   148     if ((ext = strrchr(buf, '.')) == NULL)
       
   149         return -1;
       
   150 
       
   151     // change to .cache
       
   152     strncpy(ext, ".cache", (buf + len) - ext);
       
   153 
       
   154     // hmmk
       
   155     return 0;
   141 }
   156 }
   142 
   157 
   143 int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode)
   158 int pt_image_open (struct pt_image **image_ptr, struct pt_ctx *ctx, const char *path, int cache_mode)
   144 {
   159 {
   145     struct pt_image *image;
   160     struct pt_image *image;
   146     char cache_path[_POSIX_PATH_MAX];
   161     char cache_path[_POSIX_PATH_MAX];
       
   162     int stale;
   147 
   163 
   148     // XXX: verify that the path exists and looks like a PNG file
   164     // XXX: verify that the path exists and looks like a PNG file
   149 
   165 
   150     // alloc
   166     // alloc
   151     if (pt_image_new(&image, ctx, path))
   167     if (pt_image_new(&image, ctx, path))
   159     if (pt_cache_open(&image->cache, cache_path, cache_mode))
   175     if (pt_cache_open(&image->cache, cache_path, cache_mode))
   160         goto error;
   176         goto error;
   161     
   177     
   162     // update if not fresh
   178     // update if not fresh
   163     // XXX: check cache_mode
   179     // XXX: check cache_mode
   164     // XXX: error handling
   180     if ((stale = pt_cache_stale(image->cache, image->path)) < 0)
   165     if (pt_cache_stale(image->cache, image->path))
   181         goto error;
       
   182 
       
   183     if (stale)
   166         pt_image_update_cache(image);
   184         pt_image_update_cache(image);
   167     
   185     
   168     // ok, ready for access
   186     // ok, ready for access
   169     *image_ptr = image;
   187     *image_ptr = image;
   170 
   188