otsdaq_utilities  v2_02_00
doxygen_main_editor.cpp
1 // doxygen_main_editor.cpp
2 // by rrivera at fnal dot gov
3 // created May 2018
4 //
5 // This is a simple html code injector to improve the main.html generated by doxygen.
6 //
7 // Planned to be used in conjunction with OnlineDocPushUpdate.sh
8 //
9 //compile with:
10 //g++ doxygen_main_editor.cpp -o hw.o
11 //
12 //if developing, consider appending -D_GLIBCXX_DEBUG to get more
13 //descriptive error messages
14 //
15 //run with:
16 //./doxygen_main_editor.o <full main.html path> <full inject main html file path>
17 //
18 
19 #include <iostream>
20 #include <iomanip>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 
32 
33 
34 //take only file name
35 #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
36 
37 //use this for normal printouts
38 #define __PRINTF__ printf
39 #define __COUT__ cout << __FILENAME__ << std::dec << " [" << __LINE__ << "]\t"
40 #define __E__ std::endl
41 
42 //and use this to suppress
43 //#define __PRINTF__ if(0) printf
44 //#define __COUT__ if(0) cout
45 
46 
47 using namespace std;
48 
49 
50 
51 int main(int argc, char** argv)
52 {
53  __COUT__ << "Starting doxygen main.html editor..." << __E__;
54 
55  if(argc < 4)
56  {
57  __COUT__ << "Need 3 arguments: for the full path to main.html AND to ARRAY:<html-to-inject>" << __E__;
58  return 0;
59  }
60  string mainfn = argv[1];
61  string injectfn = argv[2];
62  string inject2fn = argv[3];
63  __COUT__ << "main.html destination full path: " << mainfn << __E__;
64  __COUT__ << "main.html source full path: " << mainfn + ".bk" << __E__;
65  __COUT__ << "inject.html source full path: " << injectfn << __E__;
66  __COUT__ << "inject2.html source full path: " << inject2fn << __E__;
67 
68  FILE *mainSrc = fopen((mainfn + ".bk").c_str(),"r");
69  if(!mainSrc)
70  {
71  __COUT__ << "Failed to open... " << mainfn + ".bk" << __E__;
72  return 0;
73  }
74  FILE *injectSrc = fopen((injectfn).c_str(),"r");
75  if(!injectSrc)
76  {
77  __COUT__ << "Failed to open... " << injectfn << __E__;
78  return 0;
79  }
80  FILE *inject2Src = fopen((inject2fn).c_str(),"r");
81  if(!inject2Src)
82  {
83  __COUT__ << "Failed to open... " << inject2fn << __E__;
84  return 0;
85  }
86  FILE *mainDest = fopen((mainfn).c_str(),"w");
87  if(!mainSrc)
88  {
89  __COUT__ << "Failed to open... " << mainfn << __E__;
90  return 0;
91  }
92 
93  char line[1000];
94  unsigned int countdown = -1;
95 
96  unsigned int injectIndex = 0;
97 
98  bool injected = true;
99  while(fgets(line,1000,mainSrc))
100  {
101  fputs(line,mainDest); //output main line to file
102  __COUT__ << line << (line[strlen(line)-1] == '\n'?"":"\n");
103 
104  if(injected &&
105  !strcmp(line,"<div class=\"contents\">\n"))
106  {
107  injected = false;
108  countdown = 0;
109  injectIndex = 1;
110  //continue;
111  }
112  else if(injected &&
113  !strcmp(line,"<head>\n"))
114  {
115  injected = false;
116  countdown = 0;
117  injectIndex = 2;
118  //continue;
119  }
120 
121 
122  if(!injected && countdown == 0) //inject file
123  {
124  injected = true;
125 
126  switch(injectIndex)
127  {
128  case 1:
129  {
130  //get one more line and modify it, ie, clip ugly start and delete close tag for content div
131 // fgets(line,1000,mainSrc);
132 // __COUT__ << "MOD " << line << (line[strlen(line)-1] == '\n'?"":"\n");
133 // line[strlen(line)-7] = '\0';
134 // for(countdown=strlen(line)-16;countdown<strlen(line);++countdown)
135 // if(line[countdown]=='v') break;
136 //
137 // //keep version number
138 // fputs("<h3>",mainDest);
139 // __COUT__ << "<h3>" << __E__;
140 // fputs(&line[countdown],mainDest); //output main line to file
141 // __COUT__ << &line[countdown] << __E__;
142 
143  while(fgets(line,1000,injectSrc))
144  {
145  fputs(line,mainDest); //output inject line to file
146  __COUT__ << line << (line[strlen(line)-1] == '\n'?"":"\n");
147  }
148 
149  //add close content div
150 // fputs("</div>",mainDest);
151 // __COUT__ << "</div>" << __E__;
152 
153  break;
154  }
155  case 2:
156  {
157  while(fgets(line,1000,inject2Src))
158  {
159  fputs(line,mainDest); //output inject line to file
160  __COUT__ << line << (line[strlen(line)-1] == '\n'?"":"\n");
161  }
162  break;
163  }
164  default:
165  __COUT__ << "Unknown injection!" << __E__;
166  }
167 
168 
169  }
170  else if(!injected)
171  {
172  --countdown;
173  }
174  }
175 
176  fclose(mainDest); fclose(injectSrc); fclose(mainSrc);
177 
178  __COUT__ << "Doxygen main.html editor complete!" << __E__;
179 
180  return 0;
181 }
182