Logo coherent WaveBurst  
Library Reference Guide
Logo
cwb_csh2sh.C
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Gabriele Vedovato
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 vector<TString> getFileListFromDir(TString dir_name, TString endString="", TString beginString="");
21 
22 void cwb_csh2sh(TString script = "") {
23 
24  vector<TString> cshList = getFileListFromDir(".", ".csh");
25 
26  for(int i=0;i<cshList.size();i++) {
27 
28  TString ifile=cshList[i];
29  cout << "ifile : " << ifile.Data() << endl;
30  if(script!="") if(!ifile.Contains(script)) continue;
31 
32  ifstream in;
33  in.open(ifile.Data(),ios::in);
34  if (!in.good()) {cout << "Error Opening File : " << ifile.Data() << endl;exit(1);}
35 
37  ofile.ReplaceAll(".csh",".sh");
38  cout << "ofile : " << ofile.Data() << endl;
39 
40  ofstream out;
41  out.open(ofile.Data(),ios::out);
42  if (!out.good()) {cout << "Error Opening File : " << ofile.Data() << endl;exit(1);}
43 
44  char istring[1024];
45  while (1) {
46  in.getline(istring,1024);
47  if (!in.good()) break;
48  TString ostring(istring);
49  if(ostring.Contains("_SKIP_CSH2SH_")) {
50  out << ostring.Data() << endl;
51  continue;
52  }
53  if(!ostring.Contains("_SKIP_csh2sh_")) ostring.ReplaceAll(".csh",".sh");
54  ostring.ReplaceAll("endif","fi");
55  ostring.ReplaceAll("unsetenv","unset");
56  ostring.ReplaceAll("#!/bin/tcsh","#!/bin/bash");
57 
58  bool tag=true;
59  if(ostring.Contains("if")&&ostring.Contains("(")&&ostring.Contains(")")) {
60  tag=true;
61  for(int j=0;j<ostring.Sizeof();j++) if(ostring[j]=='(') {if(tag) tag=false; else ostring[j]=' ';}
62  tag=true;
63  for(int j=ostring.Sizeof()-1;j>=0;j--) if(ostring[j]==')') {if(tag) tag=false; else ostring[j]=' ';}
64 
65  // remove multiple blank characters within "! $?"
66  ostring = RemoveMultipleBlank(ostring,"!","$?");
67 
68  ostring.ReplaceAll("! $?","-z $");
69 
70  ostring.ReplaceAll("$1 == \'\'","-z $1");
71  ostring.ReplaceAll("$2 == \'\'","-z $2");
72  ostring.ReplaceAll("$2 != \'\'","-n $2");
73  ostring.ReplaceAll("$3 == \'\'","-z $3");
74  ostring.ReplaceAll("$3 != \'\'","-n $3");
75  ostring.ReplaceAll("(","[ ");
76  ostring.ReplaceAll("==","=");
77  ostring.ReplaceAll("\'","\"");
78  ostring.ReplaceAll(")"," ]");
79  ostring.ReplaceAll(" then","; then");
80  if(ostring.ReplaceAll("\"$PATH\" !~","! $PATH =~"))
81  {ostring.ReplaceAll(" [ "," [[ ");ostring.ReplaceAll(" ];"," ]];");}
82  if(ostring.ReplaceAll("\"$LD_LIBRARY_PATH\" !~","! $LD_LIBRARY_PATH =~"))
83  {ostring.ReplaceAll(" [ "," [[ ");ostring.ReplaceAll(" ];"," ]];");}
84  if(ostring.ReplaceAll("\"$MANPATH\" !~","! $MANPATH =~"))
85  {ostring.ReplaceAll(" [ "," [[ ");ostring.ReplaceAll(" ];"," ]];");}
86  if(ostring.ReplaceAll("\"$PYTHONPATH\" !~","! $PYTHONPATH =~"))
87  {ostring.ReplaceAll(" [ "," [[ ");ostring.ReplaceAll(" ];"," ]];");}
88  if(ostring.ReplaceAll("\"$INFOPATH\" !~","! $INFOPATH =~"))
89  {ostring.ReplaceAll(" [ "," [[ ");ostring.ReplaceAll(" ];"," ]];");}
90  ostring.ReplaceAll("*","");
91  ostring.ReplaceAll(" !~ "," != ");
92  }
93 
94  TRegexp re4("set.*=.*\".*setenv.*$");
95  if(ostring.Contains(re4)) {
96  ostring.ReplaceAll("setenv ","export ");
97  ostring.ReplaceAll("set ","");
98  ostring.ReplaceAll("$","= $");
99  ostring.ReplaceAll(" ","");
100  ostring.ReplaceAll("\"export","\"export ");
101  out << ostring.Data() << endl;
102  continue;
103  }
104 
105  ostring.ReplaceAll("exit","return 0");
106  ostring.ReplaceAll("setenv","export");
107  ostring.ReplaceAll("\t"," ");
108  ostring.ReplaceAll("else if","elif");
109 
110  int iblanks=0;
111  for(int j=0;j<ostring.Sizeof();j++) if(ostring[j]==' ') iblanks++; else break;
112  if(!ostring.Contains("$#")) if(ostring.First("#")>0) ostring.Remove(ostring.First("#"));
113  if((ostring.Contains("export")&&!ostring.Contains("unexport"))||ostring.Contains("alias")){
114  TObjArray* token = TString(ostring).Tokenize(TString(' '));
115  TObjString* tok[20];
116  for(int j=0;j<token->GetEntries();j++){ tok[j]=(TObjString*)token->At(j);}
117  ostring = tok[0]->GetString()+" "+tok[1]->GetString()+"="+tok[2]->GetString();
118 
119  if(token->GetEntries()>3){for(int j=3;j<token->GetEntries();j++)
120  {ostring.Append(" ");ostring.Append(tok[j]->GetString());}}
121  } else iblanks=0;
122 
123  TRegexp re1("set.*=.*basename");
124  if(ostring.Contains(re1)) {
125  ostring.ReplaceAll("set","");
126  ostring.ReplaceAll(" ","");
127  ostring.ReplaceAll("basename","basename ");
128  }
129 
130  TRegexp re2("set.*=.*$");
131  if(ostring.Contains(re2)&&!ostring.Contains("alias")) {
132  ostring.ReplaceAll("set","");
133  //ostring.ReplaceAll(" ","");
134  }
135 
136  TRegexp re3("@.*[-=+*:].*[0-9]");
137  if(ostring.Contains(re3)) {
138  ostring.ReplaceAll("@","(( ");
139  ostring = ostring+" ))";
140  }
141 
142  for(int j=0;j<iblanks;j++) out << " "; // restore initials blanks
143  out << ostring.Data() << endl;
144  }
145  out.close();
146  in.close();
147  }
148  exit(0);
149 }
150 
152 
153  TString reg0=left+" .*"+right;
154  TString pat0=left+" "+right;
155 
156  // remove multiple blank characters within "left right"
157  TRegexp t(reg0);
158  int index = string.Index(t);
159  if(index>=0) {
160  TString pat=left;
161  for(int j=index+1; j<string.Sizeof();j++) {if(string[j]=='$') break; pat+=" ";}
162  pat+=right;
163  string.ReplaceAll(pat,pat0);
164  }
165  return string;
166 }
167 
168 vector<TString> getFileListFromDir(TString dir_name, TString endString, TString beginString) {
169 
170  TString wdir = gSystem->WorkingDirectory();
171  TSystemDirectory gdir("", dir_name);
172  TList *dfiles = gdir.GetListOfFiles();
173  TIter dnext(dfiles);
174  TSystemFile *dfile;
175  TString fname;
176  vector<TString> fileList;
177  char path[1024];
178 
179  while ((dfile = (TSystemFile*)dnext())) {
180  fname = dfile->GetName();
181  sprintf(path,"%s/%s",dir_name.Data(),fname.Data());
182  bool fsave=true;
183  if ((endString!="")&&!fname.EndsWith(endString)) fsave=false;
184  if ((beginString!="")&&!fname.BeginsWith(beginString)) fsave=false;
185  if(fsave) fileList.push_back(path);
186  }
187 
188  gSystem->ChangeDirectory(wdir); // restore original dir
189 
190  return fileList;
191 }
192 
char ofile[1024]
wavearray< double > t(hp.size())
TString("c")
ofstream out
Definition: cwb_merge.C:214
cout<< "baudline_FFL : "<< baudline_FFL<< endl;ofstream out;out.open(baudline_FFL, ios::out);if(!out.good()) {cout<< "Error Opening File : "<< baudline_FFL<< endl;exit(1);} ifstream in;in.open(frFiles[ifoID], ios::in);if(!in.good()) {cout<< "Error Opening File : "<< frFiles[ifoID]<< endl;exit(1);} TString pfile_path="";char istring[1024];while(1) { in > istring
int j
Definition: cwb_net.C:28
i drho i
void cwb_csh2sh(TString script="")
Definition: cwb_csh2sh.C:22
char fname[1024]
vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="")
Definition: cwb_csh2sh.C:168
TObjArray * token
TFile * ifile
char tag[256]
Definition: cwb_merge.C:92
TString GetString(TTree *tree, int run, int lag, TString psfix)
Definition: Toolfun.hh:283
TString RemoveMultipleBlank(TString string, TString left, TString right)
Definition: cwb_csh2sh.C:151
ifstream in
cout<< "Starting reading output directory ..."<< endl;vector< TString > fileList
wavearray< int > index
TString dir_name[NDIR]
Definition: cwb_mkdir.C:76
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
exit(0)