// (c) 2007 Electronic Arts Inc.
#include <stdio.h>
#include <string.h>
#define rwFUNCTION()
#define rwRETURNVOID() return


// convert user host file name to mount point name
void PS3HostName(char *HostNameBuf, const char *RwFileName)
{
    rwFUNCTION();

    #define MOUNTTBLSIZE 8
    struct HostToMount
    {
        char *host;
        int hostlen; // strlen(host) to avoid strlen performance problems noted by nba street
        char *mountPoint;
    } HostToMountTable[MOUNTTBLSIZE] =
    {
        {   "bdvd:",5,  "/dev_bdvd/" },
        {   "hdd0:",5,  "/dev_hdd0/" },
        {   "hdd1:",5,  "/dev_hdd1/" },
        {   "host1:",6, "/app_home/" },

        {   "host0:",6, "/app_home/" }, // provided for general purpose name conversion
        {   "ms:",3,    "/dev_ms/"   },
        {   "cf:",3,    "/dev_cf/"   },
        {   "sd:",3,    "/dev_sd/"   }
    };

    char *d = HostNameBuf;
    for (int i=0; i<MOUNTTBLSIZE; ++i)
    {
        if (!strncmp(RwFileName,HostToMountTable[i].host,HostToMountTable[i].hostlen))
        {
            const char *s = RwFileName+HostToMountTable[i].hostlen;    // skip over device name (Host1:)
            if (*s=='/' || *s=='\\')            // skip initial directory slash
                ++s;

            sprintf(d,"%s%s",HostToMountTable[i].mountPoint,s);
            while (*d)
            {
                if (*d=='\\')
                    *d = '/';
                ++d;
            }
            rwRETURNVOID();
        }
    }

    // prepend app_home
    const char *s = RwFileName;    // skip over device name (Host1:)
    if (*s=='/' || *s=='\\')            // skip initial directory slash
        ++s;

    sprintf(d,"/app_home/%s",s);
    while (*d)
    {
        if (*d=='\\')
            *d = '/';
        ++d;
    }
    *d = '\0';

    rwRETURNVOID();

    #undef MOUNTTBLSIZE
}

// convert user host file name to mount point name
static void PS3Host1Name(char *Host1namebuf, const char *name)
{
    rwFUNCTION();

    #define MOUNTTBLSIZE 4
    struct HostToMount
    {
        char *host;
        char *mountPoint;
    } HostToMountTable[MOUNTTBLSIZE] =
    {
        {   "host1:", "/app_home/" },
        {   "bdvd:",  "/dev_bdvd/" },
        {   "hdd0:",  "/dev_hdd0/" },
        {   "ms:",    "/dev_ms/"   }
    };

    char *d = Host1namebuf;
    for (int i=0; i<MOUNTTBLSIZE; ++i)
    {
        if (!strncmp(name,HostToMountTable[i].host,strlen(HostToMountTable[i].host)))
        {
            const char *s = name+strlen(HostToMountTable[i].host);    // skip over device name (Host1:)
            if (*s=='/' || *s=='\\')            // skip initial directory slash
                ++s;

            sprintf(d,"%s%s",HostToMountTable[i].mountPoint,s);
            while (*d)
            {
                if (*d=='\\')
                    *d = '/';
                ++d;
            }
            rwRETURNVOID();
        }
    }

    // prepend app_home
    const char *s = name;    // skip over device name (Host1:)
    if (*s=='/' || *s=='\\')            // skip initial directory slash
        ++s;

    sprintf(d,"/app_home/%s",s);
    while (*d)
    {
        if (*d=='\\')
            *d = '/';
        ++d;
    }
    *d = '\0';

    rwRETURNVOID();

    #undef MOUNTTBLSIZE
}

void test(char *s)
{
    char buf[512];

    PS3Host1Name(buf,s);
    printf("OLD PS3Host1Name name function %50s -> %s\n",s,buf);

    PS3HostName(buf,s);
    printf("New PS3HostName name function  %50s -> %s\n",s,buf);
}


main(int argc, char **argv)
{
    test("host1:test.dat");
    test("host1:/folder/test.dat");
    test("bdvd:test.dat");
    test("bdvd:/folder/test.dat");
    test("hdd0:test.dat");
    test("hdd0:/folder/test.dat");
    test("ms:test.dat");
    test("ms:/folder/test.dat");

    test("hdd0:/dev_hdd0/game/SLUS12345/USRDIR/somefilename");
    test("hdd0:game/SLUS12345/USRDIR/somefilename");

    test("cf:/folder/test.dat");
    test("sd:/test.dat");
    test("oops:/test.dat");
    test("host0:/test.dat");
    test("test.dat");

    for (int i=1; i<argc; ++i)
        test(argv[i]);
}
