test/test_physfs.c
changeset 40 8cc16df4bada
parent 38 feb16343f44c
child 42 c9cab2b09437
equal deleted inserted replaced
39:bc29e1ee7ef6 40:8cc16df4bada
       
     1 /**
       
     2  * Test program for PhysicsFS. May only work on Unix.
       
     3  *
       
     4  * Please see the file LICENSE in the source's root directory.
       
     5  *
       
     6  *  This file written by Ryan C. Gordon.
       
     7  */
       
     8 
     1 #include <stdio.h>
     9 #include <stdio.h>
     2 #include <stdlib.h>
    10 #include <stdlib.h>
       
    11 #include <errno.h>
       
    12 #include <unistd.h>
     3 #include <readline.h>
    13 #include <readline.h>
     4 #include <history.h>
    14 #include <history.h>
     5 #include "physfs.h"
    15 #include "physfs.h"
     6 
    16 
     7 #define TEST_VERSION_MAJOR  0
    17 #define TEST_VERSION_MAJOR  0
     8 #define TEST_VERSION_MINOR  1
    18 #define TEST_VERSION_MINOR  1
     9 #define TEST_VERSION_PATCH  0
    19 #define TEST_VERSION_PATCH  0
       
    20 
       
    21 static FILE *history_file = NULL;
    10 
    22 
    11 static void output_versions(void)
    23 static void output_versions(void)
    12 {
    24 {
    13     PHYSFS_Version compiled;
    25     PHYSFS_Version compiled;
    14     PHYSFS_Version linked;
    26     PHYSFS_Version linked;
    45 
    57 
    46     printf("\n");
    58     printf("\n");
    47 } /* output_archivers */
    59 } /* output_archivers */
    48 
    60 
    49 
    61 
    50 static int cmd_help(char *cmdstr)
    62 static int cmd_quit(char *args)
    51 {
       
    52     printf("Commands:\n"
       
    53            "  quit - exit this program.\n"
       
    54            "  help - this information.\n");
       
    55     return(1);
       
    56 } /* output_cmd_help */
       
    57 
       
    58 
       
    59 static int cmd_quit(char *cmdstr)
       
    60 {
    63 {
    61     return(0);
    64     return(0);
    62 } /* cmd_quit */
    65 } /* cmd_quit */
    63 
    66 
    64 
    67 
       
    68 static int cmd_init(char *args)
       
    69 {
       
    70     if (PHYSFS_init(args))
       
    71         printf("Successful.\n");
       
    72     else
       
    73         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
    74 
       
    75     return(1);
       
    76 } /* cmd_init */
       
    77 
       
    78 
       
    79 static int cmd_deinit(char *args)
       
    80 {
       
    81     if (PHYSFS_deinit())
       
    82         printf("Successful.\n");
       
    83     else
       
    84         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
    85 
       
    86     return(1);
       
    87 } /* cmd_deinit */
       
    88 
       
    89 
       
    90 static int cmd_addarchive(char *args)
       
    91 {
       
    92     char *ptr = strchr(args, ' ');
       
    93     int appending = atoi(ptr + 1);
       
    94     *ptr = '\0';
       
    95 
       
    96     if (PHYSFS_addToSearchPath(args, appending))
       
    97         printf("Successful.\n");
       
    98     else
       
    99         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   100 
       
   101     return(1);
       
   102 } /* cmd_addarchive */
       
   103 
       
   104 
       
   105 static int cmd_removearchive(char *args)
       
   106 {
       
   107     if (PHYSFS_removeFromSearchPath(args))
       
   108         printf("Successful.\n");
       
   109     else
       
   110         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   111 
       
   112     return(1);
       
   113 } /* cmd_removearchive */
       
   114 
       
   115 
       
   116 static int cmd_enumerate(char *args)
       
   117 {
       
   118     char **rc = PHYSFS_enumerateFiles(args);
       
   119 
       
   120     if (rc == NULL)
       
   121         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   122     else
       
   123     {
       
   124         int file_count;
       
   125         char **i;
       
   126         for (i = rc, file_count = 0; *i != NULL; i++, file_count++)
       
   127             printf("%s\n", *i);
       
   128 
       
   129         printf("\n total (%d) files.\n", file_count);
       
   130         PHYSFS_freeList(rc);
       
   131     } /* else */
       
   132 
       
   133     return(1);
       
   134 } /* cmd_enumerate */
       
   135 
       
   136 
       
   137 static int cmd_getdirsep(char *args)
       
   138 {
       
   139     printf("Directory separator is [%s].\n", PHYSFS_getDirSeparator());
       
   140     return(1);
       
   141 } /* cmd_getdirsep */
       
   142 
       
   143 
       
   144 static int cmd_getlasterror(char *args)
       
   145 {
       
   146     printf("last error is [%s].\n", PHYSFS_getLastError());
       
   147     return(1);
       
   148 } /* cmd_getlasterror */
       
   149 
       
   150 
       
   151 static int cmd_getcdromdirs(char *args)
       
   152 {
       
   153     char **rc = PHYSFS_getCdRomDirs();
       
   154 
       
   155     if (rc == NULL)
       
   156         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   157     else
       
   158     {
       
   159         int dir_count;
       
   160         char **i;
       
   161         for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
       
   162             printf("%s\n", *i);
       
   163 
       
   164         printf("\n total (%d) drives.\n", dir_count);
       
   165         PHYSFS_freeList(rc);
       
   166     } /* else */
       
   167 
       
   168     return(1);
       
   169 } /* cmd_getcdromdirs */
       
   170 
       
   171 
       
   172 static int cmd_getsearchpath(char *args)
       
   173 {
       
   174     char **rc = PHYSFS_getSearchPath();
       
   175 
       
   176     if (rc == NULL)
       
   177         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   178     else
       
   179     {
       
   180         int dir_count;
       
   181         char **i;
       
   182         for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
       
   183             printf("%s\n", *i);
       
   184 
       
   185         printf("\n total (%d) directories.\n", dir_count);
       
   186         PHYSFS_freeList(rc);
       
   187     } /* else */
       
   188 
       
   189     return(1);
       
   190 } /* cmd_getcdromdirs */
       
   191 
       
   192 
       
   193 static int cmd_getbasedir(char *args)
       
   194 {
       
   195     printf("Base dir is [%s].\n", PHYSFS_getBaseDir());
       
   196     return(1);
       
   197 } /* cmd_getbasedir */
       
   198 
       
   199 
       
   200 static int cmd_getuserdir(char *args)
       
   201 {
       
   202     printf("User dir is [%s].\n", PHYSFS_getUserDir());
       
   203     return(1);
       
   204 } /* cmd_getuserdir */
       
   205 
       
   206 
       
   207 static int cmd_getwritedir(char *args)
       
   208 {
       
   209     printf("Write dir is [%s].\n", PHYSFS_getWriteDir());
       
   210     return(1);
       
   211 } /* cmd_getwritedir */
       
   212 
       
   213 
       
   214 static int cmd_setwritedir(char *args)
       
   215 {
       
   216     if (PHYSFS_setWriteDir(args))
       
   217         printf("Successful.\n");
       
   218     else
       
   219         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   220 
       
   221     return(1);
       
   222 } /* cmd_setwritedir */
       
   223 
       
   224 
       
   225 static int cmd_permitsyms(char *args)
       
   226 {
       
   227     int num = atoi(args);
       
   228     PHYSFS_permitSymbolicLinks(num);
       
   229     printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
       
   230     return(1);
       
   231 } /* cmd_permitsyms */
       
   232 
       
   233 
       
   234 static int cmd_setsaneconfig(char *args)
       
   235 {
       
   236     char *appName;
       
   237     char *arcExt;
       
   238     int inclCD;
       
   239     int arcsFirst;
       
   240     char *ptr = args;
       
   241 
       
   242         /* ugly. */
       
   243     appName = ptr;
       
   244     ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; arcExt = ptr;
       
   245     ptr = strchr(ptr, ' '); *ptr = '\0'; ptr++; inclCD = atoi(arcExt);
       
   246     arcsFirst = atoi(ptr);
       
   247 
       
   248     if (strcmp(appName, "!") == 0)
       
   249         appName = NULL;
       
   250 
       
   251     if (strcmp(arcExt, "!") == 0)
       
   252         arcExt = NULL;
       
   253 
       
   254     if (PHYSFS_setSaneConfig(appName, arcExt, inclCD, arcsFirst))
       
   255         printf("Successful.\n");
       
   256     else
       
   257         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
       
   258 
       
   259     return(1);
       
   260 } /* cmd_setsaneconfig */
       
   261 
       
   262 
       
   263 /* must have spaces trimmed prior to this call. */
       
   264 static int count_args(const char *str)
       
   265 {
       
   266     int retval = 0;
       
   267 
       
   268     if (str != NULL)
       
   269     {
       
   270         for (; *str != '\0'; str++)
       
   271         {
       
   272             if (*str == ' ')
       
   273                 retval++;
       
   274         } /* for */
       
   275         retval++;
       
   276     } /* if */
       
   277 
       
   278     return(retval);
       
   279 } /* count_args */
       
   280 
       
   281 
       
   282 static int cmd_help(char *args);
       
   283 
    65 typedef struct
   284 typedef struct
    66 {
   285 {
    67     const char *cmd;
   286     const char *cmd;
    68     int (*func)(char *cmdstr);
   287     int (*func)(char *args);
       
   288     int argcount;
       
   289     const char *usage;
    69 } command_info;
   290 } command_info;
    70 
   291 
    71 static command_info commands[] =
   292 static const command_info commands[] =
    72 {
   293 {
    73     {"quit", cmd_quit},
   294     { "quit",           cmd_quit,           0, NULL                         },
    74     {"q",    cmd_quit},
   295     { "q",              cmd_quit,           0, NULL                         },
    75     {"help", cmd_help},
   296     { "help",           cmd_help,           0, NULL                         },
    76     {NULL, NULL}
   297     { "init",           cmd_init,           1, "<argv0>"                    },
       
   298     { "deinit",         cmd_deinit,         0, NULL                         },
       
   299     { "addarchive",     cmd_addarchive,     2, "<archiveLocation> <append>" },
       
   300     { "removearchive",  cmd_removearchive,  1, "<archiveLocation>"          },
       
   301     { "enumerate",      cmd_enumerate,      1, "<dirToEnumerate>"           },
       
   302     { "getlasterror",   cmd_getlasterror,   0, NULL                         },
       
   303     { "getdirsep",      cmd_getdirsep,      0, NULL                         },
       
   304     { "getcdromdirs",   cmd_getcdromdirs,   0, NULL                         },
       
   305     { "getsearchpath",  cmd_getsearchpath,  0, NULL                         },
       
   306     { "getbasedir",     cmd_getbasedir,     0, NULL                         },
       
   307     { "getuserdir",     cmd_getuserdir,     0, NULL                         },
       
   308     { "getwritedir",    cmd_getwritedir,    0, NULL                         },
       
   309     { "setwritedir",    cmd_setwritedir,    1, "<newWriteDir>"              },
       
   310     { "permitsymlinks", cmd_permitsyms,     1, "<1or0>"                     },
       
   311     { "setsaneconfig",  cmd_setsaneconfig,  4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },
       
   312     { NULL,             NULL,              -1, NULL                         }
    77 };
   313 };
    78 
   314 
       
   315 static void output_usage(const char *intro, const command_info *cmdinfo)
       
   316 {
       
   317     if (cmdinfo->argcount == 0)
       
   318         printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);
       
   319     else
       
   320         printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);
       
   321 } /* output_usage */
       
   322 
       
   323 
       
   324 static int cmd_help(char *args)
       
   325 {
       
   326     const command_info *i;
       
   327 
       
   328     printf("Commands:\n");
       
   329     for (i = commands; i->cmd != NULL; i++)
       
   330         output_usage("  -", i);
       
   331 
       
   332     return(1);
       
   333 } /* output_cmd_help */
       
   334 
       
   335 
       
   336 static void trim_command(const char *orig, char *copy)
       
   337 {
       
   338     const char *i;
       
   339     char *writeptr = copy;
       
   340     int spacecount = 0;
       
   341     int have_first = 0;
       
   342 
       
   343     for (i = orig; *i != '\0'; i++)
       
   344     {
       
   345         if (*i == ' ')
       
   346         {
       
   347             if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))
       
   348             {
       
   349                 if ((have_first) && (!spacecount))
       
   350                 {
       
   351                     spacecount++;
       
   352                     *writeptr = ' ';
       
   353                     writeptr++;
       
   354                 } /* if */
       
   355             } /* if */
       
   356         } /* if */
       
   357         else
       
   358         {
       
   359             have_first = 1;
       
   360             spacecount = 0;
       
   361             *writeptr = *i;
       
   362             writeptr++;
       
   363         } /* else */
       
   364     } /* for */
       
   365 
       
   366     *writeptr = '\0';
       
   367 
       
   368     /*
       
   369     printf("\n command is [%s].\n", copy);
       
   370     */
       
   371 } /* trim_command */
       
   372 
    79 
   373 
    80 static int process_command(char *complete_cmd)
   374 static int process_command(char *complete_cmd)
    81 {
   375 {
    82     command_info *i;
   376     const command_info *i;
    83     char *ptr = strchr(complete_cmd, ' ');
   377     char *cmd_copy = malloc(strlen(complete_cmd) + 1);
    84     char *cmd = NULL;
   378     char *args;
    85     int rc = 1;
   379     int rc = 1;
    86 
   380 
    87     if (ptr == NULL)
   381     if (cmd_copy == NULL)
    88     {
   382     {
    89         cmd = malloc(strlen(complete_cmd) + 1);
   383         printf("\n\n\nOUT OF MEMORY!\n\n\n");
    90         strcpy(cmd, complete_cmd);
   384         return(0);
    91     } /* if */
   385     } /* if */
    92     else
   386 
    93     {
   387     trim_command(complete_cmd, cmd_copy);
    94         *ptr = '\0';
   388     args = strchr(cmd_copy, ' ');
    95         cmd = malloc(strlen(complete_cmd) + 1);
   389     if (args != NULL)
    96         strcpy(cmd, complete_cmd);
   390     {
    97         *ptr = ' ';
   391         *args = '\0';
       
   392         args++;
    98     } /* else */
   393     } /* else */
    99 
   394 
   100     for (i = commands; i->cmd != NULL; i++)
   395     if (cmd_copy[0] != '\0')
   101     {
   396     {
   102         if (strcmp(i->cmd, cmd) == 0)
   397         for (i = commands; i->cmd != NULL; i++)
   103         {
   398         {
   104             rc = i->func(complete_cmd);
   399             if (strcmp(i->cmd, cmd_copy) == 0)
   105             break;
   400             {
       
   401                 if ((i->argcount >= 0) && (count_args(args) != i->argcount))
       
   402                     output_usage("usage:", i);
       
   403                 else
       
   404                     rc = i->func(args);
       
   405                 break;
       
   406             } /* if */
       
   407         } /* for */
       
   408 
       
   409         if (i->cmd == NULL)
       
   410             printf("Unknown command. Enter \"help\" for instructions.\n");
       
   411 
       
   412         add_history(complete_cmd);
       
   413         if (history_file)
       
   414         {
       
   415             fprintf(history_file, "%s\n", complete_cmd);
       
   416             fflush(history_file);
   106         } /* if */
   417         } /* if */
   107     } /* for */
   418     } /* if */
   108 
   419 
   109     if (i->cmd == NULL)
   420     free(cmd_copy);
   110         printf("Unknown command. Enter \"help\" for instructions.\n");
       
   111 
       
   112     free(cmd);
       
   113     return(rc);
   421     return(rc);
   114 } /* process_command */
   422 } /* process_command */
   115 
   423 
   116 
   424 
       
   425 static void open_history_file(void)
       
   426 {
       
   427     const char *envr = getenv("TESTPHYSFS_HISTORY");
       
   428     if (!envr)
       
   429         return;
       
   430 
       
   431     if (access(envr, F_OK) == 0)
       
   432     {
       
   433         char buf[512];
       
   434         FILE *f = fopen(envr, "r");
       
   435         if (!f)
       
   436         {
       
   437             printf("\n\n"
       
   438                    "Could not open history file [%s] for reading!\n"
       
   439                    "  Will not have past history available.\n\n",
       
   440                     envr);
       
   441             return;
       
   442         } /* if */
       
   443 
       
   444         do
       
   445         {
       
   446             fgets(buf, sizeof (buf), f);
       
   447             if (buf[strlen(buf) - 1] == '\n')
       
   448                 buf[strlen(buf) - 1] = '\0';
       
   449             add_history(buf);
       
   450         } while (!feof(f));
       
   451 
       
   452         fclose(f);
       
   453     } /* if */
       
   454 
       
   455     history_file = fopen(envr, "ab");
       
   456     if (!history_file)
       
   457     {
       
   458         printf("\n\n"
       
   459                "Could not open history file [%s] for appending!\n"
       
   460                "  Will not be able to record this session's history.\n\n",
       
   461                 envr);
       
   462     } /* if */
       
   463 } /* open_history_file */
       
   464 
       
   465 
   117 int main(int argc, char **argv)
   466 int main(int argc, char **argv)
   118 {
   467 {
   119     char *buf = NULL;
   468     char *buf = NULL;
   120     int rc = 0;
   469     int rc = 0;
   121 
   470 
   122     printf("\n");
   471     printf("\n");
   123 
   472 
   124     if (!PHYSFS_init(argv[0]))
   473     if (!PHYSFS_init(argv[0]))
   125     {
   474     {
   126         printf("PHYSFS_init() failed!\n  reason: %s\n", PHYSFS_getLastError());
   475         printf("PHYSFS_init() failed!\n  reason: %s.\n", PHYSFS_getLastError());
   127         return(1);
   476         return(1);
   128     } /* if */
   477     } /* if */
   129 
   478 
   130     output_versions();
   479     output_versions();
   131     output_archivers();
   480     output_archivers();
       
   481 
       
   482     open_history_file();
   132 
   483 
   133     printf("Enter commands. Enter \"help\" for instructions.\n");
   484     printf("Enter commands. Enter \"help\" for instructions.\n");
   134 
   485 
   135     do
   486     do
   136     {
   487     {
   137         buf = readline("> ");
   488         buf = readline("> ");
   138         rc = process_command(buf);
   489         rc = process_command(buf);
   139         free(buf);
   490         free(buf);
   140     } while (rc);
   491     } while (rc);
   141 
   492 
       
   493     if (!PHYSFS_deinit())
       
   494         printf("PHYSFS_deinit() failed!\n  reason: %s.\n", PHYSFS_getLastError());
       
   495 
       
   496     if (history_file)
       
   497         fclose(history_file);
       
   498 
       
   499 /*
       
   500     printf("\n\ntest_physfs written by ryan c. gordon.\n");
       
   501     printf(" it makes you shoot teh railgun bettar.\n");
       
   502 */
       
   503 
   142     return(0);
   504     return(0);
   143 } /* main */
   505 } /* main */
   144 
   506 
   145 /* end of test_physfs.c ... */
   507 /* end of test_physfs.c ... */
   146 
   508