예제 #1
0
 bool WeatherFile_Impl::makeUrlAbsolute(const openstudio::path& searchDirectory) {
   boost::optional<openstudio::path> currentPath = this->path();
   if (currentPath){
     if (currentPath->is_complete() && boost::filesystem::exists(*currentPath)) {
       return true;
     }
     openstudio::path newPath, workingPath(*currentPath);
     if (!currentPath->is_complete()) {
       newPath = boost::filesystem::system_complete(workingPath);
       LOG(Debug,"Current path '" << toString(*currentPath) << "' not complete. "
           << "After calling system_complete have '" << toString(newPath) << "'.");
     }
     if (newPath.empty() || !boost::filesystem::exists(newPath)) {
       newPath = searchDirectory / *currentPath;
       LOG(Debug,"Going to look for '" << toString(newPath) << "'.");
     }
     if (newPath.empty() || !boost::filesystem::exists(newPath)) {
       workingPath = toPath(currentPath->filename());
       newPath = searchDirectory / workingPath;
       LOG(Debug,"Going to look for '" << toString(newPath) << "'.");
     }
     if (newPath.empty() || !boost::filesystem::exists(newPath)) {
       return false;
     }
     std::string weatherFileUrl = toString(toURL(newPath));
     LOG(Debug,"Setting weather file url to " << weatherFileUrl);
     return setString(OS_WeatherFileFields::Url,weatherFileUrl);
   }
   return false;
 }
예제 #2
0
  bool create_folder_with_all_permissions(const std::string& workingdirectory)
  {
    if ( !boost::filesystem::exists(workingdirectory) )
      {
        boost::filesystem::path workingPath(workingdirectory);
        try
          {
            boost::filesystem::create_directories(workingPath);
          }
        catch (...)
	  {
	    GERROR("Error creating the working directory.\n");
	    return false;
	  }

        // set the permission for the folder
#ifdef _WIN32
	try
	  {
	    boost::filesystem::permissions(workingPath, all_all);
	  }
	catch(...)
	  {
	    GERROR("Error changing the permission of the working directory.\n");
	    return false;
	  }
#else
	// in case an older version of boost is used in non-win system
	// the system call is used
	int res = chmod(workingPath.string().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH);
	if ( res != 0 )
	  {
	    GERROR("Error changing the permission of the working directory.\n");
	    return false;
	  }
#endif // _WIN32
      }

    return true;
  }
예제 #3
0
bool FileReference::makePathAbsolute(const openstudio::path& searchDirectory) {
  // trivial completion
  openstudio::path currentPath = path();
  if (currentPath.is_complete() && openstudio::filesystem::exists(currentPath)) {
    return true;
  }
  openstudio::path workingPath(currentPath);
  // if currentPath is complete but does not exist, go to extreme measures
  if (currentPath.is_complete()) {
    workingPath = currentPath.filename();
  }
  if (searchDirectory.empty()) {
    return false;
  }
  openstudio::path newPath = openstudio::filesystem::complete(workingPath,searchDirectory);
  if (newPath.empty() || !openstudio::filesystem::exists(newPath)) {
    return false;
  }
  m_path = completeAndNormalize(newPath);
  m_versionUUID = createUUID();
  return true;
}
예제 #4
0
void FileTestContext::removeWorkingFile(const char* filename)
{
   UtlString working;
   workingFilePath(filename, working);
   OsPath workingPath(working);
   OsFile testFile(workingPath);

   if (testFile.exists())
   {
      OsStatus removeStatus = OsFileSystem::remove( workingPath
                                                   ,TRUE // Recursive
                                                   ,TRUE // Force
                                                   );
      UtlString msg("failed to remove working file '");
      msg.append(working);
      msg.append("'");
      CPPUNIT_ASSERT_EQUAL_MESSAGE(msg.data(),OS_SUCCESS,removeStatus);

      Os::Logger::instance().log(FAC_UNIT_TEST, PRI_NOTICE, "FileTestContext::removeWorkingFile '%s'",
                    filename);
   }
}
예제 #5
0
static std::vector<std::string> listFiles(std::string path)
{
    std::vector<std::string> results;

#ifdef _WIN32
    if (path.back() != '/')
        path.append("/*");
    else
        path.append("*");

    //convert to wide chars for windows
    std::basic_string<TCHAR> wPath;
    wPath.assign(path.begin(), path.end());

    WIN32_FIND_DATA findData;
    HANDLE hFind = FindFirstFile(wPath.c_str(), &findData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        std::cout << "Failed to find file data, invalid file handle returned" << std::endl;
        return results;
    }

    do
    {
        if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) //not a directory
        {
            //convert from wide char
            std::basic_string<TCHAR> wName(findData.cFileName);
            std::string fileName;
            fileName.assign(wName.begin(), wName.end());
            results.push_back(fileName);
        }

    } while (FindNextFile(hFind, &findData) != 0);
    FindClose(hFind);

    return std::move(results);
#else
    if (path.back() != '/')
        path.append("/.");
    else
        path.append(".");

    struct dirent* dp;
    DIR* dir = opendir(path.c_str());

    if (dir)
    {
        while ((dp = readdir(dir)) != nullptr)
        {
            std::string workingPath(path);
            workingPath.append("/");
            workingPath.append((dp->d_name));

            struct stat buf;
            if (!stat(workingPath.c_str(), &buf))
            {
                if (!S_ISDIR(buf.st_mode))
                {
                    results.emplace_back(dp->d_name);
                }
            }
        }
        closedir(dir);
    }
    return std::move(results);
#endif //_WIN32
}