You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Environments.cpp 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "Environments.h"
  2. #include <QProcess>
  3. #include <QRegExp>
  4. stEnvironment::stEnvironment(const QString &unparsed)
  5. {
  6. const QRegExp parser(R"(([^=]+)=(.+))");
  7. if (parser.indexIn(unparsed) == 0)
  8. stEnvironment(parser.cap(1), parser.cap(2));
  9. }
  10. Environments::Environments(bool includeSystem, QObject *parent) : QObject(parent)
  11. {
  12. if (includeSystem)
  13. for (auto &unparsed : QProcess::systemEnvironment())
  14. _envs.push_back(stEnvironment(unparsed));
  15. }
  16. Environments::Environments(QStringList envs, bool includeSystem, QObject *parent)
  17. {
  18. Environments(includeSystem, parent);
  19. for (auto &unparsed : envs)
  20. _envs.push_back(stEnvironment(unparsed));
  21. }
  22. void Environments::addEnvironment(const QString &key, const QString &value)
  23. {
  24. addEnvironment(stEnvironment(key, value));
  25. }
  26. void Environments::addEnvironment(const stEnvironment &env)
  27. {
  28. _envs.push_back(env);
  29. }
  30. void Environments::modEnvironment(const QString &key, const QString &value)
  31. {
  32. for (auto &env : _envs)
  33. if (env.key == key)
  34. env.value = value;
  35. }
  36. void Environments::modEnvironment(const stEnvironment &env)
  37. {
  38. modEnvironment(env.key, env.value);
  39. }
  40. QString &Environments::environmentValue(const QString &key)
  41. {
  42. for (auto &env : _envs)
  43. if (env.key == key)
  44. return env.value;
  45. throw "key " + key + " not defined";
  46. }
  47. QString &Environments::operator[](const QString &key)
  48. {
  49. return environmentValue(key);
  50. }
  51. QStringList Environments::environments()
  52. {
  53. QStringList ret;
  54. for (auto &env : _envs)
  55. ret.push_back(env.key + "=" + env.value);
  56. return ret;
  57. }