⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_physfs.c

📁 嵌入式环境下的GUI
💻 C
📖 第 1 页 / 共 2 页
字号:
                    printf("\n (Error condition in reading. Reason: [%s])\n\n",                           PHYSFS_getLastError());                } /* if */                PHYSFS_close(f);                return(1);            } /* if */        } /* while */    } /* else */    return(1);} /* cmd_cat */static int cmd_filelength(char *args){    PHYSFS_file *f;    if (*args == '\"')    {        args++;        args[strlen(args) - 1] = '\0';    } /* if */    f = PHYSFS_openRead(args);    if (f == NULL)        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());    else    {        PHYSFS_sint64 len = PHYSFS_fileLength(f);        if (len == -1)            printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError());        else            printf(" (cast to int) %d bytes.\n", (int) len);        PHYSFS_close(f);    } /* else */    return(1);} /* cmd_filelength */#define WRITESTR "The cat sat on the mat.\n\n"static int cmd_append(char *args){    PHYSFS_file *f;    if (*args == '\"')    {        args++;        args[strlen(args) - 1] = '\0';    } /* if */    f = PHYSFS_openAppend(args);    if (f == NULL)        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());    else    {        size_t bw = strlen(WRITESTR);        PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);        if (rc != bw)        {            printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",                   (int) rc, (int) bw, PHYSFS_getLastError());        } /* if */        else        {            printf("Successful.\n");        } /* else */        PHYSFS_close(f);    } /* else */    return(1);} /* cmd_append */static int cmd_write(char *args){    PHYSFS_file *f;    if (*args == '\"')    {        args++;        args[strlen(args) - 1] = '\0';    } /* if */    f = PHYSFS_openWrite(args);    if (f == NULL)        printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError());    else    {        size_t bw = strlen(WRITESTR);        PHYSFS_sint64 rc = PHYSFS_write(f, WRITESTR, 1, bw);        if (rc != bw)        {            printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n",                   (int) rc, (int) bw, PHYSFS_getLastError());        } /* if */        else        {            printf("Successful.\n");        } /* else */        PHYSFS_close(f);    } /* else */    return(1);} /* cmd_write */static void modTimeToStr(PHYSFS_sint64 modtime, char *modstr, size_t strsize){    time_t t = (time_t) modtime;    char *str = ctime(&t);    strncpy(modstr, str, strsize);    modstr[strsize-1] = '\0';} /* modTimeToStr */static int cmd_getlastmodtime(char *args){    PHYSFS_sint64 rc = PHYSFS_getLastModTime(args);    if (rc == -1)        printf("Failed to determine. Reason: [%s].\n", PHYSFS_getLastError());    else    {        char modstr[64];        modTimeToStr(rc, modstr, sizeof (modstr));        printf("Last modified: %s (%ld).\n", modstr, (long) rc);    } /* else */    return(1);} /* cmd_getLastModTime *//* must have spaces trimmed prior to this call. */static int count_args(const char *str){    int retval = 0;    int in_quotes = 0;    if (str != NULL)    {        for (; *str != '\0'; str++)        {            if (*str == '\"')                in_quotes = !in_quotes;            else if ((*str == ' ') && (!in_quotes))                retval++;        } /* for */        retval++;    } /* if */    return(retval);} /* count_args */static int cmd_help(char *args);typedef struct{    const char *cmd;    int (*func)(char *args);    int argcount;    const char *usage;} command_info;static const command_info commands[] ={    { "quit",           cmd_quit,           0, NULL                         },    { "q",              cmd_quit,           0, NULL                         },    { "help",           cmd_help,           0, NULL                         },    { "init",           cmd_init,           1, "<argv0>"                    },    { "deinit",         cmd_deinit,         0, NULL                         },    { "addarchive",     cmd_addarchive,     2, "<archiveLocation> <append>" },    { "removearchive",  cmd_removearchive,  1, "<archiveLocation>"          },    { "enumerate",      cmd_enumerate,      1, "<dirToEnumerate>"           },    { "ls",             cmd_enumerate,      1, "<dirToEnumerate>"           },    { "getlasterror",   cmd_getlasterror,   0, NULL                         },    { "getdirsep",      cmd_getdirsep,      0, NULL                         },    { "getcdromdirs",   cmd_getcdromdirs,   0, NULL                         },    { "getsearchpath",  cmd_getsearchpath,  0, NULL                         },    { "getbasedir",     cmd_getbasedir,     0, NULL                         },    { "getuserdir",     cmd_getuserdir,     0, NULL                         },    { "getwritedir",    cmd_getwritedir,    0, NULL                         },    { "setwritedir",    cmd_setwritedir,    1, "<newWriteDir>"              },    { "permitsymlinks", cmd_permitsyms,     1, "<1or0>"                     },    { "setsaneconfig",  cmd_setsaneconfig,  4, "<appName> <arcExt> <includeCdRoms> <archivesFirst>" },    { "mkdir",          cmd_mkdir,          1, "<dirToMk>"                  },    { "delete",         cmd_delete,         1, "<dirToDelete>"              },    { "getrealdir",     cmd_getrealdir,     1, "<fileToFind>"               },    { "exists",         cmd_exists,         1, "<fileToCheck>"              },    { "isdir",          cmd_isdir,          1, "<fileToCheck>"              },    { "issymlink",      cmd_issymlink,      1, "<fileToCheck>"              },    { "cat",            cmd_cat,            1, "<fileToCat>"                },    { "filelength",     cmd_filelength,     1, "<fileToCheck>"              },    { "append",         cmd_append,         1, "<fileToAppend>"             },    { "write",          cmd_write,          1, "<fileToCreateOrTrash>"      },    { "getlastmodtime", cmd_getlastmodtime, 1, "<fileToExamine>"            },    { NULL,             NULL,              -1, NULL                         }};static void output_usage(const char *intro, const command_info *cmdinfo){    if (cmdinfo->argcount == 0)        printf("%s \"%s\" (no arguments)\n", intro, cmdinfo->cmd);    else        printf("%s \"%s %s\"\n", intro, cmdinfo->cmd, cmdinfo->usage);} /* output_usage */static int cmd_help(char *args){    const command_info *i;    printf("Commands:\n");    for (i = commands; i->cmd != NULL; i++)        output_usage("  -", i);    return(1);} /* output_cmd_help */static void trim_command(const char *orig, char *copy){    const char *i;    char *writeptr = copy;    int spacecount = 0;    int have_first = 0;    for (i = orig; *i != '\0'; i++)    {        if (*i == ' ')        {            if ((*(i + 1) != ' ') && (*(i + 1) != '\0'))            {                if ((have_first) && (!spacecount))                {                    spacecount++;                    *writeptr = ' ';                    writeptr++;                } /* if */            } /* if */        } /* if */        else        {            have_first = 1;            spacecount = 0;            *writeptr = *i;            writeptr++;        } /* else */    } /* for */    *writeptr = '\0';    /*    printf("\n command is [%s].\n", copy);    */} /* trim_command */static int process_command(char *complete_cmd){    const command_info *i;    char *cmd_copy = malloc(strlen(complete_cmd) + 1);    char *args;    int rc = 1;    if (cmd_copy == NULL)    {        printf("\n\n\nOUT OF MEMORY!\n\n\n");        return(0);    } /* if */    trim_command(complete_cmd, cmd_copy);    args = strchr(cmd_copy, ' ');    if (args != NULL)    {        *args = '\0';        args++;    } /* else */    if (cmd_copy[0] != '\0')    {        for (i = commands; i->cmd != NULL; i++)        {            if (strcmp(i->cmd, cmd_copy) == 0)            {                if ((i->argcount >= 0) && (count_args(args) != i->argcount))                    output_usage("usage:", i);                else                    rc = i->func(args);                break;            } /* if */        } /* for */        if (i->cmd == NULL)            printf("Unknown command. Enter \"help\" for instructions.\n");#if (defined PHYSFS_HAVE_READLINE)        add_history(complete_cmd);        if (history_file)        {            fprintf(history_file, "%s\n", complete_cmd);            fflush(history_file);        } /* if */#endif    } /* if */    free(cmd_copy);    return(rc);} /* process_command */static void open_history_file(void){#if (defined PHYSFS_HAVE_READLINE)#if 0    const char *envr = getenv("TESTPHYSFS_HISTORY");    if (!envr)        return;#else    char envr[256];    strcpy(envr, PHYSFS_getUserDir());    strcat(envr, ".testphys_history");#endif    if (access(envr, F_OK) == 0)    {        char buf[512];        FILE *f = fopen(envr, "r");        if (!f)        {            printf("\n\n"                   "Could not open history file [%s] for reading!\n"                   "  Will not have past history available.\n\n",                    envr);            return;        } /* if */        do        {            fgets(buf, sizeof (buf), f);            if (buf[strlen(buf) - 1] == '\n')                buf[strlen(buf) - 1] = '\0';            add_history(buf);        } while (!feof(f));        fclose(f);    } /* if */    history_file = fopen(envr, "ab");    if (!history_file)    {        printf("\n\n"               "Could not open history file [%s] for appending!\n"               "  Will not be able to record this session's history.\n\n",                envr);    } /* if */#endif} /* open_history_file */int main(int argc, char **argv){    char *buf = NULL;    int rc = 0;#if (defined __MWERKS__)    extern tSIOUXSettings SIOUXSettings;    SIOUXSettings.asktosaveonclose = 0;    SIOUXSettings.autocloseonquit = 1;    SIOUXSettings.rows = 40;    SIOUXSettings.columns = 120;#endif    printf("\n");    if (!PHYSFS_init(argv[0]))    {        printf("PHYSFS_init() failed!\n  reason: %s.\n", PHYSFS_getLastError());        return(1);    } /* if */    output_versions();    output_archivers();    open_history_file();    printf("Enter commands. Enter \"help\" for instructions.\n");    do    {#if (defined PHYSFS_HAVE_READLINE)        buf = readline("> ");#else        int i;        buf = malloc(512);        memset(buf, '\0', 512);        printf("> ");        for (i = 0; i < 511; i++)        {            int ch = fgetc(stdin);            if (ch == EOF)            {                strcpy(buf, "quit");                break;            } /* if */            else if ((ch == '\n') || (ch == '\r'))            {                buf[i] = '\0';                break;            } /* else if */            else if (ch == '\b')            {                if (i > 0)                    i--;            } /* else if */            else            {                buf[i] = (char) ch;            } /* else */        } /* for */#endif        rc = process_command(buf);        free(buf);    } while (rc);    if (!PHYSFS_deinit())        printf("PHYSFS_deinit() failed!\n  reason: %s.\n", PHYSFS_getLastError());    if (history_file)        fclose(history_file);/*    printf("\n\ntest_physfs written by ryan c. gordon.\n");    printf(" it makes you shoot teh railgun bettar.\n");*/    return(0);} /* main *//* end of test_physfs.c ... */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -