test/test_physfs.c
changeset 38 feb16343f44c
parent 35 1fba29d8a172
child 40 8cc16df4bada
equal deleted inserted replaced
37:768c8484b0cd 38:feb16343f44c
     1 #include <stdio.h>
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
       
     3 #include <readline.h>
       
     4 #include <history.h>
     2 #include "physfs.h"
     5 #include "physfs.h"
     3 
     6 
     4 #define TEST_VERSION_MAJOR  0
     7 #define TEST_VERSION_MAJOR  0
     5 #define TEST_VERSION_MINOR  1
     8 #define TEST_VERSION_MINOR  1
     6 #define TEST_VERSION_PATCH  0
     9 #define TEST_VERSION_PATCH  0
     7 
    10 
     8 void output_versions(void)
    11 static void output_versions(void)
     9 {
    12 {
    10     PHYSFS_Version compiled;
    13     PHYSFS_Version compiled;
    11     PHYSFS_Version linked;
    14     PHYSFS_Version linked;
    12 
    15 
    13     PHYSFS_VERSION(&compiled);
    16     PHYSFS_VERSION(&compiled);
    20             compiled.major, compiled.minor, compiled.patch,
    23             compiled.major, compiled.minor, compiled.patch,
    21             linked.major, linked.minor, linked.patch);
    24             linked.major, linked.minor, linked.patch);
    22 } /* output_versions */
    25 } /* output_versions */
    23 
    26 
    24 
    27 
    25 void output_archivers(void)
    28 static void output_archivers(void)
    26 {
    29 {
    27     const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
    30     const PHYSFS_ArchiveInfo **rc = PHYSFS_supportedArchiveTypes();
    28     const PHYSFS_ArchiveInfo **i;
    31     const PHYSFS_ArchiveInfo **i;
    29 
    32 
    30     printf("Supported archive types:\n");
    33     printf("Supported archive types:\n");
    37             printf(" * %s: %s\n    Written by %s.\n    %s\n",
    40             printf(" * %s: %s\n    Written by %s.\n    %s\n",
    38                     (*i)->extension, (*i)->description,
    41                     (*i)->extension, (*i)->description,
    39                     (*i)->author, (*i)->url);
    42                     (*i)->author, (*i)->url);
    40         } /* for */
    43         } /* for */
    41     } /* else */
    44     } /* else */
       
    45 
       
    46     printf("\n");
    42 } /* output_archivers */
    47 } /* output_archivers */
       
    48 
       
    49 
       
    50 static int cmd_help(char *cmdstr)
       
    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 {
       
    61     return(0);
       
    62 } /* cmd_quit */
       
    63 
       
    64 
       
    65 typedef struct
       
    66 {
       
    67     const char *cmd;
       
    68     int (*func)(char *cmdstr);
       
    69 } command_info;
       
    70 
       
    71 static command_info commands[] =
       
    72 {
       
    73     {"quit", cmd_quit},
       
    74     {"q",    cmd_quit},
       
    75     {"help", cmd_help},
       
    76     {NULL, NULL}
       
    77 };
       
    78 
       
    79 
       
    80 static int process_command(char *complete_cmd)
       
    81 {
       
    82     command_info *i;
       
    83     char *ptr = strchr(complete_cmd, ' ');
       
    84     char *cmd = NULL;
       
    85     int rc = 1;
       
    86 
       
    87     if (ptr == NULL)
       
    88     {
       
    89         cmd = malloc(strlen(complete_cmd) + 1);
       
    90         strcpy(cmd, complete_cmd);
       
    91     } /* if */
       
    92     else
       
    93     {
       
    94         *ptr = '\0';
       
    95         cmd = malloc(strlen(complete_cmd) + 1);
       
    96         strcpy(cmd, complete_cmd);
       
    97         *ptr = ' ';
       
    98     } /* else */
       
    99 
       
   100     for (i = commands; i->cmd != NULL; i++)
       
   101     {
       
   102         if (strcmp(i->cmd, cmd) == 0)
       
   103         {
       
   104             rc = i->func(complete_cmd);
       
   105             break;
       
   106         } /* if */
       
   107     } /* for */
       
   108 
       
   109     if (i->cmd == NULL)
       
   110         printf("Unknown command. Enter \"help\" for instructions.\n");
       
   111 
       
   112     free(cmd);
       
   113     return(rc);
       
   114 } /* process_command */
    43 
   115 
    44 
   116 
    45 int main(int argc, char **argv)
   117 int main(int argc, char **argv)
    46 {
   118 {
       
   119     char *buf = NULL;
       
   120     int rc = 0;
       
   121 
       
   122     printf("\n");
       
   123 
    47     if (!PHYSFS_init(argv[0]))
   124     if (!PHYSFS_init(argv[0]))
    48     {
   125     {
    49         printf("PHYSFS_init() failed!\n  reason: %s\n", PHYSFS_getLastError());
   126         printf("PHYSFS_init() failed!\n  reason: %s\n", PHYSFS_getLastError());
    50         return(1);
   127         return(1);
    51     } /* if */
   128     } /* if */
    52 
   129 
    53     output_versions();
   130     output_versions();
    54     output_archivers();
   131     output_archivers();
    55 
   132 
       
   133     printf("Enter commands. Enter \"help\" for instructions.\n");
       
   134 
       
   135     do
       
   136     {
       
   137         buf = readline("> ");
       
   138         rc = process_command(buf);
       
   139         free(buf);
       
   140     } while (rc);
       
   141 
    56     return(0);
   142     return(0);
    57 } /* main */
   143 } /* main */
    58 
   144 
    59 /* end of test_physfs.c ... */
   145 /* end of test_physfs.c ... */
    60 
   146