test/test_physfs.c
changeset 170 d826bf2c38c8
parent 165 cbb1b0dcf6da
child 182 a47c39af46ad
equal deleted inserted replaced
169:34829282ffe8 170:d826bf2c38c8
    27 
    27 
    28 #include "physfs.h"
    28 #include "physfs.h"
    29 
    29 
    30 #define TEST_VERSION_MAJOR  0
    30 #define TEST_VERSION_MAJOR  0
    31 #define TEST_VERSION_MINOR  1
    31 #define TEST_VERSION_MINOR  1
    32 #define TEST_VERSION_PATCH  0
    32 #define TEST_VERSION_PATCH  1
    33 
    33 
    34 static FILE *history_file = NULL;
    34 static FILE *history_file = NULL;
    35 
    35 
    36 static void output_versions(void)
    36 static void output_versions(void)
    37 {
    37 {
    78 } /* cmd_quit */
    78 } /* cmd_quit */
    79 
    79 
    80 
    80 
    81 static int cmd_init(char *args)
    81 static int cmd_init(char *args)
    82 {
    82 {
       
    83     if (*args == '\"')
       
    84     {
       
    85         args++;
       
    86         args[strlen(args) - 1] = '\0';
       
    87     } /* if */
       
    88 
    83     if (PHYSFS_init(args))
    89     if (PHYSFS_init(args))
    84         printf("Successful.\n");
    90         printf("Successful.\n");
    85     else
    91     else
    86         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
    92         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
    87 
    93 
   108 
   114 
   109     if (*args == '\"')
   115     if (*args == '\"')
   110     {
   116     {
   111         args++;
   117         args++;
   112         *(ptr - 1) = '\0';
   118         *(ptr - 1) = '\0';
   113     }
   119     } /* if */
   114 
   120 
   115     /*printf("[%s], [%d]\n", args, appending);*/
   121     /*printf("[%s], [%d]\n", args, appending);*/
   116 
   122 
   117     if (PHYSFS_addToSearchPath(args, appending))
   123     if (PHYSFS_addToSearchPath(args, appending))
   118         printf("Successful.\n");
   124         printf("Successful.\n");
   123 } /* cmd_addarchive */
   129 } /* cmd_addarchive */
   124 
   130 
   125 
   131 
   126 static int cmd_removearchive(char *args)
   132 static int cmd_removearchive(char *args)
   127 {
   133 {
       
   134     if (*args == '\"')
       
   135     {
       
   136         args++;
       
   137         args[strlen(args) - 1] = '\0';
       
   138     } /* if */
       
   139 
   128     if (PHYSFS_removeFromSearchPath(args))
   140     if (PHYSFS_removeFromSearchPath(args))
   129         printf("Successful.\n");
   141         printf("Successful.\n");
   130     else
   142     else
   131         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   143         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   132 
   144 
   134 } /* cmd_removearchive */
   146 } /* cmd_removearchive */
   135 
   147 
   136 
   148 
   137 static int cmd_enumerate(char *args)
   149 static int cmd_enumerate(char *args)
   138 {
   150 {
   139     char **rc = PHYSFS_enumerateFiles(args);
   151     char **rc;
       
   152 
       
   153     if (*args == '\"')
       
   154     {
       
   155         args++;
       
   156         args[strlen(args) - 1] = '\0';
       
   157     } /* if */
       
   158 
       
   159     rc = PHYSFS_enumerateFiles(args);
   140 
   160 
   141     if (rc == NULL)
   161     if (rc == NULL)
   142         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   162         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   143     else
   163     else
   144     {
   164     {
   172 static int cmd_getcdromdirs(char *args)
   192 static int cmd_getcdromdirs(char *args)
   173 {
   193 {
   174     char **rc = PHYSFS_getCdRomDirs();
   194     char **rc = PHYSFS_getCdRomDirs();
   175 
   195 
   176     if (rc == NULL)
   196     if (rc == NULL)
   177         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   197         printf("Failure. Reason: [%s].\n", PHYSFS_getLastError());
   178     else
   198     else
   179     {
   199     {
   180         int dir_count;
   200         int dir_count;
   181         char **i;
   201         char **i;
   182         for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
   202         for (i = rc, dir_count = 0; *i != NULL; i++, dir_count++)
   232 } /* cmd_getwritedir */
   252 } /* cmd_getwritedir */
   233 
   253 
   234 
   254 
   235 static int cmd_setwritedir(char *args)
   255 static int cmd_setwritedir(char *args)
   236 {
   256 {
       
   257     if (*args == '\"')
       
   258     {
       
   259         args++;
       
   260         args[strlen(args) - 1] = '\0';
       
   261     } /* if */
       
   262 
   237     if (PHYSFS_setWriteDir(args))
   263     if (PHYSFS_setWriteDir(args))
   238         printf("Successful.\n");
   264         printf("Successful.\n");
   239     else
   265     else
   240         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   266         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   241 
   267 
   243 } /* cmd_setwritedir */
   269 } /* cmd_setwritedir */
   244 
   270 
   245 
   271 
   246 static int cmd_permitsyms(char *args)
   272 static int cmd_permitsyms(char *args)
   247 {
   273 {
   248     int num = atoi(args);
   274     int num;
       
   275 
       
   276     if (*args == '\"')
       
   277     {
       
   278         args++;
       
   279         args[strlen(args) - 1] = '\0';
       
   280     } /* if */
       
   281 
       
   282     num = atoi(args);
   249     PHYSFS_permitSymbolicLinks(num);
   283     PHYSFS_permitSymbolicLinks(num);
   250     printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
   284     printf("Symlinks are now %s.\n", num ? "permitted" : "forbidden");
   251     return(1);
   285     return(1);
   252 } /* cmd_permitsyms */
   286 } /* cmd_permitsyms */
   253 
   287 
   280 } /* cmd_setsaneconfig */
   314 } /* cmd_setsaneconfig */
   281 
   315 
   282 
   316 
   283 static int cmd_mkdir(char *args)
   317 static int cmd_mkdir(char *args)
   284 {
   318 {
       
   319     if (*args == '\"')
       
   320     {
       
   321         args++;
       
   322         args[strlen(args) - 1] = '\0';
       
   323     } /* if */
       
   324 
   285     if (PHYSFS_mkdir(args))
   325     if (PHYSFS_mkdir(args))
   286         printf("Successful.\n");
   326         printf("Successful.\n");
   287     else
   327     else
   288         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   328         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   289 
   329 
   291 } /* cmd_mkdir */
   331 } /* cmd_mkdir */
   292 
   332 
   293 
   333 
   294 static int cmd_delete(char *args)
   334 static int cmd_delete(char *args)
   295 {
   335 {
       
   336     if (*args == '\"')
       
   337     {
       
   338         args++;
       
   339         args[strlen(args) - 1] = '\0';
       
   340     } /* if */
       
   341 
   296     if (PHYSFS_delete(args))
   342     if (PHYSFS_delete(args))
   297         printf("Successful.\n");
   343         printf("Successful.\n");
   298     else
   344     else
   299         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   345         printf("Failure. reason: %s.\n", PHYSFS_getLastError());
   300 
   346 
   302 } /* cmd_delete */
   348 } /* cmd_delete */
   303 
   349 
   304 
   350 
   305 static int cmd_getrealdir(char *args)
   351 static int cmd_getrealdir(char *args)
   306 {
   352 {
   307     const char *rc = PHYSFS_getRealDir(args);
   353     const char *rc;
       
   354 
       
   355     if (*args == '\"')
       
   356     {
       
   357         args++;
       
   358         args[strlen(args) - 1] = '\0';
       
   359     } /* if */
       
   360 
       
   361     rc = PHYSFS_getRealDir(args);
   308     if (rc)
   362     if (rc)
   309         printf("Found at [%s].\n", rc);
   363         printf("Found at [%s].\n", rc);
   310     else
   364     else
   311         printf("Not found.\n");
   365         printf("Not found.\n");
   312 
   366 
   314 } /* cmd_getrealdir */
   368 } /* cmd_getrealdir */
   315 
   369 
   316 
   370 
   317 static int cmd_exists(char *args)
   371 static int cmd_exists(char *args)
   318 {
   372 {
   319     int rc = PHYSFS_exists(args);
   373     int rc;
       
   374 
       
   375     if (*args == '\"')
       
   376     {
       
   377         args++;
       
   378         args[strlen(args) - 1] = '\0';
       
   379     } /* if */
       
   380 
       
   381     rc = PHYSFS_exists(args);
   320     printf("File %sexists.\n", rc ? "" : "does not ");
   382     printf("File %sexists.\n", rc ? "" : "does not ");
   321     return(1);
   383     return(1);
   322 } /* cmd_exists */
   384 } /* cmd_exists */
   323 
   385 
   324 
   386 
   325 static int cmd_isdir(char *args)
   387 static int cmd_isdir(char *args)
   326 {
   388 {
   327     int rc = PHYSFS_isDirectory(args);
   389     int rc;
       
   390 
       
   391     if (*args == '\"')
       
   392     {
       
   393         args++;
       
   394         args[strlen(args) - 1] = '\0';
       
   395     } /* if */
       
   396 
       
   397     rc = PHYSFS_isDirectory(args);
   328     printf("File %s a directory.\n", rc ? "is" : "is NOT");
   398     printf("File %s a directory.\n", rc ? "is" : "is NOT");
   329     return(1);
   399     return(1);
   330 } /* cmd_isdir */
   400 } /* cmd_isdir */
   331 
   401 
   332 
   402 
   333 static int cmd_issymlink(char *args)
   403 static int cmd_issymlink(char *args)
   334 {
   404 {
   335     int rc = PHYSFS_isSymbolicLink(args);
   405     int rc;
       
   406 
       
   407     if (*args == '\"')
       
   408     {
       
   409         args++;
       
   410         args[strlen(args) - 1] = '\0';
       
   411     } /* if */
       
   412 
       
   413     rc = PHYSFS_isSymbolicLink(args);
   336     printf("File %s a symlink.\n", rc ? "is" : "is NOT");
   414     printf("File %s a symlink.\n", rc ? "is" : "is NOT");
   337     return(1);
   415     return(1);
   338 } /* cmd_issymlink */
   416 } /* cmd_issymlink */
   339 
   417 
   340 
   418 
   341 static int cmd_cat(char *args)
   419 static int cmd_cat(char *args)
   342 {
   420 {
   343     PHYSFS_file *f = PHYSFS_openRead(args);
   421     PHYSFS_file *f;
       
   422 
       
   423     if (*args == '\"')
       
   424     {
       
   425         args++;
       
   426         args[strlen(args) - 1] = '\0';
       
   427     } /* if */
       
   428 
       
   429     f = PHYSFS_openRead(args);
   344     if (f == NULL)
   430     if (f == NULL)
   345         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   431         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
   346     else
   432     else
   347     {
   433     {
   348         while (1)
   434         while (1)
   369         } /* while */
   455         } /* while */
   370     } /* else */
   456     } /* else */
   371 
   457 
   372     return(1);
   458     return(1);
   373 } /* cmd_cat */
   459 } /* cmd_cat */
       
   460 
       
   461 
       
   462 static int cmd_filelength(char *args)
       
   463 {
       
   464     PHYSFS_file *f;
       
   465 
       
   466     if (*args == '\"')
       
   467     {
       
   468         args++;
       
   469         args[strlen(args) - 1] = '\0';
       
   470     } /* if */
       
   471 
       
   472     f = PHYSFS_openRead(args);
       
   473     if (f == NULL)
       
   474         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
       
   475     else
       
   476     {
       
   477         PHYSFS_sint64 len = PHYSFS_fileLength(f);
       
   478         if (len == -1)
       
   479             printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());
       
   480         else
       
   481             printf(" (cast to int) %d bytes.\n", (int) len);
       
   482 
       
   483         PHYSFS_close(f);
       
   484     } /* else */
       
   485 
       
   486     return(1);
       
   487 } /* cmd_filelength */
       
   488 
       
   489 
       
   490 #define WRITESTR "The cat sat on the mat.\n\n"
       
   491 
       
   492 static int cmd_append(char *args)
       
   493 {
       
   494     PHYSFS_file *f;
       
   495 
       
   496     if (*args == '\"')
       
   497     {
       
   498         args++;
       
   499         args[strlen(args) - 1] = '\0';
       
   500     } /* if */
       
   501 
       
   502     f = PHYSFS_openAppend(args);
       
   503     if (f == NULL)
       
   504         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
       
   505     else
       
   506     {
       
   507         size_t bw = strlen(WRITESTR);
       
   508         PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
       
   509         if (rc != bw)
       
   510         {
       
   511             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", rc, bw,
       
   512                    PHYSFS_getLastError());
       
   513         } /* if */
       
   514         else
       
   515         {
       
   516             printf("Successful.\n");
       
   517         } /* else */
       
   518 
       
   519         PHYSFS_close(f);
       
   520     } /* else */
       
   521 
       
   522     return(1);
       
   523 } /* cmd_append */
       
   524 
       
   525 
       
   526 static int cmd_write(char *args)
       
   527 {
       
   528     PHYSFS_file *f;
       
   529 
       
   530     if (*args == '\"')
       
   531     {
       
   532         args++;
       
   533         args[strlen(args) - 1] = '\0';
       
   534     } /* if */
       
   535 
       
   536     f = PHYSFS_openWrite(args);
       
   537     if (f == NULL)
       
   538         printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());
       
   539     else
       
   540     {
       
   541         size_t bw = strlen(WRITESTR);
       
   542         PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);
       
   543         if (rc != bw)
       
   544         {
       
   545             printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", rc, bw,
       
   546                    PHYSFS_getLastError());
       
   547         } /* if */
       
   548         else
       
   549         {
       
   550             printf("Successful.\n");
       
   551         } /* else */
       
   552 
       
   553         PHYSFS_close(f);
       
   554     } /* else */
       
   555 
       
   556     return(1);
       
   557 } /* cmd_write */
   374 
   558 
   375 
   559 
   376 /* must have spaces trimmed prior to this call. */
   560 /* must have spaces trimmed prior to this call. */
   377 static int count_args(const char *str)
   561 static int count_args(const char *str)
   378 {
   562 {
   430     { "getrealdir",     cmd_getrealdir,     1, "<fileToFind>"               },
   614     { "getrealdir",     cmd_getrealdir,     1, "<fileToFind>"               },
   431     { "exists",         cmd_exists,         1, "<fileToCheck>"              },
   615     { "exists",         cmd_exists,         1, "<fileToCheck>"              },
   432     { "isdir",          cmd_isdir,          1, "<fileToCheck>"              },
   616     { "isdir",          cmd_isdir,          1, "<fileToCheck>"              },
   433     { "issymlink",      cmd_issymlink,      1, "<fileToCheck>"              },
   617     { "issymlink",      cmd_issymlink,      1, "<fileToCheck>"              },
   434     { "cat",            cmd_cat,            1, "<fileToCat>"                },
   618     { "cat",            cmd_cat,            1, "<fileToCat>"                },
       
   619     { "filelength",     cmd_filelength,     1, "<fileToCheck>"              },
       
   620     { "append",         cmd_append,         1, "<fileToAppend>"             },
       
   621     { "write",          cmd_write,          1, "<fileToCreateOrTrash>"      },
   435     { NULL,             NULL,              -1, NULL                         }
   622     { NULL,             NULL,              -1, NULL                         }
   436 };
   623 };
   437 
   624 
   438 static void output_usage(const char *intro, const command_info *cmdinfo)
   625 static void output_usage(const char *intro, const command_info *cmdinfo)
   439 {
   626 {