vtkPDirectory.cxx 5.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPDirectory.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notice for more information.

  =========================================================================*/
#include "vtkPDirectory.h"

#include <vtkMultiProcessController.h>
#include "vtkObjectFactory.h"
#include "vtkPSystemTools.h"
#include "vtkStringArray.h"
#include <vtksys/Directory.hxx>
22
#include <vtksys/SystemTools.hxx>
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include <string>

vtkStandardNewMacro(vtkPDirectory);

//----------------------------------------------------------------------------
vtkPDirectory::vtkPDirectory()
{
  this->Files = vtkStringArray::New();
}

//----------------------------------------------------------------------------
vtkPDirectory::~vtkPDirectory()
{
  this->Files->Delete();
37
  this->Files = nullptr;
38 39 40 41 42 43 44 45 46 47 48 49
}

//----------------------------------------------------------------------------
bool vtkPDirectory::Load(const std::string& name)
{
  this->Clear();

  vtkMultiProcessController* controller =
    vtkMultiProcessController::GetGlobalController();

  long numFiles = 0;
  if(controller->GetLocalProcessId() == 0)
50
  {
51 52
    vtksys::Directory dir;
    if (dir.Load(name) == false)
53
    {
54 55 56
      numFiles = -1; // failure
      controller->Broadcast(&numFiles, 1, 0);
      return false;
57
    }
58 59

    for(unsigned long i=0;i<dir.GetNumberOfFiles();i++)
60
    {
61
      this->Files->InsertNextValue(dir.GetFile(i));
62
    }
63 64 65
    numFiles = static_cast<long>(dir.GetNumberOfFiles());
    controller->Broadcast(&numFiles, 1, 0);
    for(long i=0;i<numFiles;i++)
66
    {
67 68
      vtkPSystemTools::BroadcastString(this->Files->GetValue(i), 0);
    }
69
  }
70
  else
71
  {
72 73
    controller->Broadcast(&numFiles, 1, 0);
    if(numFiles == -1)
74
    {
75
      return false;
76
    }
77
    for(long i=0;i<numFiles;i++)
78
    {
79 80 81 82
      std::string str;
      vtkPSystemTools::BroadcastString(str, 0);
      this->Files->InsertNextValue(str);
    }
83
  }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

  this->Path = name;
  return true;
}

//----------------------------------------------------------------------------
int vtkPDirectory::Open(const char* name)
{
  return static_cast<int>(this->Load(name));
}

//----------------------------------------------------------------------------
vtkIdType vtkPDirectory::GetNumberOfFiles() const
{
  return this->Files->GetNumberOfTuples();
}

//----------------------------------------------------------------------------
const char* vtkPDirectory::GetFile(vtkIdType index) const
{
  if ( index >= this->Files->GetNumberOfTuples() )
105
  {
106
    return nullptr;
107
  }
108 109 110 111 112 113 114 115 116 117 118
  return this->Files->GetValue(index).c_str();
}

//----------------------------------------------------------------------------
int vtkPDirectory::FileIsDirectory(const char *name)
{
  // The vtksys::SystemTools::FileIsDirectory()
  // does not equal the following code (it probably should),
  // and it will broke KWWidgets. Reverse back to 1.30
  // return vtksys::SystemTools::FileIsDirectory(name);

119
  if (name == nullptr)
120
  {
121
    return 0;
122
  }
123 124 125 126 127 128

  int result = 0;
  vtkMultiProcessController* controller =
    vtkMultiProcessController::GetGlobalController();

  if(controller->GetLocalProcessId() == 0)
129
  {
130 131 132
    int absolutePath = 0;
#if defined(_WIN32)
    if (name[0] == '/' || name[0] == '\\')
133
    {
134
      absolutePath = 1;
135
    }
136
    else
137
    {
138
      for (int i = 0; name[i] != '\0'; i++)
139
      {
140
        if (name[i] == ':')
141
        {
142 143
          absolutePath = 1;
          break;
144
        }
145
        else if (name[i] == '/' || name[i] == '\\')
146
        {
147 148 149
          break;
        }
      }
150
    }
151 152
#else
    if (name[0] == '/')
153
    {
154
      absolutePath = 1;
155
    }
156 157 158 159 160 161
#endif

    char *fullPath;

    int n = 0;
    if (!absolutePath && !this->Path.empty())
162
    {
163
      n = static_cast<int>(this->Path.size());
164
    }
165 166 167 168 169 170

    int m = static_cast<int>(strlen(name));

    fullPath = new char[n+m+2];

    if (!absolutePath && !this->Path.empty())
171
    {
172 173 174 175
      strcpy(fullPath, this->Path.c_str());
#if defined(_WIN32)
      if (fullPath[n-1] != '/'
          && fullPath[n-1] != '\\')
176
      {
177 178 179 180 181
#if !defined(__CYGWIN__)
        fullPath[n++] = '\\';
#else
        fullPath[n++] = '/';
#endif
182
      }
183 184
#else
      if (fullPath[n-1] != '/')
185
      {
186 187
        fullPath[n++] = '/';
      }
188 189
#endif
    }
190 191 192

    strcpy(&fullPath[n], name);

193 194
    vtksys::SystemTools::Stat_t fs;
    if(vtksys::SystemTools::Stat(fullPath, &fs) == 0)
195
    {
196 197 198 199 200
#if defined(_WIN32)
      result = ((fs.st_mode & _S_IFDIR) != 0);
#else
      result = S_ISDIR(fs.st_mode);
#endif
201
    }
202 203

    delete [] fullPath;
204
  }
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  controller->Broadcast(&result, 1, 0);

  return result;
}

//----------------------------------------------------------------------------
const char* vtkPDirectory::GetPath() const
{
  return this->Path.c_str();
}

//----------------------------------------------------------------------------
void vtkPDirectory::Clear()
{
  this->Path.clear();
  this->Files->Reset();
}

//----------------------------------------------------------------------------
void vtkPDirectory::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Files:  (" << this->Files << ")\n";
  if(this->Path.empty())
230
  {
231 232
    os << indent << "Directory not open\n";
    return;
233
  }
234 235 236 237 238

  os << indent << "Directory for: " <<  this->Path << "\n";
  os << indent << "Contains the following files:\n";
  indent = indent.GetNextIndent();
  for(int i = 0; i < this->Files->GetNumberOfValues(); i++)
239
  {
240
    os << indent << this->Files->GetValue(i) << "\n";
241
  }
242
}