Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Sohns committed Apr 6, 2022
1 parent aabe11a commit e910a3c
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 61 deletions.
6 changes: 4 additions & 2 deletions src/common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@
#endif // ACE_WIN32 || ACE_WIN64

#define COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY "etc"
#define COMMON_LOCATION_DATA_SUBDIRECTORY "data"
#define COMMON_LOCATION_DATA_SUBDIRECTORY "share"
#define COMMON_LOCATION_PARENT_SUBDIRECTORY ".."
#define COMMON_LOCATION_SCRIPTS_SUBDIRECTORY "scripts"
#define COMMON_LOCATION_SOURCE_SUBDIRECTORY "src"
#define COMMON_LOCATION_TEST_I_SUBDIRECTORY "test_i"
#define COMMON_LOCATION_TEST_U_SUBDIRECTORY "test_u"

// // software

Expand Down Expand Up @@ -220,7 +222,7 @@
#endif // ACE_WIN32 || ACE_WIN64
#define COMMON_ENVIRONMENT_DIRECTORY_ROOT_ACE "ACE_ROOT"
#define COMMON_ENVIRONMENT_DIRECTORY_ROOT_LIB "LIB_ROOT"
#define COMMON_ENVIRONMENT_DIRECTORY_ROOT_PROJECTS "PROJECTS_ROOT"
#define COMMON_ENVIRONMENT_DIRECTORY_ROOT_PROJECTS "PRJ_ROOT"

// *** state machine ***
#define COMMON_STATEMACHINE_THREAD_GROUP_ID 110
Expand Down
153 changes: 118 additions & 35 deletions src/common_file_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "common_defines.h"
#include "common_macros.h"
#include "common_string_tools.h"
#include "common_tools.h"

#if defined (ACE_WIN32) || defined (ACE_WIN64)
Expand All @@ -50,7 +51,8 @@
#endif // HAVE_CONFIG_H

// initialize statics
std::string Common_File_Tools::executableBase = ACE_TEXT_ALWAYS_CHAR("");
std::string Common_File_Tools::executable = ACE_TEXT_ALWAYS_CHAR ("");
std::string Common_File_Tools::executableBase = ACE_TEXT_ALWAYS_CHAR ("");

void
Common_File_Tools::initialize (const std::string& argv0_in)
Expand All @@ -60,6 +62,9 @@ Common_File_Tools::initialize (const std::string& argv0_in)
// sanity check(s)
ACE_ASSERT (Common_File_Tools::isValidFilename (argv0_in));

Common_File_Tools::executable =
ACE_TEXT_ALWAYS_CHAR (ACE::basename (argv0_in.c_str (), ACE_DIRECTORY_SEPARATOR_CHAR));

Common_File_Tools::executableBase =
ACE_TEXT_ALWAYS_CHAR (ACE::dirname (argv0_in.c_str(), ACE_DIRECTORY_SEPARATOR_CHAR));
ACE_ASSERT (Common_File_Tools::isDirectory (Common_File_Tools::executableBase));
Expand Down Expand Up @@ -1808,6 +1813,23 @@ Common_File_Tools::realPath (const std::string& path_in)
return return_value;
}

bool
Common_File_Tools::setWorkingDirectory (const std::string& path_in)
{
COMMON_TRACE (ACE_TEXT ("Common_File_Tools::setWorkingDirectory"));

int result = chdir (path_in.c_str ());
if (unlikely (result == -1))
{
ACE_DEBUG ((LM_ERROR,
ACE_TEXT ("failed to ::chdir(\"%s\"): %m, aborting\n"),
ACE_TEXT (path_in.c_str ())));
return false;
} // end IF

return true;
}

