18 std::string ReadSelfPath()
21 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
24 return std::string(buff);
30 std::string ConcatPath(const std::string& path1, const std::string& path2)
32 std::string path(path1);
33 if (path.back() == PATH_SEPARATOR)
39 path += PATH_SEPARATOR;
46 void AppendPath(std::string& path1, const std::string& path2)
48 if (path1.back() == PATH_SEPARATOR)
54 path1 += PATH_SEPARATOR;
59 std::string AbsolutePath(const std::string& path)
63 char realPath[PATH_MAX];
64 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
66 absPath.assign(realPath);
72 std::string Basename(const std::string& path)
74 auto pos = path.find_last_of(PATH_SEPARATOR);
75 if (pos != std::string::npos)
77 return path.substr(0, pos);
81 return std::string(".");
86 bool EndWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& filename)
88 std::string::size_type len1 = str1.length();
89 std::string::size_type len2 = str2.length();
90 if (len2 > len1) return false;
93 bool result = std::all_of(str1.cend() - len2, str1.end(),
94 [&i, &str2] (char x) {
95 return std::tolower(x) == std::tolower(str2[i++]);
99 filename = str1.substr(0, len1 - len2);
104 bool FileNotExist(const std::string& path)
107 return (stat(path.c_str(), &sb) != 0) || !S_ISREG(sb.st_mode);
110 static bool ExtCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& filename)
112 std::string fname(entry->d_name);
113 if (fname.length() < ext.length() ||
114 fname.compare(fname.length() - ext.length(), ext.length(), ext) != 0)
118 std::string fullname = ConcatPath(dir, entry->d_name);
119 switch (entry->d_type)
124 if (FileNotExist(fullname))
137 std::string StripNIDLL(const std::string& path)
139 std::string npath(path);
140 if (path.size() < 5) return npath;
141 if (strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
143 npath = path.substr(0, path.size()-4);
144 }else if (strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
146 npath = path.substr(0, path.size()-4);
148 if (strncasecmp(npath.c_str() + npath.size() - 3, ".ni", 3))
150 return npath.substr(0, npath.size()-3);
155 std::string JoinStrings(const std::vector<std::string>& strings, const char* const delimeter)
157 switch (strings.size())
164 std::ostringstream os;
165 std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
166 os << *strings.rbegin();
171 void AssembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
173 static const std::string tpaExtensions[] =
174 {".ni.dll", ".dll", ".ni.exe", ".exe"};
176 std::set<std::string> addedAssemblies;
179 struct dirent* entry = nullptr;
181 for (auto directory : directories)
183 dir = opendir(directory.c_str());
189 for (auto ext : tpaExtensions)
191 while ((entry = readdir(dir)) != nullptr)
193 std::string fullname;
194 if (ExtCheckAndGetFileNameIfExist(directory.c_str(), ext, entry, fullname))
196 std::string filename = StripNIDLL(fullname);
197 if (addedAssemblies.find(filename) == addedAssemblies.end())
199 addedAssemblies.insert(filename);
200 tpaList += fullname + ':';
208 if (tpaList.back() == ':')