File Paths in UnrealScript

Here’s the translation of the Go code example to UnrealScript, formatted in Markdown suitable for Hugo:

The FilePath class provides functions to parse and construct file paths in a way that is portable between operating systems; dir/file on Linux vs. dir\file on Windows, for example.

class FilePath extends Object;

// Join should be used to construct paths in a portable way.
// It takes any number of arguments and constructs a hierarchical path from them.
static function string Join(array<string> PathParts)
{
    local string Result;
    local int i;

    for (i = 0; i < PathParts.Length; i++)
    {
        if (i > 0)
        {
            Result $= "/";
        }
        Result $= PathParts[i];
    }

    return Result;
}

// Dir returns all but the last element of path, typically the path's directory.
static function string Dir(string Path)
{
    local int LastSlash;

    LastSlash = InStr(Path, "/", true);
    if (LastSlash != -1)
    {
        return Left(Path, LastSlash);
    }
    return "";
}

// Base returns the last element of path.
static function string Base(string Path)
{
    local int LastSlash;

    LastSlash = InStr(Path, "/", true);
    if (LastSlash != -1)
    {
        return Mid(Path, LastSlash + 1);
    }
    return Path;
}

// IsAbs reports whether the path is absolute.
static function bool IsAbs(string Path)
{
    return Left(Path, 1) == "/";
}

// Ext returns the file name extension used by path.
static function string Ext(string Path)
{
    local int LastDot;

    LastDot = InStr(Path, ".", true);
    if (LastDot != -1)
    {
        return Mid(Path, LastDot);
    }
    return "";
}

// Rel returns a relative path that is lexically equivalent to targpath when
// joined to basepath with an intervening separator.
static function string Rel(string BasePath, string TargPath)
{
    // This is a simplified implementation and might not cover all cases
    if (Left(TargPath, Len(BasePath)) == BasePath)
    {
        return Mid(TargPath, Len(BasePath) + 1);
    }
    return TargPath;
}

static function TestFilePaths()
{
    local array<string> PathParts;
    local string P, Filename, Ext, Rel;

    PathParts.AddItem("dir1");
    PathParts.AddItem("dir2");
    PathParts.AddItem("filename");
    P = Join(PathParts);
    `log("p:" @ P);

    `log(Join(["dir1//", "filename"]));
    `log(Join(["dir1/../dir1", "filename"]));

    `log("Dir(p):" @ Dir(P));
    `log("Base(p):" @ Base(P));

    `log("IsAbs(dir/file):" @ IsAbs("dir/file"));
    `log("IsAbs(/dir/file):" @ IsAbs("/dir/file"));

    Filename = "config.json";
    Ext = Ext(Filename);
    `log("Ext:" @ Ext);

    `log("TrimSuffix:" @ Left(Filename, Len(Filename) - Len(Ext)));

    Rel = Rel("a/b", "a/b/t/file");
    `log("Rel:" @ Rel);

    Rel = Rel("a/b", "a/c/t/file");
    `log("Rel:" @ Rel);
}

This UnrealScript implementation provides similar functionality to the original example. However, there are some differences due to the language’s limitations:

  1. UnrealScript doesn’t have a built-in filepath manipulation library, so we’ve implemented basic versions of these functions.
  2. The Join function takes an array of strings instead of a variable number of arguments.
  3. Error handling is not implemented in this example, as UnrealScript doesn’t have a try-catch mechanism.
  4. The TrimSuffix functionality is implemented directly in the example, as UnrealScript doesn’t have a built-in function for this.
  5. The Rel function is a simplified version and might not cover all edge cases.

To use this code, you would typically call the TestFilePaths function, which demonstrates the usage of all the implemented file path functions. The results would be output to the UnrealEngine log.

Remember that in a real UnrealScript project, you would typically split this into separate files and use the appropriate class structure and game framework integration.