std::string
Common_File_Tools::getWorkingDirectory ()
{
Expand Down Expand Up @@ -1883,34 +1905,18 @@ Common_File_Tools::getSourceDirectory (const std::string& packageName_in,
}

std::string
Common_File_Tools::getConfigurationDataDirectory (const std::string& packageName_in,
const std::string& moduleName_in,
bool isConfiguration_in)
Common_File_Tools::getSystemConfigurationDataDirectory (const std::string& packageName_in,
const std::string& moduleName_in,
bool isConfiguration_in)
{
COMMON_TRACE (ACE_TEXT ("Common_File_Tools::getConfigurationDataDirectory"));
COMMON_TRACE (ACE_TEXT ("Common_File_Tools::getSystemConfigurationDataDirectory"));

// initialize return value(s)
std::string return_value;

// sanity check(s)
ACE_ASSERT (!packageName_in.empty ());

#if defined (DEBUG_DEBUGGER)
return_value = Common_File_Tools::getSourceDirectory (packageName_in,
moduleName_in);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_DATA_SUBDIRECTORY));
#elif defined (BASEDIR)
return_value = ACE_TEXT_ALWAYS_CHAR (BASEDIR);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value += packageName_in;
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_DATA_SUBDIRECTORY));
#else
#if defined (ACE_WIN32) || defined (ACE_WIN64)
#if defined (UNICODE)
#if defined (ACE_USES_WCHAR)
Expand All @@ -1932,14 +1938,14 @@ Common_File_Tools::getConfigurationDataDirectory (const std::string& packageName

HRESULT result_2 =
#if defined (UNICODE)
SHGetFolderPathW (NULL, // hwndOwner
SHGetFolderPathW (NULL, // hwndOwner
#else
SHGetFolderPathA (NULL, // hwndOwner
SHGetFolderPathA (NULL, // hwndOwner
#endif // UNICODE
CSIDL_APPDATA | CSIDL_FLAG_DONT_VERIFY, // nFolder
NULL, // hToken
SHGFP_TYPE_CURRENT, // dwFlags
buffer_a); // pszPath
CSIDL_LOCAL_APPDATA | CSIDL_FLAG_DONT_VERIFY, // nFolder
NULL, // hToken
SHGFP_TYPE_CURRENT, // dwFlags
buffer_a); // pszPath
if (unlikely (FAILED (result_2)))
{
ACE_DEBUG ((LM_WARNING,
Expand All @@ -1957,27 +1963,104 @@ Common_File_Tools::getConfigurationDataDirectory (const std::string& packageName
#else
return_value =
ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_APPLICATION_STORAGE_ROOT_DIRECTORY);
// *TODO*: support 'local' installations (i.e. /usr/local/...)
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_APPLICATION_STORAGE_LOCAL_SUBDIRECTORY);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_APPLICATION_STORAGE_SUBDIRECTORY));
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value += packageName_in;
if (!moduleName_in.empty ())
{
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value += moduleName_in;
} // end IF
#endif // ACE_WIN32 || ACE_WIN64
#endif // DEBUG_DEBUGGER || BASEDIR

return return_value;
}

std::string
Common_File_Tools::getConfigurationDataDirectory (const std::string& packageName_in,
const std::string& moduleName_in,
bool isConfiguration_in)
{
COMMON_TRACE (ACE_TEXT ("Common_File_Tools::getConfigurationDataDirectory"));

// initialize return value(s)
std::string return_value;

// sanity check(s)
if (unlikely (!Common_File_Tools::isDirectory (return_value)))
ACE_ASSERT (!packageName_in.empty ());

bool is_test_b =
(!ACE_OS::strcmp (moduleName_in.c_str (), ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_TEST_I_SUBDIRECTORY)) ||
!ACE_OS::strcmp (moduleName_in.c_str (), ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_TEST_U_SUBDIRECTORY)));

if (Common_Error_Tools::inDebugSession ())
{
ACE_DEBUG ((LM_WARNING,
ACE_TEXT ("not a directory (was: \"%s\"), falling back\n"),
ACE_TEXT (return_value.c_str ())));
if (!is_test_b)
{
return_value = Common_File_Tools::getSourceDirectory (packageName_in,
moduleName_in);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_DATA_SUBDIRECTORY));
} // end IF
else
{
// sanity check(s)
ACE_ASSERT (!Common_File_Tools::executable.empty ());

return_value =
Common_File_Tools::getSourceDirectory (packageName_in,
ACE_TEXT_ALWAYS_CHAR (""));
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_PARENT_SUBDIRECTORY);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value += moduleName_in;
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
Common_String_Tools::tolower (Common_File_Tools::basename (Common_File_Tools::executable, true));
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_DATA_SUBDIRECTORY));
} // end ELSE
// sanity check(s)
ACE_ASSERT (Common_File_Tools::isDirectory (return_value));

// fallback
return Common_File_Tools::getWorkingDirectory ();
return return_value;
} // end IF

// not running in a debug session

#if defined (ACE_WIN32) || defined (ACE_WIN64)
return_value = Common_File_Tools::getWorkingDirectory ();
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_PARENT_SUBDIRECTORY);
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
(isConfiguration_in ? ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_CONFIGURATION_SUBDIRECTORY)
: ACE_TEXT_ALWAYS_CHAR (COMMON_LOCATION_DATA_SUBDIRECTORY));
return_value += ACE_DIRECTORY_SEPARATOR_STR;
return_value +=
Common_String_Tools::tolower (Common_File_Tools::basename (Common_File_Tools::executable, true));
#else
return_value =
Common_File_Tools::getSystemConfigurationDataDirectory (packageName_in,
moduleName_in,
isConfiguration_in);
#endif // ACE_WIN32 || ACE_WIN64

