| 1 |
#include "stdafx.h" |
|---|
| 2 |
#include "filefuncs.h" |
|---|
| 3 |
#include "PCRE/pme-1.0.4/pme.h" |
|---|
| 4 |
|
|---|
| 5 |
int determineEncoding(unsigned char* pBuf, int nLen, EElephantEncoding& eEncoding) { |
|---|
| 6 |
eEncoding = eUnknown; |
|---|
| 7 |
|
|---|
| 8 |
int nRet = 0; |
|---|
| 9 |
|
|---|
| 10 |
if (nLen > 1) { |
|---|
| 11 |
if (pBuf[0] == Utf8_16::k_Boms[eUtf16BigEndian][0] && pBuf[1] == Utf8_16::k_Boms[eUtf16BigEndian][1]) { |
|---|
| 12 |
eEncoding = eUtf16BigEndian; |
|---|
| 13 |
nRet = 2; |
|---|
| 14 |
} else if (pBuf[0] == Utf8_16::k_Boms[eUtf16LittleEndian][0] && pBuf[1] == Utf8_16::k_Boms[eUtf16LittleEndian][1]) { |
|---|
| 15 |
eEncoding = eUtf16LittleEndian; |
|---|
| 16 |
nRet = 2; |
|---|
| 17 |
} else if (nLen > 2 && pBuf[0] == Utf8_16::k_Boms[eUtf8][0] && pBuf[1] == Utf8_16::k_Boms[eUtf8][1] && pBuf[2] == Utf8_16::k_Boms[eUtf8][2]) { |
|---|
| 18 |
eEncoding = eUtf8; |
|---|
| 19 |
nRet = 3; |
|---|
| 20 |
} |
|---|
| 21 |
} |
|---|
| 22 |
|
|---|
| 23 |
return nRet; |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
EElephantSaveFormat determineLineEndings(char* pBuf, int nLen) |
|---|
| 27 |
{ |
|---|
| 28 |
char c, n, p; |
|---|
| 29 |
int linesCRLF, linesCR, linesLF; |
|---|
| 30 |
|
|---|
| 31 |
linesCRLF = linesCR = linesLF = 0; |
|---|
| 32 |
p = NULL; |
|---|
| 33 |
|
|---|
| 34 |
for(int i = 0; i < nLen; i++) |
|---|
| 35 |
{ |
|---|
| 36 |
c = pBuf[i]; |
|---|
| 37 |
n = ((i < nLen) ? pBuf[i+1] : NULL); |
|---|
| 38 |
|
|---|
| 39 |
if (c == '\r') |
|---|
| 40 |
{ |
|---|
| 41 |
if (n == '\n') |
|---|
| 42 |
{ |
|---|
| 43 |
linesCRLF++; |
|---|
| 44 |
// Skip the next character (\n). |
|---|
| 45 |
i++; |
|---|
| 46 |
p = '\n'; |
|---|
| 47 |
continue; |
|---|
| 48 |
} |
|---|
| 49 |
else |
|---|
| 50 |
linesCR++; |
|---|
| 51 |
} |
|---|
| 52 |
else if (c == '\n') |
|---|
| 53 |
{ |
|---|
| 54 |
linesLF++; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
p = c; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
if (((linesLF >= linesCR) && (linesLF > linesCRLF)) || ((linesLF > linesCR) && (linesLF >= linesCRLF))) |
|---|
| 61 |
return ElephantSF_Unix; |
|---|
| 62 |
else if (((linesCR >= linesLF) && (linesCR > linesCRLF)) || ((linesCR > linesLF) && (linesCR >= linesCRLF))) |
|---|
| 63 |
return ElephantSF_Mac; |
|---|
| 64 |
|
|---|
| 65 |
else if (((linesCRLF >= linesLF) && (linesCRLF > linesCR)) || ((linesCRLF > linesLF) && (linesCRLF >= linesCR))) |
|---|
| 66 |
return ElephantSF_Windows; |
|---|
| 67 |
|
|---|
| 68 |
// Default |
|---|
| 69 |
return ElephantSF_Windows; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
string GetPathFromFiles(vector<string>& paths, const string &file) |
|---|
| 74 |
{ |
|---|
| 75 |
for (int i = 0 ; i < paths.size(); i++) |
|---|
| 76 |
|
|---|
| 77 |
{ |
|---|
| 78 |
if ( StripFileName(paths[i]) == file) return paths[i]; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
return ""; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
string GetRelativePath( const string& basePath, const string& comparePath) |
|---|
| 85 |
{ |
|---|
| 86 |
vector<string> baseElements, compareElements; |
|---|
| 87 |
Split(basePath,"\\",baseElements); |
|---|
| 88 |
Split(comparePath,"\\",compareElements); |
|---|
| 89 |
int depth = 0; |
|---|
| 90 |
for ( int i = 0; i < baseElements.size(); i++ ) |
|---|
| 91 |
{ |
|---|
| 92 |
if ( i >= compareElements.size() ) { |
|---|
| 93 |
depth = i; |
|---|
| 94 |
break; |
|---|
| 95 |
} |
|---|
| 96 |
if ( baseElements[i] == compareElements[i]) continue; |
|---|
| 97 |
else { |
|---|
| 98 |
depth = i; |
|---|
| 99 |
break; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
string base; |
|---|
| 105 |
int fileCounter = depth; |
|---|
| 106 |
|
|---|
| 107 |
if ( depth == 0 ) |
|---|
| 108 |
{ |
|---|
| 109 |
fileCounter = baseElements.size(); |
|---|
| 110 |
} |
|---|
| 111 |
else { |
|---|
| 112 |
depth = baseElements.size() - depth; |
|---|
| 113 |
|
|---|
| 114 |
for (int j = 0 ; j < ( depth ); j++) { |
|---|
| 115 |
base += "..\\"; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
for ( int k = fileCounter;k < compareElements.size();k++) |
|---|
| 120 |
{ |
|---|
| 121 |
base += compareElements[k] + "\\"; |
|---|
| 122 |
} |
|---|
| 123 |
return base; |
|---|
| 124 |
|
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
void GetFilesFromDir(const string& dir, vector<string> &files, const string& extension) |
|---|
| 128 |
{ |
|---|
| 129 |
WIN32_FIND_DATA fdat; |
|---|
| 130 |
char x[500]; |
|---|
| 131 |
GetCurrentDirectory(500,x); |
|---|
| 132 |
if (!SetCurrentDirectory(dir.c_str())) |
|---|
| 133 |
{ |
|---|
| 134 |
//MessageBox(0,"Couldnt set directory",dir.c_str(),0); |
|---|
| 135 |
return; |
|---|
| 136 |
} |
|---|
| 137 |
HANDLE hfind=FindFirstFile("*",&fdat); |
|---|
| 138 |
|
|---|
| 139 |
while (hfind!=INVALID_HANDLE_VALUE) |
|---|
| 140 |
{ |
|---|
| 141 |
if ((strcmp(fdat.cFileName, ".") == 0) || (strcmp(fdat.cFileName,"..") ==0)) ; //if it is . or .. dont push it on |
|---|
| 142 |
else |
|---|
| 143 |
{ |
|---|
| 144 |
if ( extension == "" ) files.push_back(fdat.cFileName); |
|---|
| 145 |
else if (LowerCase(FileExtension(fdat.cFileName)) == extension ) files.push_back(fdat.cFileName); |
|---|
| 146 |
} |
|---|
| 147 |
if (!FindNextFile(hfind,&fdat)) {FindClose(hfind); hfind=INVALID_HANDLE_VALUE;} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
if (!SetCurrentDirectory(x)) |
|---|
| 151 |
{ |
|---|
| 152 |
return; |
|---|
| 153 |
MessageBox(0,"Couldnt set directory",x,0); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
FindClose(hfind); |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
string ReadTemplate(const string& path, const string& name,const string& pname) |
|---|
| 160 |
{ |
|---|
| 161 |
string buffer = ""; |
|---|
| 162 |
|
|---|
| 163 |
if (FileExists(path)) { |
|---|
| 164 |
string line = ""; |
|---|
| 165 |
ifstream in(path.c_str() ); |
|---|
| 166 |
while (!in.eof () ) { |
|---|
| 167 |
std::getline(in,line ); |
|---|
| 168 |
buffer += ReplaceConstants(line, name, pname) + "\n"; |
|---|
| 169 |
} |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
return buffer; |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
bool CopyTemplateFile(const string& source, const string& dest, const string & name, const string& pname ) |
|---|
| 178 |
{ |
|---|
| 179 |
|
|---|
| 180 |
if ( !FileExists(source)) return false; |
|---|
| 181 |
|
|---|
| 182 |
ifstream in(source.c_str()); |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
if (!in.good() ) return false; |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
string line = "", buffer = ""; |
|---|
| 190 |
|
|---|
| 191 |
while (!in.eof () ) { |
|---|
| 192 |
std::getline(in,line ); |
|---|
| 193 |
buffer += ReplaceConstants(line, name, pname) + "\n"; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
in.close(); |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
ofstream out(dest.c_str()); |
|---|
| 200 |
|
|---|
| 201 |
if (!out.good() ) return false; |
|---|
| 202 |
|
|---|
| 203 |
out << buffer; |
|---|
| 204 |
out.close(); |
|---|
| 205 |
|
|---|
| 206 |
return true; |
|---|
| 207 |
|
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
bool ReplaceLine(const string& path, int lineNo, const string& regex, const string& replaceRegex, const string& options ) |
|---|
| 211 |
{ |
|---|
| 212 |
|
|---|
| 213 |
PME reg(regex, options); |
|---|
| 214 |
|
|---|
| 215 |
if ( !FileExists(path)) return false; |
|---|
| 216 |
|
|---|
| 217 |
ifstream in(path.c_str()); |
|---|
| 218 |
int currentLine = 1; |
|---|
| 219 |
string buffer, line; |
|---|
| 220 |
while ( !in.eof() ) |
|---|
| 221 |
{ |
|---|
| 222 |
std::getline(in,line); |
|---|
| 223 |
|
|---|
| 224 |
if ( currentLine == lineNo) |
|---|
| 225 |
{ |
|---|
| 226 |
line = reg.sub( line, replaceRegex); |
|---|
| 227 |
buffer += line + "\n"; |
|---|
| 228 |
|
|---|
| 229 |
} |
|---|
| 230 |
else buffer += line + "\n"; |
|---|
| 231 |
|
|---|
| 232 |
currentLine++; |
|---|
| 233 |
|
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
in.close(); |
|---|
| 237 |
|
|---|
| 238 |
ofstream out(path.c_str()); |
|---|
| 239 |
out << buffer; |
|---|
| 240 |
out.close(); |
|---|
| 241 |
|
|---|
| 242 |
return true; |
|---|
| 243 |
|
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
void readImportDirFiles(const string& dir, vector<string> &files, bool stripExtension) |
|---|
| 248 |
{ |
|---|
| 249 |
WIN32_FIND_DATA fdat; |
|---|
| 250 |
char x[500]; |
|---|
| 251 |
GetCurrentDirectory(500,x); |
|---|
| 252 |
if (!SetCurrentDirectory(dir.c_str())) return; |
|---|
| 253 |
HANDLE hfind=FindFirstFile("*",&fdat); |
|---|
| 254 |
|
|---|
| 255 |
while (hfind!=INVALID_HANDLE_VALUE) |
|---|
| 256 |
{ |
|---|
| 257 |
if ((strcmp(fdat.cFileName, ".") == 0) || (strcmp(fdat.cFileName,"..") ==0)) ; //if it is . or .. dont push it on |
|---|
| 258 |
else { |
|---|
| 259 |
|
|---|
| 260 |
DWORD Code = GetFileAttributes(fdat.cFileName); |
|---|
| 261 |
if ( !( Code & FILE_ATTRIBUTE_DIRECTORY ) ) { |
|---|
| 262 |
|
|---|
| 263 |
if ( FileExtension(fdat.cFileName) == "d" ) |
|---|
| 264 |
{ |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
string str = fdat.cFileName; |
|---|
| 268 |
StripExtension(str); |
|---|
| 269 |
files.push_back(str); |
|---|
| 270 |
|
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
} |
|---|
| 274 |
} |
|---|
| 275 |
if (!FindNextFile(hfind,&fdat)) {FindClose(hfind); hfind=INVALID_HANDLE_VALUE;} |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
if (!SetCurrentDirectory(x)) return; |
|---|
| 279 |
FindClose(hfind); |
|---|
| 280 |
} |
|---|
| 281 |
void readImportDirDirs(const string& dir, vector<string> &files ) |
|---|
| 282 |
{ |
|---|
| 283 |
WIN32_FIND_DATA fdat; |
|---|
| 284 |
char x[500]; |
|---|
| 285 |
GetCurrentDirectory(500,x); |
|---|
| 286 |
if (!SetCurrentDirectory(dir.c_str())) return; |
|---|
| 287 |
|
|---|
| 288 |
HANDLE hfind=FindFirstFile("*",&fdat); |
|---|
| 289 |
while (hfind!=INVALID_HANDLE_VALUE) |
|---|
| 290 |
{ |
|---|
| 291 |
DWORD Code = GetFileAttributes(fdat.cFileName); |
|---|
| 292 |
if ((Code != -1) && (Code & FILE_ATTRIBUTE_DIRECTORY )) { |
|---|
| 293 |
if ((strcmp(fdat.cFileName, ".") == 0) || (strcmp(fdat.cFileName,"..") ==0)) ; //if it is . or .. dont push it on |
|---|
| 294 |
else |
|---|
| 295 |
{ |
|---|
| 296 |
// if ( markAsDir ) |
|---|
| 297 |
// { |
|---|
| 298 |
// string temp = fdat.cFileName; |
|---|
| 299 |
// temp += "/"; |
|---|
| 300 |
// files.push_back(temp); |
|---|
| 301 |
//changed my mind on this one, redo later |
|---|
| 302 |
// files.push_back(fdat.cFileName); |
|---|
| 303 |
// } |
|---|
| 304 |
// else |
|---|
| 305 |
// { |
|---|
| 306 |
|
|---|
| 307 |
files.push_back(fdat.cFileName); |
|---|
| 308 |
//} |
|---|
| 309 |
|
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
} |
|---|
| 313 |
|
|---|
| 314 |
if (!FindNextFile(hfind,&fdat)) { |
|---|
| 315 |
FindClose(hfind); |
|---|
| 316 |
hfind=INVALID_HANDLE_VALUE; |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
} |
|---|
| 320 |
if (!SetCurrentDirectory(x)) return; |
|---|
| 321 |
FindClose(hfind); |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
void readImportDirContents(const string& dir, vector<string> &files) |
|---|
| 325 |
{ |
|---|
| 326 |
readImportDirDirs(dir,files); |
|---|
| 327 |
readImportDirFiles(dir,files,true); |
|---|
| 328 |
|
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
bool CreateDirRecursive(const string& dir ) |
|---|
| 332 |
{ |
|---|
| 333 |
vector<string> items; |
|---|
| 334 |
Split(dir,"\\",items ); |
|---|
| 335 |
string createDir; |
|---|
| 336 |
for ( int i = 0 ; i < items.size();i++ ) |
|---|
| 337 |
{ |
|---|
| 338 |
|
|---|
| 339 |
createDir += items[i] + "\\"; |
|---|
| 340 |
// AfxMessageBox(createDir.c_str() ); |
|---|
| 341 |
if ( !DirectoryExists(createDir) ) |
|---|
| 342 |
{ |
|---|
| 343 |
if ( !CreateDirectory(createDir.c_str(),NULL ) ) |
|---|
| 344 |
return false; |
|---|
| 345 |
} |
|---|
| 346 |
// else AfxMessageBox(string("Dir exists" + createDir ) .c_str() ); |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
return true; |
|---|
| 352 |
} |
|---|