img-gallery

simple html generator for images and videos
git clone git://git.jakekoroman.com/img-gallery
Log | Files | Refs | README

main.c (2425B)


      1 #include <assert.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #include <dirent.h>
      6 #include <sys/types.h>
      7 
      8 #define OUTPUT_FNAME "index.html"
      9 #define OUTPUT_FNAME_LEN 11
     10 
     11 #define ELEMENT_STYLE "width='320' height='240'"
     12 
     13 typedef enum {
     14     EXT_JPG,
     15     EXT_JPEG,
     16     EXT_PNG,
     17     EXT_WEBP,
     18     EXT_MP4,
     19     EXT_MPV,
     20     EXT_MOV,
     21     EXT_UNKNOWN,
     22 } FileExt;
     23 
     24 static FileExt
     25 fileext(char *fname)
     26 {
     27     assert(fname);
     28     FileExt result = EXT_UNKNOWN;
     29 
     30     int d = 0;
     31     int n = strnlen(fname, 255);
     32     for (int i = 0; i < n; ++i) {
     33         if (fname[i] == '.')
     34             d = i;
     35     }
     36 
     37     char *ext = &fname[d];
     38 
     39     if (!strncmp(ext, ".jpg", 255))
     40         result = EXT_JPG;
     41     else if (!strncmp(ext, ".jpeg", 255))
     42         result = EXT_JPEG;
     43     else if (!strncmp(ext, ".png", 255))
     44         result = EXT_PNG;
     45     else if (!strncmp(ext, ".webp", 255))
     46         result = EXT_WEBP;
     47     else if (!strncmp(ext, ".mp4", 255))
     48         result = EXT_MP4;
     49     else if (!strncmp(ext, ".mpv", 255))
     50         result = EXT_MPV;
     51     else if (!strncmp(ext, ".mov", 255))
     52         result = EXT_MOV;
     53 
     54     return result;
     55 }
     56 
     57 int
     58 main(int argc, char **argv)
     59 {
     60     DIR *dir = opendir(argc > 1 ? argv[1] : "./");
     61     struct dirent *db = NULL;
     62 
     63     FILE *fp = fopen(OUTPUT_FNAME, "w");
     64     assert(fp);
     65 
     66     fprintf(fp, "<head>\n\t<title>DOCTORS HATE ME FOR THIS ONE SIMPLE TRICK!!!</title>\n</head>\n");
     67 
     68     fprintf(fp, "<body>\n");
     69     while ((db = readdir(dir)) != NULL) {
     70         /* NOTE(jake): this may not work on all filesystems ... may have to call lstat */
     71         if (db->d_type == DT_DIR)
     72             continue;
     73         if (!strncmp(db->d_name, OUTPUT_FNAME, OUTPUT_FNAME_LEN))
     74             continue;
     75 
     76         switch (fileext(db->d_name)) {
     77         case EXT_JPG:
     78         case EXT_JPEG:
     79         case EXT_PNG:
     80         case EXT_WEBP:
     81             fprintf(fp, "\t<img "ELEMENT_STYLE" src='%s'/>\n", db->d_name);
     82             break;
     83 
     84         case EXT_MP4:
     85         case EXT_MPV:
     86         case EXT_MOV:
     87             fprintf(fp, "\t<video "ELEMENT_STYLE" controls>\n");
     88             fprintf(fp, "\t\t<source src='%s'>\n", db->d_name);
     89             fprintf(fp, "\t</video>\n");
     90             break;
     91 
     92         case EXT_UNKNOWN:
     93             fprintf(stdout, "[INFO]: skipping unknown file extenstion from file %s\n", db->d_name);
     94             break;
     95         }
     96     }
     97     fprintf(fp, "</body>\n");
     98 
     99     fprintf(stdout, "created %s\n", OUTPUT_FNAME);
    100 
    101     return 0;
    102 }