Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cwb_mkhtml_file.C
Go to the documentation of this file.
1 // convert file into html
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <iostream>
5 #include <string>
6 #include <algorithm>
7 #include <vector>
8 
9 void cwb_mkhtml_file(TString file_name, TString options="") {
10 
11  // get title
12  TString option_title = CWB::Toolbox::getParameter(options,"--title");
13  option_title.ReplaceAll("#"," ");
14 
15  // get subtitle
16  TString option_subtitle = CWB::Toolbox::getParameter(options,"--subtitle");
17  option_subtitle.ReplaceAll("#"," ");
18 
19  // get multi, if multi=='true' the plots are presented with 2 columns format
20  TString option_multi = CWB::Toolbox::getParameter(options,"--multi");
21  option_multi.ToUpper();
22 
23  if(file_name=="") {
24  cout << "cwb_mkhtml_file.C - Error : empty file name !!!" << endl;exit(1);
25  }
26  if(file_name.Contains("..")) {
27  cout << "cwb_mkhtml_file.C - Error : file_name must not contains '..' !!!" << endl;exit(1);
28  }
29  if(file_name==".") file_name=gSystem->WorkingDirectory();
30  // check if file_name is a directory
31  Long_t id,size=0,flags,mt;
32  int estat = gSystem->GetPathInfo(file_name.Data(),&id,&size,&flags,&mt);
33  // create an html file with the list of png plots contained in the file_name dir
34  if(flags&2 || file_name==".") { // is a directory
35  TString odir = file_name;
36  // get the list of png files
37  vector<TString> pngList = CWB::Toolbox::getFileListFromDir(odir, ".png");
38  if(!pngList.size()) {
39  cout << "cwb_mkhtml_file.C - Error : no *.png files found !!!" << endl;exit(1);
40  }
41  // sort list
42  vector<TString> pngList = CWB::Toolbox::sortStrings(pngList);
43 
44  TString texiName=odir+"/png_html_index.texi";
45  bool overwrite=CWB::Toolbox::checkFile(texiName,true);
46  if(!overwrite) gSystem->Exit(0);
47  ofstream out;
48  out.open(texiName,ios::out);
49  out << "@c include texi macros" << endl;
50  out << "@include macros.texi" << endl;
51  out << "@include mathjax.texi" << endl << endl;
52  out << "@*" << endl;
53  if(option_title!="") {
54  out << "@center @txtfont{"<<"<br>"<<option_title<<"<br>"<<", blue, h1}" << endl;
55  if(option_subtitle!="") out << "@center @txtcolor{"<<option_subtitle.Data()<<",black}" << endl;
56  out << "@*" << endl;
57  out << "@drawline" << endl;
58  }
59  out << "@*" << endl;
60  if(option_multi=="TRUE") {
61  for(int i=0;i<pngList.size();i++) {
62  TString name=pngList[i];name.ReplaceAll(".png","");
63  name=gSystem->BaseName(name);
64  if(i%2==0) {
65  out << "@multitable @columnfractions .5 .5" << endl;
66  out << "@item @center @txtfont{"<<i+1<<", red, h2}" << endl;
67  out << "@center @displayimage{../.,"<<name<<",470}"<<endl;
68  } else {
69  out << "@tab @center @txtfont{"<<i+1<<", red, h2}" << endl;
70  out << "@center @displayimage{../.,"<<name<<",470}"<<endl;
71  out << "@end multitable" << endl;
72  }
73  }
74  if(pngList.size()%2==1) out << "@end multitable" << endl;
75  } else {
76  for(int i=0;i<pngList.size();i++) {
77  TString name=pngList[i];name.ReplaceAll(".png","");
78  name=gSystem->BaseName(name);
79  out << "@center @txtfont{"<<i+1<<", red, h2}" << endl;
80  out << "@center @displayimage{../.,"<<name<<",1000}"<<endl;
81  }
82  }
83  out.close();
84  // convert texi into html
85  TString cwb_scripts = TString(gSystem->Getenv("CWB_SCRIPTS"));
86  TString exec_cmd = TString::Format("%s/cwb_mkhtml.csh %s wheader;rm %s",
87  cwb_scripts.Data(),texiName.Data(),texiName.Data());
88  int ret=gSystem->Exec(exec_cmd);
89  if(ret) {
90  cout << "cwb_mkhtml_file.C : Error while executing cwb_mkhtml png_html_index.texi !!!" << endl;
91  exit(1);
92  }
93  gSystem->Exit(0);
94  }
95 
96  if(!file_name.EndsWith(".C")) {
97  } else if(!file_name.EndsWith(".cc")) {
98  } else if(!file_name.EndsWith(".hh")) {
99  } else if(!file_name.EndsWith(".c")) {
100  } else if(!file_name.EndsWith(".h")) {
101  } else {
102  cout << "cwb_mkhtml_file.C - Error : file name must ends with .C/.c/.h/.cc/.hh !!!" << endl;exit(1);
103  }
104  // if file_name is not an absolute path add working dir path
105  if(!file_name.BeginsWith("/")) {
106  TString tmp = file_name;
107  file_name = TString(gSystem->WorkingDirectory())+"/"+tmp;
108  }
109 
110  // create output html dir
111  TString html_dir = file_name;
112  html_dir.ReplaceAll(".C","");
113  html_dir.ReplaceAll(".cc","");
114  html_dir.ReplaceAll(".hh","");
115  html_dir.ReplaceAll(".c","");
116  html_dir.ReplaceAll(".h","");
117  CWB::Toolbox::mkDir(html_dir,true);
118 
119  THtml html;
120  html.SetEtcDir(gSystem->ExpandPathName("$HOME_WAT/html/etc/html"));
121  html.SetProductName("CWB");
122  TString html_input_dir=html_dir;
123  html.SetInputDir(html_input_dir.Data());
124 
125  char cmd[1024];
126  sprintf(cmd,"cp %s/html/etc/html/ROOT.css %s/",gSystem->ExpandPathName("$HOME_WAT"),html_dir.Data());
127  gSystem->Exec(cmd);
128  sprintf(cmd,"cp %s/html/etc/html/ROOT.js %s/",gSystem->ExpandPathName("$HOME_WAT"),html_dir.Data());
129  gSystem->Exec(cmd);
130 
131  // redirect stderr to /dev/null to getrid of messages produced by html.Convert
132  fpos_t poserr; fflush(stderr); fgetpos(stderr, &poserr);
133  int fderr = dup(fileno(stderr)); freopen("/dev/null", "w", stderr);
134  // redirect stdout to /dev/null to getrid of messages produced by html.Convert
135  fpos_t posout; fflush(stdout); fgetpos(stdout, &posout);
136  int fdout = dup(fileno(stdout)); freopen("/dev/null", "w", stdout);
137 
138  // convert file to html
139  html.Convert(file_name.Data(),file_name.Data(),html_dir,"");
140  // add html header
141 /* it is not necessary because header is added by html.Convert using $HOME_WAT/html/etc/html/header.html
142  sprintf(cmd,"root -n -l -b ${CWB_ROOTLOGON_FILE} '${HOME_CWB}/info/macros/MakeHTML.C\(\"%s\",false\)'",
143  html_dir.Data());
144  gSystem->Exec(cmd);
145 */
146 
147  // restore the stderr output
148  fflush(stderr); dup2(fderr, fileno(stderr)); close(fderr);
149  clearerr(stderr); fsetpos(stderr, &poserr);
150  // restore the stdout output
151  fflush(stdout); dup2(fdout, fileno(stdout)); close(fdout);
152  clearerr(stdout); fsetpos(stdout, &posout);
153 
154  TString html_file = html_dir+"/"+gSystem->BaseName(file_name)+".html";
155  cout << endl << "created html file : " << html_file << endl << endl;
156 
157  gSystem->Exit(0);
158 }
TString cwb_scripts
static vector< TString > getFileListFromDir(TString dir_name, TString endString="", TString beginString="", TString containString="", bool fast=false)
Definition: Toolbox.cc:4333
char cmd[1024]
par[0] name
TString("c")
char odir[1024]
Long_t flags
THtml html
Long_t size
i drho i
static bool checkFile(TString fName, bool question=false, TString message="")
Definition: Toolbox.cc:3956
ofstream out
Definition: cwb_merge.C:196
double * tmp
Definition: testWDM_5.C:31
static vector< TString > sortStrings(vector< TString > ilist)
Definition: Toolbox.cc:5894
char options[256]
static TString getParameter(TString options, TString param="")
Definition: Toolbox.cc:5943
static void mkDir(TString dir, bool question=false, bool remove=true)
Definition: Toolbox.cc:4000
void cwb_mkhtml_file(TString file_name, TString options="")
int estat
sprintf(tfres,"(1/%g)x(%g) (sec)x(Hz)", 2 *df, df)
Long_t mt
bool overwrite
Definition: cwb_dump_inj.C:82
Long_t id
in close()
exit(0)