1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 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 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*=========================================================================
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 <sys/stat.h>
#include <vtksys/Directory.hxx>
#include <string>
vtkStandardNewMacro(vtkPDirectory);
//----------------------------------------------------------------------------
vtkPDirectory::vtkPDirectory()
{
this->Files = vtkStringArray::New();
}
//----------------------------------------------------------------------------
vtkPDirectory::~vtkPDirectory()
{
this->Files->Delete();
this->Files = 0;
}
//----------------------------------------------------------------------------
bool vtkPDirectory::Load(const std::string& name)
{
this->Clear();
vtkMultiProcessController* controller =
vtkMultiProcessController::GetGlobalController();
long numFiles = 0;
if(controller->GetLocalProcessId() == 0)
{
vtksys::Directory dir;
if (dir.Load(name) == false)
{
numFiles = -1; // failure
controller->Broadcast(&numFiles, 1, 0);
return false;
}
for(unsigned long i=0;i<dir.GetNumberOfFiles();i++)
{
this->Files->InsertNextValue(dir.GetFile(i));
}
numFiles = static_cast<long>(dir.GetNumberOfFiles());
controller->Broadcast(&numFiles, 1, 0);
for(long i=0;i<numFiles;i++)
{
vtkPSystemTools::BroadcastString(this->Files->GetValue(i), 0);
}
}
else
{
controller->Broadcast(&numFiles, 1, 0);
if(numFiles == -1)
{
return false;
}
for(long i=0;i<numFiles;i++)
{
std::string str;
vtkPSystemTools::BroadcastString(str, 0);
this->Files->InsertNextValue(str);
}
}
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() )
{
return NULL;
}
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);
if (name == 0)
{
return 0;
}
int result = 0;
vtkMultiProcessController* controller =
vtkMultiProcessController::GetGlobalController();
if(controller->GetLocalProcessId() == 0)
{
int absolutePath = 0;
#if defined(_WIN32)
if (name[0] == '/' || name[0] == '\\')
{
absolutePath = 1;
}
else
{
for (int i = 0; name[i] != '\0'; i++)
{
if (name[i] == ':')
{
absolutePath = 1;
break;
}
else if (name[i] == '/' || name[i] == '\\')
{
break;
}
}
}
#else
if (name[0] == '/')
{
absolutePath = 1;
}
#endif
char *fullPath;
int n = 0;
if (!absolutePath && !this->Path.empty())
{
n = static_cast<int>(this->Path.size());
}
int m = static_cast<int>(strlen(name));
fullPath = new char[n+m+2];
if (!absolutePath && !this->Path.empty())
{
strcpy(fullPath, this->Path.c_str());
#if defined(_WIN32)
if (fullPath[n-1] != '/'
&& fullPath[n-1] != '\\')
{
#if !defined(__CYGWIN__)
fullPath[n++] = '\\';
#else
fullPath[n++] = '/';
#endif
}
#else
if (fullPath[n-1] != '/')
{
fullPath[n++] = '/';
}
#endif
}
strcpy(&fullPath[n], name);
struct stat fs;
if(stat(fullPath, &fs) == 0)
{
#if defined(_WIN32)
result = ((fs.st_mode & _S_IFDIR) != 0);
#else
result = S_ISDIR(fs.st_mode);
#endif
}
delete [] fullPath;
}
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())
{
os << indent << "Directory not open\n";
return;
}
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++)
{
os << indent << this->Files->GetValue(i) << "\n";
}
}
|