OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimTempFilename.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 // Description: This is a tmeporary filename class it will create a temporary
10 // file and will also delete it upon destruction
11 //
12 //*************************************************************************
13 // $Id: ossimTempFilename.cpp 15524 2009-09-30 01:02:26Z dburken $
14 #include <stdlib.h>
15 #include <fstream>
18 #include <time.h>
19 
21  const ossimString& prefix,
22  const ossimString& extension,
23  bool autodelete,
24  bool useWildcardDelete)
25  :theTempDir(tempDir),
26  thePrefix(prefix),
27  theExtension(extension),
28  theAutoDeleteFlag(autodelete),
29  theWildCardDeleteFlag(useWildcardDelete)
30 {
31 }
32 
34 {
35  if(*((ossimFilename*)this) != "")
36  {
37 
39  {
41  {
42  wildcardRemove(*this + ".*");
43  }
44  else
45  {
46  remove();
47  }
48  }
49  }
50 }
51 
53 {
54  generate(false);
55 }
56 
58 {
59  generate(true);
60 }
61 
62 void ossimTempFilename::generate(bool createAsDirectoryFlag)
63 {
64  srand(time(0));
65  ossimString tempDirCopy = theTempDir;
66 
67  if(tempDirCopy == "")
68  {
70  if(tempDirCopy=="")
71  {
73  }
74  if(tempDirCopy == "")
75  {
76  if(ossimFilename("/tmp").exists())
77  {
78  tempDirCopy = "/tmp";
79  }
80  }
81  }
82 
83  int count = 0;
84  int randNumber1 = rand();
85  ossimFilename prefixDir = ossimFilename(tempDirCopy);
86  ossimFilename result = prefixDir.dirCat(thePrefix+
87  ossimString::toString(randNumber1));
88 
89  while((count < RAND_MAX)&&result.exists())
90  {
91  randNumber1 = rand();
92  result = prefixDir.dirCat(thePrefix+
93  ossimString::toString(randNumber1));
94 
95  ++count;
96  }
97 
98  if(theExtension != "")
99  {
100  result = result.setExtension(theExtension);
101  }
102  *((ossimFilename*)this) = result;
103  if(result != "")
104  {
105  if(createAsDirectoryFlag)
106  {
107  createDirectory();
108  }
109  else
110  {
111  std::ofstream out(result.c_str());
112  out.close();
113  }
114  }
115 }
ossimString theTempDir
bool wildcardRemove() const
static ossimString toString(bool aValue)
Numeric to string methods.
ossimTempFilename(const ossimString &tempDir="", const ossimString &prefix="", const ossimString &extension="", bool autodelete=true, bool useWildcardDelete=true)
ossimString theExtension
bool exists() const
static ossimEnvironmentUtility * instance()
ossimFilename dirCat(const ossimFilename &file) const
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
ossimString getEnvironmentVariable(const ossimString &variable) const
std::basic_ofstream< char > ofstream
Class for char output file streams.
Definition: ossimIosFwd.h:47
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
bool createDirectory(bool recurseFlag=true, int perm=0775) const
void generate(bool createAsDirectoryFlag)