[go: up one dir, main page]

Menu

[r269]: / gizmod3 / libH / UtilFile.cpp  Maximize  Restore  History

Download this file

172 lines (149 with data), 4.1 kB

  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
/**
*********************************************************************
*************************************************************************
***
*** \file UtilFile.cpp
*** \brief UtilFile class body
***
*****************************************
*****************************************
**/
/*
Copyright (c) 2007, Tim Burrell
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "UtilFile.hpp"
#include "Debug.hpp"
#include <fstream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#ifndef WIN32
#include <dirent.h>
#include <unistd.h>
#else
#include <Windows.h>
#include <shlobj.h>
#endif
#include <fcntl.h>
#include <time.h>
using namespace H;
using namespace std;
////////////////////////////////////////////////////////////////////////////
// Defines / Type Defs
///////////////////////////////////////
/**
* \def DIRCHAR
* \brief Platform dependant directory separator char
**/
/**
* \def DIRSTR
* \brief Platform dependant directory separator string
**/
#ifdef WIN32
#define DIRCHAR '\\'
#define DIRSTR "\\"
#else
#define DIRCHAR '/'
#define DIRSTR "/"
#endif
////////////////////////////////////////////////////////////////////////////
// Construction / Deconstruction
///////////////////////////////////////
/**
* \brief Default Constructor
**/
UtilFile::UtilFile() {
}
/**
* \brief Destructor
**/
UtilFile::~UtilFile() {
}
////////////////////////////////////////////////////////////////////////////
// Class Body
///////////////////////////////////////
/**
* \brief Create a directory
* \param FileName The directory to create
* \return true on success
**/
bool UtilFile::createDirectory(std::string const & FileName) {
#ifndef WIN32
if (mkdir(FileName.c_str(), 0755) == -1)
return false;
else
return true;
#else
return CreateDirectory(FileName, NULL) != 0;
#endif
}
/**
* \brief Change a file path from relative to absolute
* \param FilePath The path
**/
void UtilFile::relativeToAbsolute(std::string & FilePath) {
size_t tPos;
if ((tPos = FilePath.find("~" DIRSTR)) == string::npos)
return;
const char * home = getenv("HOME");
if (!home)
return;
FilePath = FilePath.substr(0, tPos) + home + DIRSTR + FilePath.substr(2);
}
/**
* \brief Touch a file
* \param FilePath The file / directory to touch
* \return true on success
**/
bool UtilFile::touch(std::string const & FilePath) {
// check if it's a directory
if (FilePath[FilePath.length() - 1] == DIRCHAR) {
// create a dir
return createDirectory(FilePath);
} else {
// create a file
ofstream oFile(FilePath.c_str(), ios::app);
if (!oFile.is_open())
return false;
return true;
}
}
/**
* \brief Recursively touch a file
* \param FilePath The file to touch
* \param DoRecursive Do the operation recursively if true
* \return true on success
**/
bool UtilFile::touchRecursive(std::string const & FilePath, bool DoRecursive) {
// let's try to create it!
if (touch(FilePath))
return true;
// recursion requested?
if (!DoRecursive)
return false;
// if still no go break it down into smaller pieces
string strFilePath = FilePath;
int Modifier = 0;
if (strFilePath[strFilePath.length() - 1] == DIRCHAR)
Modifier = 1;
size_t sPos = strFilePath.rfind(DIRSTR, strFilePath.length() - Modifier - 1);
if (sPos == string::npos)
return touch(FilePath);
string SubLevel = strFilePath.substr(0, sPos + 1);
if (!touchRecursive(SubLevel, true))
return false;
return touch(FilePath);
}