finderrors.c
branchtrunk
changeset 256 8339b461ef26
parent 254 13184ff204b9
child 257 ac8da90450f0
equal deleted inserted replaced
255:796394130035 256:8339b461ef26
    10 #include <stdio.h>
    10 #include <stdio.h>
    11 #include <stdlib.h>
    11 #include <stdlib.h>
    12 #include <string.h>
    12 #include <string.h>
    13 #include <stdarg.h>
    13 #include <stdarg.h>
    14 #include <sys/types.h>
    14 #include <sys/types.h>
    15 #include <dirent.h>
       
    16 #include <errno.h>
    15 #include <errno.h>
    17 #include <unistd.h>
    16 #include <unistd.h>
    18 
    17 
    19 #include "mojoshader.h"
    18 #include "mojoshader.h"
    20 
    19 
    23 #if FINDERRORS_COMPILE_SHADERS
    22 #if FINDERRORS_COMPILE_SHADERS
    24 #include "SDL.h"
    23 #include "SDL.h"
    25 #endif
    24 #endif
    26 
    25 
    27 #ifdef _MSC_VER
    26 #ifdef _MSC_VER
       
    27 #define WIN32_LEAN_AND_MEAN 1
       
    28 #include <windows.h>
    28 #define snprintf _snprintf
    29 #define snprintf _snprintf
       
    30 #else
       
    31 #include <dirent.h>
    29 #endif
    32 #endif
    30 
    33 
    31 #define report printf
    34 #define report printf
    32 
    35 
       
    36 static int do_file(const char *profile, const char *dname, const char *fn, int *total)
       
    37 {
       
    38     int do_quit = 0;
       
    39 
       
    40     #if FINDERRORS_COMPILE_SHADERS
       
    41     SDL_Event e;  // pump event queue to keep OS happy.
       
    42     while (SDL_PollEvent(&e))
       
    43     {
       
    44         if (e.type == SDL_QUIT)
       
    45             do_quit = 1;
       
    46     } // while
       
    47     SDL_GL_SwapBuffers();
       
    48     #endif
       
    49 
       
    50     if (do_quit)
       
    51     {
       
    52         report("FAIL: user requested quit!\n");
       
    53         return 0;
       
    54     } // if
       
    55 
       
    56     if (strstr(fn, ".bytecode") == NULL)
       
    57         return 1;
       
    58 
       
    59     (*total)++;
       
    60 
       
    61     char *fname = (char *) alloca(strlen(fn) + strlen(dname) + 1);
       
    62     sprintf(fname, "%s/%s", dname, fn);
       
    63     FILE *io = fopen(fname, "rb");
       
    64     if (io == NULL)
       
    65     {
       
    66         report("FAIL: %s fopen() failed.\n", fname);
       
    67         return 1;
       
    68     } // if
       
    69 
       
    70     static unsigned char buf[1024 * 256];
       
    71     int rc = fread(buf, 1, sizeof (buf), io);
       
    72     fclose(io);
       
    73     if (rc == -1)
       
    74     {
       
    75         report("FAIL: %s %s\n", fname, strerror(errno));
       
    76         return 1;
       
    77     } // if
       
    78 
       
    79     #if FINDERRORS_COMPILE_SHADERS
       
    80     MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc);
       
    81     if (shader == NULL)
       
    82         report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
       
    83     else
       
    84         report("PASS: %s\n", fname);
       
    85     MOJOSHADER_glDeleteShader(shader);
       
    86     #else
       
    87     const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
       
    88     if (pd->error != NULL)
       
    89         report("FAIL: %s %s\n", fname, pd->error);
       
    90     else
       
    91         report("PASS: %s\n", fname);
       
    92     MOJOSHADER_freeParseData(pd);
       
    93     #endif
       
    94 
       
    95     return 1;
       
    96 } // do_file
       
    97 
       
    98 
    33 static int do_dir(const char *dname, const char *profile)
    99 static int do_dir(const char *dname, const char *profile)
    34 {
   100 {
    35     const int dirlen = strlen(dname) + 1;
       
    36     int total = 0;
   101     int total = 0;
       
   102 
       
   103 #ifdef _MSC_VER
       
   104     WIN32_FIND_DATA dent;
       
   105     HANDLE dirp = INVALID_HANDLE_VALUE;
       
   106     FindFirstFileA(wSearchPath, &entw);
       
   107     if (dirp != INVALID_HANDLE_VALUE)
       
   108     {
       
   109         do
       
   110         {
       
   111             if (!do_file(profile, dname, dent.cFileName, &total))
       
   112                 break;
       
   113         } while (pFindNextFileA(dirp, &dent) != 0);
       
   114         CloseHandle(dirp);
       
   115     } // if
       
   116 #else
       
   117     struct dirent *dent = NULL;
    37     DIR *dirp = opendir(dname);
   118     DIR *dirp = opendir(dname);
    38     if (dirp != NULL)
   119     if (dirp != NULL)
    39     {
   120     {
    40         int do_quit = 0;
       
    41         struct dirent *dent;
       
    42         while ((dent = readdir(dirp)) != NULL)
   121         while ((dent = readdir(dirp)) != NULL)
    43         {
   122         {
    44             #if FINDERRORS_COMPILE_SHADERS
   123             if (!do_file(profile, dname, dent->d_name, &total))
    45             SDL_Event e;  // pump event queue to keep OS happy.
       
    46             while (SDL_PollEvent(&e))
       
    47             {
       
    48                 if (e.type == SDL_QUIT)
       
    49                     do_quit = 1;
       
    50             } // while
       
    51             SDL_GL_SwapBuffers();
       
    52             #endif
       
    53 
       
    54             if (do_quit)
       
    55             {
       
    56                 report("FAIL: user requested quit!\n");
       
    57                 break;
   124                 break;
    58             } // if
       
    59 
       
    60             if (strstr(dent->d_name, ".bytecode") == NULL)
       
    61                 continue;
       
    62 
       
    63             total++;
       
    64 
       
    65             char *fname = (char *) alloca(strlen(dent->d_name) + dirlen);
       
    66             sprintf(fname, "%s/%s", dname, dent->d_name);
       
    67             FILE *io = fopen(fname, "rb");
       
    68             if (io == NULL)
       
    69             {
       
    70                 report("FAIL: %s fopen() failed.\n", fname);
       
    71                 continue;
       
    72             } // if
       
    73 
       
    74             static unsigned char buf[1024 * 256];
       
    75             int rc = fread(buf, 1, sizeof (buf), io);
       
    76             fclose(io);
       
    77             if (rc == -1)
       
    78             {
       
    79                 report("FAIL: %s %s\n", fname, strerror(errno));
       
    80                 continue;
       
    81             } // if
       
    82 
       
    83             #if FINDERRORS_COMPILE_SHADERS
       
    84             MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc);
       
    85             if (shader == NULL)
       
    86                 report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
       
    87             else
       
    88                 report("PASS: %s\n", fname);
       
    89             MOJOSHADER_glDeleteShader(shader);
       
    90             #else
       
    91             const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
       
    92             if (pd->error != NULL)
       
    93                 report("FAIL: %s %s\n", fname, pd->error);
       
    94             else
       
    95                 report("PASS: %s\n", fname);
       
    96             MOJOSHADER_freeParseData(pd);
       
    97             #endif
       
    98         } // while
   125         } // while
    99         closedir(dirp);
   126         closedir(dirp);
   100     } // if
   127     } // if
       
   128 #endif
   101 
   129 
   102     return total;
   130     return total;
   103 } // do_dir
   131 } // do_dir
   104 
   132 
   105 
   133