My Project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Typedefs | Functions
portable.h File Reference

Portable versions of functions that are platform dependent. More...

#include <sys/types.h>
#include <stdio.h>
#include <qglobal.h>

Go to the source code of this file.

Typedefs

typedef off_t portable_off_t
 

Functions

int portable_system (const char *command, const char *args, bool commandHasConsole=TRUE)
 
uint portable_pid ()
 
const char * portable_getenv (const char *variable)
 
void portable_setenv (const char *variable, const char *value)
 
void portable_unsetenv (const char *variable)
 
portable_off_t portable_fseek (FILE *f, portable_off_t offset, int whence)
 
portable_off_t portable_ftell (FILE *f)
 
FILE * portable_fopen (const char *fileName, const char *mode)
 
char portable_pathSeparator ()
 
char portable_pathListSeparator ()
 
const char * portable_ghostScriptCommand ()
 
const char * portable_commandExtension ()
 
bool portable_fileSystemIsCaseSensitive ()
 
FILE * portable_popen (const char *name, const char *type)
 
int portable_pclose (FILE *stream)
 
void portable_sysTimerStart ()
 
void portable_sysTimerStop ()
 
double portable_getSysElapsedTime ()
 
void portable_sleep (int ms)
 
bool portable_isAbsolutePath (const char *fileName)
 
void * portable_iconv_open (const char *tocode, const char *fromcode)
 
size_t portable_iconv (void *cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
 
int portable_iconv_close (void *cd)
 

Detailed Description

Portable versions of functions that are platform dependent.

Definition in file portable.h.

Typedef Documentation

typedef off_t portable_off_t

Definition at line 11 of file portable.h.

Function Documentation

const char* portable_commandExtension ( )

Definition at line 382 of file portable.cpp.

Referenced by getMscImageMapFromFile(), writeDiaGraphFromFile(), and writeMscGraphFromFile().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
return ".exe";
#else
return "";
#endif
}
bool portable_fileSystemIsCaseSensitive ( )

Definition at line 391 of file portable.cpp.

Referenced by getFilterFromList().

{
#if defined(_WIN32) || defined(macintosh) || defined(__MACOSX__) || defined(__APPLE__)
return FALSE;
#else
return TRUE;
#endif
}
FILE* portable_fopen ( const char *  fileName,
const char *  mode 
)

Definition at line 344 of file portable.cpp.

Referenced by checkDotResult(), initWarningFormat(), LodePNG_loadFile(), LodePNG_saveFile(), Store::open(), and readCodeFragment().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
QString fn(fileName);
QString m(mode);
return _wfopen((wchar_t*)fn.ucs2(),(wchar_t*)m.ucs2());
#else
return fopen(fileName,mode);
#endif
}
portable_off_t portable_fseek ( FILE *  f,
portable_off_t  offset,
int  whence 
)

Definition at line 322 of file portable.cpp.

Referenced by Store::alloc(), Store::dumpBlock(), Store::read(), Store::release(), Store::seek(), and Store::write().

{
#if defined(__MINGW32__)
return fseeko64(f,offset,whence);
#elif defined(_WIN32) && !defined(__CYGWIN__)
return _fseeki64(f,offset,whence);
#else
return fseeko(f,offset,whence);
#endif
}
portable_off_t portable_ftell ( FILE *  f)

Definition at line 333 of file portable.cpp.

Referenced by Store::alloc(), Store::end(), Store::read(), and Store::write().

{
#if defined(__MINGW32__)
return ftello64(f);
#elif defined(_WIN32) && !defined(__CYGWIN__)
return _ftelli64(f);
#else
return ftello(f);
#endif
}
const char* portable_getenv ( const char *  variable)

Definition at line 317 of file portable.cpp.

Referenced by dateToString(), initDoxygen(), parseInput(), and setDotFontPath().

{
return getenv(variable);
}
double portable_getSysElapsedTime ( )

Definition at line 420 of file portable.cpp.

References g_sysElapsedTime.

Referenced by generateOutput().

{
}
const char* portable_ghostScriptCommand ( )

Definition at line 373 of file portable.cpp.

Referenced by FormulaList::generateBitmaps().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
return "gswin32c.exe";
#else
return "gs";
#endif
}
size_t portable_iconv ( void *  cd,
char **  inbuf,
size_t *  inbytesleft,
char **  outbuf,
size_t *  outbytesleft 
)

Definition at line 24 of file portable_c.c.

Referenced by encodeForOutput(), HtmlHelp::recode(), TemplateContextImpl::recode(), transcodeCharacterBuffer(), and transcodeCharacterStringToUTF8().

{
return iconv((iconv_t)cd,inbuf,inbytesleft,outbuf,outbytesleft);
}
int portable_iconv_close ( void *  cd)

Definition at line 30 of file portable_c.c.

