Doxygen
Loading...
Searching...
No Matches
portable.cpp File Reference
#include "portable.h"
#include "qcstring.h"
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include <mutex>
#include <map>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <string>
#include "fileinfo.h"
#include "message.h"
#include "util.h"
#include "dir.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 QCString &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 QCString & fileName)
static

Definition at line 393 of file portable.cpp.

394{
395 FileInfo fi1(fileName.str());
396 if (fi1.exists()) return true;
397
398 QCString paths = Portable::getenv("PATH");
399 char listSep = Portable::pathListSeparator()[0];
400 char pathSep = Portable::pathSeparator()[0];
401 int strt = 0;
402 int idx;
403 while ((idx = paths.find(listSep,strt)) != -1)
404 {
405 QCString locFile(paths.mid(strt,idx-strt));
406 locFile += pathSep;
407 locFile += fileName;
408 FileInfo fi(locFile.str());
409 if (fi.exists()) return true;
410 strt = idx + 1;
411 }
412 // to be sure the last path component is checked as well
413 QCString locFile(paths.mid(strt));
414 if (!locFile.isEmpty())
415 {
416 locFile += pathSep;
417 locFile += fileName;
418 FileInfo fi(locFile.str());
419 if (fi.exists()) return true;
420 }
421 return false;
422}
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
This is an alternative implementation of QCString.
Definition qcstring.h:101
int find(char c, int index=0, bool cs=TRUE) const
Definition qcstring.cpp:43
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:241
const std::string & str() const
Definition qcstring.h:552
QCString pathSeparator()
Definition portable.cpp:375
QCString pathListSeparator()
Definition portable.cpp:384
QCString getenv(const QCString &variable)
Definition portable.cpp:322

References FileInfo::exists(), QCString::find(), Portable::getenv(), QCString::isEmpty(), QCString::mid(), Portable::pathListSeparator(), Portable::pathSeparator(), and QCString::str().

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

◆ loadEnvironment()

void loadEnvironment ( )

Definition at line 261 of file portable.cpp.

262{
263 if(environ != nullptr)
264 {
265 unsigned int i = 0;
266 char* current = environ[i];
267
268 while(current != nullptr) // parse all strings contained by environ til the last element (nullptr)
269 {
270 std::string env_var(current); // load current environment variable string
271 size_t pos = env_var.find("=");
272 if(pos != std::string::npos) // only parse the variable, if it is a valid environment variable...
273 { // ...which has to contain an equal sign as delimiter by definition
274 std::string name = env_var.substr(0,pos); // the string til the equal sign contains the name
275 std::string value = env_var.substr(pos + 1); // the string from the equal sign contains the value
276 proc_env[name] = std::move(value); // save the value by the name as its key in the classes map
277 }
278 i++;
279 current = environ[i];
280 }
281 }
282
283 environmentLoaded = true;
284}
static bool environmentLoaded
Definition portable.cpp:38
static std::map< std::string, std::string > proc_env
Definition portable.cpp:39
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 573 of file portable.cpp.

575{
576 const char *const last_possible = haystack + haystack_len - needle_len;
577
578 if (needle_len == 0)
579 // The first occurrence of the empty string should to occur at the beginning of the string.
580 {
581 return haystack;
582 }
583
584 // Sanity check
585 if (haystack_len < needle_len)
586 {
587 return nullptr;
588 }
589
590 for (const char *begin = haystack; begin <= last_possible; ++begin)
591 {
592 if (begin[0] == needle[0] && !memcmp(&begin[1], needle + 1, needle_len - 1))
593 {
594 return begin;
595 }
596 }
597
598 return nullptr;
599}
DirIterator begin(DirIterator it) noexcept
Definition dir.cpp:170

References begin().

Referenced by Portable::strnstr().

Variable Documentation

◆ environ

char** environ
extern

◆ environmentLoaded

bool environmentLoaded = false
static

Definition at line 38 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