g4tools  5.4.0
file_name
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_file_name
5 #define tools_file_name
6 
7 #include "srep"
8 
9 #include <cstdlib>
10 
11 namespace tools {
12 
13 // OpenPAW, Panoramix :
14 inline bool rep_env(std::string& a_string) {
15  char spec_char = '\n';
16  std::string::size_type dollar;
17  while((dollar=a_string.find('$'))!=std::string::npos){
18  std::string::size_type slash = a_string.find('/',dollar+1);
19  std::string::size_type back_slash = a_string.find('\\',dollar+1);
20  std::string::size_type pos = std::string::npos;
21  if(slash!=std::string::npos) {
22  if(back_slash!=std::string::npos) {
23  pos = slash<back_slash?slash:back_slash;
24  } else {
25  pos = slash;
26  }
27  } else {
28  if(back_slash!=std::string::npos) {
29  pos = back_slash;
30  } else {
31  pos = std::string::npos;
32  }
33  }
34  std::string env;
35  if(pos==std::string::npos) {
36  env = a_string.substr(dollar+1,a_string.length()-(dollar+1));
37  } else {
38  // abc$xxx/ef
39  // 0 3 7 9
40  env = a_string.substr(dollar+1,pos-(dollar+1));
41  }
42  char* val = ::getenv(env.c_str());
43  if(!val) {
44  // We may have $ in a_string not attached to an env variable.
45  // For example at LAL, some Windows account name have a leading $
46  // that is found back in the home directory path. To bypass this
47  // problem we change temporarily the $ by spec_char and continue
48  // the loop. We change back all the spec_chars to $ at end.
49  a_string[dollar] = spec_char;
50  } else {
51  std::string value = a_string.substr(0,dollar);
52  value += val;
53  if(pos!=std::string::npos) value += a_string.substr(pos,a_string.length()-pos);
54  a_string = value;
55  }
56  }
57  replace(a_string,spec_char,'$');
58  return true;
59 }
60 
61 inline bool file_name(const std::string& a_path,std::string& a_name) {
62  //used in osc packages and a tools::xml::parser::load_file() compatible for OnX.
63  a_name = a_path;
64  if(!rep_env(a_name)) {a_name.clear();return false;}
65 #ifdef _WIN32
66  replace(a_name,"/","\\");
67  replace(a_name,"\"","");
68 #endif
69  return true;
70 }
71 
72 }
73 
74 #endif
tools::value
Definition: value:18
tools::replace
void replace(std::string &a_string, char a_old, char a_new)
Definition: srep:14
tools::rep_env
bool rep_env(std::string &a_string)
Definition: file_name:14
tools::file_name
bool file_name(const std::string &a_path, std::string &a_name)
Definition: file_name:61
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
srep