#include "DPathUtils.h" #include using namespace std; using namespace ccdb; //______________________________________________________________________________ string ccdb::DPathUtils::ExtractDirectory( const string& path ) { return path.substr( 0, path.find_last_of( '/' ) ); //will get directory without final / } //______________________________________________________________________________ string ccdb::DPathUtils::ExtractObjectname( const string& path ) { return path.substr( path.find_last_of( '/' ) +1 ); } //______________________________________________________________________________ string ccdb::DPathUtils::CombinePath( const string& left, const string& right ) { if(right.length()==0) return left; if(left.length()==0) return right; char separator = '/'; string result = left; bool needAddSeparator = false; char leftLast = left[left.length()-1]; char rightFirst = right[0]; if((leftLast == separator) && (rightFirst == separator)) { //it is a situation we have both "left/ + /right" //so erase one of them result.erase(result.length()); //needAddSeparator should be false by default so we dont touch it } else if((leftLast != separator) && (rightFirst != separator)) { //it is a situation we have "left + right" //needs separator needAddSeparator = true; } //The last case (leftLast != separator) || (rightFirst != separator) //gives us needAddSeparator = false, but it is false by default if(needAddSeparator) result += separator; return result+right; } //______________________________________________________________________________ bool ccdb::DPathUtils::WildCardCheck( const char* pattern, const char* source ) { char *cp, *mp; while ((*source) && (*pattern != '*')) { if ((*pattern != *source) && (*pattern != '?')) { return 0; } pattern++; source++; } while (*source) { if (*pattern == '*') { if (!*++pattern) { return 1; } mp = const_cast(pattern); cp = const_cast(source+1); } else if ((*pattern == *source) || (*pattern == '?')) { pattern++; source++; } else { pattern = mp; source = cp++; } } while (*pattern == '*') { pattern++; } return !*pattern; } //______________________________________________________________________________ time_t ccdb::DPathUtils::ParseTime( const string &timeStr, bool * succsess ) { /** @brief ParseTime * parses time as any part of * YYYY:MM:DD-hh:mm:ss * * 1) in place of ':' and '-' might be any ONE non digit character * * 2) One could place only part of the string, * the latest date for this part will be returned. * I.e. if timeString is "2011" - (it means the year 2011 and nothing more), * this function returns result as if it were 2011:12:31-23:59:59 * thus using such timestamp would get the latest constants for year 2011 * * @parameter [in] const string & timeString string to parse * @parameter [out] succsess true if success * @return time_t */ //default result tm time; time.tm_hour = 23; time.tm_mday = 31; time.tm_min = 59; time.tm_mon = 11; time.tm_sec = 59; time.tm_isdst=false; if(succsess!=NULL) *succsess = true; //some tmp values string tmpStr =""; //tmp string to buffer chars int delimCount=0; //number of delimeters we've met string workStr(timeStr); //we appended a work string for one more non digit symbol workStr.push_back(' '); //so the logic behind would work in any case bool lastIsDigit; //last symbol was digit //scan all symbols for (size_t i=0; i='0'&&symbol<='9') //Check if it is number { tmpStr.push_back(symbol); lastIsDigit = true; } else if(lastIsDigit) //it is a delimeter after the value; { delimCount++; if(delimCount ==1) //it was a year { if(tmpStr.length()!=4) //it is an error with lengtn of year { if(succsess!=NULL) succsess = false; return 0; } time.tm_year = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } if(delimCount ==2) //it was a month { if(tmpStr.length()!=2) //it is an error with lengtn of year { if(succsess!=NULL) succsess = false; return 0; } time.tm_mon = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } if(delimCount ==3) //it was a day { if(tmpStr.length()!=2) //it is an error with lengtn of day { if(succsess!=NULL) succsess = false; return 0; } time.tm_mday = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } if(delimCount ==4) //it was a hour { if(tmpStr.length()!=2) //it is an error with lengtn { if(succsess!=NULL) succsess = false; return 0; } time.tm_hour = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } if(delimCount ==5) //it was a min { if(tmpStr.length()!=2) //it is an error with lengtn of min { if(succsess!=NULL) succsess = false; return 0; } time.tm_min = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } if(delimCount ==6) //it was a sec { if(tmpStr.length()!=2) //it is an error with lengtn of sec { if(succsess!=NULL) succsess = false; return 0; } time.tm_sec = DStringUtils::ParseInt(tmpStr); //since in tm the year is from 1900 } //clear temp string for the next digit seria tmpStr.clear(); } else { tmpStr.clear(); //in this case we clear temp string too } } time_t result = mktime(&time); return result; } //______________________________________________________________________________ ccdb::DParseRequestResult ccdb::DPathUtils::ParseRequest( const string& requestStr ) { /** @brief Parses request string and returns corresponding * @see DParseRequestResult structure. * * This function is used to parse user requests. The user requests * full form of request is * /path/to/data:run:variation:time * but request might be given in any shorter form * /path/to/data - just path to date, no run, variation and timestamp specified * /path/to/data::mc - no run or date specified. * /path/to/data:::2029 - only path and date * * @parameter [in] requestStr - user request * @return structure that represent user result */ //Set default parameters DParseRequestResult result; result.RunNumber=0; // Run number result.WasParsedRunNumber=false; // true if Run number was non empty result.IsInvalidRunNumber=false; // true if was an error parsing runnumber result.Path = ""; // Object path result.WasParsedPath=false; // true if Path was nonempty result.Variation=""; // Variation name result.WasParsedVariation=false; // true if variation was not empty result.Time=0; // Time stamp result.WasParsedTime=false; // true if time stamp was not empty result.TimeString=""; // Original string with time int colonCount=0; string runStr =""; for (size_t i=0; i