Files
geant4/source/externals/g4tools/include/tools/file_name
T
2021-06-25 16:12:29 +02:00

75 lines
2.1 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_file_name
#define tools_file_name
#include "srep"
#include <cstdlib>
namespace tools {
// OpenPAW, Panoramix :
inline bool rep_env(std::string& a_string) {
char spec_char = '\n';
std::string::size_type dollar;
while((dollar=a_string.find('$'))!=std::string::npos){
std::string::size_type slash = a_string.find('/',dollar+1);
std::string::size_type back_slash = a_string.find('\\',dollar+1);
std::string::size_type pos = std::string::npos;
if(slash!=std::string::npos) {
if(back_slash!=std::string::npos) {
pos = slash<back_slash?slash:back_slash;
} else {
pos = slash;
}
} else {
if(back_slash!=std::string::npos) {
pos = back_slash;
} else {
pos = std::string::npos;
}
}
std::string env;
if(pos==std::string::npos) {
env = a_string.substr(dollar+1,a_string.length()-(dollar+1));
} else {
// abc$xxx/ef
// 0 3 7 9
env = a_string.substr(dollar+1,pos-(dollar+1));
}
char* val = ::getenv(env.c_str());
if(!val) {
// We may have $ in a_string not attached to an env variable.
// For example at LAL, some Windows account name have a leading $
// that is found back in the home directory path. To bypass this
// problem we change temporarily the $ by spec_char and continue
// the loop. We change back all the spec_chars to $ at end.
a_string[dollar] = spec_char;
} else {
std::string value = a_string.substr(0,dollar);
value += val;
if(pos!=std::string::npos) value += a_string.substr(pos,a_string.length()-pos);
a_string = value;
}
}
replace(a_string,spec_char,'$');
return true;
}
inline bool file_name(const std::string& a_path,std::string& a_name) {
//used in osc packages and a tools::xml::parser::load_file() compatible for OnX.
a_name = a_path;
if(!rep_env(a_name)) {a_name.clear();return false;}
#ifdef _WIN32
replace(a_name,"/","\\");
replace(a_name,"\"","");
#endif
return true;
}
}
#endif