Edit

kc3-lang/angle/src/common/system_utils.cpp

Branch :

  • Show log

    Commit

  • Author : Ian Elliott
    Date : 2020-10-27 10:01:05
    Hash : 3980b1f8
    Message : Refactor FrameCapture.cpp to use common utility The frame-capture-specific AndroidGetEnvFromProp() function has been ported to the general utility, GetEnvironmentVarFromAndroidProperty(). Bug: b/170249632 Change-Id: I97de8205ceef140dfd8fab8e6f2d52b90cd996cf Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2502772 Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Ian Elliott <ianelliott@google.com>

  • src/common/system_utils.cpp
  • //
    // Copyright 2018 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    //
    
    // system_utils.cpp: Implementation of common functions
    
    #include "common/system_utils.h"
    
    #include <stdlib.h>
    
    namespace angle
    {
    std::string GetExecutableName()
    {
    #if defined(ANGLE_PLATFORM_ANDROID) && __ANDROID_API__ >= 21
        // Support for "getprogname" function in bionic was introduced in L (API level 21)
        const char *executableName = getprogname();
        return (executableName) ? std::string(executableName) : "ANGLE";
    #else
        std::string executableName = GetExecutablePath();
        size_t lastPathSepLoc      = executableName.find_last_of(GetPathSeparator());
        return (lastPathSepLoc > 0 ? executableName.substr(lastPathSepLoc + 1, executableName.length())
                                   : "ANGLE");
    #endif  // ANGLE_PLATFORM_ANDROID
    }
    
    // Call out to 'getprop' on a shell to get an Android property.  If the value was set, set an
    // environment variable with that value.  Return the value of the environment variable.
    std::string GetEnvironmentVarOrAndroidProperty(const char *variableName, const char *propertyName)
    {
    #if defined(ANGLE_PLATFORM_ANDROID) && __ANDROID_API__ >= 21
        std::string sanitizedPropertyName = propertyName;
        sanitizedPropertyName.erase(
            std::remove(sanitizedPropertyName.begin(), sanitizedPropertyName.end(), '\''),
            sanitizedPropertyName.end());
    
        std::string command("getprop '");
        command += sanitizedPropertyName;
        command += "'";
    
        // Run the command and open a I/O stream to read the value
        constexpr int kStreamSize = 64;
        char stream[kStreamSize]  = {};
        FILE *pipe                = popen(command.c_str(), "r");
        if (pipe != nullptr)
        {
            fgets(stream, kStreamSize, pipe);
            pclose(pipe);
        }
    
        // Right strip white space
        std::string value(stream);
        value.erase(value.find_last_not_of(" \n\r\t") + 1);
    
        // Set the environment variable with the value.
        SetEnvironmentVar(variableName, value.c_str());
        return value;
    #endif  // ANGLE_PLATFORM_ANDROID
        // Return the environment variable's value.
        return GetEnvironmentVar(variableName);
    }
    
    bool PrependPathToEnvironmentVar(const char *variableName, const char *path)
    {
        std::string oldValue = GetEnvironmentVar(variableName);
        const char *newValue = nullptr;
        std::string buf;
        if (oldValue.empty())
        {
            newValue = path;
        }
        else
        {
            buf = path;
            buf += GetPathSeparatorForEnvironmentVar();
            buf += oldValue;
            newValue = buf.c_str();
        }
        return SetEnvironmentVar(variableName, newValue);
    }
    }  // namespace angle