[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
Functions
src/klftools/klfutil.cpp File Reference
#include <stdlib.h>
#include <QFile>
#include <QDir>
#include <QLibraryInfo>
#include <QUrl>
#include <QUrlQuery>
#include <QMessageBox>
#include <QPushButton>
#include <QApplication>
#include <QDesktopWidget>
#include <QProcess>
#include "klfutil.h"
#include "klfsysinfo.h"
Include dependency graph for klfutil.cpp:

Go to the source code of this file.

Functions

KLF_EXPORT bool klfEnsureDir (const QString &dir)
 Ensure existence of a directory.
KLF_EXPORT uint klfUrlCompare (const QUrl &url1, const QUrl &url2, uint interestFlags, const QStringList &interestQueryItems)
 Compares two URLs and returns some flags as to how they differ.
KLF_EXPORT bool klfMatch (const QVariant &testForHitCandidateValue, const QVariant &queryValue, Qt::MatchFlags flags, const QString &queryStringCache)
 Generalized value matching.
KLF_EXPORT QStringList klfSearchFind (const QString &wildcard_expression, int limit)
 Find files matching a path with wildcards.
KLF_EXPORT QString klfSearchPath (const QString &programName, const QString &extra_path)
 Smart executable searching in a given path list with wildcards.
KLF_EXPORT QString klfSearchPath (const QString &fname, const QStringList &paths)
 Search for a file withing a list of directories.
KLF_EXPORT QString klfPrefixedPath (const QString &path_, const QString &ref_)
 Returns absolute path to path as seen from reference.
QString klfGetEnvironmentVariable (const QStringList &env, const QString &var)
 returns the value of an environment variable, defined in env.
KLF_EXPORT void klfSetEnvironmentVariable (QStringList *env, const QString &var, const QString &value)
 set the value of a variable in environment variables list, replacing existing definition if any.
QStringList klfSetEnvironmentVariable (const QStringList &env, const QString &var, const QString &value)
 set the value of a variable in environment variables list, replacing existing definition if any.
QStringList klfMapToEnvironmentList (const QMap< QString, QString > &map)
 convert a map of variable names to values to an environment list
QMap< QString, QStringklfEnvironmentListToMap (const QStringList &env)
 convert environment list into a map of variable names to values
void klfMergeEnvironment (QStringList *env, const QStringList &addvars, const QStringList &pathvars, uint actions)
 merge two environment definitions
QStringList klfMergeEnvironment (const QStringList &env, const QStringList &addvars, const QStringList &pathvars, uint actions)
 merge two environment definitions
QStringList klfGetEnvironmentPath (const QStringList &env, const QString &var)
 get the path items of an environment variable (commonly $PATH)
QStringList klfSplitEnvironmentPath (const QString &value)
 split the value of a PATH-like environment variable into paths (common for $PATH)
QString klfJoinEnvironmentPath (const QStringList &paths)
 join several paths together to form a PATH-like environment variable value (common for $PATH)
QStringList klfSetEnvironmentPath (const QStringList &oldpaths, const QStringList &newpaths, uint action)
 set/add path items to a PATH-like environment variable (commonly $PATH)
QString klfSetEnvironmentPath (const QString &oldpaths, const QString &newpaths, uint action)
 set/add path items to a PATH-like environment variable (commonly $PATH)
QStringList klfSetEnvironmentPath (const QStringList &env, const QStringList &newpaths, const QString &var, uint action)
 set/add path items to an environment variable (commonly $PATH)
void klfSetEnvironmentPath (QStringList *env, const QStringList &newpaths, const QString &var, uint action)
 set/add path items to an environment variable (commonly $PATH)
QString klfExpandEnvironmentVariables (const QString &expression, const QStringList &env, bool recursive)
 Expands references to environment variables to their values.
KLF_EXPORT QStringList klfCurrentEnvironment ()
 Returns the current system's environment.
KLF_EXPORT QString klfUrlLocalFilePath (const QUrl &url)

Function Documentation

Returns the current system's environment.

it is formatted like for klfGetEnvironmentVariable().

Definition at line 621 of file klfutil.cpp.

References QProcess::systemEnvironment().

KLF_EXPORT bool klfEnsureDir ( const QString dir)

Ensure existence of a directory.

Definition at line 41 of file klfutil.cpp.

References QDir::mkpath(), and QFile::setPermissions().

convert environment list into a map of variable names to values

Definition at line 426 of file klfutil.cpp.

References QMap::contains(), and klfWarning.

QString klfExpandEnvironmentVariables ( const QString expression,
const QStringList env = QStringList(),
bool  recursive = true 
)

Expands references to environment variables to their values.

Examines expression, and replaces occurrences of "$VARNAME" by the value of VARNAME in the environment env. (env is expected to be like for klfGetEnvironmentVariable()). If env is empty, the current environment is queried.

Definition at line 614 of file klfutil.cpp.

Referenced by klfMergeEnvironment().

QStringList klfGetEnvironmentPath ( const QStringList env,
const QString var = QLatin1String("PATH") 
)

get the path items of an environment variable (commonly $PATH)

Like klfGetEnvironmentVariable(), but splits the result at colons `:' for Unix/Mac and at semicolons for Windows.

Definition at line 473 of file klfutil.cpp.

References klfGetEnvironmentVariable(), and klfSplitEnvironmentPath().

Referenced by klfSetEnvironmentPath().

QString klfGetEnvironmentVariable ( const QStringList env,
const QString var 
)

returns the value of an environment variable, defined in env.

env is expected to be a list of strings, each starting with "VARNAME=", and the rest being the value, like the output of 'env' in a terminal.

A null QString() is returned if var is not defined in env.

Definition at line 364 of file klfutil.cpp.

References QString::length(), QList::mid(), and QList::size().

Referenced by klfGetEnvironmentPath(), and klfMergeEnvironment().

join several paths together to form a PATH-like environment variable value (common for $PATH)

Returns:
the value the corresponding environment variable should take, without the "VARNAME=" beginning (obviously since this function doesn't have access to the variable name).

Definition at line 497 of file klfutil.cpp.

References QStringList::join(), and KLF_PATH_SEP.

Referenced by klfSetEnvironmentPath().

convert a map of variable names to values to an environment list

Definition at line 403 of file klfutil.cpp.

References QMap::begin(), QMap::end(), and QList::value().

KLF_EXPORT bool klfMatch ( const QVariant testForHitCandidateValue,
const QVariant queryValue,
Qt::MatchFlags  flags,
const QString queryStringCache = QString() 
)

Generalized value matching.

This function tests to see if the value testForHitCandidateValue matches the value queryValue according to the match flags flags.

If you call this function repeatedly with the same queryValue, the query value may be converted (unnecessarily) repeatedly to a string with queryValue.toString(). To optimize this, you may cache that string and pass each time the string representation for the queryValue as parameter to queryStringCache. If however a null string is passed, the conversion is performed automatically.

Definition at line 145 of file klfutil.cpp.

References QString::compare(), QString::contains(), QString::endsWith(), QString::isNull(), QString::startsWith(), and QVariant::toString().

void klfMergeEnvironment ( QStringList env,
const QStringList addvars,
const QStringList pathvars = QStringList(),
uint  mergeaction = KlfEnvPathPrepend 
)

merge two environment definitions

Addes the environment variables specified in addvars into env, overwriting previous entries for those variables if they already exist.

If some variable names are specified in pathvars, then those variables will be treated as containing paths like PATH or TEXINPUTS, and the new values will be prepended to the old ones.

Definition at line 443 of file klfutil.cpp.

References QStringList::contains(), KLF_DEBUG_BLOCK, KLF_FUNC_NAME, KlfEnvMergeExpandNotRecursive, KlfEnvMergeExpandVars, klfExpandEnvironmentVariables(), klfGetEnvironmentVariable(), klfSetEnvironmentPath(), and klfSetEnvironmentVariable().

Referenced by klfMergeEnvironment().

QStringList klfMergeEnvironment ( const QStringList env,
const QStringList addvars,
const QStringList pathvars = QStringList(),
uint  mergeaction = KlfEnvPathPrepend 
)

merge two environment definitions

Addes the environment variables specified in addvars into env, overwriting previous entries for those variables if they already exist.

If some variable names are specified in pathvars, then those variables will be treated as containing paths like PATH or TEXINPUTS, and the new values will be prepended to the old ones.

env itself is not modified, and the merged list is retured.

Definition at line 463 of file klfutil.cpp.

References KLF_DEBUG_BLOCK, KLF_FUNC_NAME, and klfMergeEnvironment().

KLF_EXPORT QString klfPrefixedPath ( const QString path,
const QString reference = QString() 
)

Returns absolute path to path as seen from reference.

If path is absolute, then path is returned as is. Otherwise, an absolute path constructed by concatenating path to reference is returned.

If reference is empty, then the reference is considered to be the application's location, see QCoreApplication::applicationDirPath().

Definition at line 316 of file klfutil.cpp.

References QFileInfo::absoluteFilePath(), QCoreApplication::applicationDirPath(), QString::endsWith(), QDir::homePath(), QFileInfo::isAbsolute(), QString::isEmpty(), KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klfDbg, QString::mid(), and QString::startsWith().

KLF_EXPORT QStringList klfSearchFind ( const QString wildcard_expression,
int  limit = -1 
)

Find files matching a path with wildcards.

This function returns at most limit file names that match the given wildcard_expression. The latter may be any absolute path in which (any number of) * and ? wildcards may be placed.

This function splits the wildcard_expression at '/' characters, and by starting at the root directory, recursively exploring all directories matching the given section of the pattern. (native '\' separators on windows are appropriately converted to universal '/', so you don't have to worry about passing '\'-style paths).

For example, searching for "/usr/lib*/kde4/kate*.so" will start looking in the root directory for a directory named "usr", in which a directory matching "lib*" is searched for. In each of those matches, a directory named "kde4" is searched for, in which files named "lib*.so.*" are listed. All found matches are returned (up to a given limit number of entries if limit is positive).

The drive letter in wildcard_expression on windows may not contain a wildcard.

Definition at line 243 of file klfutil.cpp.

References QRegExp::exactMatch(), QDir::fromNativeSeparators(), klfDbg, QList::pop_front(), and QString::split().

Referenced by klfSearchPath().

KLF_EXPORT QString klfSearchPath ( const QString prog,
const QString extra_path = "" 
)

Smart executable searching in a given path list with wildcards.

This function looks for an executable named programName. It looks in the directories given in argument extra_path, and in the system environment variable PATH. extra_path and PATH are assumed to be a colon-separated list of directories (semicolon-separated on windows, see KLF_PATH_SEP). Each given directory may contain wildcards (in particular, wildcards in PATH are also parsed). programName itself may also contain wildcards.

This function splits extra_path and PATH into a directory list, and then, for each directory in that list, calls klfSearchFind() with as argument the string "<directory>/<programName>". This function then returns the first result that is an executable file (this check is explicitely performed).

Definition at line 261 of file klfutil.cpp.

References QFileInfo::exists(), QFileInfo::isAbsolute(), QString::isEmpty(), QFileInfo::isExecutable(), KLF_DEBUG_BLOCK, KLF_FUNC_NAME, KLF_PATH_SEP, klfDbg, klfSearchFind(), QList::size(), and QString::split().

KLF_EXPORT QString klfSearchPath ( const QString fname,
const QStringList path 
)

Search for a file withing a list of directories.

Todo:
doc......

very much like klfSearchPath(const QString&, const QString&), except that it is not explicitely targeted at searching for executables. Just look for a file named 'fname' in the given path list. Both 'path' and 'fname' may contain klfSearchFind()-compatible wildcards.

Definition at line 294 of file klfutil.cpp.

References QFile::exists(), KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klfDbg, klfSearchFind(), and QList::size().

QStringList klfSetEnvironmentPath ( const QStringList oldpaths,
const QStringList newpaths,
uint  action = KlfEnvPathAppend|KlfEnvPathNoDuplicates 
)

set/add path items to a PATH-like environment variable (commonly $PATH)

This function only processes changes to the variable value. You will still need to use klfSetEnvironmentVariable() to apply that change to an environment.

See KlfEnvAction enum for possible actions.

Parameters:
oldpathsa list of paths of "old" values.
newpathsa list of paths of "new" values.

Definition at line 514 of file klfutil.cpp.

References QList::append(), QStringList::contains(), KlfEnvPathActionMask, KlfEnvPathAppend, KlfEnvPathNoAction, KlfEnvPathNoDuplicates, KlfEnvPathPrepend, KlfEnvPathReplace, klfWarning, and QList::size().

Referenced by klfMergeEnvironment(), and klfSetEnvironmentPath().

QString klfSetEnvironmentPath ( const QString oldpaths,
const QString newpaths,
uint  action = KlfEnvPathAppend|KlfEnvPathNoDuplicates 
)

set/add path items to a PATH-like environment variable (commonly $PATH)

This function only processes changes to the variable value. You will still need to use klfSetEnvironmentVariable() to apply that change to an environment.

See KlfEnvAction enum for possible actions.

Parameters:
oldpathsa colon-separated list of paths of "old" values. don't include the variable name.
newpathsa colon-separated list of paths of "new" values. don't include the variable name.

Definition at line 548 of file klfutil.cpp.

References KLF_PATH_SEP, klfSetEnvironmentPath(), and klfSplitEnvironmentPath().

QStringList klfSetEnvironmentPath ( const QStringList env,
const QStringList newitems,
const QString var = QLatin1String("PATH"),
uint  action = KlfEnvPathAppend|KlfEnvPathNoDuplicates 
)

set/add path items to an environment variable (commonly $PATH)

See KlfEnvPathAction enum for possible actions.

Returns:
the new environment variable list. The argument env itself is not changed.

Definition at line 555 of file klfutil.cpp.

References klfGetEnvironmentPath(), klfJoinEnvironmentPath(), klfSetEnvironmentPath(), and klfSetEnvironmentVariable().

void klfSetEnvironmentPath ( QStringList env,
const QStringList newitems,
const QString var = QLatin1String("PATH"),
uint  action = KlfEnvPathAppend|KlfEnvPathNoDuplicates 
)

set/add path items to an environment variable (commonly $PATH)

See KlfEnvPathAction enum for possible actions.

Modifies env directly.

Definition at line 563 of file klfutil.cpp.

References klfGetEnvironmentPath(), klfJoinEnvironmentPath(), klfSetEnvironmentPath(), and klfSetEnvironmentVariable().

KLF_EXPORT void klfSetEnvironmentVariable ( QStringList env,
const QString var,
const QString value 
)

set the value of a variable in environment variables list, replacing existing definition if any.

If variable var exists in env, then its definition is replaced. If it does not exist, a definition for var is appended to env.

env is expected to be in the form described in klfGetEnvironmentVariable().

Definition at line 378 of file klfutil.cpp.

References QList::append(), QList::size(), and QList::startsWith().

Referenced by klfMergeEnvironment(), klfSetEnvironmentPath(), and klfSetEnvironmentVariable().

QStringList klfSetEnvironmentVariable ( const QStringList env,
const QString var,
const QString value 
)

set the value of a variable in environment variables list, replacing existing definition if any.

Same as klfSetEnvironmentVariable(QStringList *, const QString&, const QString&), except this function does not modify the original list and returns the new one.

Returns:
the new environment variable list. The argument env itself is not changed.

Definition at line 395 of file klfutil.cpp.

References klfSetEnvironmentVariable().

split the value of a PATH-like environment variable into paths (common for $PATH)

Do NOT specify the variable name (i.e. "VARNAME=") in the value.

Definition at line 480 of file klfutil.cpp.

References QString::isEmpty(), QList::isEmpty(), KLF_PATH_SEP, QList::removeAt(), QList::size(), and QString::split().

Referenced by klfGetEnvironmentPath(), and klfSetEnvironmentPath().

KLF_EXPORT uint klfUrlCompare ( const QUrl url1,
const QUrl url2,
uint  interestFlags = 0xffffffff,
const QStringList interestQueryItems = QStringList() 
)

Compares two URLs and returns some flags as to how they differ.

The return value is an binary-OR'ed value of flags given in KlfUrlCompareFlag.

If the interestFlag parameter is set, only the tests that are given in interestFlags are performed. The returned flags are those flags set in interestFlags that are true.

If the interestQueryItems is set, all query items other than those specified in interestQueryItems are ignored. If interestQueryItems is an empty list, no query items are ignored, they are all taken into account.

Definition at line 78 of file klfutil.cpp.

References KLF_DEBUG_BLOCK, KLF_FUNC_NAME, klfDbg, klfMapIsIncludedIn(), KlfUrlCompareBaseEqual, KlfUrlCompareEqual, klfUrlCompareFlagIgnoreQueryItemValueCase, KlfUrlCompareLessSpecific, KlfUrlCompareMoreSpecific, and QUrl::setQuery().

KLF_EXPORT QString klfUrlLocalFilePath ( const QUrl url)

Returns the file path represented in url, interpreted as an (absolute) path to a local file.

On windows, this ensures that there is no slash preceeding the drive letter, eg. fixes "/C:/..." to "C:/...", but keeps forward-slashes.

Definition at line 634 of file klfutil.cpp.

References QString::mid(), QUrl::path(), and QString::startsWith().


Generated by doxygen 1.7.6.1. The KLatexFormula website is hosted on sourceforge.net