Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C language bindings for SystemPaths::FindFile and Console::SetVerbosity #168

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/ign.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <cstring>

#include <ignition/common/Console.hh>
#include <ignition/common/SystemPaths.hh>

#include "ignition/common/config.hh"
#include "ign.hh"

//////////////////////////////////////////////////
extern "C" IGNITION_COMMON_VISIBLE char* ignitionCommonVersion()
{
return strdup(IGNITION_COMMON_VERSION_FULL);
}

//////////////////////////////////////////////////
extern "C" IGNITION_COMMON_VISIBLE void cmdVerbosity(
const char* _verbosity)
{
ignition::common::Console::SetVerbosity(std::atoi(_verbosity));
}

//////////////////////////////////////////////////
extern "C" IGNITION_COMMON_VISIBLE char* findFileInPathEnv(
const char* _fileName, const char* _envName, const char* _defaultPath)
{
ignition::common::SystemPaths paths;
paths.SetFilePathEnv(_envName);
if (strlen(_defaultPath) > 0)
paths.AddFilePaths(_defaultPath);

const auto foundPath = paths.FindFile(_fileName, false);
return strdup(ignition::common::copyFromUnixPath(foundPath).c_str());
}
43 changes: 43 additions & 0 deletions src/ign.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_COMMON_IGN_HH_
#define IGNITION_COMMON_IGN_HH_

#include "ignition/common/Export.hh"

/// \brief External hook to read the library version.
/// \return C-string representing the version. Ex.: 0.1.2
extern "C" IGNITION_COMMON_VISIBLE char* ignitionCommonVersion();

/// \brief Set console verbosity of the other commands in this library.
/// \param[in] _verbosity 0 to 4
extern "C" IGNITION_COMMON_VISIBLE void cmdVerbosity(
const char* _verbosity);

/// \brief Set console verbosity of the other commands in this library.
/// \param[in] _fileName Name of the searched file.
/// \param[in] _envName Name of the environment variable that holds additional
/// search paths. The content of the variable is treated as a colon/semicolon
/// separated list.
/// \param[in] _defaultPath Other directories to search (searched after those
/// extracted from the environment variable). A colon/semicolon separated list.
/// \return Path to the found existing file, or empty string if the file could
/// not be found. The path will use native separators.
extern "C" IGNITION_COMMON_VISIBLE char* findFileInPathEnv(
const char* _fileName, const char* _envName, const char* _defaultPath = "");

#endif
100 changes: 100 additions & 0 deletions src/ign_TEST.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include <gtest/gtest.h>
#include <cstdio>
#include <cstdlib>

#include <string>
#include <ign.hh>
#include <ignition/common/Console.hh>
#include <ignition/common/Util.hh>

using namespace ignition::common;

/////////////////////////////////////////////////
TEST(CBindings, Verbosity)
{
cmdVerbosity("4");
EXPECT_EQ(4, Console::Verbosity());

cmdVerbosity("3");
EXPECT_EQ(3, Console::Verbosity());

cmdVerbosity("2");
EXPECT_EQ(2, Console::Verbosity());

cmdVerbosity("1");
EXPECT_EQ(1, Console::Verbosity());
}

/////////////////////////////////////////////////
TEST(CBindings, FindFile)
{
const auto kEnvName = "IGN_TEST_PATH";
const auto thisFilePath = __FILE__;
const auto thisFile = ignition::common::basename(thisFilePath);
const auto thisDir = parentPath(thisFilePath);
const auto projectDir = parentPath(thisDir);

setenv(kEnvName, thisDir);
auto path = findFileInPathEnv(thisFile.c_str(), kEnvName);
EXPECT_STREQ(thisFilePath, path);

setenv(kEnvName, thisDir + SystemPaths::Delimiter() + "foo");
path = findFileInPathEnv(thisFile.c_str(), kEnvName);
EXPECT_STREQ(thisFilePath, path);

setenv(kEnvName, std::string("foo") + SystemPaths::Delimiter() + thisDir);
path = findFileInPathEnv(thisFile.c_str(), kEnvName);
EXPECT_STREQ(thisFilePath, path);

setenv(kEnvName, "foo");
path = findFileInPathEnv(thisFile.c_str(), kEnvName, thisDir.c_str());
EXPECT_STREQ(thisFilePath, path);

setenv(kEnvName, thisDir);
path = findFileInPathEnv(thisFile.c_str(), kEnvName, "foo");
EXPECT_STREQ(thisFilePath, path);

// empty search path means the file should not be found
setenv(kEnvName, "");
path = findFileInPathEnv(thisFile.c_str(), kEnvName);
EXPECT_STREQ("", path);

const auto thisCMakeLists = joinPaths(thisDir, "CMakeLists.txt");
const auto projectCMakeLists = joinPaths(projectDir, "CMakeLists.txt");
ASSERT_TRUE(exists(thisCMakeLists));
ASSERT_TRUE(exists(projectCMakeLists));

// paths from environment take precedence over default paths
setenv(kEnvName, thisDir);
path = findFileInPathEnv("CMakeLists.txt", kEnvName, projectDir.c_str());
EXPECT_STREQ(thisCMakeLists.c_str(), path);

// paths from environment are searched left to right
setenv(kEnvName, thisDir + SystemPaths::Delimiter() + projectDir);
path = findFileInPathEnv("CMakeLists.txt", kEnvName);
EXPECT_STREQ(thisCMakeLists.c_str(), path);
}

/////////////////////////////////////////////////
int main(int _argc, char **_argv)
{
::testing::InitGoogleTest(&_argc, _argv);
return RUN_ALL_TESTS();
}