utils.h 684 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. class utUtils {
  6. public:
  7. void get_dir_exec(char* dir, char* exec) {
  8. char* filename = nullptr;
  9. if (readlink("/proc/self/exe", dir, 1024) < 0) {
  10. dir[0] = '\0';
  11. return;
  12. }
  13. filename = strrchr(dir, '/');
  14. if (filename == nullptr) {
  15. dir[0] = '\0';
  16. return;
  17. }
  18. ++filename;
  19. if (exec) {
  20. sprintf(exec, "%s", filename);
  21. }
  22. *filename = '\0';
  23. return;
  24. }
  25. std::string get_env(const std::string env) {
  26. char* value = getenv(env.c_str());
  27. if (value == nullptr) {
  28. return std::string();
  29. }
  30. return std::string(value);
  31. }
  32. };