Modify tizen coding style
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index 1019db4..d5cf5d3 100644 (file)
 
 #include "utils.h"
 
-bool ICompare(const std::string& a, const std::string& b)
+bool iCompare(const std::string& a, const std::string& b)
 {
-  return a.length() == b.length() &&
-    std::equal(b.begin(), b.end(), a.begin(),
-        [](unsigned char a, unsigned char b)
-        { return std::tolower(a) == std::tolower(b); });
+       return a.length() == b.length() &&
+               std::equal(b.begin(), b.end(), a.begin(),
+                       [](unsigned char a, unsigned char b)
+                       { return std::tolower(a) == std::tolower(b); });
 }
 
-bool ICompare(const std::string& a, int a_offset, const std::string& b, int b_offset, int length)
+bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
 {
-  return static_cast<int>(a.length()) - length >= a_offset &&
-    static_cast<int>(b.length()) - length >= b_offset &&
-    std::equal(b.begin() + b_offset, b.begin() + b_offset + length, a.begin() + a_offset,
-        [](unsigned char a, unsigned char b)
-        { return std::tolower(a) == std::tolower(b); });
+       return static_cast<int>(a.length()) - length >= aOffset &&
+               static_cast<int>(b.length()) - length >= bOffset &&
+               std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
+                       [](unsigned char a, unsigned char b)
+                       { return std::tolower(a) == std::tolower(b); });
 }
 
-bool IsManagedAssembly(const std::string& filename)
+bool isManagedAssembly(const std::string& fileName)
 {
-  return ICompare(filename, filename.size()-4, ".dll", 0, 4) ||
-    ICompare(filename, filename.size()-4, ".exe", 0, 4);
+       return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
+               iCompare(fileName, fileName.size()-4, ".exe", 0, 4);
 }
 
-bool IsNativeImage(const std::string& filename)
+bool isNativeImage(const std::string& fileName)
 {
-  return ICompare(filename, filename.size()-7, ".ni", 0, 3);
+       return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
 }
 
-std::string ReadSelfPath()
+std::string readSelfPath()
 {
-  char buff[PATH_MAX];
-  ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
-  if (len != -1) {
-    buff[len] = '\0';
-    return std::string(buff);
-  }
-
-  return "";
+       char buff[PATH_MAX];
+       ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
+       if (len != -1) {
+               buff[len] = '\0';
+               return std::string(buff);
+       }
+
+       return "";
 }
 
-std::string ConcatPath(const std::string& path1, const std::string& path2)
+std::string concatPath(const std::string& path1, const std::string& path2)
 {
-  std::string path(path1);
-  if (path.back() == PATH_SEPARATOR)
-  {
-    path.append(path2);
-  }
-  else
-  {
-    path += PATH_SEPARATOR;
-    path.append(path2);
-  }
-
-  return path;
+       std::string path(path1);
+       if (path.back() == PATH_SEPARATOR) {
+               path.append(path2);
+       } else {
+               path += PATH_SEPARATOR;
+               path.append(path2);
+       }
+
+       return path;
 }
 
-void AppendPath(std::string& path1, const std::string& path2)
+void appendPath(std::string& path1, const std::string& path2)
 {
-  if (path1.back() == PATH_SEPARATOR)
-  {
-    path1.append(path2);
-  }
-  else
-  {
-    path1 += PATH_SEPARATOR;
-    path1.append(path2);
-  }
+       if (path1.back() == PATH_SEPARATOR) {
+               path1.append(path2);
+       } else {
+               path1 += PATH_SEPARATOR;
+               path1.append(path2);
+       }
 }
 
-std::string AbsolutePath(const std::string& path)
+std::string absolutePath(const std::string& path)
 {
-  std::string absPath;
+       std::string absPath;
+       char realPath[PATH_MAX];
+       if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
+               absPath.assign(realPath);
 
-  char realPath[PATH_MAX];
-  if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
-  {
-    absPath.assign(realPath);
-  }
-
-  return absPath;
+       return absPath;
 }
 
-std::string Basename(const std::string& path)
+std::string baseName(const std::string& path)
 {
-  auto pos = path.find_last_of(PATH_SEPARATOR);
-  if (pos != std::string::npos)
-  {
-    return path.substr(0, pos);
-  }
-  else
-  {
-    return std::string(".");
-  }
-  return path;
+       auto pos = path.find_last_of(PATH_SEPARATOR);
+       if (pos != std::string::npos)
+               return path.substr(0, pos);
+       else
+               return std::string(".");
+       return path;
 }
 
