| 1 | /* |
|---|
| 2 | * $Id: Filename.cpp 122 2009-06-03 08:15:51Z rosiere $ |
|---|
| 3 | * |
|---|
| 4 | * [ Description ] |
|---|
| 5 | * |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "Common/include/Filename.h" |
|---|
| 9 | #include "Common/include/ToString.h" |
|---|
| 10 | #include <fstream> |
|---|
| 11 | #include <sstream> |
|---|
| 12 | #include <sys/time.h> |
|---|
| 13 | #include <unistd.h> |
|---|
| 14 | |
|---|
| 15 | namespace morpheo { |
|---|
| 16 | |
|---|
| 17 | std::string filename (std::string directory, |
|---|
| 18 | std::string filename_prefix, |
|---|
| 19 | std::string filename_suffix, |
|---|
| 20 | std::string extension, |
|---|
| 21 | bool with_date, |
|---|
| 22 | bool with_pid, |
|---|
| 23 | bool overwrite |
|---|
| 24 | ) |
|---|
| 25 | { |
|---|
| 26 | std::string _return = directory+"/"+filename_prefix; |
|---|
| 27 | |
|---|
| 28 | if (with_date) |
|---|
| 29 | { |
|---|
| 30 | time_t current_time; |
|---|
| 31 | time (¤t_time); |
|---|
| 32 | |
|---|
| 33 | struct tm * _localtime = localtime (¤t_time); |
|---|
| 34 | |
|---|
| 35 | int year = 1900+_localtime->tm_year; |
|---|
| 36 | int mon = 1+_localtime->tm_mon ; |
|---|
| 37 | int mday = _localtime->tm_mday; |
|---|
| 38 | int hour = _localtime->tm_hour; |
|---|
| 39 | int min = _localtime->tm_min ; |
|---|
| 40 | int sec = _localtime->tm_sec ; |
|---|
| 41 | |
|---|
| 42 | _return+= "_"+ toString<int>(year) |
|---|
| 43 | +((mon <10)?"0":"") + toString<int>(mon ) |
|---|
| 44 | +((mday<10)?"0":"") + toString<int>(mday) |
|---|
| 45 | +"_"+((hour<10)?"0":"") + toString<int>(hour) |
|---|
| 46 | +((min <10)?"0":"") + toString<int>(min ) |
|---|
| 47 | +((sec <10)?"0":"") + toString<int>(sec ); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | if (with_pid) |
|---|
| 51 | { |
|---|
| 52 | _return+="_"+toString<pid_t>(getpid()); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | _return+=filename_suffix; |
|---|
| 56 | |
|---|
| 57 | if (not overwrite) |
|---|
| 58 | { |
|---|
| 59 | int it = 0; |
|---|
| 60 | std::string _it = ""; |
|---|
| 61 | bool find; |
|---|
| 62 | do |
|---|
| 63 | { |
|---|
| 64 | find = false; |
|---|
| 65 | |
|---|
| 66 | _it = ((it!=0)?("_"+toString<int>(it)):""); |
|---|
| 67 | |
|---|
| 68 | std::ifstream inputStream; |
|---|
| 69 | std::string testname = _return+_it+"."+extension; |
|---|
| 70 | |
|---|
| 71 | inputStream.open(testname.c_str(),std::ios_base::in); |
|---|
| 72 | |
|---|
| 73 | if (inputStream) |
|---|
| 74 | { |
|---|
| 75 | find = true; |
|---|
| 76 | it ++; |
|---|
| 77 | } |
|---|
| 78 | inputStream.close(); |
|---|
| 79 | } |
|---|
| 80 | while (find); |
|---|
| 81 | |
|---|
| 82 | _return+= _it; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | _return+="."+extension; |
|---|
| 86 | |
|---|
| 87 | return _return; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | }; // end namespace morpheo |
|---|