示例#1
0
// The LoadSet Functions will update the static values.
const bool OTPaths::LoadSetPrefixFolder	// eg. /usr/local/  
	(	
	OTSettings * pConfig,	//optional
	const OTString & strPrefixFolder				//optional
	//const bool & bIsRelative (cannot be relative);
	)
{
	/*
	The prefix path is special.

	This path is tested if it is different to the
	one that would be automatically selected by this program
	(aka either compiled into, or from the registry, or the default user data directory).

	If the set path is different to what would be supplied and the ‘override path’ value is set.
	Then we will use that path.

	Otherwise, we will update the path in the configuration to link against the updated path.

	Users will need to set the ‘override path’ flag in the configuration,
	if they want to manually set the prefix path.
	*/

	if (NULL == pConfig)  { OT_ASSERT(false); return false; }

	const bool bPreLoaded(pConfig->IsLoaded());

	if (!bPreLoaded)
	{
		pConfig->Reset();
		if(!pConfig->Load()) { OT_ASSERT(false); return false; }
	}

	// get default path
	OTString strDefaultPrefixPath(OT_PREFIX_PATH);

#ifdef _WIN32
	OTString strTemp;
	if (OTPaths::Win_GetInstallFolderFromRegistry(strTemp))
	{
		strDefaultPrefixPath = strTemp;
	}
#endif

	// now check the configuration to see what values we have:
	OTString strConfigPath = "";
	bool bNewPath = false;
	bool bHaveRelativePath = false; // should always be false.
	bool bPrefixPathOverride = false;
	bool bNoOverrideFlagWasSet = false;

	if(!pConfig->CheckSet_str("paths","prefix_path",strDefaultPrefixPath,strConfigPath,bNewPath)) { return false; }

	OTString strPrefixPathOverride("prefix_path_override");
	if(!pConfig->CheckSet_bool("paths",strPrefixPathOverride,false,bPrefixPathOverride,bNoOverrideFlagWasSet,"; Set this if you don't want this path to change")) {return false; }

	OTString strLocalPrefixPath = "";

	// if the caller has supplied a prefix folder, lets set that.
	if(strPrefixFolder.Exists() && (3 < strPrefixFolder.GetLength()))
	{
		if (!strConfigPath.Compare(strPrefixFolder))
		{
			// we set the new path (from this function caller)
			bool bNewOrUpdate = false;
			if(!pConfig->Set_str("paths","prefix_path",strPrefixFolder,bNewOrUpdate)) { return false; }
		}
		strLocalPrefixPath = strPrefixFolder; // set
	}
	else
	{
		// we should update the path
		if (!bPrefixPathOverride)
		{
			if (!strConfigPath.Compare(strDefaultPrefixPath))
			{
				// we set the new default path (since we have no overide set)
				bool bNewOrUpdate = false;
				if(!pConfig->Set_str("paths","prefix_path",strDefaultPrefixPath,bNewOrUpdate)) { return false; }
			}
			strLocalPrefixPath = strDefaultPrefixPath; // set
		}
		else
		{
			strLocalPrefixPath = strConfigPath; // set
		}
	}

	if (!strLocalPrefixPath.Exists()) { OT_ASSERT(false); }

	if(!ToReal(strLocalPrefixPath,strLocalPrefixPath)) { OT_ASSERT(false); return false; }
	if(!FixPath(strLocalPrefixPath,strLocalPrefixPath,true)) { OT_ASSERT(false); return false; }

	m_strPrefixFolder = strLocalPrefixPath;

	if (!bPreLoaded)
	{
		if(!pConfig->Save()) { OT_ASSERT(false); return false; }
		pConfig->Reset();
	}
	return true;
}
示例#2
0
// The LoadSet Functions will update the static values.
// static
bool OTPaths::LoadSetPrefixFolder  // eg. /usr/local/
    (OTSettings& config,           // optional
     const String& strPrefixFolder // optional
     // const bool& bIsRelative (cannot be relative);
     )
{
    /*
    The prefix path is special.

    This path is tested if it is different to the
    one that would be automatically selected by this program
    (aka either compiled into, or from the registry, or the default user data
    directory).

    If the set path is different to what would be supplied and the override path
    value is set.
    Then we will use that path.

    Otherwise, we will update the path in the configuration to link against the
    updated path.

    Users will need to set the override path flag in the configuration,
    if they want to manually set the prefix path.
    */

    if (&config == &s_settings) ConfigureDefaultSettings();

    const bool bPreLoaded(config.IsLoaded());

    if (!bPreLoaded) {
        config.Reset();
        if (!config.Load()) {
            OT_FAIL;
        }
    }

    {
        // get default path
        String strDefaultPrefixPath(OT_PREFIX_PATH);
        {
            if (!strDefaultPrefixPath.Exists()) {
                otErr << __FUNCTION__ << ": Error: OT_PREFIX_PATH is not set!";
                OT_FAIL;
            }

#ifdef _WIN32
            String strTemp;
            if (OTPaths::Win_GetInstallFolderFromRegistry(strTemp)) {
                strDefaultPrefixPath = strTemp;
            }
#endif

            if (!ToReal(strDefaultPrefixPath, strDefaultPrefixPath)) {
                OT_FAIL;
            }
            if (!FixPath(strDefaultPrefixPath, strDefaultPrefixPath, true)) {
                OT_FAIL;
            }
        }

        String strLocalPrefixPath = "";
        bool bPrefixPathOverride = false;

        {
            // now check the configuration to see what values we have:
            String strConfigPath = "";

            bool bIsNew = false;
            String strPrefixPathOverride("prefix_path_override");

            if (!config.CheckSet_str("paths", "prefix_path",
                                     strDefaultPrefixPath, strConfigPath,
                                     bIsNew)) {
                return false;
            }
            if (!config.CheckSet_bool(
                    "paths", strPrefixPathOverride, false, bPrefixPathOverride,
                    bIsNew, "; This will force the prefix not to change")) {
                return false;
            }

            // if the config dosn't have a prefix path set. Lets set the
            // default.
            // if a prefix path was passed in, we will override with that later.
            if (!strConfigPath.Exists() || (3 > strConfigPath.GetLength())) {
                otErr << __FUNCTION__ << ": Error: Bad "
                      << "prefix_path"
                      << " in config, will reset!";

                strConfigPath = strDefaultPrefixPath; // set
                bPrefixPathOverride = false;

                // lets set the default path, and reset override
                bool bNewOrUpdate = false;
                if (!config.Set_str("paths", "prefix_path",
                                    strDefaultPrefixPath, bNewOrUpdate)) {
                    return false;
                }
                if (!config.Set_bool("paths", strPrefixPathOverride, false,
                                     bNewOrUpdate)) {
                    return false;
                }
            }

            strLocalPrefixPath = strConfigPath;
        }

        {
            if (!bPrefixPathOverride) {
                bool bUpdate = false;

                // default
                if (!strLocalPrefixPath.Compare(strDefaultPrefixPath)) {
                    strLocalPrefixPath = strDefaultPrefixPath;
                    bUpdate = true;
                }

                // passed in
                if (strPrefixFolder.Exists() &&
                    (3 < strPrefixFolder.GetLength())) {
                    // a prefix folder was passed in... lets use it, and update
                    // the config if the override isn't set
                    String strTmp = strPrefixFolder;

                    if (!ToReal(strTmp, strTmp)) {
                        OT_FAIL;
                    }

                    if (!FixPath(strTmp, strTmp, true)) {
                        OT_FAIL;
                    }

                    if (!strLocalPrefixPath.Compare(strTmp)) {
                        strLocalPrefixPath = strTmp;
                        bUpdate = true;
                    }
                }

                // we need to update the path in the config
                if (bUpdate) {
                    bool bNewOrUpdate = false;
                    if (!config.Set_str("paths", "prefix_path",
                                        strLocalPrefixPath, bNewOrUpdate)) {
                        return false;
                    }
                }
            }
        }

        {
            if (!strLocalPrefixPath.Exists()) {
                OT_FAIL;
            }

            if (!ToReal(strLocalPrefixPath, strLocalPrefixPath)) {
                OT_FAIL;
            }
            if (!FixPath(strLocalPrefixPath, strLocalPrefixPath, true)) {
                OT_FAIL;
            }
            s_strPrefixFolder = strLocalPrefixPath;
        }
    }

    if (!bPreLoaded) {
        if (!config.Save()) {
            OT_FAIL;
        }
        config.Reset();
    }
    return true;
}