-bool EndWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& filename)
+bool endWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& fileName)
 {
-  std::string::size_type len1 = str1.length();
-  std::string::size_type len2 = str2.length();
-  if (len2 > len1) return false;
-
-  int i = 0;
-  bool result = std::all_of(str1.cend() - len2, str1.end(),
-        [&i, &str2] (char x) {
-          return std::tolower(x) == std::tolower(str2[i++]);
-        });
-  if (result)
-  {
-    filename = str1.substr(0, len1 - len2);
-  }
-  return result;
+       std::string::size_type len1 = str1.length();
+       std::string::size_type len2 = str2.length();
+       if (len2 > len1)
+               return false;
+
+       int i = 0;
+       bool result = std::all_of(str1.cend() - len2, str1.end(),
+                               [&i, &str2] (char x) {
+                                       return std::tolower(x) == std::tolower(str2[i++]);
+                               });
+       if (result)
+               fileName = str1.substr(0, len1 - len2);
+
+       return result;
 }
 
-bool FileNotExist(const std::string& path)
+bool fileNotExist(const std::string& path)
 {
-  struct stat sb;
-  return stat(path.c_str(), &sb) != 0;
+       struct stat sb;
+       return stat(path.c_str(), &sb) != 0;
 }
 
 #ifdef NOT_USE_FUNCTION
