LCOV - code coverage report
Current view: top level - ugbase/bridge/misc_bridges - util_bridge.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.7 % 111 84
Test Date: 2025-09-21 23:31:46 Functions: 9.1 % 11 1

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2011-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Sebastian Reiter
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : #include "../util_overloaded.h"
      34              : #include "ug.h"
      35              : #include "bridge/bridge.h"
      36              : #include "common/stopwatch.h"
      37              : #include "common/util/file_util.h"
      38              : #include "common/util/path_provider.h"
      39              : #include "common/util/table.h"
      40              : #include "common/util/variant.h"
      41              : #include "registry/registry.h"
      42              : #if defined (__APPLE__) || defined (__linux__)
      43              :         #include "common/util/mem_info.h"
      44              : #endif
      45              : 
      46              : #include <cstdlib>
      47              : #include <string>
      48              : 
      49              : using namespace std;
      50              : 
      51              : namespace ug{
      52              : 
      53              : // determines when to show the progress bar
      54              : extern size_t g_minSecondUntilProgress;
      55              : 
      56              : namespace bridge{
      57              : 
      58              : /// \defgroup util_bridge Utility Bridge
      59              : /// \ingroup misc_bridge
      60              : /// \{
      61              : 
      62            0 : static string GetRootPath()
      63            0 : {return PathProvider::get_path(ROOT_PATH);}
      64              : 
      65            0 : static string GetAppsPath()
      66            0 : {return PathProvider::get_path(APPS_PATH);}
      67              : 
      68            0 : static string GetBinPath()
      69            0 : {return PathProvider::get_path(BIN_PATH);}
      70              : 
      71            0 : static string GetScriptPath()
      72            0 : {return PathProvider::get_path(SCRIPT_PATH);}
      73              : 
      74            0 : static string GetCurrentPath()
      75            0 : {return PathProvider::get_current_path();}
      76              : 
      77              : /**
      78              :  * This function executes the given string in a system shell (if available)
      79              :  *
      80              :  * \param[in]   cmd             The shell command to execute
      81              :  * \returns     1               if no shell available
      82              :  */
      83            0 : static int ExecuteSystemCommand(const char* cmd)
      84              : {
      85              : //      check that some shell exists
      86            0 :         if(!system(NULL)) return 1;
      87              : 
      88              : //      run the command
      89            0 :         return system(cmd);
      90              : }
      91              : 
      92            0 : void int_srand(int seed)
      93              : {
      94            0 :         srand((unsigned int)seed);
      95            0 : }
      96              : 
      97            0 : void SetMinSecondsUntilProgress(size_t s)
      98              : {
      99            0 :         g_minSecondUntilProgress=s;
     100            0 : }
     101              : 
     102            0 : std::vector<std::string> GetFilesInDir(const char* dir)
     103              : {
     104              :         std::vector<std::string> files;
     105            0 :         GetFilesInDirectory(files, dir);
     106            0 :         return files;
     107            0 : }
     108              : 
     109            0 : std::vector<std::string> GetDirsInDir(const char* dir)
     110              : {
     111              :         std::vector<std::string> dirs;
     112            0 :         GetDirectoriesInDirectory(dirs, dir);
     113            0 :         return dirs;
     114            0 : }
     115              : 
     116            1 : void RegisterBridge_Util(Registry& reg, string parentGroup)
     117              : {
     118              :         string grp(parentGroup);
     119            1 :         grp.append("/Util");
     120              : 
     121            3 :         reg.add_function("ug_get_root_path", &GetRootPath, grp,
     122              :                          "pathName", "", "Returns ug's root path");
     123              : 
     124            3 :         reg.add_function("ug_get_apps_path", &GetAppsPath, grp,
     125              :                          "pathName", "", "Returns the path in which ug's apps are stored");
     126              : 
     127            3 :         reg.add_function("ug_get_bin_path", &GetBinPath, grp,
     128              :                          "pathName", "", "Returns the path in which the ug executable lies");
     129              : 
     130            3 :         reg.add_function("ug_get_script_path", &GetScriptPath, grp,
     131              :                          "pathName", "", "Returns the script path");
     132              : 
     133            3 :         reg.add_function("ug_get_current_path", &GetCurrentPath, grp,
     134              :                          "pathName", "", "Returns the current path");
     135              : 
     136            3 :         reg.add_function("ug_set_root_path", static_cast<void(*)(const std::string&)>(&SetRootPath), grp,
     137              :                          "", "pathName", "Sets the paths relative to passed root path");
     138              : 
     139            3 :         reg.add_function("ug_set_script_path", static_cast<void(*)(const std::string&)>(&SetScriptPath), grp,
     140              :                          "", "pathName", "Sets the script path");
     141              : 
     142            3 :         reg.add_function("ug_set_apps_path", static_cast<void(*)(const std::string&)>(&SetAppsPath), grp,
     143              :                          "", "pathName", "Sets the script path");
     144              : 
     145            3 :         reg.add_function("ug_set_plugin_path", static_cast<void(*)(const std::string&)>(&SetPluginPath), grp,
     146              :                          "", "pathName", "Sets the plugin path");
     147              : 
     148            3 :         reg.add_function("ExecuteSystemCommand", &ExecuteSystemCommand, grp,
     149              :                          "success", "command", "Executes a command in the system shell");
     150              : 
     151            3 :         reg.add_function("srand", int_srand, grp, 
     152              :                          "", "seed", "The pseudo-random number generator is initialized using the argument passed as seed.")
     153            3 :                 .add_function("ug_file_exists", OVERLOADED_FUNCTION_PTR(bool, FileExists, (const char*)), grp,
     154              :                           "exists", "", "Returns true if a path exists, false if not.")
     155            3 :                 .add_function("exit", &UGForceExit, grp,
     156              :                           "", "", "Immediatly terminates the application.")
     157            3 :                 .add_function("quit", &UGForceExit, grp,
     158              :                           "", "", "Immediatly terminates the application.");
     159              : 
     160            2 :         reg.add_function("CreateDirectory", static_cast<bool (*)(const char *) > (&CreateDirectoryTMP) );
     161            2 :         reg.add_function("DirectoryExists", static_cast<bool (*)(const char *) > (&DirectoryExists) );
     162            2 :         reg.add_function("FileExists", static_cast<bool (*)(const char *) > (&FileExists) );
     163            2 :         reg.add_function("GetFilesInDir", GetFilesInDir);
     164            2 :         reg.add_function("GetDirsInDir", GetDirsInDir);
     165            2 :         reg.add_function("GetTmpPath", GetTmpPath);
     166            2 :         reg.add_function("ChangeDirectory", ChangeDirectory);
     167            2 :         reg.add_function("CurrentWorkingDirectory", CurrentWorkingDirectory);
     168            2 :         reg.add_function("FileCompare", FileCompare);
     169            3 :         reg.add_function("SetMinSecondsUntilProgress", SetMinSecondsUntilProgress, grp, "", "seconds", "determines after which time a progress bar can show up");
     170              : 
     171            2 :         reg.add_function("FindFileInStandardPaths", FindFileInStandardPaths);
     172              : 
     173              :         {
     174              :                 typedef Variant T;
     175            3 :                 reg.add_class_<T>("Variant", grp)
     176            1 :                 .add_constructor()
     177            2 :                 .add_constructor<void (*)(bool)>()
     178            2 :                 .add_constructor<void (*)(int)>()
     179            2 :                 .add_constructor<void (*)(size_t)>()
     180            2 :                 .add_constructor<void (*)(float)>()
     181            2 :                 .add_constructor<void (*)(double)>()
     182            2 :                 .add_constructor<void (*)(const char*)>()
     183            2 :                 .add_constructor<void (*)(const Variant&)>()
     184            2 :                 .add_method("to_bool", &T::to_bool)
     185            2 :                 .add_method("to_int", &T::to_int)
     186            2 :                 .add_method("to_size_t", &T::to_size_t)
     187            2 :                 .add_method("to_float", &T::to_float)
     188            2 :                 .add_method("to_double", &T::to_double)
     189            2 :                 .add_method("to_number", &T::to_number)
     190            2 :                 .add_method("to_string", &T::to_c_string)
     191            1 :                 .construct_as_smart_pointer();
     192              :         }
     193              : 
     194              :         {
     195              :                 typedef StringTable T;
     196            3 :                 reg.add_class_<T>("StringTable", grp)
     197            1 :                 .add_constructor()
     198            2 :                 .add_constructor< void (*) ( size_t numRows, size_t numCols) > ()
     199            2 :                 .add_method("set", &T::set)
     200            2 :                 .add_method("get", &T::get)
     201            2 :                 .add_method("add_rows", &T::add_rows)
     202            2 :                 .add_method("add_cols", &T::add_cols)
     203            2 :                 .add_method("num_rows", &T::num_rows)
     204            2 :                 .add_method("num_cols", &T::num_cols)
     205            2 :                 .add_method("set_col_alignment", &T::set_col_alignment)
     206            2 :                 .add_method("set_col_alignments", &T::set_col_alignments)
     207            2 :                 .add_method("set_default_col_alignment", &T::set_default_col_alignment)
     208              : 
     209            2 :                 .add_method("set_row_seperator", &T::set_row_seperator)
     210            2 :                 .add_method("set_row_seperators", &T::set_row_seperators)
     211            2 :                 .add_method("set_default_row_seperator", &T::set_default_row_seperator)
     212              : 
     213            2 :                 .add_method("set_col_seperator", &T::set_col_seperator)
     214            2 :                 .add_method("set_col_seperators", &T::set_col_seperators)
     215            2 :                 .add_method("set_default_col_seperator", &T::set_default_col_seperator)
     216              : 
     217            2 :                 .add_method("to_latex", &T::to_latex)
     218            2 :                 .add_method("to_csv", &T::to_csv)
     219            2 :                 .add_method("__tostring", &T::to_string)
     220              : 
     221            2 :                 .add_method("transpose", &T::transpose)
     222              :                 
     223            1 :                 .construct_as_smart_pointer();
     224              :         }
     225              : 
     226              :         {
     227              :                 // Matlab like stop watch
     228              :                  typedef CuckooClock T;
     229            3 :                  reg.add_class_<T>("CuckooClock", grp)
     230            1 :                         .add_constructor()
     231            2 :                     .add_method("tic", &T::tic)
     232            2 :                     .add_method("toc", &T::toc)
     233            2 :                     .add_method("cuckoo", &T::cuckoo);
     234              :         }
     235              : 
     236              : #if defined (__APPLE__) || defined (__linux__)
     237              :         // MemInfo provides information about memory usage
     238              :         {
     239              :                 typedef MemInfo T;
     240            1 :                 string name = string("MemInfo");
     241            3 :                 reg.add_class_<T>(name, grp)
     242            1 :                         .add_constructor()
     243            2 :                         .add_method("memory_consumption", &T::memory_consumption, "", "", "")
     244            2 :                         .add_method("local_resident_memory", &T::local_resident_memory, "", "", "")
     245            2 :                         .add_method("local_virtual_memory", &T::local_virtual_memory, "", "", "")
     246            2 :                         .add_method("global_resident_memory", &T::global_resident_memory, "", "", "")
     247            2 :                         .add_method("global_virtual_memory", &T::global_virtual_memory, "", "", "")
     248            2 :                         .add_method("max_resident_memory", &T::max_resident_memory, "", "", "")
     249            2 :                         .add_method("max_virtual_memory", &T::max_virtual_memory, "", "", "")
     250              : 
     251            1 :                         .set_construct_as_smart_pointer(true);
     252              :         }
     253              : #endif
     254              : 
     255            1 : }
     256              : 
     257              : // end group util_bridge
     258              : /// \}
     259              : 
     260              : }// end of namespace bridge
     261              : }// end of namespace ug
        

Generated by: LCOV version 2.0-1