00001 #pragma ident "$Id: FileHunter.cpp 1470 2008-11-20 00:35:59Z ckiesch $"
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00051 #include "FileHunter.hpp"
00052
00053 using namespace std;
00054 using namespace gpstk;
00055 using namespace gpstk::StringUtils;
00056
00057
00058 #ifndef _WIN32
00059 #include <unistd.h>
00060 #include <dirent.h>
00061 #else
00062 #include <io.h>
00063 #include <direct.h>
00064 #define PATH_MAX _MAX_PATH
00065 #endif
00066
00067 namespace gpstk
00068 {
00069 FileHunter::FileHunter(const string& filespec)
00070 throw(FileHunterException)
00071 {
00072 try
00073 {
00074 init(filespec);
00075 }
00076 catch (FileHunterException& e)
00077 {
00078 GPSTK_RETHROW(e);
00079 }
00080 }
00081
00082 FileHunter::FileHunter(const FileSpec& filespec)
00083 throw(FileHunterException)
00084 {
00085 try
00086 {
00087 init(filespec.getSpecString());
00088 }
00089 catch (FileHunterException& e)
00090 {
00091 GPSTK_RETHROW(e);
00092 }
00093 }
00094
00095 FileHunter& FileHunter::newHunt(const string& filespec)
00096 throw(FileHunterException)
00097 {
00098 try
00099 {
00100 init(filespec);
00101 }
00102 catch (FileHunterException& e)
00103 {
00104 GPSTK_RETHROW(e);
00105 }
00106 return *this;
00107 }
00108
00109 FileHunter& FileHunter::setFilter(const FileSpec::FileSpecType fst,
00110 const vector<string>& filter)
00111 throw(FileHunterException)
00112 {
00113
00114 vector<FileSpec>::iterator itr = fileSpecList.begin();
00115 while (itr != fileSpecList.end())
00116 {
00117 if ((*itr).hasField(fst))
00118 break;
00119 itr++;
00120 }
00121
00122 if (itr != fileSpecList.end())
00123 {
00124 filterList.push_back(FilterPair(fst, filter));
00125 return *this;
00126 }
00127
00128 else
00129 {
00130 FileHunterException fhe("The FileSpec does not have a field: " +
00131 FileSpec::convertFileSpecType(fst));
00132 return *this;
00133 }
00134 }
00135
00136 vector<string> FileHunter::find(const DayTime& start,
00137 const DayTime& end,
00138 const FileSpec::FileSpecSortType fsst,
00139 enum FileChunking chunk) const
00140 throw(FileHunterException)
00141 {
00142
00143
00144 if (end < start)
00145 {
00146 FileHunterException fhe("The times are specified incorrectly");
00147 GPSTK_THROW(fhe);
00148 }
00149
00150
00151
00152 DayTime exStart;
00153 switch(chunk)
00154 {
00155 case WEEK:
00156 exStart = DayTime(start.GPSfullweek(), 0.0, start.GPSyear());
00157 break;
00158 case DAY:
00159 exStart = DayTime(start.DOYyear(), start.DOYday(), 0.0);
00160 break;
00161 case HOUR:
00162 exStart = DayTime(start.year(), start.month(),
00163 start.day(), start.hour(),
00164 0, 0.0);
00165 break;
00166 case MINUTE:
00167 exStart = DayTime(start.year(), start.month(),
00168 start.day(), start.hour(),
00169 start.minute(), 0.0);
00170 break;
00171 }
00172
00173 vector<string> toReturn;
00174
00175 toReturn.push_back(string());
00176
00177 try
00178 {
00179 vector<FileSpec>::const_iterator itr = fileSpecList.begin();
00180
00181 #ifdef _WIN32
00182
00183 if (itr != fileSpecList.end())
00184 {
00185 toReturn[0] = (*itr).getSpecString() + string(1,'\\');
00186 itr++;
00187 }
00188 #endif
00189 #ifdef __CYGWIN__
00190
00191
00192 if (itr != fileSpecList.end())
00193 {
00194 toReturn[0] = string(1,slash) + (*itr).getSpecString();
00195 itr++;
00196 }
00197 #endif
00198
00199 while (itr != fileSpecList.end())
00200 {
00201 vector<string> toReturnTemp;
00202
00203
00204 vector<string>::size_type i,j;
00205
00206 for(i = 0; i < toReturn.size(); i++)
00207 {
00208
00209
00210
00211
00212
00213
00214
00215
00216 vector<string> newEntries = searchHelper(toReturn[i],*itr);
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 filterHelper(newEntries, *itr);
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 for(j = 0; j < newEntries.size(); j++)
00253 {
00254 try
00255 {
00256 DayTime fileDT = (*itr).extractDayTime(newEntries[j]);
00257 if ( (fileDT >= exStart) && (fileDT <= end) )
00258 {
00259 #ifdef _WIN32
00260 if (toReturn[i].empty())
00261 toReturnTemp.push_back(newEntries[j]);
00262 else
00263 {
00264 if ( toReturn[i][toReturn[i].size()-1]=='\\')
00265 toReturnTemp.push_back(toReturn[i] + newEntries[j]);
00266 else
00267 toReturnTemp.push_back(toReturn[i] + string(1,'\\') +
00268 newEntries[j]);
00269 }
00270 #else
00271 toReturnTemp.push_back(toReturn[i] + string(1,slash) +
00272 newEntries[j]);
00273 #endif
00274 }
00275 }
00276
00277
00278
00279 catch (FileSpecException &e)
00280 {
00281 #ifdef _WIN32
00282 if (toReturn[i].empty())
00283 toReturnTemp.push_back(newEntries[j]);
00284 else
00285 {
00286 if ( toReturn[i][toReturn[i].size()-1]=='\\')
00287 toReturnTemp.push_back(toReturn[i] + newEntries[j]);
00288 else
00289 toReturnTemp.push_back(toReturn[i] + string(1,'\\') +
00290 newEntries[j]);
00291 }
00292 #else
00293 toReturnTemp.push_back(toReturn[i] + string(1,slash) +
00294 newEntries[j]);
00295 #endif
00296 }
00297 }
00298 }
00299
00300 toReturn = toReturnTemp;
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 if (toReturn.empty())
00319 return toReturn;
00320
00321 itr++;
00322 }
00323
00324
00325 itr--;
00326 (*itr).sortList(toReturn, fsst);
00327
00328 return toReturn;
00329 }
00330 catch(...)
00331 {
00332 return toReturn;
00333 }
00334 }
00335
00336 void FileHunter::init(const string& filespec)
00337 throw(FileHunterException)
00338 {
00339 try
00340 {
00341 fileSpecList.clear();
00342 filterList.clear();
00343
00344 string fs(filespec);
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 #ifdef __CYGWIN__
00360
00361 char backSlash = '\\';
00362 string::size_type st;
00363 if (fs[1] == ':')
00364 {
00365
00366
00367 char driveLetter = fs[0];
00368
00369
00370 while ((st = fs.find( backSlash )) != fs.npos)
00371 {
00372
00373 fs = fs.replace(st, 1, 1, slash );
00374 }
00375
00376
00377
00378
00379 fs.erase(0,2);
00380
00381
00382
00383 fs.insert(0, 1, driveLetter);
00384 fs.insert(0,"/cygdrive/");
00385 }
00386 else
00387 {
00388
00389
00390 char* cwd = getcwd(NULL, PATH_MAX);
00391
00392
00393 while ((st = fs.find( backSlash )) != fs.npos)
00394 {
00395
00396 fs = fs.replace(st, 1, 1, slash );
00397 }
00398
00399
00400
00401 if (fs[0]!=slash)
00402 {
00403 fs.insert(0,string(1,slash));
00404 fs.insert(0,cwd);
00405 }
00406 }
00407
00408
00409 #endif
00410
00411
00412
00413 #ifndef _WIN32
00414 if (fs[0] != slash)
00415 {
00416
00417
00418
00419 char* cwd = getcwd(NULL, PATH_MAX);
00420
00421 if (cwd == NULL)
00422 {
00423 FileHunterException fhe("Cannot get working directory");
00424 GPSTK_THROW(fhe);
00425 }
00426 string wd(cwd);
00427
00428 if (wd[wd.size()-1] != slash)
00429 wd += std::string(1,slash);
00430 fs.insert(0, wd);
00431 free(cwd);
00432 }
00433
00434
00435 if (fs[fs.size()-1] != '/') fs += std::string(1,'/');
00436 #else
00437
00438
00439 if (fs[1]!=':')
00440 {
00441 char* cwdW = _getcwd(NULL, PATH_MAX);
00442 if (cwdW == NULL)
00443 {
00444 FileHunterException fhe("Cannot get working directory");
00445 GPSTK_THROW(fhe);
00446 }
00447 string wdW(cwdW);
00448
00449
00450 if (wdW[wdW.size()-1] != '\\')
00451 wdW += std::string(1,'\\');
00452 fs.insert(0, wdW);
00453 free(cwdW);
00454 }
00455
00456
00457 if (fs[fs.size()-1] != '\\') fs += std::string(1,'\\');
00458 #endif
00459
00460
00461
00462 while (!fs.empty())
00463 {
00464 #ifndef _WIN32
00465 if (fs[0] != slash)
00466 {
00467 FileHunterException fhe("Unexpected character: " +
00468 fs.substr(0,1));
00469 GPSTK_THROW(fhe);
00470 }
00471 else
00472
00473 fs.erase(0,1);
00474
00475 string::size_type slashpos = fs.find(slash);
00476 FileSpec tempfs(fs.substr(0, slashpos));
00477
00478
00479
00480
00481
00482 if (slashpos!=string::npos) fileSpecList.push_back(tempfs);
00483 fs.erase(0, slashpos);
00484 #else
00485
00486 if (fs[0] == '\\') fs.erase(0,1);
00487 string::size_type slashpos;
00488 slashpos = fs.find('\\');
00489 FileSpec tempfs(fs.substr(0, slashpos));
00490
00491 if (slashpos!=string::npos) fileSpecList.push_back(tempfs);
00492 fs.erase(0, slashpos);
00493 #endif
00494 }
00495 }
00496 catch(FileHunterException &e)
00497 {
00498 GPSTK_RETHROW(e);
00499 }
00500 catch(FileSpecException &e)
00501 {
00502 FileHunterException fhe(e);
00503 fhe.addText("Error in the file spec");
00504 GPSTK_THROW(fhe);
00505 }
00506 catch(Exception &e)
00507 {
00508 FileHunterException fhe(e);
00509 GPSTK_THROW(fhe);
00510 }
00511 catch(std::exception &e)
00512 {
00513 FileHunterException fhe("std::exception caught: " + string(e.what()));
00514 GPSTK_THROW(fhe);
00515 }
00516 catch(...)
00517 {
00518 FileHunterException fhe("unknown exception caught");
00519 GPSTK_THROW(fhe);
00520 }
00521 }
00522
00523 vector<string> FileHunter::searchHelper(const string& directory,
00524 const FileSpec& fs) const
00525 throw(FileHunterException)
00526 {
00527 try
00528 {
00529 vector<string> toReturn;
00530
00531
00532 string searchString = fs.createSearchString();
00533
00534 #ifndef _WIN32
00535
00536 DIR* theDir;
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 if (directory.empty())
00548 theDir = opendir(std::string(1,slash).c_str());
00549 else
00550 theDir = opendir(directory.c_str());
00551
00552
00553
00554 if (theDir == NULL)
00555 {
00556 FileHunterException fhe("Cannot open directory: " + directory);
00557 GPSTK_THROW(fhe);
00558 }
00559
00560
00561 struct dirent* entry;
00562
00563 while ( (entry = readdir(theDir)) != NULL)
00564 {
00565 string filename(entry->d_name);
00566
00567
00568
00569
00570 if ((filename.length() == searchString.length()) &&
00571 (filename != ".") && (filename != "..") &&
00572 isLike(filename, searchString, '*', '+', '?'))
00573 {
00574 toReturn.push_back(filename);
00575 }
00576 }
00577
00578
00579
00580 if (closedir(theDir) != 0)
00581 {
00582 FileHunterException fhe("Error closing directory: " +
00583 directory);
00584 GPSTK_THROW(fhe);
00585 }
00586 #endif
00587 #ifdef _WIN32
00588
00589 char* cwd = _getcwd(NULL, PATH_MAX);
00590 _chdir(directory.c_str());
00591
00592 struct _finddata_t c_file;
00593 long hFile;
00594
00595 if ( (hFile = _findfirst( searchString.c_str(), &c_file )) != -1 )
00596 {
00597 std::string filename(c_file.name);
00598 if ((filename != ".") && (filename != ".."))
00599 {
00600 toReturn.push_back(filename);
00601 }
00602 while( _findnext( hFile, &c_file ) == 0 )
00603 {
00604 filename = std::string(c_file.name);
00605 if ((filename != ".") && (filename != ".."))
00606 {
00607 toReturn.push_back(filename);
00608 }
00609 }
00610 }
00611 _findclose(hFile);
00612 _chdir(cwd);
00613 #endif
00614 return toReturn;
00615 }
00616 catch(Exception& e)
00617 {
00618 FileHunterException fhe(e);
00619 fhe.addText("Search failed");
00620 GPSTK_THROW(fhe);
00621 }
00622 catch(std::exception& e)
00623 {
00624 FileHunterException fhe("std::exception caught: " + string(e.what()));
00625 fhe.addText("Search failed");
00626 GPSTK_THROW(fhe);
00627 }
00628 catch(...)
00629 {
00630 FileHunterException fhe("unknown exception");
00631 fhe.addText("Search failed");
00632 GPSTK_THROW(fhe);
00633 }
00634 }
00635
00636 void FileHunter::filterHelper(vector<std::string>& fileList,
00637 const FileSpec& fs) const
00638 throw(FileHunterException)
00639 {
00640
00641
00642
00643
00644 vector<FilterPair>::const_iterator filterItr = filterList.begin();
00645 while(filterItr != filterList.end())
00646 {
00647
00648 if (fs.hasField((*filterItr).first))
00649 {
00650
00651
00652
00653 vector<string>::iterator fileListItr = fileList.begin();
00654 while (fileListItr != fileList.end())
00655 {
00656
00657
00658 string thisField = fs.extractField(*fileListItr,
00659 (*filterItr).first);
00660
00661 vector<string>::const_iterator filterStringItr =
00662 (*filterItr).second.begin();
00663
00664
00665
00666
00667 while (filterStringItr != (*filterItr).second.end())
00668 {
00669 if (thisField == rightJustify(*filterStringItr,
00670 thisField.size(),
00671 '0'))
00672 break;
00673 filterStringItr++;
00674 }
00675
00676 if (filterStringItr == (*filterItr).second.end())
00677 fileList.erase(fileListItr);
00678 else
00679 fileListItr++;
00680 }
00681 }
00682 filterItr++;
00683 }
00684 }
00685
00686
00687 void FileHunter::dump(ostream& o) const
00688 {
00689 vector<FileSpec>::const_iterator itr = fileSpecList.begin();
00690 while(itr != fileSpecList.end())
00691 {
00692 (*itr).dump(o);
00693 itr++;
00694 }
00695 }
00696
00697 }
00698