-static bool ExtCheckAndGetFileNameIfExist(const std::string&  dir, const std::string& ext, struct dirent* entry, std::string& filename)
+static bool extCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& fileName)
 {
-  std::string fname(entry->d_name);
-  if (fname.length() < ext.length() ||
-      fname.compare(fname.length() - ext.length(), ext.length(), ext) != 0)
-  {
-    return false;
-  }
-  std::string fullname = ConcatPath(dir, entry->d_name);
-  switch (entry->d_type)
-  {
-    case DT_REG: break;
-    case DT_LNK:
-    case DT_UNKNOWN:
-      if (FileNotExist(fullname))
-      {
-        return false;
-      }
-    default:
-      return false;
-  }
-
-  filename = fullname;
-
-  return true;
+       std::string fName(entry->d_name);
+       if (fName.length() < ext.length() ||
+                       fHame.compare(fName.length() - ext.length(), ext.length(), ext) != 0) {
+               return false;
+
+       std::string fullName = concatPath(dir, entry->d_name);
+       switch (entry->d_type) {
+               case DT_REG: break;
+               case DT_LNK:
+               case DT_UNKNOWN:
+                       if (fileNotExist(fullName))
+                               return false;
+               default:
+                       return false;
+       }
+
+       fileName = fullName;
+
+       return true;
 }
 #endif
 
-std::string StripNIDLL(const std::string& path)
+std::string stripNiDLL(const std::string& path)
 {
-  std::string npath(path);
-  if (path.size() < 5) return npath;
-  if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
-  {
-    npath = path.substr(0, path.size()-4);
-  }else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
-  {
-    npath = path.substr(0, path.size()-4);
-  }
-  if (!strncasecmp(npath.c_str() + npath.size() - 3, ".ni", 3))
-  {
-    return npath.substr(0, npath.size()-3);
-  }
-  return npath;
+       std::string niPath(path);
+       if (path.size() < 5) return niPath;
+       if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
+               niPath = path.substr(0, path.size()-4);
+       else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
+               niPath = path.substr(0, path.size()-4);
+
+       if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
+               return niPath.substr(0, niPath.size()-3);
+
+       return niPath;
 }
 
-std::string JoinStrings(const std::vector<std::string>& strings, const char* const delimeter)
+std::string joinStrings(const std::vector<std::string>& strings, const char* const delimeter)
 {
-  switch (strings.size())
-  {
-    case 0:
-      return "";
-    case 1:
-      return strings[0];
-    default:
-      std::ostringstream os; 
-      std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
-      os << *strings.rbegin();
-      return os.str();
-  }
+       switch (strings.size()) {
+               case 0:
+                       return "";
+               case 1:
+                       return strings[0];
+               default:
+                       std::ostringstream os;
+                       std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
+                       os << *strings.rbegin();
+                       return os.str();
+       }
 }
 
-struct AssemblyFile
-{
-  std::string noext;
-  std::string ext;
+struct AssemblyFile {
+       std::string noExt;
+       std::string ext;
 };
 
 bool operator == (const AssemblyFile& lhs, const AssemblyFile& rhs)
 {
-  return lhs.noext == rhs.noext && lhs.ext == rhs.ext;
+       return lhs.noExt == rhs.noExt && lhs.ext == rhs.ext;
 }
 
-namespace std
-{
-  template<>
-  struct hash<AssemblyFile>
-  {
-    std::size_t operator () (const AssemblyFile& f) const
-    {
-      const std::size_t h1 = std::hash<std::string>{}(f.noext);
-      const std::size_t h2 = std::hash<std::string>{}(f.ext);
-
-      return h1 ^ (h2 << 1);
-    }
-  };
+namespace std {
+       template<>
+       struct hash<AssemblyFile> {
+               std::size_t operator () (const AssemblyFile& f) const {
+                       const std::size_t h1 = std::hash<std::string>{}(f.noExt);
+                       const std::size_t h2 = std::hash<std::string>{}(f.ext);
+
+                       return h1 ^ (h2 << 1);
+               }
+       };
 }
 
-void AssembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
+void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
 {
-  std::map<std::string, std::string> assemblyList;
-  std::map<std::string, std::string> tmpList;
-
-  auto reader = [&assemblyList, &tmpList] (const char* path, const char* name)
-  {
-    std::string _path(path);
-    if (IsManagedAssembly(_path))
-    {
-      std::string dll_name = StripNIDLL(name);
-      std::pair<std::map<std::string, std::string>::iterator, bool> ret;
-      ret = tmpList.insert(std::pair<std::string, std::string>(dll_name, _path));
-      if (ret.second == false)
-      {
-        if (IsNativeImage(_path))
-        {
-          tmpList[dll_name] = _path;
-        }
-      }
-    }
-  };
-
-  for (auto directory : directories)
-  {
-    ScanFilesInDir(directory.c_str(), reader, 1);
-    // merge scaned dll list to tpa list.
-    // if the dll is already exist in the list, that is skipped.
-    assemblyList.insert(tmpList.begin(), tmpList.end());
-  }
-
-  std::map<std::string, std::string>::iterator it;
-  for (it = assemblyList.begin(); it != assemblyList.end(); it++)
-  {
-    tpaList += it->second + ':';
-  }
-  if (tpaList.back() == ':')
-  {
-    tpaList.pop_back();
-  }
+       std::map<std::string, std::string> assemblyList;
+       std::map<std::string, std::string> tmpList;
+
+       auto reader = [&assemblyList, &tmpList] (const char* path, const char* name) {
+               std::string pathStr(path);
+               if (isManagedAssembly(pathStr)) {
+                       std::string dllName = stripNiDLL(name);
+                       std::pair<std::map<std::string, std::string>::iterator, bool> ret;
+                       ret = tmpList.insert(std::pair<std::string, std::string>(dllName, pathStr));
+                       if (ret.second == false) {
+                               if (isNativeImage(pathStr))
+                                       tmpList[dllName] = pathStr;
+                       }
+               }
+       };
+
+       for (auto directory : directories) {
+               scanFilesInDir(directory.c_str(), reader, 1);
+               // merge scaned dll list to tpa list.
+               // if the dll is already exist in the list, that is skipped.
+               assemblyList.insert(tmpList.begin(), tmpList.end());
+       }
+
+       std::map<std::string, std::string>::iterator it;
+       for (it = assemblyList.begin(); it != assemblyList.end(); it++)
+               tpaList += it->second + ':';
+
+       if (tpaList.back() == ':')
+               tpaList.pop_back();
 }
 
-void ScanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
+void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
 {
-  DIR *dir;
-  struct dirent* entry;
-  bool isDir;
-
-  dir = opendir(directory);
-
-  if (dir == nullptr)
-  {
-    //_ERR("Can not open directory : %s", directory);
-    return;
-  }
-
-  std::vector<std::string> innerDirectories;
-
-  while ((entry = readdir(dir)) != nullptr)
-  {
-    isDir = false;
-    std::string path = ConcatPath(directory, entry->d_name);
-    switch (entry->d_type)
-    {
-      case DT_REG: break;
-      case DT_DIR:
-        isDir = true;
-        break;
-      case DT_LNK:
-      case DT_UNKNOWN:
-        struct stat sb;
-        if (stat(path.c_str(), &sb) == -1)
-        {
-          continue;
-        }
-
-        if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
-        {
-          break;
-        }
-      default:
-        continue;
-    }
-    if (!isDir)
-    {
-      reader(path.c_str(), entry->d_name);
-    }
-    else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
-    {
-      innerDirectories.push_back(path);
-    }
-  }
-
-  if (depth != 0)
-  {
-    for (auto& d : innerDirectories)
-    {
-      ScanFilesInDir(d.c_str(), reader, depth-1);
-    }
-  }
-
-  closedir(dir);
+       DIR *dir;
+       struct dirent* entry;
+       bool isDir;
+
+       dir = opendir(directory);
+
+       if (dir == nullptr)
+               return;
+
+       std::vector<std::string> innerDirectories;
+
+       while ((entry = readdir(dir)) != nullptr) {
+               isDir = false;
+               std::string path = concatPath(directory, entry->d_name);
+               switch (entry->d_type) {
+                       case DT_REG: break;
+                       case DT_DIR:
+                               isDir = true;
+                               break;
+                       case DT_LNK:
+                       case DT_UNKNOWN:
+                               struct stat sb;
+                               if (stat(path.c_str(), &sb) == -1)
+                                       continue;
+
+                               if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
+                                       break;
+                       default:
+                               continue;
+               }
+               if (!isDir)
+                       reader(path.c_str(), entry->d_name);
+               else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+                       innerDirectories.push_back(path);
+       }
+
+       if (depth != 0)
+               for (auto& d : innerDirectories)
+                       scanFilesInDir(d.c_str(), reader, depth-1);
+
+       closedir(dir);
 }
\ No newline at end of file