LCOV - code coverage report
Current view: top level - zeroeq/detail - common.h (source / functions) Hit Total Coverage
Test: ZeroEQ Lines: 31 38 81.6 %
Date: 2017-12-01 01:44:57 Functions: 6 6 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2014-2017, Human Brain Project
       3             :  *                          Daniel Nachbaur <daniel.nachbaur@epfl.ch>
       4             :  *                          Stefan.Eilemann@epfl.ch
       5             :  */
       6             : 
       7             : #pragma once
       8             : 
       9             : #include "../log.h"
      10             : #include "constants.h"
      11             : 
      12             : #include <zeroeq/uri.h>
      13             : 
      14             : #include <cstring>
      15             : #include <sstream>
      16             : 
      17             : // getlogin()
      18             : #ifdef _MSC_VER
      19             : #define WIN32_LEAN_AND_MEAN
      20             : #include <Lmcons.h>
      21             : #include <windows.h>
      22             : #else
      23             : #include <climits>
      24             : #include <unistd.h>
      25             : #endif
      26             : 
      27             : #if __APPLE__
      28             : #include <dirent.h>
      29             : #include <mach-o/dyld.h>
      30             : #endif
      31             : 
      32             : namespace
      33             : {
      34          69 : inline std::string buildZmqURI(const std::string& schema, std::string host,
      35             :                                const uint16_t port)
      36             : {
      37          69 :     if (host.empty())
      38          31 :         host = "*";
      39             : 
      40         138 :     const std::string zmqURI(schema + "://" + host);
      41          69 :     if (port == 0) // zmq expects host:* instead of host:0
      42          35 :         return zmqURI + ":*";
      43             : 
      44          34 :     return zmqURI + ":" + std::to_string(int(port));
      45             : }
      46             : 
      47          70 : inline std::string buildZmqURI(const zeroeq::URI& uri)
      48             : {
      49          70 :     if (uri.getScheme() == DEFAULT_SCHEMA)
      50          63 :         return buildZmqURI(uri.getScheme(), uri.getHost(), uri.getPort());
      51           7 :     return std::to_string(uri);
      52             : }
      53             : 
      54         136 : inline std::string getUserName()
      55             : {
      56             : #ifdef _MSC_VER
      57             :     char user[UNLEN + 1];
      58             :     DWORD userLength = UNLEN + 1;
      59             :     GetUserName(user, &userLength);
      60             : #else
      61         136 :     const char* user = getlogin();
      62             : #endif
      63         136 :     return user ? user : UNKNOWN_USER;
      64             : }
      65             : 
      66          48 : inline std::string getApplicationName()
      67             : {
      68             : // http://stackoverflow.com/questions/933850
      69             : #ifdef _MSC_VER
      70             :     char result[MAX_PATH];
      71             :     const std::string execPath(result,
      72             :                                GetModuleFileName(NULL, result, MAX_PATH));
      73             : #elif __APPLE__
      74             :     char result[PATH_MAX + 1];
      75             :     uint32_t size = sizeof(result);
      76             :     if (_NSGetExecutablePath(result, &size) != 0)
      77             :         return std::string();
      78             :     const std::string execPath(result);
      79             : #else
      80             :     char result[PATH_MAX];
      81          48 :     const ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
      82          48 :     if (count < 0)
      83             :     {
      84             :         // Not all UNIX have /proc/self/exe
      85           0 :         ZEROEQWARN << "Could not find absolute executable path" << std::endl;
      86           0 :         return std::string();
      87             :     }
      88          96 :     const std::string execPath(result, count > 0 ? count : 0);
      89             : #endif
      90             : 
      91             : #ifdef _MSC_VER
      92             :     const size_t lastSeparator = execPath.find_last_of('\\');
      93             : #else
      94          48 :     const size_t lastSeparator = execPath.find_last_of('/');
      95             : #endif
      96          48 :     if (lastSeparator == std::string::npos)
      97           0 :         return execPath;
      98             :     // lastSeparator + 1 may be at most equal to filename.size(), which is good
      99          48 :     return execPath.substr(lastSeparator + 1);
     100             : }
     101             : 
     102          89 : inline std::string getDefaultPubSession()
     103             : {
     104          89 :     const char* pubSession = getenv(zeroeq::ENV_PUB_SESSION.c_str());
     105          89 :     const char* session = getenv(ENV_SESSION.c_str());
     106          89 :     if (session)
     107             :         ZEROEQWARN << "Found deprecated " << ENV_SESSION
     108           0 :                    << " in environment, please use " << zeroeq::ENV_PUB_SESSION
     109           0 :                    << std::endl;
     110             : 
     111          89 :     if (pubSession && strcmp(pubSession, "") != 0)
     112           0 :         return pubSession;
     113          89 :     if (session && strcmp(session, "") != 0)
     114           0 :         return session;
     115          89 :     return getUserName();
     116             : }
     117             : 
     118           2 : inline std::string getDefaultRepSession()
     119             : {
     120           2 :     const char* session = getenv(zeroeq::ENV_REP_SESSION.c_str());
     121           2 :     return session && strcmp(session, "") != 0 ? session : getApplicationName();
     122             : }
     123             : }

Generated by: LCOV version 1.11