[go: up one dir, main page]

Menu

[r9]: / kernel.hpp  Maximize  Restore  History

Download this file

326 lines (271 with data), 9.4 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Copyright (C) 2010 Arnaud Champenois arthelion92@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KERNEL_H
#define KERNEL_H
#include <string>
#include <sstream>
#include <iomanip>
#include <strings.h>
#define SIZEOF_ARRAY(a) (sizeof(a)/sizeof(a[0]))
template <class T> inline void swap(T& a,T& b)
{ T c = b; b = a; a = c; }
// Class to manage filename components.
// Manages / and windows \ in directory
class FileNameString
{
public:
inline FileNameString();
inline FileNameString(const char*aPathName);
inline FileNameString(const std::string&aPathName);
inline FileNameString(const FileNameString&aPathName);
inline ~FileNameString();
inline FileNameString& operator =(const FileNameString& aPathName);
inline FileNameString& operator =(const char*aPathName);
inline FileNameString& operator =(const std::string&aPathName);
// Full patch, with an optional prefix number on the filename
inline const char*GetPath(int aFileNumber=0) const;
// Drive letter with ':'
inline void SetDrive(const std::string& aDrive);
inline const char* GetDrive() const;
// Directory path, no trailing slash
inline void SetDirectory(const std::string& aPath);
inline void AppendDirectory(const std::string& aPath);
inline const char*GetDirectory() const;
// Full directory with drive, no trailing slash
inline std::string GetFullDirectory() const;
// FileName with extension
inline void SetFileName(const std::string&aFileName);
inline std::string GetFileName() const;
// BaseFileName without extension
inline void SetBaseFileName(const std::string&aFileName);
inline const char*GetBaseFileName() const;
inline void AppendBaseFileName(const std::string&aString);
inline void PrependBaseFileName(const std::string&aString);
// Extension
inline void SetExtension(const std::string&anExtension);
inline const char*GetExtension() const;
inline bool TestExtension(const char*) const;
inline bool HasExtension() const;
// Is there somethin missing to define the path ?
inline bool IsEmpty() const;
inline int GetNumber() const;
protected:
inline void Split();
inline void SplitFileName();
inline void Merge(int aFileNumber) const;
inline void TransfoToSlash(std::string& aString);
private:
std::string mDrive;
std::string mPath;
std::string mFileName;
std::string mExtension;
mutable std::string mFullName;
};
//====================================
// implementation
FileNameString::FileNameString(const char*aFilename)
{ if(aFilename)
mFullName = aFilename; else mFullName.clear();
Split(); }
FileNameString:: FileNameString() {};
FileNameString::FileNameString(const std::string&aString)
{ mFullName = aString; Split(); }
FileNameString::FileNameString(const FileNameString&aFileName)
{
mFullName = aFileName.mFullName; Split();
}
FileNameString::~FileNameString() {};
FileNameString& FileNameString::operator =(const FileNameString& aFileName)
{ mFullName = aFileName.mFullName; Split(); return *this; }
FileNameString& FileNameString::operator =(const char*aPathName)
{ if(aPathName)
mFullName = aPathName; else mFullName.clear();
Split(); return *this; }
FileNameString& FileNameString::operator =(const std::string&aString)
{ mFullName = aString; Split(); return *this; }
const char*FileNameString::GetPath(int aFileNumber) const
{ if(IsEmpty()) return NULL; Merge(aFileNumber); return mFullName.c_str();}
// Drive
void FileNameString::SetDrive(const std::string& aDrive)
{ mDrive = aDrive; if(!mDrive.empty() && mDrive[mDrive.length()-1] != ':') mDrive += ':'; }
const char*FileNameString::GetDrive() const { return mDrive.c_str(); }
void FileNameString::TransfoToSlash(std::string& aString)
{
#if defined(__WIN32__)
std::string::iterator anIter;
for(anIter = aString.begin(); anIter != aString.end();anIter++)
{ if(*anIter == '\\') *anIter = '/'; }
#endif
}
// Path
void FileNameString::SetDirectory(const std::string& aPath)
{ mPath = aPath;
TransfoToSlash(mPath);
std::string::size_type aDriveIndex;
aDriveIndex = mPath.find(':');
if(aDriveIndex != std::string::npos)
{
mDrive.assign(mPath,0,aDriveIndex);
aDriveIndex++;
std::string aNewPath;
aNewPath.assign(mPath,aDriveIndex,mPath.length());
mPath = aNewPath;
}
int aLength = mPath.length();
while(aLength && mPath[aLength-1] == '/') { mPath.erase(aLength-1); aLength--; }
}
void FileNameString::AppendDirectory(const std::string& aPath)
{
if(aPath.empty()) return;
std::string aNewPath = aPath;
TransfoToSlash(aNewPath);
if(aNewPath[0] != '/')
mPath += '/';
mPath += aNewPath;
}
const char*FileNameString::GetDirectory() const { return mPath.c_str(); }
// Full directory
std::string FileNameString::GetFullDirectory() const
{
std::string aString;
if(!mDrive.empty()) { aString = mDrive; aString += ':'; }
aString += mPath;
return aString;
}
// FileName
void FileNameString::SetFileName(const std::string&aFileName)
{ mFileName = aFileName; SplitFileName(); }
std::string FileNameString::GetFileName() const
{ std::string aString = mFileName; aString += '.'; aString += mExtension; return aString; }
// BaseFileName
void FileNameString::SetBaseFileName(const std::string&aFileName)
{ mFileName = aFileName; }
const char*FileNameString::GetBaseFileName() const { return mFileName.c_str(); }
void FileNameString::AppendBaseFileName(const std::string&aString)
{ mFileName += aString; }
void FileNameString::PrependBaseFileName(const std::string&aString)
{ mFileName = aString+mFileName; }
// Extension
void FileNameString::SetExtension(const std::string&anExtension)
{ if(anExtension.empty()) mExtension.clear();
else
{ if(anExtension[0] == '.') mExtension = anExtension.c_str()+1; else mExtension = anExtension; }
}
const char*FileNameString::GetExtension() const
{ return mExtension.c_str(); }
bool FileNameString::TestExtension(const char*anExtension) const
{ return (strcasecmp(mExtension.c_str(),anExtension) == 0); }
bool FileNameString::HasExtension() const
{ return (!mExtension.empty()); }
bool FileNameString::IsEmpty() const
{ return mFileName.empty(); }
void FileNameString::Split()
{
mDrive.clear(); mPath.clear(); mFileName.clear(); mExtension.clear();
std::string::size_type aDriveIndex,aSlashIndex;
TransfoToSlash(mFullName);
aDriveIndex = mFullName.find(':');
if(aDriveIndex != std::string::npos)
{
mDrive.assign(mFullName,0,aDriveIndex);
aDriveIndex++;
}
else aDriveIndex = 0;
aSlashIndex = mFullName.rfind('/');
if(aSlashIndex != std::string::npos)
{
if(aSlashIndex>aDriveIndex)
{
mPath.assign(mFullName,aDriveIndex,aSlashIndex-aDriveIndex);
size_t aLength = mPath.length();
while(aLength && mPath[aLength-1] == '/') { mPath.erase(aLength-1); aLength--; }
aSlashIndex++;
}
}
else
aSlashIndex = aDriveIndex;
if(aSlashIndex<mFullName.length())
{
mFileName = mFullName.c_str()+aSlashIndex;
SplitFileName();
}
}
void FileNameString::SplitFileName()
{
mExtension.clear();
std::string::size_type aDotIndex;
aDotIndex = mFileName.rfind('.');
if(aDotIndex != std::string::npos)
{
mExtension = mFileName.c_str()+aDotIndex+1;
mFileName.erase(aDotIndex,mFileName.length()-aDotIndex);
}
}
inline int FileNameString::GetNumber() const
{
if(mFileName.empty()) return 0;
std::string aPreNumber;
const char*aPtr = mFileName.c_str();
bool aHasSep = false;
while(*aPtr && (isdigit(*aPtr) || *aPtr == '_' || *aPtr == '-' || *aPtr == ' '))
{
aHasSep = (*aPtr == '_' || *aPtr == '-' || *aPtr == ' ');
aPreNumber += *aPtr;
aPtr++;
}
return atoi(aPreNumber.c_str());
}
void FileNameString::Merge(int aFileNumber) const
{
mFullName.clear();
if(!mDrive.empty())
{
mFullName = mDrive;
mFullName += ':';
}
mFullName += mPath;
if(!mFileName.empty())
{
if(!mPath.empty())
mFullName += '/';
if(aFileNumber>0)
{
std::string aPreNumber;
const char*aPtr = mFileName.c_str();
bool aHasSep = false;
char aSep = '-';
int aCount = 0;
while(*aPtr && (isdigit(*aPtr) || *aPtr == '_' || *aPtr == '-' || *aPtr == ' '))
{
aHasSep = (*aPtr == '_' || *aPtr == '-' || *aPtr == ' ');
if(aHasSep) aSep = *aPtr;
else aCount++;
aPreNumber += *aPtr;
aPtr++;
}
std::ostringstream os;
if(aCount == 0) aCount = 3;
os << std::setw(aCount) << std::setfill('0') << aFileNumber << std::setw(0) << aSep << aPtr;
mFullName += os.str();
}
else
mFullName += mFileName;
}
if(!mExtension.empty())
{
mFullName += '.';
mFullName += mExtension;
}
}
#endif