35 # include <sys/utime.h> 38 # include <sys/types.h> 40 # include <sys/stat.h> 69 typedef WIN32_FIND_DATA FIND_STRUCT;
70 typedef HANDLE FIND_DATA;
71 typedef DWORD FIND_ATTR;
82 ossimFileHandle(
const ossimString& filename, OpenMode mode)
84 m_hFile = ::CreateFile(
86 mode ==
Read ? GENERIC_READ
96 if ( m_hFile == INVALID_HANDLE_VALUE )
106 if ( m_hFile != INVALID_HANDLE_VALUE )
108 if ( !::CloseHandle(m_hFile) )
116 bool isOk()
const {
return m_hFile != INVALID_HANDLE_VALUE; }
119 operator HANDLE()
const {
return m_hFile; }
125 static void convertOssimToFileTime(FILETIME *ft,
const ossimDate& dt)
137 if ( !::SystemTimeToFileTime(&st, &ftLocal) )
142 if ( !::LocalFileTimeToFileTime(&ftLocal, ft) )
148 static void convertFileTimeToOssim(
ossimLocalTm &dt,
const FILETIME &ft)
150 FILETIME ftcopy = ft;
152 if ( !::FileTimeToLocalFileTime(&ftcopy, &ftLocal) )
158 if ( !::FileTimeToSystemTime(&ftLocal, &st) )
174 static inline bool IsFindDataOk(FIND_DATA fd)
176 return fd != INVALID_HANDLE_VALUE;
179 static inline FIND_DATA FindFirst(
const ossimString& spec,
180 FIND_STRUCT *finddata)
182 return ::FindFirstFile(spec.
c_str(), finddata);
185 static bool ossimGetDirectoryTimes(
const ossimString& dirname,
192 FIND_DATA fd = FindFirst(dirname, &fs);
193 if ( !IsFindDataOk(fd) )
198 *ftAccess = fs.ftLastAccessTime;
199 *ftCreate = fs.ftCreationTime;
200 *ftMod = fs.ftLastWriteTime;
284 void ossimFilename::convertBackToForwardSlashes()
286 std::string::iterator currentChar = this->
begin();
288 while(currentChar != this->
end())
290 if(*currentChar ==
'\\')
298 void ossimFilename::convertForwardToBackSlashes()
300 std::string::iterator currentChar = this->
begin();
302 while(currentChar != this->
end())
304 if(*currentChar ==
'/')
310 m_pathSeparator =
'\\';
333 FILETIME ftAccess, ftCreate, ftWrite;
337 convertOssimToFileTime(&ftCreate, *createTime);
341 convertOssimToFileTime(&ftAccess, *accessTime);
345 convertOssimToFileTime(&ftWrite, *modTime);
347 if ( ::SetFileTime(fh,
348 createTime ? &ftCreate : NULL,
349 accessTime ? &ftAccess : NULL,
350 modTime ? &ftWrite : NULL) )
357 if ( !accessTime && !modTime )
365 if ( utime(expand().c_str(), &utm) == 0 )
387 FILETIME ftAccess, ftCreate, ftWrite;
391 &ftAccess, &ftCreate, &ftWrite);
399 ok = ::GetFileTime(fh,
400 createTime ? &ftCreate : NULL,
401 accessTime ? &ftAccess : NULL,
402 modTime ? &ftWrite : NULL) != 0;
414 convertFileTimeToOssim(*createTime, ftCreate);
418 convertFileTimeToOssim(*accessTime, ftAccess);
422 convertFileTimeToOssim(*modTime, ftWrite);
429 stat(
c_str(), &sbuf);
462 cerr <<
"ossimFilename::lastAccessed() not implemented for windows!" << endl;
465 stat(
c_str(), &sbuf);
468 time_t atime = sbuf.st_atime;
479 #if defined( _WIN32 ) 522 result = homeDir.
dirCat(s);
524 else if( (
size() > 1) &&
530 else if ( (
size() > 2) && (*(
begin()) ==
'.')
536 else if (result ==
".")
539 getCurrentWorkingDir();
545 getCurrentWorkingDir();
546 result = cwd.
dirCat(result);
550 if ( result.
size() && ((*(result.
begin())) !=
'$') )
553 getCurrentWorkingDir();
554 result = cwd.
dirCat(result);
564 const char* tempPtr = result.
c_str();
568 while(scanIdx < resultSize)
571 if(tempPtr[scanIdx] ==
'$')
573 if(tempPtr[scanIdx+1] ==
'(')
584 finalResult += tempPtr[scanIdx];
589 else if(tempPtr[scanIdx] ==
')')
595 getEnvironmentVariable(
ossimString(tempPtr+startIdx,
597 #if defined(_WIN32) // do windows style replacment 600 finalResult += value;
607 finalResult += tempPtr[scanIdx];
611 else if(startIdx == -1)
613 finalResult += tempPtr[scanIdx];
624 finalResult.
gsub(
"//",
"/",
true);
626 result = finalResult;
653 if (
isUrl() ==
false )
672 if ( _stat(
c_str(), &sbuf ) == -1)
674 return (_S_IFMT & sbuf.st_mode ?
true :
false);
678 stat(
c_str(), &sbuf);
679 return ((sbuf.st_mode & S_IFMT) == S_IFREG);
691 const char& lastChar = temp[temp.
size()-1];
692 if ( lastChar ==
'/' || lastChar ==
'\\' )
700 if ( _stat(temp.
c_str(), &sbuf ) == -1)
702 return (_S_IFDIR & sbuf.st_mode ?
true :
false);
705 if (stat(temp.
c_str(), &sbuf) == -1)
707 return (S_ISDIR(sbuf.st_mode));
716 if ( _stat(
c_str(), &sbuf ) == -1)
718 return (_S_IREAD & sbuf.st_mode ?
true :
false);
733 std::size_t found =
m_str.find( std::string(
"://") );
734 if ( ( found != std::string::npos ) && ( found > 1 ) )
747 if ( _stat(
c_str(), &sbuf ) == -1)
749 return (_S_IWRITE & sbuf.st_mode ?
true :
false);
760 if ( _stat(
c_str(), &sbuf ) == -1)
762 return (_S_IEXEC & sbuf.st_mode ?
true :
false);
770 std::string::size_type pos =
m_str.rfind(
'.');
771 if (pos == std::string::npos)
782 if (pos == std::string::npos)
795 if (pos == std::string::npos)
823 std::string::size_type dot_pos =
m_str.rfind(
'.');
826 if(dot_pos == std::string::npos)
828 if(slash_pos == std::string::npos)
838 else if(slash_pos == std::string::npos)
842 else if(slash_pos < dot_pos)
845 this->
begin() + dot_pos);
859 split(drivePart, pathPart, filePart, extPart);
865 result.
merge(drivePart, pathPart, filePart, extPart);
970 pathPart = pathPart.
substitute(drivePart,
"");
1012 string::const_iterator i = dir.
end();
1022 std::string::iterator iter = tempFile.
begin();
1023 if (iter != tempFile.
end())
1029 else if (tempFile.
size() > 1)
1038 dir += std::string(iter, tempFile.
end());
1047 if (
isUrl() ==
false )
1050 if ( stat( this->
c_str(), &sbuf ) == 0 )
1059 in.seekg(0, std::ios_base::end);
1067 createIstream( this->
string() );
1077 in->seekg(0, std::ios_base::end);
1089 if(
exists())
return true;
1091 if (
empty() )
return false;
1097 vector<ossimString> result;
1106 bool bGotUNC =
false;
1107 if ( current.
length() == 0 && tempString.
length() > 2 )
1109 const char* fstr = tempString.
c_str();
1110 const char fstar0 = fstr[0];
1111 const char fstar1 = fstr[1];
1112 if ( fstar0==
'\\' && fstar1==
'\\' )
1125 if ( bGotUNC ==
true && i==1 )
1136 #if defined(__BORLANDC__) 1137 if ( _mkdir(current.
c_str()) != 0 )
1138 #elif defined(_WIN32) 1139 if ( _mkdir(current.
c_str()) != 0 )
1141 if ( mkdir(current.
c_str(), perm) != 0 )
1153 #if defined (__BORLANDC__) 1154 if ( _mkdir(
c_str()) != 0 )
1155 #elif defined(_WIN32) 1156 if ( _mkdir(
c_str()) != 0 )
1158 if ( mkdir(
c_str(), perm) != 0 )
1175 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__) || \ 1176 defined(__GNUWIN32__) || defined(_MSC_VER) 1179 if(pathname.
isDir())
1182 result = ( RemoveDirectory( pathname.
c_str() ) != 0 );
1186 result = ( DeleteFile( pathname.
c_str() ) != 0 );
1189 if(pathname.
isDir())
1191 result = ( rmdir( pathname.
c_str() ) == 0 );
1195 result = ( unlink( pathname.
c_str() ) == 0 );
1204 std::vector<ossimFilename> fileListToRemove;
1207 if(!tempPathname.
isDir())
1231 for(idx = 0; idx < fileListToRemove.size(); ++idx)
1233 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(__WATCOMC__) || \ 1234 defined(__GNUWIN32__) || defined(_MSC_VER) 1236 if(
remove(fileListToRemove[idx].
c_str()) != 0)
1241 if (unlink(fileListToRemove[idx]) == -1)
1253 if ( this->
string() != destFile.
string() )
1255 if ( overwriteDestinationFlag && destFile.
exists() )
1260 if ( destFile.
exists() == false )
1263 if ( std::rename(this->
c_str(), destFile.
c_str()) != 0 )
1271 <<
"ossimFilenam::rename WARNING:" 1272 <<
"\nDestination File Exists: " << destFile << std::endl;
1291 bool result =
false;
1314 if ( is.eof() && !os.fail())
1322 <<
"ossimFilename::copyFileTo WARNING:" 1323 <<
"\nError detected writing from file " 1324 << this->
c_str() <<
" to file " << f.
c_str() << std::endl;
1331 <<
"ossimFilename::copyFileTo WARNING:" 1332 <<
"\nCannot open: " << f.
c_str() << std::endl;
1339 <<
"ossimFilename::copyFileTo WARNING:" 1340 <<
"\nFiles the same!" << std::endl;
1348 <<
"ossimFilename::copyFileTo WARNING:" 1349 <<
"\nCannot open: " << this->
c_str() << std::endl;
1368 if ( (*(
begin()) ==
'/') || (*(
begin()) ==
'\\') )
1387 bool result =
false;
1391 if (
isUrl() == false )
1394 if (result ==
false)
1397 std::string::size_type pos =
m_str.find(
'$', 0);
1399 if (pos != std::string::npos)
1419 const std::string format =
"%Y%m%d-%H%Mh%Ss";
1420 std::string timestamp;
1423 return append(timestamp);
1433 split(drivePart, pathPart, filePart, extPart);
1434 filePart += append_this;
1435 merge(drivePart, pathPart, filePart, extPart);
1451 std::string s =
m_str;
1460 void ossimFilename::convertToNative()
1463 if (
isUrl() ==
false )
1465 convertForwardToBackSlashes();
1468 convertBackToForwardSlashes();
void clear()
Erases the entire container.
ossimLocalTm & setDay(int day)
ossimString substitute(const ossimString &searchKey, const ossimString &replacementValue, bool replaceAll=false) const
Substitutes searchKey string with replacementValue and returns a string.
ossimFilename noExtension() const
static bool remove(const ossimFilename &pathname)
Removes pathname from filesystem if supported by platform.
OSSIM_DLL ossim_int64 getTime()
Gets the current time.
static const ossimFilename NIL
This was taken from Wx widgets for performing touch and access date stamps.
ossimFilename & appendTimestamp()
Convenience method to append a generic timestamp to the base-name portion of the filename.
ossimFilename & setFile(const ossimString &f)
std::basic_ifstream< char > ifstream
Class for char input file streams.
ossim_int64 fileSize() const
ossimFilename expand() const
Method to do file name expansion.
bool getTimes(ossimLocalTm *accessTime, ossimLocalTm *modTime, ossimLocalTm *createTime) const
bool wildcardRemove() const
ossimFilename & setPath(const ossimString &p)
void split(ossimString &drivePart, ossimString &pathPart, ossimString &filePart, ossimString &extPart) const
void split(std::vector< ossimString > &result, const ossimString &separatorList, bool skipBlankFields=false) const
Splits this string into a vector of strings (fields) using the delimiter list specified.
static StreamFactoryRegistry * instance()
void findAllFilesThatMatch(std::vector< ossimFilename > &result, const ossimString ®ularExpressionPattern, int flags=OSSIM_DIR_DEFAULT)
ossim_int64 lastAccessed() const
Time in seconds since last accessed.
std::string::iterator end()
bool copyFileTo(const ossimFilename &outputFile) const
Copies this file to output file.
friend OSSIM_DLL bool operator==(const char *lhs, const ossimString &rhs)
ossimString()
default constructor
bool isExecutable() const
bool exists(const std::string &connectionString) const
Methods to test if connection exists.
std::string::size_type length() const
OSSIM_DLL void getFormattedTime(const std::string &format, bool gmtFlag, std::string &result)
Gets the current time.
std::string::size_type size() const
std::string::iterator begin()
virtual ossim_int64 getFileSize() const =0
Pure virtual file size method.
ossimLocalTm & setMonth(int month)
unsigned int ossim_uint32
static const char OSSIM_NATIVE_PATH_SEPARATOR
static const char OSSIM_FILENAME_PATH_SEPARATOR
ossim_uint32 start() const
ossimLocalTm & setYear(int year)
bool setTimes(ossimLocalTm *accessTime, ossimLocalTm *modTime, ossimLocalTm *createTime) const
Writes f to the output stream os.
bool open(const ossimFilename &dir)
std::string native() const
ossimLocalTm & setHour(int h)
const ossimFilename & operator=(const ossimFilename &f)
ossimLocalTm & setSec(int s)
static ossimEnvironmentUtility * instance()
char getPathSeparator() const
bool isRelative() const
Checks whether file name is relative or absolute.
ossimFilename fileNoExtension() const
ossimFilename & append(const ossimString &append_this_to_filename)
Convenience method to append a string to the base-name portion of the filename.
ossimLocalTm & setMin(int m)
ossimFilename dirCat(const ossimFilename &file) const
void merge(const ossimString &drivePart, const ossimString &pathPart, const ossimString &filePart, const ossimString &extPart)
ossimString beforePos(std::string::size_type pos) const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string's contents...
bool needsExpansion() const
Method to check if expansion is needed.
bool operator==(const ossimFilename &rhs) const
ossimFilename file() const
std::basic_ofstream< char > ofstream
Class for char output file streams.
ossimString & gsub(const ossimString &searchKey, const ossimString &replacementValue, bool replaceAll=false)
Substitutes searchKey string with replacementValue and returns a reference to *this.
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
bool createDirectory(bool recurseFlag=true, int perm=0775) const
ossimFilename getUserDir() const
ossimFilename & setDrive(const ossimString &d)
Sets the file path and drive.
ossimFilename path() const
ossimFilename drive() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
time_t getTicks() const
getTicks() will call getEpoc.
bool rename(const ossimFilename &destFile, bool overwriteDestinationFlag=true) const
const std::string & string() const
void converPathSeparator()
Converts slashes of this to internal '/' format.