Doxygen
Loading...
Searching...
No Matches
portable.cpp File Reference
#include "portable.h"
#include <cctype>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include "dir.h"
#include "dstring.h"
#include "fileinfo.h"
#include "message.h"
#include "util.h"
#include "debug.h"
#include "filesystem.hpp"
Include dependency graph for portable.cpp:

Go to the source code of this file.

Classes

class  SysTimeKeeper
struct  SysTimeKeeper::TimeData
class  AutoTimeKeeper

Functions

void loadEnvironment ()
static bool ExistsOnPath (const DString &fileName)
static const char * portable_memmem (const char *haystack, size_t haystack_len, const char *needle, size_t needle_len)

Variables

char ** environ
static bool environmentLoaded = false
static std::map< std::string, std::string > proc_env = std::map<std::string,std::string>()

Function Documentation

◆ ExistsOnPath()

bool ExistsOnPath ( const DString & fileName)
static

Definition at line 408 of file portable.cpp.

409{
410 FileInfo fi1(fileName.str());
411 if (fi1.exists()) return true;
412
413 DString paths = Portable::getenv("PATH");
414 char listSep = Portable::pathListSeparator()[0];
415 char pathSep = Portable::pathSeparator()[0];
416 size_t strt = 0;
417 size_t idx;
418 while ((idx = paths.find(listSep,strt)) != DString::npos)
419 {
420 DString locFile(paths.mid(strt,idx-strt));
421 locFile += pathSep;
422 locFile += fileName;
423 FileInfo fi(locFile.str());
424 if (fi.exists()) return true;
425 strt = idx + 1;
426 }
427 // to be sure the last path component is checked as well
428 DString locFile(paths.mid(strt));
429 if (!locFile.empty())
430 {
431 locFile += pathSep;
432 locFile += fileName;
433 FileInfo fi(locFile.str());
434 if (fi.exists()) return true;
435 }
436 return false;
437}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
const std::string & str() const
Definition dstring.h:645
Minimal replacement for QFileInfo.
Definition fileinfo.h:26
DString getenv(const DString &variable)
Definition portable.cpp:337
DString pathSeparator()
Definition portable.cpp:390
DString pathListSeparator()
Definition portable.cpp:399

References DString::empty(), FileInfo::exists(), DString::find(), Portable::getenv(), DString::mid(), DString::npos, Portable::pathListSeparator(), Portable::pathSeparator(), and DString::str().

Referenced by Portable::checkForExecutable(), and Portable::ghostScriptCommand().

◆ loadEnvironment()

void loadEnvironment ( )

Definition at line 276 of file portable.cpp.

277{
278 if(environ != nullptr)
279 {
280 unsigned int i = 0;
281 char* current = environ[i];
282
283 while(current != nullptr) // parse all strings contained by environ til the last element (nullptr)
284 {
285 std::string env_var(current); // load current environment variable string
286 size_t pos = env_var.find("=");
287 if(pos != std::string::npos) // only parse the variable, if it is a valid environment variable...
288 { // ...which has to contain an equal sign as delimiter by definition
289 std::string name = env_var.substr(0,pos); // the string til the equal sign contains the name
290 std::string value = env_var.substr(pos + 1); // the string from the equal sign contains the value
291 proc_env[name] = std::move(value); // save the value by the name as its key in the classes map
292 }
293 i++;
294 current = environ[i];
295 }
296 }
297
298 environmentLoaded = true;
299}
static bool environmentLoaded
Definition portable.cpp:53
static std::map< std::string, std::string > proc_env
Definition portable.cpp:54
char ** environ

References environ, environmentLoaded, and proc_env.

Referenced by Portable::getenv(), and Portable::setenv().

◆ portable_memmem()

const char * portable_memmem ( const char * haystack,
size_t haystack_len,
const char * needle,
size_t needle_len )
static

Definition at line 588 of file portable.cpp.

590{
591 const char *const last_possible = haystack + haystack_len - needle_len;
592
593 if (needle_len == 0)
594 // The first occurrence of the empty string should to occur at the beginning of the string.
595 {
596 return haystack;
597 }
598
599 // Sanity check
600 if (haystack_len < needle_len)
601 {
602 return nullptr;
603 }
604
605 for (const char *begin = haystack; begin <= last_possible; ++begin)
606 {
607 if (begin[0] == needle[0] && !memcmp(&begin[1], needle + 1, needle_len - 1))
608 {
609 return begin;
610 }
611 }
612
613 return nullptr;
614}
DirIterator begin(DirIterator it) noexcept
Definition dir.cpp:176

References begin().

Referenced by Portable::strnstr().

Variable Documentation

◆ environ

char** environ
extern

◆ environmentLoaded

bool environmentLoaded = false
static

Definition at line 53 of file portable.cpp.

Referenced by Portable::getenv(), loadEnvironment(), and Portable::setenv().

◆ proc_env

std::map<std::string,std::string> proc_env = std::map<std::string,std::string>()
static