Referenced by encodeForOutput(), TemplateContextImpl::setEncoding(), transcodeCharacterBuffer(), transcodeCharacterStringToUTF8(), and HtmlHelp::~HtmlHelp().

{
return iconv_close((iconv_t)cd);
}
void* portable_iconv_open ( const char *  tocode,
const char *  fromcode 
)

Definition at line 19 of file portable_c.c.

Referenced by encodeForOutput(), HtmlHelp::initialize(), TemplateContextImpl::setEncoding(), transcodeCharacterBuffer(), and transcodeCharacterStringToUTF8().

{
return iconv_open(tocode,fromcode);
}
bool portable_isAbsolutePath ( const char *  fileName)

Definition at line 434 of file portable.cpp.

Referenced by generateOutput(), and readTextFileByName().

{
# ifdef _WIN32
if (isalpha (fileName [0]) && fileName[1] == ':')
fileName += 2;
# endif
char const fst = fileName [0];
if (fst == '/') {
return true;
}
# ifdef _WIN32
if (fst == '\\')
return true;
# endif
return false;
}
char portable_pathListSeparator ( )

Definition at line 364 of file portable.cpp.

Referenced by generatePlantUMLOutput(), parseInput(), and setDotFontPath().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
return ';';
#else
return ':';
#endif
}
char portable_pathSeparator ( )
int portable_pclose ( FILE *  stream)

Definition at line 405 of file portable.cpp.

Referenced by FileDef::acquireFileVersion(), readCodeFragment(), and readInputFile().

{
return pclose(stream);
}
uint portable_pid ( )

Definition at line 186 of file portable.cpp.

Referenced by parseInput().

{
uint pid;
#if !defined(_WIN32) || defined(__CYGWIN__)
pid = (uint)getpid();
#else
pid = (uint)GetCurrentProcessId();
#endif
return pid;
}
FILE* portable_popen ( const char *  name,
const char *  type 
)

Definition at line 400 of file portable.cpp.

Referenced by FileDef::acquireFileVersion(), readCodeFragment(), and readInputFile().

{
return popen(name,type);
}
void portable_setenv ( const char *  variable,
const char *  value 
)

Definition at line 202 of file portable.cpp.

References environ, and last_environ.

Referenced by initDoxygen(), parseInput(), setDotFontPath(), and unsetDotFontPath().

{
if (value==0) value="";
#if defined(_WIN32) && !defined(__CYGWIN__)
SetEnvironmentVariable(name,value);
#else
register char **ep = 0;
register size_t size;
const size_t namelen=qstrlen(name);
const size_t vallen=qstrlen(value) + 1;
size = 0;
if (environ!=0)
{
for (ep = environ; *ep; ++ep)
{
if (!qstrncmp (*ep, name, (uint)namelen) &&
(*ep)[namelen] == '=')
break;
else
++size;
}
}
if (environ==0 || *ep==0) /* add new string */
{
char **new_environ;
if (environ == last_environ && environ!=0)
{
// We allocated this space; we can extend it.
new_environ = (char **) realloc (last_environ, (size + 2) * sizeof (char *));
}
else
{
new_environ = (char **) malloc ((size + 2) * sizeof (char *));
}
if (new_environ==0) // no more memory
{
return;
}
new_environ[size] = (char *)malloc (namelen + 1 + vallen);
if (new_environ[size]==0)
{
free (new_environ);
return;
}
if (environ != last_environ)
{
memcpy ((char *) new_environ, environ, size * sizeof (char *));
}
memcpy(new_environ[size], name, namelen);
new_environ[size][namelen] = '=';
memcpy(&new_environ[size][namelen + 1], value, vallen);
new_environ[size + 1] = 0;
last_environ = environ = new_environ;
}
else /* replace existing string */
{
size_t len = qstrlen (*ep);
if (len + 1 < namelen + 1 + vallen)
{
/* The existing string is too short; malloc a new one. */
char *newString = (char *)malloc(namelen + 1 + vallen);
if (newString==0)
{
return;
}
*ep = newString;
}
memcpy(*ep, name, namelen);
(*ep)[namelen] = '=';
memcpy(&(*ep)[namelen + 1], value, vallen);
}
#endif
}
void portable_sleep ( int  ms)

Definition at line 425 of file portable.cpp.

Referenced by DotManager::run().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
Sleep(ms);
#else
usleep(1000*ms);
#endif
}
int portable_system ( const char *  command,
const char *  args,
bool  commandHasConsole = TRUE 
)

taken from the system() manpage on my Linux box

Definition at line 33 of file portable.cpp.

References environ, Debug::ExtCmd, and Debug::print().

Referenced by createSVG(), FlowChart::createSVG(), Htags::execute(), findAndCopyImage(), FormulaList::generateBitmaps(), generateOutput(), CiteDict::generatePage(), generatePlantUMLOutput(), getMscImageMapFromFile(), DotRunner::run(), writeDiaGraphFromFile(), ClassDiagram::writeFigure(), and writeMscGraphFromFile().

