/// \brief Simple utility function that appends a \p New string to the given
/// \p Old string, using the \p Buffer for storage.
///
/// \param Old The string to which we are appending. This parameter will be
/// updated to reflect the complete string.
///
///
/// \param New The string to append to \p Old.
///
/// \param Buffer A buffer that stores the actual, concatenated string. It will
/// be used if the old string is already-non-empty.
static void AppendToString(StringRef &Old, StringRef New,
                           llvm::SmallString<256> &Buffer) {
  if (Old.empty()) {
    Old = New;
    return;
  }
  
  if (Buffer.empty())
    Buffer.append(Old.begin(), Old.end());
  Buffer.append(New.begin(), New.end());
  Old = Buffer.str();
}
Esempio n. 2
0
bool ConfigFile::locate(llvm::SmallString<128> &p, const char* argv0, void* mainAddr, const char* filename)
{
    // temporary configuration

    // try the current working dir
    if (!sys::fs::current_path(p))
    {
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }

    // try next to the executable
    p = getMainExecutable(argv0, mainAddr);
    sys::path::remove_filename(p);
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // user configuration

    // try ~/.ldc
    p = getUserHomeDirectory();
    sys::path::append(p, ".ldc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

#if _WIN32
    // try home dir
    p = getUserHomeDirectory();
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;
#endif

    // system configuration

    // try in etc relative to the executable: exe\..\etc
    // do not use .. in path because of security risks
    p = getMainExecutable(argv0, mainAddr);
    sys::path::remove_filename(p);
    sys::path::remove_filename(p);
    if (!p.empty())
    {
        sys::path::append(p, "etc");
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }

#if _WIN32
    // Try reading path from registry
    if (ReadPathFromRegistry(p))
    {
        sys::path::append(p, "etc");
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }
#else
    // try the install-prefix/etc
    p = LDC_INSTALL_PREFIX;
    sys::path::append(p, "etc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try the install-prefix/etc/ldc
    p = LDC_INSTALL_PREFIX;
    sys::path::append(p, "etc");
    sys::path::append(p, "ldc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try /etc (absolute path)
    p = "/etc";
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try /etc/ldc (absolute path)
    p = "/etc/ldc";
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;
#endif

    return false;
}