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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*
Copyright 2009 - 2010 Sun Microsystems, Inc. All rights reserved.
The MySQL Connector/C++ is licensed under the terms of the GPL
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
MySQL Connectors. There are special exceptions to the terms and
conditions of the GPL as it is applied to this software, see the
FLOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
*/
#include "file.h"
#include "stringutils.h"
#include <stdlib.h>
namespace FileUtils
{
std::map<String, ccppFile *> ccppFile::_2deleteAtexit;
ccppFile::ccppFile( const String & fileName )
: name ( fileName )
, binary ( true )
{
init();
//fileExists ||=
}
void ccppFile::init()
{
fileExists= ( stat( name.c_str(), &fileInfo ) == 0 );
}
ccppFile::~ccppFile()
{
close();
removeObjectFromDeleteList( name );
}
void ccppFile::removeFileFromDeleteList( const String & fileName )
{
FilesMap::iterator it= _2deleteAtexit.find( fileName );
if ( it != _2deleteAtexit.end() )
_2deleteAtexit.erase( it );
}
void ccppFile::removeObjectFromDeleteList( const String & fileName )
{
FilesMap::iterator it= _2deleteAtexit.find( fileName );
if ( it != _2deleteAtexit.end() )
it->second= NULL;
}
void ccppFile::reset()
{
fileExists= false;
}
std::fstream & ccppFile::getStream()
{
if ( ! stream.is_open() )
{
if ( exists() )
stream.open( name.c_str(), std::ios_base::out | std::ios_base::in
| ( binary ? std::ios_base::binary : static_cast<std::ios_base::openmode>(0) ) );
else
stream.open( name.c_str(), std::ios_base::out
| ( binary ? std::ios_base::binary : static_cast<std::ios_base::openmode>(0) ) );
}
else
stream.clear();
return stream;
}
int ccppFile::getSize () const
{
if ( ! fileExists )
return -1;
return static_cast<int>( fileInfo.st_size );
}
bool ccppFile::exists () const
{
return fileExists;
}
bool ccppFile::readable() const
{
return exists() && ( fileInfo.st_mode & S_IREAD );
}
bool ccppFile::writable() const
{
return exists() && ( fileInfo.st_mode & S_IWRITE );
}
bool ccppFile::isDirectory() const
{
return exists() && ( fileInfo.st_mode & S_IFDIR );
}
void ccppFile::deleteFile()
{
if( remove( name.c_str() ) == 0 )
{
removeFileFromDeleteList( name );
reset();
}
}
void ccppFile::deleteAtExit()
{
deleteAtExit( name, this );
}
void ccppFile::deleteAtExit( const String & fileName )
{
deleteAtExit( fileName, NULL );
}
void ccppFile::deleteAtExit( const String & fileName
, ccppFile * fileObject )
{
_2deleteAtexit.insert( std::make_pair( fileName, fileObject ) );
//if ( _2deleteAtexit.size() == 1 )
//atexit( filesDeleter );
}
void ccppFile::filesDeleter()
{
// the possible problem i can see at the moment - more than one object created
// for one file.
for ( FilesMap::iterator it= _2deleteAtexit.begin();
it !=_2deleteAtexit.end();
++it )
{
if ( it->second != NULL )
{
it->second->deleteFile();
}
else
remove( it->first.c_str() );
}
}
ccppFile * ccppFile::createTempFile( const String & prefix, const String & suffix )
{
/*
String name= prefix;
name+= suffix;*/
//probably had to add vacant file name picking.
ccppFile * file= new ccppFile( prefix + suffix );
file->deleteAtExit();
return file;
}
void ccppFile::close()
{
if ( stream.is_open() )
{
stream.close();
}
init();
}
String::size_type loadFile( String & container )
{
return container.length();
}
/*
bool OpenFile(std::ifstream & fileStream, const String & fileName
, const char * _possibleLocations[])
{
fileStream.open(fileName.c_str());
int i=0;
while (!fileStream.is_open()
&& _possibleLocations != NULL && _possibleLocations[ i ] != NULL)
{
fileStream.clear();
fileStream.open((String(_possibleLocations[ i ]) + "/" + fileName).c_str());
++i;
}
return fileStream.is_open();
}
int LoadProperties(const String & fileName, Properties & props
, const char * _possibleLocations[])
{
int counter=0;
std::ifstream propsFile;
if (OpenFile(propsFile, fileName, _possibleLocations))
{
String line;
while (getline(propsFile, line))
{
StringUtils::trim(line);
// Not empty line or a comment
if (!propsFile.eof() && line.size() > 0 && line.c_str()[0] != '#')
{
String::size_type pos=line.find("=");
if (pos != String::npos && pos > 0)
{
String key=StringUtils::trim(line.substr(0, pos));
String val=StringUtils::trim(line.substr(pos + 1));
props.insert(Properties::value_type(key, val));
++counter;
}
}
}
propsFile.close();
}
else
{
std::cout << "Unable to open file" << std::endl;
return -1;
}
return counter;
}*/
}
|