// sanity check(s)
ACE_ASSERT (Common_File_Tools::isDirectory (return_value));

return return_value;
}

Expand Down
19 changes: 9 additions & 10 deletions src/common_file_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,11 @@ class Common_File_Tools
// this defaults to $PROJECTS_ROOT/$PACKAGENAME/src
static std::string getSourceDirectory (const std::string&, // package name
const std::string&); // module name
// *PORTABILITY*: this can be influenced by #define BASEDIR and returns
// BASEDIR/$PACKAGENAME/xxx
// - on UNIX this defaults to /usr/[local]/[share|etc]/$PACKAGENAME,
// depending on distribution (and version; see also: LSB)
// *TODO*: consider enforcing [/var|[/etc||/usr/[local]/etc]]/$PACKAGENAME to
// more effectively untangle sharing of (application-) asset data
// - on WIN32 this defaults to $APPDATA/$PACKAGENAME/xxx
// *NOTE*: iff DEBUG_DEBUGGER is #define'd, this returns
// getSourceDirectory()../$PACKAGENAME/xxx instead
// *NOTE*: returns APPDATA/packagename[/modulename] | /usr/local/etc|share/packagename
static std::string getSystemConfigurationDataDirectory (const std::string&, // package name
const std::string&, // module name
bool); // configuration ? : data
// *NOTE*: depends on whether running in a debug session
static std::string getConfigurationDataDirectory (const std::string&, // package name
const std::string&, // module name
bool); // configuration ? : data
Expand All @@ -162,13 +158,16 @@ class Common_File_Tools
// *NOTE*: (try to) create the directory if it doesn't exist
static std::string getUserConfigurationDirectory ();
static std::string getUserDownloadDirectory (const std::string&); // user name (empty ? current user)

static bool setWorkingDirectory (const std::string&); // path string
static std::string getWorkingDirectory ();

static std::string getTempDirectory ();
static std::string getTempFilename (const std::string&, // prefix
bool = true); // return full path ?

static std::string executableBase;
static std::string executable; // executable base name + extension
static std::string executableBase; // executables' base directory

private:
ACE_UNIMPLEMENTED_FUNC (Common_File_Tools ())
Expand Down
35 changes: 23 additions & 12 deletions test_u/common_test_u_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@
#include "ace/Log_Msg.h"

#include "common_macros.h"
#include "common_tools.h"
#include "common_file_tools.h"

//std::string
//Common_Test_U_Tools::version (const std::string& programName_in)
//{
// COMMON_TRACE (ACE_TEXT ("Common_Test_U_Tools::version"));
//
// std::string result = programName_in;
// result += ACE_TEXT_ALWAYS_CHAR ("");
//
//
// return result;
//}
#include "common_error_tools.h"

bool
Common_Test_U_Tools::initialize ()
{
COMMON_TRACE (ACE_TEXT ("Common_Test_U_Tools::initialize"));

if (likely (!Common_Error_Tools::inDebugSession ()))
{
if (unlikely (!Common_File_Tools::setWorkingDirectory (ACE_TEXT_ALWAYS_CHAR (".."))))
{
ACE_DEBUG ((LM_ERROR,
ACE_TEXT ("failed to Common_File_Tools::setWorkingDirectory(\"..\"), aborting\n")));
return false;
} // end IF
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("set working directory to \"%s\"\n"),
ACE_TEXT (Common_File_Tools::getWorkingDirectory ().c_str ())));
} // end IF

return true;
}
4 changes: 2 additions & 2 deletions test_u/common_test_u_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
class Common_Test_U_Tools
{
public:
static bool initialize ();

template <typename ThreadDataType,
typename CallbackDataType>
static bool spawn (const std::string&, // thread name
Expand All @@ -37,8 +39,6 @@ class Common_Test_U_Tools
const CallbackDataType&, // callback data
ACE_Thread_ID&); // return value: thread id

//static std::string version ();

private:
ACE_UNIMPLEMENTED_FUNC (Common_Test_U_Tools ())
ACE_UNIMPLEMENTED_FUNC (Common_Test_U_Tools (const Common_Test_U_Tools&))
Expand Down

0 comments on commit e910a3c

Please sign in to comment.