{
if (command==0) return 1;
QCString fullCmd=command;
fullCmd=fullCmd.stripWhiteSpace();
if (fullCmd.at(0)!='"' && fullCmd.find(' ')!=-1)
{
// add quotes around command as it contains spaces and is not quoted already
fullCmd="\""+fullCmd+"\"";
}
fullCmd += " ";
fullCmd += args;
#ifndef NODEBUG
Debug::print(Debug::ExtCmd,0,"Executing external command `%s`\n",qPrint(fullCmd));
#endif
#if !defined(_WIN32) || defined(__CYGWIN__)
(void)commandHasConsole;
int pid,status=0;
#ifdef _OS_SOLARIS // for Solaris we use vfork since it is more memory efficient
// on Solaris fork() duplicates the memory usage
// so we use vfork instead
// spawn shell
if ((pid=vfork())<0)
{
status=-1;
}
else if (pid==0)
{
execl("/bin/sh","sh","-c",fullCmd.data(),(char*)0);
_exit(127);
}
else
{
while (waitpid(pid,&status,0 )<0)
{
if (errno!=EINTR)
{
status=-1;
break;
}
}
}
return status;
#else // Other Unices just use fork
pid = fork();
if (pid==-1)
{
perror("fork error");
return -1;
}
if (pid==0)
{
const char * argv[4];
argv[0] = "sh";
argv[1] = "-c";
argv[2] = fullCmd.data();
argv[3] = 0;
execve("/bin/sh",(char * const *)argv,environ);
exit(127);
}
for (;;)
{
if (waitpid(pid,&status,0)==-1)
{
if (errno!=EINTR) return -1;
}
else
{
if (WIFEXITED(status))
{
return WEXITSTATUS(status);
}
else
{
return status;
}
}
}
#endif // !_OS_SOLARIS
#else // Win32 specific
if (commandHasConsole)
{
return system(fullCmd);
}
else
{
// Because ShellExecuteEx can delegate execution to Shell extensions
// (data sources, context menu handlers, verb implementations) that
// are activated using Component Object Model (COM), COM should be
// initialized before ShellExecuteEx is called. Some Shell extensions
// require the COM single-threaded apartment (STA) type.
// For that case COM is initialized as follows
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
QString commandw = QString::fromUtf8( command );
QString argsw = QString::fromUtf8( args );
// gswin32 is a GUI api which will pop up a window and run
// asynchronously. To prevent both, we use ShellExecuteEx and
// WaitForSingleObject (thanks to Robert Golias for the code)
SHELLEXECUTEINFOW sInfo = {
sizeof(SHELLEXECUTEINFOW), /* structure size */
SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI, /* tell us the process
* handle so we can wait till it's done |
* do not display msg box if error
*/
NULL, /* window handle */
NULL, /* action to perform: open */
(LPCWSTR)commandw.ucs2(), /* file to execute */
(LPCWSTR)argsw.ucs2(), /* argument list */
NULL, /* use current working dir */
SW_HIDE, /* minimize on start-up */
0, /* application instance handle */
NULL, /* ignored: id list */
NULL, /* ignored: class name */
NULL, /* ignored: key class */
0, /* ignored: hot key */
NULL, /* ignored: icon */
NULL /* resulting application handle */
};
if (!ShellExecuteExW(&sInfo))
{
return -1;
}
else if (sInfo.hProcess) /* executable was launched, wait for it to finish */
{
WaitForSingleObject(sInfo.hProcess,INFINITE);
/* get process exit code */
DWORD exitCode;
if (!GetExitCodeProcess(sInfo.hProcess,&exitCode))
{
exitCode = -1;
}
CloseHandle(sInfo.hProcess);
return exitCode;
}
}
#endif
}
void portable_sysTimerStart ( )
void portable_sysTimerStop ( )
void portable_unsetenv ( const char *  variable)

Definition at line 283 of file portable.cpp.

References environ.

Referenced by setDotFontPath(), and unsetDotFontPath().

{
#if defined(_WIN32) && !defined(__CYGWIN__)
SetEnvironmentVariable(variable,0);
#else
/* Some systems don't have unsetenv(), so we do it ourselves */
size_t len;
char **ep;
if (variable == NULL || *variable == '\0' || strchr (variable, '=') != NULL)
{
return; // not properly formatted
}
len = qstrlen(variable);
ep = environ;
while (*ep != NULL)
{
if (!qstrncmp(*ep, variable, (uint)len) && (*ep)[len]=='=')
{
/* Found it. Remove this pointer by moving later ones back. */
char **dp = ep;
do dp[0] = dp[1]; while (*dp++);
/* Continue the loop in case NAME appears again. */
}
else
{
++ep;
}
}
#endif
}